public static bool HasPath(this CrackingDirectedGraph graph, int v, int w) { var queue = new Queue <int>(); var visited = new bool[graph.Size]; queue.Enqueue(v); while (queue.Count > 0) { int x = queue.Dequeue(); visited[x] = true; if (x == w) { return(true); } foreach (var adj in graph.Adjacents(x)) { if (!visited[adj]) { queue.Enqueue(adj); } } } return(false); }
public static void Test() { var graph = new CrackingDirectedGraph(10); graph.Connect(1, 2); graph.Connect(3, 4); graph.Connect(5, 3); graph.Connect(3, 8); graph.Connect(8, 9); graph.Connect(2, 5); Console.WriteLine(graph.HasPath(1, 9)); Console.WriteLine(!graph.HasPath(4, 9)); }