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); } } } }
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"); }
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. "); }
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); } }