public void TestPath() { StreamReader sr = new StreamReader(@"E:\Study\ALG2017\ALGRKC\data\tinyCG.txt"); Graph g = new Graph(sr); int source = 0; // BreadthFirstPaths path= new BreadthFirstPaths(g, source); DepthFirstPaths path = new DepthFirstPaths(g, source); for (int i = 0; i < g.V(); i++) { Debug.Write("From " + source + " to " + i + " : "); if (path.HasPathTo(i)) { //print path foreach (int j in path.PathTo(i)) { Debug.Write(j + "-"); } Debug.WriteLine(""); } else { Debug.WriteLine("No path from " + source + " to " + i); } } }
//0 to 0 : 0 //0 to 1 : 0-2-1 //0 to 2 : 0-2 //0 to 3 : 0-2-3 //0 to 4 : 0-2-3-4 //0 to 5 : 0-2-3-5 public void PathToTest() { using (StreamReader sr = new StreamReader(@"E:\Study\ALG2017\ALGRKC\dataSelf\tinyCG.txt")) { Graph g = new Graph(sr); int source = 0; DepthFirstPaths dfp = new DepthFirstPaths(g, source); for (int v = 0; v < g.V(); v++) { Console.Write(source + " to " + v + " : "); if (dfp.HasPathTo(v)) { foreach (int x in dfp.PathTo(v)) { if (x == source) { Console.Write(x); } else { Console.Write("-" + x); } } } Console.WriteLine(); } } }
public void PathToTest() { Graph G = new Graph(11); DepthFirstPaths testObject; G.AddEdge(0, 1); G.AddEdge(0, 2); G.AddEdge(2, 3); G.AddEdge(3, 5); G.AddEdge(2, 4); G.AddEdge(4, 6); G.AddEdge(6, 7); G.AddEdge(6, 8); G.AddEdge(9, 10); testObject = new DepthFirstPaths(G, 0); // Expected Data. int[] ePath1 = { 0, 2, 3, 5 }; Stack <int> sPath1 = testObject.PathTo(5); int[] aPath1 = sPath1.ToArray(); for (int i = 0; i < aPath1.Length; i++) { Assert.AreEqual(ePath1[i], aPath1[i], "Expected: " + ePath1[i].ToString() + " Actual: " + aPath1[i].ToString()); } }
public void PathTo() { Graph graph = Create(@"DataStore\tinyCG.txt"); var path = new DepthFirstPaths(graph, 0); IEnumerable <int> actual = path.PathTo(4); Assert.Equal(new[] { 0, 5, 3, 2, 4 }, actual); }
public void PathTo_WhenPathDoesNotExistBetweenVertices_WillReturnNull() { Graph graph = new Graph(5); graph.AddEdge(0, 1); graph.AddEdge(1, 2); graph.AddEdge(1, 4); DepthFirstPaths paths = new DepthFirstPaths(graph, 0); Assert.IsNull(paths.PathTo(3)); }
static void Main(string[] args) { Graph graph = new Graph(6); graph.AddEdge(0, 2); graph.AddEdge(0, 1); graph.AddEdge(0, 5); graph.AddEdge(1, 2); graph.AddEdge(2, 3); graph.AddEdge(2, 4); graph.AddEdge(3, 4); graph.AddEdge(3, 5); int startVertex = 0; DepthFirstPaths dfsPaths = new DepthFirstPaths(graph, startVertex); Console.WriteLine($"Path {startVertex} - 3? {dfsPaths.HasPathTo(3)} : {dfsPaths.PathTo(3).PrintStackLine()}"); Console.WriteLine($"Path {startVertex} - 4? {dfsPaths.HasPathTo(4)} : {dfsPaths.PathTo(4).PrintStackLine()}"); Console.WriteLine($"Path {startVertex} - 5? {dfsPaths.HasPathTo(5)} : {dfsPaths.PathTo(5).PrintStackLine()}"); }
private static int FindPath(DepthFirstPaths search, int v) { int counter = 0; if (search.HasPathTo(v)) { foreach (var x in search.PathTo(v)) { counter++; } } return counter; }
static void Main(string[] args) { Graph g = GetGraph(); int[] paths; Paths path; path = new DepthFirstPaths(g, 0); paths = path.PathTo(4); path = new BreadthFirstPaths(g, 0); paths = path.PathTo(4); Console.ReadKey(); }
public void DfsPathToTest() { var g = GraphSample(); var dfs = new DepthFirstPaths(g, 0); Assert.True(dfs.HasPathTo(5)); var path = dfs.PathTo(5); int[] result = { 5, 3, 2, 0 }; int i = 0; foreach (int v in path) { Assert.Equal(result[i], v); i++; } }
public void PathTo_WhenPathExistsBetweenVertices_WillReturnVerticesInPath() { Graph graph = new Graph(5); graph.AddEdge(0, 1); graph.AddEdge(1, 2); graph.AddEdge(1, 4); graph.AddEdge(2, 3); graph.AddEdge(3, 4); DepthFirstPaths paths = new DepthFirstPaths(graph, 3); int[] pathVertices = paths.PathTo(0).ToArray(); Assert.AreEqual(pathVertices[0], 2); Assert.AreEqual(pathVertices[1], 1); Assert.AreEqual(pathVertices[2], 0); Assert.AreEqual(pathVertices.Length, 3); }
public void TestCase1() { var g = new Graph(6); g.AddEdge(0, 5); g.AddEdge(2, 4); g.AddEdge(2, 3); g.AddEdge(1, 2); g.AddEdge(0, 1); g.AddEdge(3, 4); g.AddEdge(3, 5); g.AddEdge(0, 2); var dfsPaths = new DepthFirstPaths(g, 0); var expected = new int[] { 0, 5, 3 }; var actual = dfsPaths.PathTo(3).ToArray(); Assert.Equal(expected, actual); }
public void Run() { Console.WriteLine("Choose file:"); // Prompt Console.WriteLine("1 - tinyCG.txt"); // Prompt Console.WriteLine("2 - mediumG.txt"); // Prompt Console.WriteLine("or quit"); // Prompt var fileNumber = Console.ReadLine(); var fieName = string.Empty; switch (fileNumber) { case "1": fieName = "tinyCG.txt"; break; case "2": fieName = "mediumG.txt"; break; case "quit": return; default: return; } var @in = new In($"Files\\Graphs\\{fieName}"); var lines = @in.ReadAllLines(); var lineIterator = 0; var v = 0; var e = 0; var edges = new List <EdgeU>(); foreach (var line in lines) { if (lineIterator == 0) { v = Convert.ToInt32(line); } if (lineIterator == 1) { e = Convert.ToInt32(line); } if (lineIterator > 1) { var lineSplitted = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); var ve = Convert.ToInt32(lineSplitted[0]); var we = Convert.ToInt32(lineSplitted[1]); var edge = new EdgeU(ve, we); edges.Add(edge); } lineIterator++; } var graph = new Graph(v, e, edges); Console.WriteLine(graph); const int s = 0; var dfs1 = new DepthFirstPaths(graph, s); for (var vi = 0; vi < graph.V; vi++) { if (dfs1.HasPathTo(vi)) { Console.Write($"{s} to {vi}: "); foreach (int x in dfs1.PathTo(vi)) { if (x == s) { Console.Write(x); } else { Console.Write($"-{x}"); } } Console.WriteLine(); } else { Console.WriteLine($"{s} to {v}: not connected{Environment.NewLine}"); } } //Console.WriteLine("------------------------------------------------"); Console.ReadLine(); }
public void Testing() { var graph = new UndirectedGraph(13); graph.InsertEdge(0, 5); graph.InsertEdge(4, 3); graph.InsertEdge(0, 1); graph.InsertEdge(9, 12); graph.InsertEdge(6, 4); graph.InsertEdge(5, 4); graph.InsertEdge(0, 2); graph.InsertEdge(11, 12); graph.InsertEdge(9, 10); graph.InsertEdge(0, 6); graph.InsertEdge(7, 8); graph.InsertEdge(9, 11); graph.InsertEdge(5, 3); var degree = graph.VertexDegree(5); var maxDegree = graph.GraphMaxVertexDegree(); var avgDegree = graph.GraphAverageVertexDegree(); var selfLoops = graph.GraphNumberOfSelfLoops(); var graph2 = new UndirectedGraph(6); graph2.InsertEdge(0, 5); graph2.InsertEdge(2, 4); graph2.InsertEdge(2, 3); graph2.InsertEdge(1, 2); graph2.InsertEdge(0, 1); graph2.InsertEdge(3, 4); graph2.InsertEdge(3, 5); graph2.InsertEdge(0, 2); var depthFirstSearch = new DepthFirstSearch(graph2, 0); var depthFirstSearchCount = depthFirstSearch.Count(); var depthFirstSearchTest0 = depthFirstSearch.Marked(0); var depthFirstSearchTest1 = depthFirstSearch.Marked(1); var depthFirstSearchTest2 = depthFirstSearch.Marked(2); var depthFirstSearchTest3 = depthFirstSearch.Marked(3); var depthFirstSearchTest4 = depthFirstSearch.Marked(4); var depthFirstSearchTest5 = depthFirstSearch.Marked(5); var depthFirstSearchNonRecursive = new DepthFirstSearchNonRecursive(graph2, 0); var depthFirstSearchNonRecursiveCount = depthFirstSearchNonRecursive.Count(); var depthFirstSearchNonRecursiveTest0 = depthFirstSearchNonRecursive.Marked(0); var depthFirstSearchNonRecursiveTest1 = depthFirstSearchNonRecursive.Marked(1); var depthFirstSearchNonRecursiveTest2 = depthFirstSearchNonRecursive.Marked(2); var depthFirstSearchNonRecursiveTest3 = depthFirstSearchNonRecursive.Marked(3); var depthFirstSearchNonRecursiveTest4 = depthFirstSearchNonRecursive.Marked(4); var depthFirstSearchNonRecursiveTest5 = depthFirstSearchNonRecursive.Marked(5); var depthFirstSearchTestConnected = depthFirstSearchCount == graph2.Vertices ? "Connected" : "NOT Connected"; var depthFirstPath = new DepthFirstPaths(graph2, 0); var depthFirstPathTest = depthFirstPath.PathTo(4); var breadthFirstSearch = new BreadthFirstSearch(graph2, 0); var breadthFirstSearchCount = breadthFirstSearch.Count(); var breadthFirstSearchTest0 = breadthFirstSearch.Marked(0); var breadthFirstSearchTest1 = breadthFirstSearch.Marked(1); var breadthFirstSearchTest2 = breadthFirstSearch.Marked(2); var breadthFirstSearchTest3 = breadthFirstSearch.Marked(3); var breadthFirstSearchTest4 = breadthFirstSearch.Marked(4); var breadthFirstSearchTest5 = breadthFirstSearch.Marked(5); var breadthFirstSearchTestConnected = breadthFirstSearchCount == graph2.Vertices ? "Connected" : "NOT Connected"; var breadthFirstPath = new BreadthFirstPaths(graph2, 0); var breadthFirstPathTest = breadthFirstPath.PathTo(4); var depthFirstComponents = new DepthFirstComponents(graph); var depthFirstComponentsTest0 = depthFirstComponents.IsConnected(0, 7); var depthFirstComponentsTest1 = depthFirstComponents.IsConnected(0, 6); var depthFirstComponentsTest2 = depthFirstComponents.IsConnected(7, 9); var depthFirstComponentsTest3 = depthFirstComponents.IsConnected(7, 8); var depthFirstComponentsTest4 = depthFirstComponents.IsConnected(9, 0); var depthFirstComponentsTest5 = depthFirstComponents.IsConnected(9, 12); var depthFirstCycle = new DepthFirstCycle(graph); var depthFirstCycleTest = depthFirstCycle.HasCycle(); var depthFirstBipartite = new DepthFirstBipartite(graph); var depthFirstBipartiteTest = depthFirstBipartite.IsBipartite(); var symbolGraph = new SymbolGraph(); symbolGraph.Insert(new [] { "JFK", "MCO" }); symbolGraph.Insert(new[] { "ORD", "DEN" }); symbolGraph.Insert(new[] { "ORD", "HOU" }); symbolGraph.Insert(new[] { "DFW", "PHX" }); symbolGraph.Insert(new[] { "JFK", "ATL" }); symbolGraph.Insert(new[] { "ORD", "DFW" }); symbolGraph.Insert(new[] { "ORD", "PHX" }); symbolGraph.Insert(new[] { "ATL", "HOU" }); symbolGraph.Insert(new[] { "DEN", "PHX" }); symbolGraph.Insert(new[] { "PHX", "LAX" }); symbolGraph.Insert(new[] { "JFK", "ORD" }); symbolGraph.Insert(new[] { "DEN", "LAS" }); symbolGraph.Insert(new[] { "DFW", "HOU" }); symbolGraph.Insert(new[] { "ORD", "ATL" }); symbolGraph.Insert(new[] { "LAS", "LAX" }); symbolGraph.Insert(new[] { "ATL", "MCO" }); symbolGraph.Insert(new[] { "HOU", "MCO" }); symbolGraph.Insert(new[] { "LAS", "PHX" }); symbolGraph.Build(); var symbolGraphTest0 = symbolGraph.Contains("LAS"); var symbolGraphTest1 = symbolGraph.Contains("MOO"); var symbolGraphTest2 = symbolGraph.Index("LAX"); var symbolGraphTest3 = symbolGraph.Name(symbolGraphTest2); var symbolGraph2 = new SymbolGraphDegreesOfSeparation(); symbolGraph2.Insert(new[] { "JFK", "MCO" }); symbolGraph2.Insert(new[] { "ORD", "DEN" }); symbolGraph2.Insert(new[] { "ORD", "HOU" }); symbolGraph2.Insert(new[] { "DFW", "PHX" }); symbolGraph2.Insert(new[] { "JFK", "ATL" }); symbolGraph2.Insert(new[] { "ORD", "DFW" }); symbolGraph2.Insert(new[] { "ORD", "PHX" }); symbolGraph2.Insert(new[] { "ATL", "HOU" }); symbolGraph2.Insert(new[] { "DEN", "PHX" }); symbolGraph2.Insert(new[] { "PHX", "LAX" }); symbolGraph2.Insert(new[] { "JFK", "ORD" }); symbolGraph2.Insert(new[] { "DEN", "LAS" }); symbolGraph2.Insert(new[] { "DFW", "HOU" }); symbolGraph2.Insert(new[] { "ORD", "ATL" }); symbolGraph2.Insert(new[] { "LAS", "LAX" }); symbolGraph2.Insert(new[] { "ATL", "MCO" }); symbolGraph2.Insert(new[] { "HOU", "MCO" }); symbolGraph2.Insert(new[] { "LAS", "PHX" }); symbolGraph2.Insert(new[] { "ZZZ", "YYY" }); symbolGraph2.Build(); var symbolGraph2Test0 = symbolGraph2.IsConnected("JFK", "LAS"); var symbolGraph2Test1 = symbolGraph2.IsConnected("JFK", "DFW"); var symbolGraph2Test2 = symbolGraph2.IsConnected("HOU", "YYY"); }
public void Testing() { var graph = new DirectedGraph(13); graph.InsertEdge(4, 2); graph.InsertEdge(2, 3); graph.InsertEdge(3, 2); graph.InsertEdge(6, 0); graph.InsertEdge(0, 1); graph.InsertEdge(2, 0); graph.InsertEdge(11, 12); graph.InsertEdge(12, 9); graph.InsertEdge(9, 10); graph.InsertEdge(9, 11); graph.InsertEdge(8, 9); graph.InsertEdge(10, 12); graph.InsertEdge(11, 4); graph.InsertEdge(4, 3); graph.InsertEdge(3, 5); graph.InsertEdge(7, 8); graph.InsertEdge(8, 7); graph.InsertEdge(5, 4); graph.InsertEdge(0, 5); graph.InsertEdge(6, 4); graph.InsertEdge(6, 9); graph.InsertEdge(7, 6); var vertexOutDegree = graph.VertexOutDegree(6); var vertexInDegre = graph.VertexInDegree(6); var graphReveresed = graph.Reverse(); var depthFirstSearch = new DepthFirstSearch(graph, 0); var depthFirstSearchCount = depthFirstSearch.Count(); var depthFirstSearchTest0 = depthFirstSearch.Marked(0); var depthFirstSearchTest1 = depthFirstSearch.Marked(1); var depthFirstSearchTest2 = depthFirstSearch.Marked(2); var depthFirstSearchTest3 = depthFirstSearch.Marked(3); var depthFirstSearchTest4 = depthFirstSearch.Marked(4); var depthFirstSearchTest5 = depthFirstSearch.Marked(5); var depthFirstSearchTest6 = depthFirstSearch.Marked(6); var depthFirstSearchTestConnected = depthFirstSearchCount == graph.Vertices ? "Connected" : "NOT Connected"; var depthFirstPath = new DepthFirstPaths(graph, 0); var depthFirstPathTest = depthFirstPath.PathTo(2); var breadthFirstSearch = new BreadthFirstSearch(graph, 0); var breadthFirstSearchCount = breadthFirstSearch.Count(); var breadthFirstSearchTest0 = breadthFirstSearch.Marked(0); var breadthFirstSearchTest1 = breadthFirstSearch.Marked(1); var breadthFirstSearchTest2 = breadthFirstSearch.Marked(2); var breadthFirstSearchTest3 = breadthFirstSearch.Marked(3); var breadthFirstSearchTest4 = breadthFirstSearch.Marked(4); var breadthFirstSearchTest5 = breadthFirstSearch.Marked(5); var breadthFirstSearchTest6 = breadthFirstSearch.Marked(6); var breadthFirstSearchTestConnected = breadthFirstSearchCount == graph.Vertices ? "Connected" : "NOT Connected"; var breadthFirstPath = new BreadthFirstPaths(graph, 0); var breadthFirstPathTest = breadthFirstPath.PathTo(2); var depthFirstCycle = new DepthFirstCycle(graph); var depthFirstCycleTest0 = depthFirstCycle.HasCycle(); var depthFirstCycleTest1 = depthFirstCycle.Cycle(); var graph2 = new DirectedGraph(13); graph2.InsertEdge(2, 3); graph2.InsertEdge(0, 6); graph2.InsertEdge(0, 1); graph2.InsertEdge(2, 0); graph2.InsertEdge(11, 12); graph2.InsertEdge(9, 12); graph2.InsertEdge(9, 10); graph2.InsertEdge(9, 11); graph2.InsertEdge(3, 5); graph2.InsertEdge(8, 7); graph2.InsertEdge(5, 4); graph2.InsertEdge(0, 5); graph2.InsertEdge(6, 4); graph2.InsertEdge(6, 9); graph2.InsertEdge(7, 6); var depthFirstCycle2 = new DepthFirstCycle(graph2); var depthFirstCycle2Test0 = depthFirstCycle2.HasCycle(); var depthFirstCycle2Test1 = depthFirstCycle2.Cycle(); var depthFirstOrder = new DepthFirstOrder(graph2); var depthFirstOrderTest0 = depthFirstOrder.PreOrder(); var depthFirstOrderTest1 = depthFirstOrder.PostOrder(); var depthFirstOrderTest2 = depthFirstOrder.ReversePostOrder(); var symbolGraph = new SymbolGraph(); symbolGraph.Insert(new[] { "JFK", "MCO" }); symbolGraph.Insert(new[] { "ORD", "DEN" }); symbolGraph.Insert(new[] { "ORD", "HOU" }); symbolGraph.Insert(new[] { "DFW", "PHX" }); symbolGraph.Insert(new[] { "JFK", "ATL" }); symbolGraph.Insert(new[] { "ORD", "DFW" }); symbolGraph.Insert(new[] { "ORD", "PHX" }); symbolGraph.Insert(new[] { "ATL", "HOU" }); symbolGraph.Insert(new[] { "DEN", "PHX" }); symbolGraph.Insert(new[] { "PHX", "LAX" }); symbolGraph.Insert(new[] { "JFK", "ORD" }); symbolGraph.Insert(new[] { "DEN", "LAS" }); symbolGraph.Insert(new[] { "DFW", "HOU" }); symbolGraph.Insert(new[] { "ORD", "ATL" }); symbolGraph.Insert(new[] { "LAS", "LAX" }); symbolGraph.Insert(new[] { "ATL", "MCO" }); symbolGraph.Insert(new[] { "HOU", "MCO" }); symbolGraph.Insert(new[] { "LAS", "PHX" }); symbolGraph.Build(); var symbolGraphTest0 = symbolGraph.Contains("LAS"); var symbolGraphTest1 = symbolGraph.Contains("MOO"); var symbolGraphTest2 = symbolGraph.Index("LAX"); var symbolGraphTest3 = symbolGraph.Name(symbolGraphTest2); var symbolGraph2 = new SymbolGraph(); symbolGraph2.Insert(new[] { "Algorithms", "Theoretical CS", "Databases", "Scientific Computing" }); symbolGraph2.Insert(new[] { "Introduction to CS", "Advanced Programming", "Algorithms" }); symbolGraph2.Insert(new[] { "Advanced Programming", "Scientific Computing" }); symbolGraph2.Insert(new[] { "Scientific Computing", "Computational Biology" }); symbolGraph2.Insert(new[] { "Theoretical CS", "Computational Biology", "Artificial Intelligence" }); symbolGraph2.Insert(new[] { "Linear Algebra", "Theoretical CS" }); symbolGraph2.Insert(new[] { "Calculus", "Linear Algebra" }); symbolGraph2.Insert(new[] { "Artificial Intelligence", "Neural Networks", "Robotics", "Machine Learning" }); symbolGraph2.Insert(new[] { "Machine Learning", "Neural Networks" }); symbolGraph2.Build(); var topologicalSort = new TopologicalSort(symbolGraph2.Graph()); var topologicalSorted = new List <string>(); foreach (var item in topologicalSort.Order) { topologicalSorted.Add(symbolGraph2.Name(item)); } var depthFirstComponents = new DepthFirstComponents(graph); var depthFirstComponentsTest0 = depthFirstComponents.IsStronglyConnected(1, 7); var depthFirstComponentsTest1 = depthFirstComponents.IsStronglyConnected(0, 2); var depthFirstComponentsTest2 = depthFirstComponents.IsStronglyConnected(2, 7); var depthFirstComponentsTest3 = depthFirstComponents.IsStronglyConnected(9, 12); var depthFirstComponentsTest4 = depthFirstComponents.IsStronglyConnected(6, 8); var depthFirstComponentsTest5 = depthFirstComponents.IsStronglyConnected(7, 8); }