public void DoTest() { var V01 = new string[] { "A", "B", "C", "D", "E", "X" }; var DAG01 = new DirectedSparseGraph <string>(); // Insert new values of V DAG01.AddVertices(V01); // Insert new value for edges DAG01.AddEdge("A", "B"); DAG01.AddEdge("A", "X"); DAG01.AddEdge("B", "C"); DAG01.AddEdge("C", "D"); DAG01.AddEdge("D", "E"); DAG01.AddEdge("E", "X"); // PRINT THE GRAPH // [*] DAG (Directed Asyclic Graph): // CALCULATE THE TOPOLOGICAL SORT var topologicalSort01 = TopologicalSorter.Sort <string>(DAG01); var output01 = string.Empty; foreach (var node in topologicalSort01) { output01 = String.Format("{0}({1}) ", output01, node); } var V02 = new int[] { 7, 5, 3, 11, 8, 2, 9, 10 }; var DAG02 = new DirectedSparseGraph <int>(); // Insert new values of V DAG02.AddVertices(V02); // Insert new value for edges DAG02.AddEdge(7, 11); DAG02.AddEdge(7, 8); DAG02.AddEdge(5, 11); DAG02.AddEdge(3, 8); DAG02.AddEdge(3, 10); DAG02.AddEdge(11, 2); DAG02.AddEdge(11, 9); DAG02.AddEdge(11, 10); DAG02.AddEdge(8, 9); // PRINT THE GRAPH // [*] DAG (Directed Asyclic Graph): // CALCULATE THE TOPOLOGICAL SORT var topologicalSort02 = TopologicalSorter.Sort <int>(DAG02); var output02 = string.Empty; foreach (var node in topologicalSort02) { output02 = String.Format("{0}({1}) ", output02, node); } }
public static void UnitTestD() { var graph = new DirectedSparseGraph <Employee>(); var employeeA = new Employee("a", "", 10); var employeeB = new Employee("b", "a", 10); var employeeC = new Employee("c", "a", 10); var employeeD = new Employee("d", "a", 10); var employeeE = new Employee("e", "b", 10); var verticesSet1 = new Employee[] { employeeA, employeeB, employeeC, employeeD, employeeE }; graph.AddVertices(verticesSet1); graph.AddEdge(employeeA, employeeB); graph.AddEdge(employeeA, employeeC); graph.AddEdge(employeeA, employeeD); graph.AddEdge(employeeB, employeeE); var allEdges = graph.Edges.ToList(); Assert.True(graph.VerticesCount == 5, "Wrong vertices count."); Assert.True(graph.EdgesCount == 4, "Wrong edges count."); Assert.True(graph.EdgesCount == allEdges.Count, "Wrong edges count."); Assert.True(graph.OutgoingEdges(employeeA).ToList().Count == 3, "Wrong outgoing edges from 'a'."); Assert.True(graph.OutgoingEdges(employeeB).ToList().Count == 1, "Wrong outgoing edges from 'b'."); Assert.True(graph.OutgoingEdges(employeeC).ToList().Count == 0, "Wrong outgoing edges from 'c'."); Assert.True(graph.OutgoingEdges(employeeD).ToList().Count == 0, "Wrong outgoing edges from 'd'."); Assert.True(graph.OutgoingEdges(employeeE).ToList().Count == 0, "Wrong outgoing edges from 'e'."); Assert.True(graph.IncomingEdges(employeeA).ToList().Count == 0, "Wrong incoming edges from 'a'."); Assert.True(graph.IncomingEdges(employeeB).ToList().Count == 1, "Wrong incoming edges from 'b'."); Assert.True(graph.IncomingEdges(employeeC).ToList().Count == 1, "Wrong incoming edges from 'c'."); Assert.True(graph.IncomingEdges(employeeD).ToList().Count == 1, "Wrong incoming edges from 'd'."); Assert.True(graph.IncomingEdges(employeeE).ToList().Count == 1, "Wrong incoming edges from 'e'."); var dfsWalk = graph.DepthFirstWalk(employeeA); foreach (var node in dfsWalk) { Console.Write(String.Format("({0})", node.Id)); } graph.Clear(); }
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 DirectedSparseGraph<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("x", "a"); graph.AddEdge("d", "f"); graph.AddEdge("d", "c"); graph.AddEdge("d", "s"); graph.AddEdge("c", "f"); graph.AddEdge("c", "v"); graph.AddEdge("c", "d"); graph.AddEdge("v", "f"); graph.AddEdge("f", "c"); var allEdges = graph.Edges.ToList(); Debug.Assert(graph.VerticesCount == 8, "Wrong vertices count."); Debug.Assert(graph.EdgesCount == 14, "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 == 1, "Wrong outgoing edges from 's'."); Debug.Assert(graph.OutgoingEdges("d").ToList().Count == 3, "Wrong outgoing edges from 'd'."); Debug.Assert(graph.OutgoingEdges("x").ToList().Count == 3, "Wrong outgoing edges from 'x'."); Debug.Assert(graph.OutgoingEdges("c").ToList().Count == 3, "Wrong outgoing edges from 'c'."); Debug.Assert(graph.OutgoingEdges("v").ToList().Count == 1, "Wrong outgoing edges from 'v'."); Debug.Assert(graph.OutgoingEdges("f").ToList().Count == 1, "Wrong outgoing edges from 'f'."); Debug.Assert(graph.OutgoingEdges("z").ToList().Count == 0, "Wrong outgoing edges from 'z'."); Debug.Assert(graph.IncomingEdges("a").ToList().Count == 1, "Wrong incoming edges from 'a'."); Debug.Assert(graph.IncomingEdges("s").ToList().Count == 2, "Wrong incoming edges from 's'."); Debug.Assert(graph.IncomingEdges("d").ToList().Count == 2, "Wrong incoming edges from 'd'."); Debug.Assert(graph.IncomingEdges("x").ToList().Count == 1, "Wrong incoming edges from 'x'."); Debug.Assert(graph.IncomingEdges("c").ToList().Count == 3, "Wrong incoming edges from 'c'."); Debug.Assert(graph.IncomingEdges("v").ToList().Count == 1, "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("[*] Directed 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 == 11, "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 == 7, "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("x", "a"); 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"); // BFS from A Console.WriteLine("Walk the graph using BFS from A:"); var bfsWalk = graph.BreadthFirstWalk("a"); // output: (s) (a) (x) (z) (d) (c) (f) (v) foreach (var node in bfsWalk) Console.Write(String.Format("({0})", node)); Console.WriteLine("\r\n"); // DFS from A Console.WriteLine("Walk the graph using DFS from A:"); var dfsWalk = graph.DepthFirstWalk("a"); // output: (s) (a) (x) (z) (d) (c) (f) (v) foreach (var node in dfsWalk) Console.Write(String.Format("({0})", node)); Console.WriteLine("\r\n"); // BFS from F Console.WriteLine("Walk the graph using BFS from F:"); bfsWalk = graph.BreadthFirstWalk("f"); // output: (s) (a) (x) (z) (d) (c) (f) (v) foreach (var node in bfsWalk) Console.Write(String.Format("({0})", node)); Console.WriteLine("\r\n"); // DFS from F Console.WriteLine("Walk the graph using DFS from F:"); dfsWalk = graph.DepthFirstWalk("f"); // output: (s) (a) (x) (z) (d) (c) (f) (v) foreach (var node in dfsWalk) Console.Write(String.Format("({0})", node)); Console.WriteLine("\r\n"); Console.ReadLine(); /********************************************************************/ 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 Directed Sparse Graph:"); Console.WriteLine("Graph nodes and edges:"); Console.WriteLine(graph.ToReadable() + "\r\n"); Console.WriteLine("Walk the graph using DFS:"); dfsWalk = graph.DepthFirstWalk(); // output: (a) (b) (e) (d) (c) (f) foreach (var node in dfsWalk) Console.Write(String.Format("({0})", node)); Console.ReadLine(); }
public static void DoTest() { var V01 = new string[] { "A", "B", "C", "D", "E", "X" }; var DAG01 = new DirectedSparseGraph <string>(); // Insert new values of V DAG01.AddVertices(V01); // Insert new value for edges DAG01.AddEdge("A", "B"); DAG01.AddEdge("A", "X"); DAG01.AddEdge("B", "C"); DAG01.AddEdge("C", "D"); DAG01.AddEdge("D", "E"); DAG01.AddEdge("E", "X"); // PRINT THE GRAPH Console.WriteLine("[*] DAG (Directed Asyclic Graph):"); Console.WriteLine(DAG01.ToReadable() + "\r\n"); // CALCULATE THE TOPOLOGICAL SORT var topologicalSort01 = TopologicalSorter.Sort <string>(DAG01); var output01 = string.Empty; foreach (var node in topologicalSort01) { output01 = String.Format("{0}({1}) ", output01, node); } // PRINT THE TOPOLOGICAL SORT Console.WriteLine("Was the previous graph cyclic? " + output01); Console.WriteLine("\r\n*********************************************\r\n"); /**************************************************************************/ var V02 = new int[] { 7, 5, 3, 11, 8, 2, 9, 10 }; var DAG02 = new DirectedSparseGraph <int>(); // Insert new values of V DAG02.AddVertices(V02); // Insert new value for edges DAG02.AddEdge(7, 11); DAG02.AddEdge(7, 8); DAG02.AddEdge(5, 11); DAG02.AddEdge(3, 8); DAG02.AddEdge(3, 10); DAG02.AddEdge(11, 2); DAG02.AddEdge(11, 9); DAG02.AddEdge(11, 10); DAG02.AddEdge(8, 9); // PRINT THE GRAPH Console.WriteLine("[*] DAG (Directed Asyclic Graph):"); Console.WriteLine(DAG02.ToReadable() + "\r\n"); // CALCULATE THE TOPOLOGICAL SORT var topologicalSort02 = TopologicalSorter.Sort <int>(DAG02); var output02 = string.Empty; foreach (var node in topologicalSort02) { output02 = String.Format("{0}({1}) ", output02, node); } // PRINT THE TOPOLOGICAL SORT Console.WriteLine("Was the previous graph cyclic? " + output02); Console.WriteLine("\r\n*********************************************\r\n"); Console.ReadLine(); }
public SubstanceNetworkSystem() { network = new DirectedSparseGraph <Entity>(); }
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(); }
public static void DoTest() { // Init graph object var digraphWithCycles = new DirectedSparseGraph <string>(); // Init V var 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); Assert.True(isCyclic == true, "Wrong status! The graph has cycles."); var 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); Assert.True(isCyclic == true, "Wrong status! The graph has cycles."); var 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); Assert.True(isCyclic == false, "Wrong status! The graph has no cycles."); }
public static void DoTest() { var graph = new DirectedSparseGraph <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("x", "a"); graph.AddEdge("d", "f"); graph.AddEdge("d", "c"); graph.AddEdge("d", "s"); graph.AddEdge("c", "f"); graph.AddEdge("c", "v"); graph.AddEdge("c", "d"); graph.AddEdge("v", "f"); graph.AddEdge("f", "c"); Debug.Assert(graph.VerticesCount == 8, "Wrong vertices count."); Debug.Assert(graph.EdgesCount == 14, "Wrong edges count."); Console.WriteLine("[*] Directed 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 == 11, "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 == 7, "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("x", "a"); 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"); // BFS from A Console.WriteLine("Walk the graph using BFS from A:"); var bfsWalk = graph.BreadthFirstWalk("a"); // output: (s) (a) (x) (z) (d) (c) (f) (v) foreach (var node in bfsWalk) { Console.Write(String.Format("({0})", node)); } Console.WriteLine("\r\n"); // DFS from A Console.WriteLine("Walk the graph using DFS from A:"); var dfsWalk = graph.DepthFirstWalk("a"); // output: (s) (a) (x) (z) (d) (c) (f) (v) foreach (var node in dfsWalk) { Console.Write(String.Format("({0})", node)); } Console.WriteLine("\r\n"); // BFS from F Console.WriteLine("Walk the graph using BFS from F:"); bfsWalk = graph.BreadthFirstWalk("f"); // output: (s) (a) (x) (z) (d) (c) (f) (v) foreach (var node in bfsWalk) { Console.Write(String.Format("({0})", node)); } Console.WriteLine("\r\n"); // DFS from F Console.WriteLine("Walk the graph using DFS from F:"); dfsWalk = graph.DepthFirstWalk("f"); // output: (s) (a) (x) (z) (d) (c) (f) (v) foreach (var node in dfsWalk) { Console.Write(String.Format("({0})", node)); } Console.WriteLine("\r\n"); Console.ReadLine(); /********************************************************************/ 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 Directed Sparse Graph:"); Console.WriteLine("Graph nodes and edges:"); Console.WriteLine(graph.ToReadable() + "\r\n"); Console.WriteLine("Walk the graph using DFS:"); dfsWalk = graph.DepthFirstWalk(); // output: (a) (b) (e) (d) (c) (f) foreach (var node in dfsWalk) { Console.Write(String.Format("({0})", node)); } Console.ReadLine(); }
// GET: /<controller>/ public IActionResult Index() { 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" }; result = result + "Vertices: " + "'r', 's', 't', 'x', 'y', 'z'" + "\n"; // 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); // PRINT THE GRAPH result = result + "[*] Directed Graph:"; result = result + DigraphWithCycles.ToReadable() + "\r\n"; result = result + "Is directed graph above cyclic? " + "=> answer is " + isCyclic + "\n\n"; ///***************************************************************************************/ //CyclicGraph = new UndirectedSparseGraph<string>(); //V = new string[] { "A", "B", "C", "D", "E" }; //result = result + "Vertices: " + "'A', 'B', 'C', 'D', 'E'" + "\n"; //// 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); //// PRINT THE GRAPH //result = result + "[*] Undirected Graph:"; //result = result + CyclicGraph.ToReadable() + "\r\n"; //result = result + "Is un-directed graph above cyclic? " + "=> answer is " + isCyclic; //Console.WriteLine("\r\n*********************************************\r\n"); ///***************************************************************************************/ DAG = new DirectedSparseGraph <string>(); V = new string[] { "A", "B", "C", "D", "E", "X" }; result = result + "Vertices: " + "'A', 'B', 'C', 'D', 'E', 'X'" + "\n"; // 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 result = result + "[*] DAG(Directed Asyclic Graph): "; result = result + DAG.ToReadable() + "\r\n"; result = result + "Is directed graph above cyclic? " + "=> answer is " + isCyclic; 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 <string>(DigraphWithCycles); Assert.True(isCyclic == true, "Wrong status! The graph has cycles."); 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); Assert.True(isCyclic == true, "Wrong status! The graph has cycles."); 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); Assert.True(isCyclic == false, "Wrong status! The graph has no cycles."); }
public DependencyGraphService() { Graph = new DirectedSparseGraph <DependencyNode>(); }
/// <summary> /// Employees constructor takes an array of strings from readallline -System.IO /// Iterates through each record in the array using the GetEnumerator() and moving the cursor. /// </summary> /// <param name="lines"></param> public Employees(string[] lines) { nodeGraph = new DirectedSparseGraph <Worker>(); workers = new Dictionary <string, Worker>(); IEnumerable <string[]> rows = lines .Select(r => r.Split('\t')); IEnumerable <IEnumerable <string> > records = from row in rows select(from item in row select item); foreach (var n in records) { var p = n.GetEnumerator(); while (p.MoveNext()) { try { var data = p.Current.Split(','); if (string.IsNullOrEmpty(data[0])) { Debug.WriteLine("Invalid employee id- please resolve this!", debug); continue; } if (string.IsNullOrEmpty(data[1]) && Owner < 1) { Owner++; } else if (string.IsNullOrEmpty(data[1]) && Owner == 1) { Debug.WriteLine($"A company can have only one C.E.O {data[1]} Adding error", debug); continue; } // Salary check using a discard if (Int32.TryParse(data[2], out _)) { var empl = new Worker(data[0], data[1], int.Parse(data[2])); try { workers.Add(empl.Id, empl); } catch { //Employee appers twice in he dictionary<string,Worker> Debug.WriteLine($"{data[1]} Has alrady been added to the list", debug); } if (!nodeGraph.HasVertex(empl))//false { nodeGraph.AddVertex(empl); } } else { Debug.WriteLine($"Invalid salary value... for user {data[1]}", debug); } } catch (Exception e) { Debug.WriteLine(e.Message, debug); } } p.Dispose(); } ///Check for d-Linking foreach (KeyValuePair <string, Worker> keyValuePair in workers) { if (!string.IsNullOrEmpty(keyValuePair.Value.Manager)) { // check for double linking bool doubleLinked = false; foreach (Worker worker in nodeGraph.D_F_S(keyValuePair.Value).ToArray()) { if (worker.Equals(keyValuePair.Value.Manager)) { doubleLinked = true; break; } } // ensure that each employee has only one manager if (nodeGraph.IncomingEdges(keyValuePair.Value).ToArray().Length < 1 && !doubleLinked) { nodeGraph.AddEdge(workers[keyValuePair.Value.Manager], keyValuePair.Value); } else { if (nodeGraph.IncomingEdges(keyValuePair.Value).ToArray().Length >= 1) { Debug.WriteLine($"Employee {keyValuePair.Value.Id} have more than one manager", debug); //resume not returning } Debug.WriteLine("Double linking not allowed", debug); } } } }
public static void DoTest() { var graph = new DirectedSparseGraph <Employee>(); var employeeA = new Employee("a", "", 10); var employeeB = new Employee("b", "a", 10); var employeeC = new Employee("c", "a", 10); var employeeD = new Employee("d", "a", 10); var employeeE = new Employee("e", "b", 10); var verticesSet1 = new Employee[] { employeeA, employeeB, employeeC, employeeD, employeeE }; graph.AddVertices(verticesSet1); graph.AddEdge(employeeA, employeeB); graph.AddEdge(employeeA, employeeC); graph.AddEdge(employeeA, employeeD); graph.AddEdge(employeeB, employeeE); var allEdges = graph.Edges.ToList(); Assert.True(graph.VerticesCount == 5, "Wrong vertices count."); Assert.True(graph.EdgesCount == 4, "Wrong edges count."); Assert.True(graph.EdgesCount == allEdges.Count, "Wrong edges count."); Assert.True(graph.OutgoingEdges(employeeA).ToList().Count == 3, "Wrong outgoing edges from 'a'."); Assert.True(graph.OutgoingEdges(employeeB).ToList().Count == 1, "Wrong outgoing edges from 'b'."); Assert.True(graph.OutgoingEdges(employeeC).ToList().Count == 0, "Wrong outgoing edges from 'c'."); Assert.True(graph.OutgoingEdges(employeeD).ToList().Count == 0, "Wrong outgoing edges from 'd'."); Assert.True(graph.OutgoingEdges(employeeE).ToList().Count == 0, "Wrong outgoing edges from 'e'."); Assert.True(graph.IncomingEdges(employeeA).ToList().Count == 0, "Wrong incoming edges from 'a'."); Assert.True(graph.IncomingEdges(employeeB).ToList().Count == 1, "Wrong incoming edges from 'b'."); Assert.True(graph.IncomingEdges(employeeC).ToList().Count == 1, "Wrong incoming edges from 'c'."); Assert.True(graph.IncomingEdges(employeeD).ToList().Count == 1, "Wrong incoming edges from 'd'."); Assert.True(graph.IncomingEdges(employeeE).ToList().Count == 1, "Wrong incoming edges from 'e'."); // DFS from A // Walk the graph using DFS from A: var dfsWalk = graph.DepthFirstWalk(employeeA); // output: (s) (a) (x) (z) (d) (c) (f) (v) // foreach (var node in dfsWalk) // { // Console.Write(String.Format("({0})", node)); // } // DFS from F // Walk the graph using DFS from F: //dfsWalk = graph.DepthFirstWalk(employeeB); // output: (s) (a) (x) (z) (d) (c) (f) (v) foreach (var node in dfsWalk) { Console.Write(String.Format("({0})", node.Id)); } /********************************************************************/ graph.Clear(); }
private DirectedSparseGraph <DependantType> BuildTypeDependencyGraph() { var graph = new DirectedSparseGraph <DependantType>(); var processedTypes = new List <DependantType>(); foreach (var syntaxTree in _context.Compilation.SyntaxTrees) { var root = syntaxTree.GetRoot(); var semanticModel = _context.Compilation.GetSemanticModel(syntaxTree); var classNodes = root.DescendantNodes().OfType <ClassDeclarationSyntax>().ToList(); foreach (var classNode in classNodes) { DependantType type = null; var classSymbol = semanticModel.GetDeclaredSymbol(classNode) as INamedTypeSymbol; var processedType = processedTypes.FirstOrDefault(t => t.Name == classSymbol.Name); if (processedType != null) { // we already added this to the graph as a dependency earlier type = processedType; type.SemanticModel = semanticModel; type.NamedTypeSymbol = classSymbol; } else { type = new DependantType() { Name = classSymbol.Name, Namespace = classSymbol.ContainingNamespace.ToString(), ContainingAssembly = classSymbol.ContainingAssembly.Name, IsExternal = false, SemanticModel = semanticModel, NamedTypeSymbol = classSymbol, TypeKind = classSymbol.TypeKind }; processedTypes.Add(type); graph.AddVertex(type); } if (_context.Solution != null) { var referencesToClass = SymbolFinder.FindReferencesAsync(classSymbol, _context.Solution).Result; } var classDependencies = classNode.DescendantNodes() .Select(n => semanticModel.GetTypeInfo(n).Type) .Where(n => n != null) .Distinct() .ToList(); foreach (var dependency in classDependencies) { DependantType dep = null; var processedDep = processedTypes.FirstOrDefault(t => t.Name == dependency.Name); if (processedDep != null) { dep = processedDep; } else { dep = new DependantType() { Name = dependency.Name, Namespace = dependency.ContainingNamespace.ToString(), ContainingAssembly = dependency.ContainingAssembly.Name, NamedTypeSymbol = dependency as INamedTypeSymbol, TypeKind = dependency.TypeKind, IsExternal = dependency.ContainingAssembly.Name == "mscorlib" }; processedTypes.Add(dep); } if (!graph.HasVertex(dep)) { graph.AddVertex(dep); } graph.AddEdge(type, dep); } } var enumNodes = root.DescendantNodes().OfType <EnumDeclarationSyntax>().ToList(); foreach (var enumNode in enumNodes) { DependantType type = null; var enumSymbol = semanticModel.GetDeclaredSymbol(enumNode) as INamedTypeSymbol; var processedType = processedTypes.FirstOrDefault(t => t.Name == enumSymbol.Name); if (processedType != null) { // we already added this to the graph as a dependency earlier type = processedType; type.SemanticModel = semanticModel; type.NamedTypeSymbol = enumSymbol; } else { type = new DependantType() { Name = enumSymbol.Name, Namespace = enumSymbol.ContainingNamespace.ToString(), ContainingAssembly = enumSymbol.ContainingAssembly.Name, IsExternal = false, SemanticModel = semanticModel, NamedTypeSymbol = enumSymbol, TypeKind = enumSymbol.TypeKind }; processedTypes.Add(type); graph.AddVertex(type); } } } return(graph); }
public static void DoTest() { var graph = new DirectedSparseGraph <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("x", "a"); graph.AddEdge("d", "f"); graph.AddEdge("d", "c"); graph.AddEdge("d", "s"); graph.AddEdge("c", "f"); graph.AddEdge("c", "v"); graph.AddEdge("c", "d"); graph.AddEdge("v", "f"); graph.AddEdge("f", "c"); var allEdges = graph.Edges.ToList(); Assert.True(graph.VerticesCount == 8, "Wrong vertices count."); Assert.True(graph.EdgesCount == 14, "Wrong edges count."); Assert.True(graph.EdgesCount == allEdges.Count, "Wrong edges count."); Assert.True(graph.OutgoingEdges("a").ToList().Count == 2, "Wrong outgoing edges from 'a'."); Assert.True(graph.OutgoingEdges("s").ToList().Count == 1, "Wrong outgoing edges from 's'."); Assert.True(graph.OutgoingEdges("d").ToList().Count == 3, "Wrong outgoing edges from 'd'."); Assert.True(graph.OutgoingEdges("x").ToList().Count == 3, "Wrong outgoing edges from 'x'."); Assert.True(graph.OutgoingEdges("c").ToList().Count == 3, "Wrong outgoing edges from 'c'."); Assert.True(graph.OutgoingEdges("v").ToList().Count == 1, "Wrong outgoing edges from 'v'."); Assert.True(graph.OutgoingEdges("f").ToList().Count == 1, "Wrong outgoing edges from 'f'."); Assert.True(graph.OutgoingEdges("z").ToList().Count == 0, "Wrong outgoing edges from 'z'."); Assert.True(graph.IncomingEdges("a").ToList().Count == 1, "Wrong incoming edges from 'a'."); Assert.True(graph.IncomingEdges("s").ToList().Count == 2, "Wrong incoming edges from 's'."); Assert.True(graph.IncomingEdges("d").ToList().Count == 2, "Wrong incoming edges from 'd'."); Assert.True(graph.IncomingEdges("x").ToList().Count == 1, "Wrong incoming edges from 'x'."); Assert.True(graph.IncomingEdges("c").ToList().Count == 3, "Wrong incoming edges from 'c'."); Assert.True(graph.IncomingEdges("v").ToList().Count == 1, "Wrong incoming edges from 'v'."); Assert.True(graph.IncomingEdges("f").ToList().Count == 3, "Wrong incoming edges from 'f'."); Assert.True(graph.IncomingEdges("z").ToList().Count == 1, "Wrong incoming edges from 'z'."); graph.RemoveEdge("d", "c"); graph.RemoveEdge("c", "v"); graph.RemoveEdge("a", "z"); Assert.True(graph.VerticesCount == 8, "Wrong vertices count."); Assert.True(graph.EdgesCount == 11, "Wrong edges count."); graph.RemoveVertex("x"); Assert.True(graph.VerticesCount == 7, "Wrong vertices count."); Assert.True(graph.EdgesCount == 7, "Wrong edges count."); graph.AddVertex("x"); graph.AddEdge("s", "x"); graph.AddEdge("x", "d"); graph.AddEdge("x", "c"); graph.AddEdge("x", "a"); graph.AddEdge("d", "c"); graph.AddEdge("c", "v"); graph.AddEdge("a", "z"); // BFS from A // Walk the graph using BFS from A: var bfsWalk = graph.BreadthFirstWalk("a"); // output: (s) (a) (x) (z) (d) (c) (f) (v) foreach (var node in bfsWalk) { Console.Write(String.Format("({0})", node)); } // DFS from A // Walk the graph using DFS from A: var dfsWalk = graph.DepthFirstWalk("a"); // output: (s) (a) (x) (z) (d) (c) (f) (v) foreach (var node in dfsWalk) { Console.Write(String.Format("({0})", node)); } // BFS from F Console.WriteLine("Walk the graph using BFS from F:"); bfsWalk = graph.BreadthFirstWalk("f"); // output: (s) (a) (x) (z) (d) (c) (f) (v) foreach (var node in bfsWalk) { Console.Write(String.Format("({0})", node)); } // DFS from F // Walk the graph using DFS from F: dfsWalk = graph.DepthFirstWalk("f"); // output: (s) (a) (x) (z) (d) (c) (f) (v) foreach (var node in dfsWalk) { Console.Write(String.Format("({0})", node)); } /********************************************************************/ graph.Clear(); // Cleared the graph from all vertices and edges 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"); Assert.True(graph.VerticesCount == 6, "Wrong vertices count."); Assert.True(graph.EdgesCount == 8, "Wrong edges count."); // Walk the graph using DFS: dfsWalk = graph.DepthFirstWalk(); // output: (a) (b) (e) (d) (c) (f) foreach (var node in dfsWalk) { Console.Write(String.Format("({0})", node)); } }
public static void DoTest() { var V01 = new string[] { "A", "B", "C", "D", "E", "X" }; var DAG01 = new DirectedSparseGraph<string>(); // Insert new values of V DAG01.AddVertices(V01); // Insert new value for edges DAG01.AddEdge("A", "B"); DAG01.AddEdge("A", "X"); DAG01.AddEdge("B", "C"); DAG01.AddEdge("C", "D"); DAG01.AddEdge("D", "E"); DAG01.AddEdge("E", "X"); // PRINT THE GRAPH Console.WriteLine("[*] DAG (Directed Asyclic Graph):"); Console.WriteLine(DAG01.ToReadable() + "\r\n"); // CALCULATE THE TOPOLOGICAL SORT var topologicalSort01 = TopologicalSorter.Sort<string>(DAG01); var output01 = string.Empty; foreach(var node in topologicalSort01) output01 = String.Format("{0}({1}) ", output01, node); // PRINT THE TOPOLOGICAL SORT Console.WriteLine("Was the previous graph cyclic? " + output01); Console.WriteLine("\r\n*********************************************\r\n"); /**************************************************************************/ var V02 = new int[] { 7, 5, 3, 11, 8, 2, 9, 10 }; var DAG02 = new DirectedSparseGraph<int>(); // Insert new values of V DAG02.AddVertices(V02); // Insert new value for edges DAG02.AddEdge(7, 11); DAG02.AddEdge(7, 8); DAG02.AddEdge(5, 11); DAG02.AddEdge(3, 8); DAG02.AddEdge(3, 10); DAG02.AddEdge(11, 2); DAG02.AddEdge(11, 9); DAG02.AddEdge(11, 10); DAG02.AddEdge(8, 9); // PRINT THE GRAPH Console.WriteLine("[*] DAG (Directed Asyclic Graph):"); Console.WriteLine(DAG02.ToReadable() + "\r\n"); // CALCULATE THE TOPOLOGICAL SORT var topologicalSort02 = TopologicalSorter.Sort<int>(DAG02); var output02 = string.Empty; foreach (var node in topologicalSort02) output02 = String.Format("{0}({1}) ", output02, node); // PRINT THE TOPOLOGICAL SORT Console.WriteLine("Was the previous graph cyclic? " + output02); Console.WriteLine("\r\n*********************************************\r\n"); Console.ReadLine(); }