Пример #1
0
        private void BFSTraversal(int start, VertexQue route, int destination)
        {
            /* make a queue of vertices */
            var queue = new VertexQue();
            /* make a mark set of vertices */
            var visited = new VertexMarkSet();

            /* enqueue and mark start */
            queue.Enqueue(start);
            visited.Add(start);
            /* while the queue is not empty */
            while (queue.NotEmpty())
            {
                /* dequeue a vertext */
                var departure = queue.Dequeue();
                var stops     = _graph.ListNeighbours(departure);

                foreach (int stop in stops)
                {
                    /* enqueue and mark all the unmarked neighbours of the vertex */
                    if (!visited.Contains(stop))
                    {
                        visited.Add(stop);
                        queue.Enqueue(stop);
                    }

                    if (stop == destination)
                    {
                        route.Enqueue(stop);
                    }
                }
            }
        }
Пример #2
0
        public void pathsFrom(char start, char end)
        {
            int       root        = _graph.GetNodeIndex(start);
            int       destination = _graph.GetNodeIndex(end);
            VertexQue route       = new VertexQue();

            BFSTraversal(root, route, destination);

            Console.Write($" There are {route.Count()} trips from {start} to {end} with max 3 stops");
        }
Пример #3
0
        public void pathsWithMaxStops(char start, char end, int max_hops)
        {
            // graph index of start and end nodes
            int root        = _graph.GetNodeIndex(start);
            int destination = _graph.GetNodeIndex(end);

            VertexQue trips = new VertexQue();

            DFSMax(root, trips, destination);

            Console.WriteLine($"There are {trips.Count()} trips from {start} to {end} with exact 4 stops. ");
        }
Пример #4
0
        private void DFSMax(int start, VertexQue trips, int destination)
        {
            // displaying current node
            var current_node = _graph.GetNodeName(start);

            // Fetching next stops
            var stops = _graph.ListNeighbours(start);

            foreach (int stop in stops)
            {
                if (stop == destination)
                {
                    trips.Enqueue(stop);
                    return;
                }

                DFSMax(stop, trips, destination);
            }
        }