public void DoTest() { DirectedWeightedSparseGraph <string> graph = new DirectedWeightedSparseGraph <string>(); // Add vertices var verticesSet1 = new string[] { "a", "z", "s", "x", "d", "c", "f", "v" }; graph.AddVertices(verticesSet1); // Add edges graph.AddEdge("a", "s", 1); graph.AddEdge("a", "z", 1); graph.AddEdge("s", "x", 1); graph.AddEdge("x", "d", 1); graph.AddEdge("x", "c", 1); graph.AddEdge("d", "f", 1); graph.AddEdge("d", "c", 1); graph.AddEdge("c", "f", 1); graph.AddEdge("c", "v", 1); graph.AddEdge("v", "f", 1); // Print the nodes in graph Console.WriteLine(" [*] DFS PrintAll: "); DepthFirstSearcher.PrintAll(graph, "a"); Console.WriteLine("\r\n"); var list = DepthFirstSearcher.FindAllPaths(graph, "a", "f"); foreach (var path in list) { Console.WriteLine(string.Join(",", path)); } Assert.AreEqual(5, list.Count); }
public static void DoTest() { IGraph <string> graph = new UndirectedSparseGraph <string>(); // Add vertices var verticesSet1 = new string[] { "a", "z", "s", "x", "d", "c", "f", "v" }; graph.AddVertices(verticesSet1); // Add edges graph.AddEdge("a", "s"); graph.AddEdge("a", "z"); graph.AddEdge("s", "x"); graph.AddEdge("x", "d"); graph.AddEdge("x", "c"); graph.AddEdge("d", "f"); graph.AddEdge("d", "c"); graph.AddEdge("c", "f"); graph.AddEdge("c", "v"); graph.AddEdge("v", "f"); // Print the nodes in graph Console.WriteLine(" [*] DFS PrintAll: "); DepthFirstSearcher.PrintAll(graph, "d"); Console.WriteLine("\r\n"); string searchResult = null; string startFromNode = "d"; Action <string> writeToConsole = (node) => Console.Write(String.Format("({0}) ", node)); Predicate <string> searchPredicate = (node) => node == "f" || node == "c"; Console.WriteLine("[*] DFS Visit All Nodes:"); Console.WriteLine("Graph traversal started at node: '" + startFromNode + "'"); DepthFirstSearcher.VisitAll(ref graph, startFromNode, writeToConsole); Console.WriteLine("\r\n"); try { searchResult = DepthFirstSearcher.FindFirstMatch(graph, startFromNode, searchPredicate); Debug.Assert(searchResult == "c" || searchResult == "f"); Console.WriteLine("[*] DFS Find First Match:"); Console.WriteLine( String.Format( "Search result: '{0}'. The search started from node: '{1}'." , searchResult , startFromNode)); } catch (Exception) { Console.WriteLine("Search predicate was not matched by any node in the graph."); } Console.WriteLine("\r\n"); }