public void DFS_Test() { DepthFirstPaths DFS = new DepthFirstPaths(basicGraph, 0); Assert.IsTrue(DFS.HasPathTo(3)); Assert.IsFalse(DFS.HasPathTo(8)); }
public void HasPathToTest() // Assert that given Vertices are ether in the graph or not. { Graph G = new Graph(11); DepthFirstPaths testObject; G.AddEdge(0, 1); // vertices 0 through 8 are in the graph. 9 and 10 are not. 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); // First assert that a graph has a path to it. for (int i = 0; i < 9; i++) // 0 through 8 { Assert.IsTrue(testObject.HasPathTo(i), i.ToString()); } // Second assert that a graph does not have a path to it. Assert.IsFalse(testObject.HasPathTo(9)); //9 and 10 Assert.IsFalse(testObject.HasPathTo(10)); }
//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 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); } } }
public void HasPathTo_WhenPathDoesNotExistBetweenVertices_WillReturnFalse() { Graph graph = new Graph(5); graph.AddEdge(0, 1); graph.AddEdge(1, 2); graph.AddEdge(1, 4); DepthFirstPaths paths = new DepthFirstPaths(graph, 0); Assert.IsFalse(paths.HasPathTo(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()}"); }
public void HasPathTo_WhenPathExistsBetweenVertices_WillReturnTrue() { 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); Assert.IsTrue(paths.HasPathTo(0)); }
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; }
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 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(); }