// GET: /<controller>/ public IActionResult Index() { string result = string.Empty; var graph = new UndirectedSparseGraph <string>(); // Add vertices var verticesSet1 = new string[] { "a", "b", "c", "d", "e", "f", "s", "v", "x", "y", "z" }; graph.AddVertices(verticesSet1); result = result + "Vertices: " + "'a', 'b', 'c', 'd', 'e', 'f', 's', 'v', 'x', 'y', 'z'" + "\n\n"; // Add edges // Connected Component #1 // the vertex "e" won't be connected to any other vertex // Connected Component #2 graph.AddEdge("a", "s"); graph.AddEdge("a", "d"); graph.AddEdge("s", "x"); graph.AddEdge("x", "d"); // Connected Component #3 graph.AddEdge("b", "c"); graph.AddEdge("b", "v"); graph.AddEdge("c", "f"); graph.AddEdge("c", "v"); graph.AddEdge("f", "b"); // Connected Component #4 graph.AddEdge("y", "z"); result = result + "Edges: "; result = result + graph.ToReadable() + "\r\n\n"; // Get connected components var connectedComponents = ConnectedComponents.Compute(graph); connectedComponents = connectedComponents.OrderBy(item => item.Count).ToList(); result = result + "# of Connected Components: " + connectedComponents.Count() + "\n"; result = result + "Components are: \n"; foreach (var items in connectedComponents) { string edge = string.Empty; foreach (var item in items) { edge = edge + item + " -> "; } result = result + edge.Remove(edge.Length - 4) + "\n"; } HtmlString html = StringHelper.GetHtmlString(result); return(View(html)); }
public static void DoTest() { string[] V; DirectedSparseGraph <string> DAG; UndirectedSparseGraph <string> CyclicGraph; DirectedSparseGraph <string> DigraphWithCycles; // Init graph object DigraphWithCycles = new DirectedSparseGraph <string>(); // Init V V = new string[6] { "r", "s", "t", "x", "y", "z" }; // Insert V DigraphWithCycles.AddVertices(V); // Insert E DigraphWithCycles.AddEdge("r", "s"); DigraphWithCycles.AddEdge("r", "t"); DigraphWithCycles.AddEdge("s", "t"); DigraphWithCycles.AddEdge("s", "x"); DigraphWithCycles.AddEdge("t", "x"); DigraphWithCycles.AddEdge("t", "y"); DigraphWithCycles.AddEdge("t", "z"); DigraphWithCycles.AddEdge("x", "y"); DigraphWithCycles.AddEdge("x", "z"); DigraphWithCycles.AddEdge("y", "z"); DigraphWithCycles.AddEdge("z", "r"); DigraphWithCycles.AddEdge("z", "s"); var isCyclic = CyclesDetector.IsCyclic(DigraphWithCycles); Debug.Assert(isCyclic, "Wrong status! The graph has cycles."); // PRINT THE GRAPH Console.WriteLine("[*] Directed Graph:"); Console.WriteLine(DigraphWithCycles.ToReadable() + "\r\n"); Console.WriteLine("Was the previous graph cyclic? " + isCyclic); Console.WriteLine("\r\n*********************************************\r\n"); /***************************************************************************************/ CyclicGraph = new UndirectedSparseGraph <string>(); V = new[] { "A", "B", "C", "D", "E" }; // Insert new values of V CyclicGraph.AddVertices(V); // Insert new value for edges CyclicGraph.AddEdge("A", "C"); CyclicGraph.AddEdge("B", "A"); CyclicGraph.AddEdge("B", "C"); CyclicGraph.AddEdge("C", "E"); CyclicGraph.AddEdge("C", "D"); CyclicGraph.AddEdge("D", "B"); CyclicGraph.AddEdge("E", "D"); isCyclic = CyclesDetector.IsCyclic(CyclicGraph); Debug.Assert(isCyclic, "Wrong status! The graph has cycles."); // PRINT THE GRAPH Console.WriteLine("[*] Undirected Graph:"); Console.WriteLine(CyclicGraph.ToReadable() + "\r\n"); Console.WriteLine("Was the previous graph cyclic? " + isCyclic); Console.WriteLine("\r\n*********************************************\r\n"); /***************************************************************************************/ DAG = new DirectedSparseGraph <string>(); V = new[] { "A", "B", "C", "D", "E", "X" }; // Insert new values of V DAG.AddVertices(V); // Insert new value for edges DAG.AddEdge("A", "B"); DAG.AddEdge("A", "X"); DAG.AddEdge("B", "C"); DAG.AddEdge("C", "D"); DAG.AddEdge("D", "E"); DAG.AddEdge("E", "X"); isCyclic = CyclesDetector.IsCyclic(DAG); Debug.Assert(isCyclic == false, "Wrong status! The graph has no cycles."); // PRINT THE GRAPH Console.WriteLine("[*] DAG (Directed Asyclic Graph):"); Console.WriteLine(DAG.ToReadable() + "\r\n"); Console.WriteLine("Was the previous graph cyclic? " + isCyclic); Console.ReadLine(); }
public static void DoTest() { var graph = new UndirectedSparseGraph <string>(); var verticesSet1 = new string[] { "a", "z", "s", "x", "d", "c", "f", "v" }; graph.AddVertices(verticesSet1); 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"); var allEdges = graph.Edges.ToList(); Debug.Assert(graph.VerticesCount == 8, "Wrong vertices count."); Debug.Assert(graph.EdgesCount == 10, "Wrong edges count."); Debug.Assert(graph.EdgesCount == allEdges.Count, "Wrong edges count."); Debug.Assert(graph.OutgoingEdges("a").ToList().Count == 2, "Wrong outgoing edges from 'a'."); Debug.Assert(graph.OutgoingEdges("s").ToList().Count == 2, "Wrong outgoing edges from 's'."); Debug.Assert(graph.OutgoingEdges("x").ToList().Count == 3, "Wrong outgoing edges from 'x'."); Debug.Assert(graph.OutgoingEdges("d").ToList().Count == 3, "Wrong outgoing edges from 'd'."); Debug.Assert(graph.OutgoingEdges("c").ToList().Count == 4, "Wrong outgoing edges from 'c'."); Debug.Assert(graph.OutgoingEdges("v").ToList().Count == 2, "Wrong outgoing edges from 'v'."); Debug.Assert(graph.OutgoingEdges("f").ToList().Count == 3, "Wrong outgoing edges from 'f'."); Debug.Assert(graph.OutgoingEdges("z").ToList().Count == 1, "Wrong outgoing edges from 'z'."); Debug.Assert(graph.IncomingEdges("a").ToList().Count == 2, "Wrong incoming edges from 'a'."); Debug.Assert(graph.IncomingEdges("s").ToList().Count == 2, "Wrong incoming edges from 's'."); Debug.Assert(graph.IncomingEdges("x").ToList().Count == 3, "Wrong incoming edges from 'x'."); Debug.Assert(graph.IncomingEdges("d").ToList().Count == 3, "Wrong incoming edges from 'd'."); Debug.Assert(graph.IncomingEdges("c").ToList().Count == 4, "Wrong incoming edges from 'c'."); Debug.Assert(graph.IncomingEdges("v").ToList().Count == 2, "Wrong incoming edges from 'v'."); Debug.Assert(graph.IncomingEdges("f").ToList().Count == 3, "Wrong incoming edges from 'f'."); Debug.Assert(graph.IncomingEdges("z").ToList().Count == 1, "Wrong incoming edges from 'z'."); Console.WriteLine("[*] Undirected Sparse Graph:"); Console.WriteLine("Graph nodes and edges:"); Console.WriteLine(graph.ToReadable() + "\r\n"); graph.RemoveEdge("d", "c"); graph.RemoveEdge("c", "v"); graph.RemoveEdge("a", "z"); Debug.Assert(graph.VerticesCount == 8, "Wrong vertices count."); Debug.Assert(graph.EdgesCount == 7, "Wrong edges count."); Console.WriteLine("After removing edges (d-c), (c-v), (a-z):"); Console.WriteLine(graph.ToReadable() + "\r\n"); graph.RemoveVertex("x"); Debug.Assert(graph.VerticesCount == 7, "Wrong vertices count."); Debug.Assert(graph.EdgesCount == 4, "Wrong edges count."); Console.WriteLine("After removing node (x):"); Console.WriteLine(graph.ToReadable() + "\r\n"); graph.AddVertex("x"); graph.AddEdge("s", "x"); graph.AddEdge("x", "d"); graph.AddEdge("x", "c"); graph.AddEdge("d", "c"); graph.AddEdge("c", "v"); graph.AddEdge("a", "z"); Console.WriteLine("Re-added the deleted vertices and edges to the graph."); Console.WriteLine(graph.ToReadable() + "\r\n"); Console.WriteLine("Walk the graph using BFS:"); graph.BreadthFirstWalk("s"); // output: (s) (a) (x) (z) (d) (c) (f) (v) Console.WriteLine("\r\n"); /********************************************************************/ Console.WriteLine("***************************************************\r\n"); graph.Clear(); Console.WriteLine("Cleared the graph from all vertices and edges.\r\n"); var verticesSet2 = new string[] { "a", "b", "c", "d", "e", "f" }; graph.AddVertices(verticesSet2); graph.AddEdge("a", "b"); graph.AddEdge("a", "d"); graph.AddEdge("b", "e"); graph.AddEdge("d", "b"); graph.AddEdge("d", "e"); graph.AddEdge("e", "c"); graph.AddEdge("c", "f"); graph.AddEdge("f", "f"); Debug.Assert(graph.VerticesCount == 6, "Wrong vertices count."); Debug.Assert(graph.EdgesCount == 8, "Wrong edges count."); Console.WriteLine("[*] NEW Undirected Sparse Graph:"); Console.WriteLine("Graph nodes and edges:"); Console.WriteLine(graph.ToReadable() + "\r\n"); Console.WriteLine("Walk the graph using DFS:"); graph.DepthFirstWalk(); // output: (a) (b) (e) (d) (c) (f) }
public static void DoTest() { var graph = new UndirectedSparseGraph<string>(); var verticesSet1 = new string[] { "a", "z", "s", "x", "d", "c", "f", "v" }; graph.AddVertices(verticesSet1); 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"); var allEdges = graph.Edges.ToList(); Debug.Assert(graph.VerticesCount == 8, "Wrong vertices count."); Debug.Assert(graph.EdgesCount == 10, "Wrong edges count."); Debug.Assert(graph.EdgesCount == allEdges.Count, "Wrong edges count."); Debug.Assert(graph.OutgoingEdges("a").ToList().Count == 2, "Wrong outgoing edges from 'a'."); Debug.Assert(graph.OutgoingEdges("s").ToList().Count == 2, "Wrong outgoing edges from 's'."); Debug.Assert(graph.OutgoingEdges("x").ToList().Count == 3, "Wrong outgoing edges from 'x'."); Debug.Assert(graph.OutgoingEdges("d").ToList().Count == 3, "Wrong outgoing edges from 'd'."); Debug.Assert(graph.OutgoingEdges("c").ToList().Count == 4, "Wrong outgoing edges from 'c'."); Debug.Assert(graph.OutgoingEdges("v").ToList().Count == 2, "Wrong outgoing edges from 'v'."); Debug.Assert(graph.OutgoingEdges("f").ToList().Count == 3, "Wrong outgoing edges from 'f'."); Debug.Assert(graph.OutgoingEdges("z").ToList().Count == 1, "Wrong outgoing edges from 'z'."); Debug.Assert(graph.IncomingEdges("a").ToList().Count == 2, "Wrong incoming edges from 'a'."); Debug.Assert(graph.IncomingEdges("s").ToList().Count == 2, "Wrong incoming edges from 's'."); Debug.Assert(graph.IncomingEdges("x").ToList().Count == 3, "Wrong incoming edges from 'x'."); Debug.Assert(graph.IncomingEdges("d").ToList().Count == 3, "Wrong incoming edges from 'd'."); Debug.Assert(graph.IncomingEdges("c").ToList().Count == 4, "Wrong incoming edges from 'c'."); Debug.Assert(graph.IncomingEdges("v").ToList().Count == 2, "Wrong incoming edges from 'v'."); Debug.Assert(graph.IncomingEdges("f").ToList().Count == 3, "Wrong incoming edges from 'f'."); Debug.Assert(graph.IncomingEdges("z").ToList().Count == 1, "Wrong incoming edges from 'z'."); Console.WriteLine("[*] Undirected Sparse Graph:"); Console.WriteLine("Graph nodes and edges:"); Console.WriteLine(graph.ToReadable() + "\r\n"); graph.RemoveEdge("d", "c"); graph.RemoveEdge("c", "v"); graph.RemoveEdge("a", "z"); Debug.Assert(graph.VerticesCount == 8, "Wrong vertices count."); Debug.Assert(graph.EdgesCount == 7, "Wrong edges count."); Console.WriteLine("After removing edges (d-c), (c-v), (a-z):"); Console.WriteLine(graph.ToReadable() + "\r\n"); graph.RemoveVertex("x"); Debug.Assert(graph.VerticesCount == 7, "Wrong vertices count."); Debug.Assert(graph.EdgesCount == 4, "Wrong edges count."); Console.WriteLine("After removing node (x):"); Console.WriteLine(graph.ToReadable() + "\r\n"); graph.AddVertex("x"); graph.AddEdge("s", "x"); graph.AddEdge("x", "d"); graph.AddEdge("x", "c"); graph.AddEdge("d", "c"); graph.AddEdge("c", "v"); graph.AddEdge("a", "z"); Console.WriteLine("Re-added the deleted vertices and edges to the graph."); Console.WriteLine(graph.ToReadable() + "\r\n"); Console.WriteLine("Walk the graph using BFS:"); graph.BreadthFirstWalk("s"); // output: (s) (a) (x) (z) (d) (c) (f) (v) Console.WriteLine("\r\n"); /********************************************************************/ Console.WriteLine("***************************************************\r\n"); graph.Clear(); Console.WriteLine("Cleared the graph from all vertices and edges.\r\n"); var verticesSet2 = new string[] { "a", "b", "c", "d", "e", "f" }; graph.AddVertices(verticesSet2); graph.AddEdge("a", "b"); graph.AddEdge("a", "d"); graph.AddEdge("b", "e"); graph.AddEdge("d", "b"); graph.AddEdge("d", "e"); graph.AddEdge("e", "c"); graph.AddEdge("c", "f"); graph.AddEdge("f", "f"); Debug.Assert(graph.VerticesCount == 6, "Wrong vertices count."); Debug.Assert(graph.EdgesCount == 8, "Wrong edges count."); Console.WriteLine("[*] NEW Undirected Sparse Graph:"); Console.WriteLine("Graph nodes and edges:"); Console.WriteLine(graph.ToReadable() + "\r\n"); Console.WriteLine("Walk the graph using DFS:"); graph.DepthFirstWalk(); // output: (a) (b) (e) (d) (c) (f) }
public static void DoTest() { string[] V; DirectedSparseGraph<string> DAG; UndirectedSparseGraph<string> CyclicGraph; DirectedSparseGraph<string> DigraphWithCycles; // Init graph object DigraphWithCycles = new DirectedSparseGraph<string>(); // Init V V = new string[6] { "r", "s", "t", "x", "y", "z" }; // Insert V DigraphWithCycles.AddVertices(V); // Insert E DigraphWithCycles.AddEdge("r", "s"); DigraphWithCycles.AddEdge("r", "t"); DigraphWithCycles.AddEdge("s", "t"); DigraphWithCycles.AddEdge("s", "x"); DigraphWithCycles.AddEdge("t", "x"); DigraphWithCycles.AddEdge("t", "y"); DigraphWithCycles.AddEdge("t", "z"); DigraphWithCycles.AddEdge("x", "y"); DigraphWithCycles.AddEdge("x", "z"); DigraphWithCycles.AddEdge("y", "z"); DigraphWithCycles.AddEdge("z", "r"); DigraphWithCycles.AddEdge("z", "s"); var isCyclic = CyclesDetector.IsCyclic<string>(DigraphWithCycles); Debug.Assert(isCyclic == true, "Wrong status! The graph has cycles."); // PRINT THE GRAPH Console.WriteLine("[*] Directed Graph:"); Console.WriteLine(DigraphWithCycles.ToReadable() + "\r\n"); Console.WriteLine("Was the previous graph cyclic? " + isCyclic); Console.WriteLine("\r\n*********************************************\r\n"); /***************************************************************************************/ CyclicGraph = new UndirectedSparseGraph<string>(); V = new string[] { "A", "B", "C", "D", "E" }; // Insert new values of V CyclicGraph.AddVertices(V); // Insert new value for edges CyclicGraph.AddEdge("A", "C"); CyclicGraph.AddEdge("B", "A"); CyclicGraph.AddEdge("B", "C"); CyclicGraph.AddEdge("C", "E"); CyclicGraph.AddEdge("C", "D"); CyclicGraph.AddEdge("D", "B"); CyclicGraph.AddEdge("E", "D"); isCyclic = CyclesDetector.IsCyclic<string>(CyclicGraph); Debug.Assert(isCyclic == true, "Wrong status! The graph has cycles."); // PRINT THE GRAPH Console.WriteLine("[*] Undirected Graph:"); Console.WriteLine(CyclicGraph.ToReadable() + "\r\n"); Console.WriteLine("Was the previous graph cyclic? " + isCyclic); Console.WriteLine("\r\n*********************************************\r\n"); /***************************************************************************************/ DAG = new DirectedSparseGraph<string>(); V = new string[] { "A", "B", "C", "D", "E", "X" }; // Insert new values of V DAG.AddVertices(V); // Insert new value for edges DAG.AddEdge("A", "B"); DAG.AddEdge("A", "X"); DAG.AddEdge("B", "C"); DAG.AddEdge("C", "D"); DAG.AddEdge("D", "E"); DAG.AddEdge("E", "X"); isCyclic = CyclesDetector.IsCyclic<string>(DAG); Debug.Assert(isCyclic == false, "Wrong status! The graph has no cycles."); // PRINT THE GRAPH Console.WriteLine("[*] DAG (Directed Asyclic Graph):"); Console.WriteLine(DAG.ToReadable() + "\r\n"); Console.WriteLine("Was the previous graph cyclic? " + isCyclic); Console.ReadLine(); }