void Start() { //SymbolGraph sg = new SymbolGraph(txt); //Graph graph = sg.Graph(); //string source = "JFK"; //输入一个顶点名并,用于获得该顶点相连顶点链表 //if (sg.Contains(source)) //{ // int s = sg.IndexOf(source); // foreach (int v in graph.Adj(s)) // { // print(" " + sg.NameOf(v)); // } //} //else print("input not contain '" + source + "'"); SymbolGraph sg = new SymbolGraph(txt.text, new char[] { '/' }); Graph graph = sg.Graph(); string source = "2 Days in the Valley (1996)"; //输入一个顶点名并,用于获得该顶点相连顶点链表 if (sg.Contains(source)) { int s = sg.IndexOf(source); foreach (int v in graph.Adj(s)) { print(" " + sg.NameOf(v)); } } else { print("input not contain '" + source + "'"); } }
void Start() { string source = "JFK"; SymbolGraph sg = new SymbolGraph(txt); Graph G = sg.Graph(); if (!sg.Contains(source)) { print(source + " not in database."); return; } int s = sg.IndexOf(source); BreadthFirstPaths bfs = new BreadthFirstPaths(G, s); string sink = "DFW"; if (sg.Contains(sink)) { int t = sg.IndexOf(sink); if (bfs.hasPathTo(t)) { foreach (int v in bfs.pathTo(t)) { print(" " + sg.NameOf(v)); } } else { print("Not connected"); } } else { print(" Not in database."); } }
public void SymbolGraphTest1() { // NOTE: the file has to be in the current working directory for unit tests string usastates = "us-state-neighbors.txt"; SymbolGraph sg; Graph G; try { sg = new SymbolGraph(usastates, " "); } catch (Exception ex) { Assert.Fail(string.Format("Symbol graph construction error: {0}", ex.Message)); return; } G = sg.G; string state = "HI"; Assert.IsTrue(sg.Contains(state)); string[] neighbors = new string[] { "CA" }; SET <string> expected = new SET <string>(neighbors); SET <string> actual = TestHelpers.getAdjacents(sg, state); Assert.AreEqual(actual, expected); state = "CA"; neighbors = new string[] { "OR", "NV", "AZ", "HI" }; expected = new SET <string>(neighbors); actual = TestHelpers.getAdjacents(sg, state); Assert.AreEqual(actual, expected); state = "IA"; neighbors = new string[] { "MN", "WI", "MO", "NE", "SD", "IL" }; expected = new SET <string>(neighbors); actual = TestHelpers.getAdjacents(sg, state); Assert.AreEqual(actual, expected); // more ... }
public void Run() { Console.WriteLine("Choose file:"); // Prompt Console.WriteLine("1 - routes.txt"); // Prompt Console.WriteLine("2 - movies.txt"); // Prompt //Console.WriteLine("3 - largeG.zip"); // Prompt Console.WriteLine("or quit"); // Prompt var fileNumber = Console.ReadLine(); string fileName; char delimiter; List <string> names; switch (fileNumber) { case "1": fileName = "routes.txt"; delimiter = '\u0020'; names = new List <string> { "JFK", "LAX" }; break; case "2": fileName = "movies.txt"; delimiter = '/'; names = new List <string> { "Tin Men (1987)", "Bacon, Kevin" }; break; //case "3": // fieName = "largeG.zip"; // break; case "quit": return; default: return; } var @in = new In($"Files\\Graphs\\{fileName}"); var lines = !fileName.EndsWith("zip") ? @in.ReadAllLines() : @in.ReadAllLinesFromZip(); var sg = new SymbolGraph(lines, delimiter); var graph = sg.G; foreach (var name in names) { Console.WriteLine($"{name}"); if (sg.Contains(name)) { var s = sg.Index(name); foreach (int v in graph.Adj(s)) { Console.WriteLine($" {sg.Name(v)}"); } } else { Console.WriteLine($"input not contain '{name}'"); } } 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); }