public void OddCycleTest() { // The graph IGraph <string> graph = new UndirectedSparseGraph <string>(); // The bipartite wrapper BipartiteColoring <UndirectedSparseGraph <string>, string> bipartiteGraph; // The status for checking bipartiteness bool initBipartiteStatus; // // Prepare the graph for the first case of testing _initializeFirstCaseGraph(ref graph); // // Test initializing the bipartite // This initialization must fail. The graph contains an odd cycle initBipartiteStatus = false; bipartiteGraph = null; try { bipartiteGraph = new BipartiteColoring <UndirectedSparseGraph <string>, string>(graph); initBipartiteStatus = bipartiteGraph.IsBipartite(); } catch { initBipartiteStatus = false; } Assert.AreEqual(initBipartiteStatus, false); }
public static void DoTest() { IGraph <string> graph = new UndirectedSparseGraph <string>(); // Add vertices var verticesSet1 = new string[] { "a", "z", "s", "x", "d", "c", "f", "v", "w", "m" }; graph.AddVertices(verticesSet1); // Add edges graph.AddEdge("a", "s"); graph.AddEdge("a", "z"); graph.AddEdge("s", "x"); graph.AddEdge("x", "d"); graph.AddEdge("x", "c"); graph.AddEdge("x", "w"); graph.AddEdge("x", "m"); graph.AddEdge("d", "f"); graph.AddEdge("d", "c"); graph.AddEdge("c", "f"); graph.AddEdge("c", "v"); graph.AddEdge("v", "f"); graph.AddEdge("w", "m"); var sourceNode = "f"; var bfsPaths = new BreadthFirstPaths <string> (graph, sourceNode); Console.WriteLine("Distance from '" + sourceNode + "' to 'a' is: " + bfsPaths.DistanceTo("a")); Console.WriteLine("Path from '" + sourceNode + "' to 'a' is : " + printPath(bfsPaths.ShortestPathTo("a"))); Console.WriteLine(); Console.WriteLine("Distance from '" + sourceNode + "' to 'w' is: " + bfsPaths.DistanceTo("w")); Console.WriteLine("Path from '" + sourceNode + "' to 'w' is : " + printPath(bfsPaths.ShortestPathTo("w"))); }
public static void DoTest() { IGraph<string> graph = new UndirectedSparseGraph<string>(); // Add vertices var verticesSet1 = new string[] { "a", "z", "s", "x", "d", "c", "f", "v", "w", "m" }; graph.AddVertices (verticesSet1); // Add edges graph.AddEdge("a", "s"); graph.AddEdge("a", "z"); graph.AddEdge("s", "x"); graph.AddEdge("x", "d"); graph.AddEdge("x", "c"); graph.AddEdge("x", "w"); graph.AddEdge("x", "m"); graph.AddEdge("d", "f"); graph.AddEdge("d", "c"); graph.AddEdge("c", "f"); graph.AddEdge("c", "v"); graph.AddEdge("v", "f"); graph.AddEdge("w", "m"); var sourceNode = "f"; var bfsPaths = new BreadthFirstPaths<string> (graph, sourceNode); Console.WriteLine ("Distance from '" + sourceNode + "' to 'a' is: " + bfsPaths.DistanceTo ("a")); Console.WriteLine ("Path from '" + sourceNode + "' to 'a' is : " + printPath(bfsPaths.ShortestPathTo("a"))); Console.WriteLine (); Console.WriteLine ("Distance from '" + sourceNode + "' to 'w' is: " + bfsPaths.DistanceTo ("w")); Console.WriteLine ("Path from '" + sourceNode + "' to 'w' is : " + printPath(bfsPaths.ShortestPathTo("w"))); }
public static void DoTest() { IGraph <string> graph = new UndirectedSparseGraph <string>(); // Add vertices var verticesSet1 = new string[] { "a", "z", "s", "x", "d", "c", "f", "v" }; graph.AddVertices(verticesSet1); // Add edges 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"); // Print the nodes in graph Console.WriteLine(" [*] DFS PrintAll: "); DepthFirstSearcher.PrintAll(graph, "d"); Console.WriteLine("\r\n"); string searchResult = null; string startFromNode = "d"; Action <string> writeToConsole = (node) => Console.Write(String.Format("({0}) ", node)); Predicate <string> searchPredicate = (node) => node == "f" || node == "c"; Console.WriteLine("[*] DFS Visit All Nodes:"); Console.WriteLine("Graph traversal started at node: '" + startFromNode + "'"); DepthFirstSearcher.VisitAll(ref graph, startFromNode, writeToConsole); Console.WriteLine("\r\n"); try { searchResult = DepthFirstSearcher.FindFirstMatch(graph, startFromNode, searchPredicate); Debug.Assert(searchResult == "c" || searchResult == "f"); Console.WriteLine("[*] DFS Find First Match:"); Console.WriteLine( String.Format( "Search result: '{0}'. The search started from node: '{1}'." , searchResult , startFromNode)); } catch (Exception) { Console.WriteLine("Search predicate was not matched by any node in the graph."); } Console.WriteLine("\r\n"); }
public static void DoTest () { IGraph<string> graph = new UndirectedSparseGraph<string>(); // Add vertices var verticesSet1 = new string[] { "a", "z", "s", "x", "d", "c", "f", "v" }; graph.AddVertices (verticesSet1); // Add edges 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"); // Print the nodes in graph Console.WriteLine(" [*] DFS PrintAll: "); DepthFirstSearcher.PrintAll(graph, "d"); Console.WriteLine("\r\n"); string searchResult = null; string startFromNode = "d"; Action<string> writeToConsole = (node) => Console.Write (String.Format ("({0}) ", node)); Predicate<string> searchPredicate = (node) => node == "f" || node == "c"; Console.WriteLine ("[*] DFS Visit All Nodes:"); Console.WriteLine ("Graph traversal started at node: '" + startFromNode + "'"); DepthFirstSearcher.VisitAll (ref graph, startFromNode, writeToConsole); Console.WriteLine ("\r\n"); try { searchResult = DepthFirstSearcher.FindFirstMatch(graph, startFromNode, searchPredicate); Debug.Assert(searchResult == "c" || searchResult == "f"); Console.WriteLine("[*] DFS Find First Match:"); Console.WriteLine( String.Format( "Search result: '{0}'. The search started from node: '{1}'." , searchResult , startFromNode)); } catch(Exception) { Console.WriteLine ("Search predicate was not matched by any node in the graph."); } Console.WriteLine ("\r\n"); }
public UninformedSearch() { UndirectedSparseGraph <String> graph = new UndirectedSparseGraph <String>(); string[] vertices = new string[] { "a", "b", "c", "d", "e", "f", "g" }; graph.AddVertices(vertices); graph.SetEdge("a", "c", 1); graph.SetEdge("a", "d", 1); graph.SetEdge("b", "c", 1); graph.SetEdge("b", "e", 1); graph.SetEdge("d", "g", 1); graph.SetEdge("e", "f", 1); graph.SetEdge("f", "g", 1); _problem = new GraphProblem <string>(graph, "a", "e"); List <EdgeAction <String> > actionsFromA = _problem.Actions("a").Cast <EdgeAction <String> >().ToList(); List <EdgeAction <String> > actionsFromB = _problem.Actions("b").Cast <EdgeAction <String> >().ToList(); List <EdgeAction <String> > actionsFromC = _problem.Actions("c").Cast <EdgeAction <String> >().ToList(); List <EdgeAction <String> > actionsFromD = _problem.Actions("d").Cast <EdgeAction <String> >().ToList(); List <EdgeAction <String> > actionsFromE = _problem.Actions("e").Cast <EdgeAction <String> >().ToList(); List <EdgeAction <String> > actionsFromF = _problem.Actions("f").Cast <EdgeAction <String> >().ToList(); List <EdgeAction <String> > actionsFromG = _problem.Actions("g").Cast <EdgeAction <String> >().ToList(); Assert.Equal(actionsFromA, new List <EdgeAction <String> > { new EdgeAction <String>(new Edge <String>("a", "c", 1)), new EdgeAction <String>(new Edge <String>("a", "d", 1)) }); Assert.Equal(actionsFromB, new List <EdgeAction <String> > { new EdgeAction <String>(new Edge <String>("b", "c", 1)), new EdgeAction <String>(new Edge <String>("b", "e", 1)) }); Assert.Equal(actionsFromC, new List <EdgeAction <String> > { new EdgeAction <String>(new Edge <String>("c", "a", 1)), new EdgeAction <String>(new Edge <String>("c", "b", 1)) }); Assert.Equal(actionsFromD, new List <EdgeAction <String> > { new EdgeAction <String>(new Edge <String>("d", "a", 1)), new EdgeAction <String>(new Edge <String>("d", "g", 1)) }); Assert.Equal(actionsFromE, new List <EdgeAction <String> > { new EdgeAction <String>(new Edge <String>("e", "b", 1)), new EdgeAction <String>(new Edge <String>("e", "f", 1)) }); Assert.Equal(actionsFromF, new List <EdgeAction <String> > { new EdgeAction <String>(new Edge <String>("f", "e", 1)), new EdgeAction <String>(new Edge <String>("f", "g", 1)) }); Assert.Equal(actionsFromG, new List <EdgeAction <String> > { new EdgeAction <String>(new Edge <String>("g", "d", 1)), new EdgeAction <String>(new Edge <String>("g", "f", 1)) }); }
public static void DoTest() { 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); // 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"); // Get connected components var connectedComponents = ConnectedComponents.Compute <string>(graph); connectedComponents = connectedComponents.OrderBy(item => item.Count).ToList(); Debug.Assert(connectedComponents.Count == 4); // the case of the (e) vertex Debug.Assert(connectedComponents[0].Count == 1); Debug.Assert(connectedComponents[0][0] == "e"); // the case of (y) and (z) vertices Debug.Assert(connectedComponents[1].Count == 2); Debug.Assert(connectedComponents[1].Contains("y")); Debug.Assert(connectedComponents[1].Contains("z")); // the case of the rest Debug.Assert(connectedComponents[2].Count == 4); Debug.Assert(connectedComponents[3].Count == 4); }
public static void DoTest() { 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); // 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"); // Get connected components var connectedComponents = ConnectedComponents.Compute<string>(graph); connectedComponents = connectedComponents.OrderBy(item => item.Count).ToList(); Debug.Assert(connectedComponents.Count == 4); // the case of the (e) vertex Debug.Assert(connectedComponents[0].Count == 1); Debug.Assert(connectedComponents[0][0] == "e"); // the case of (y) and (z) vertices Debug.Assert(connectedComponents[1].Count == 2); Debug.Assert(connectedComponents[1].Contains("y")); Debug.Assert(connectedComponents[1].Contains("z")); // the case of the rest Debug.Assert(connectedComponents[2].Count == 4); Debug.Assert(connectedComponents[3].Count == 4); }
public void AddMovie(Movie movie) { if (graphs.ContainsKey(movie.Category)) { var graph = graphs[movie.Category]; var newNode = movie.GetNode(); graph.AddVertex(newNode); var nodes = graph.BreadthFirstWalk(); foreach (var node in nodes) { var resultingTags = newNode.Tags.Intersect(node.Tags); if (resultingTags.Count() > 0) { graph.AddEdge(newNode, node); } } } else { var graph = new UndirectedSparseGraph <MovieNode>(); graph.AddVertex(movie.GetNode()); graphs.Add(movie.Category, graph); } }
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() { // The graph IGraph <string> graph = new UndirectedSparseGraph <string>(); // The bipartite wrapper BipartiteColoring <UndirectedSparseGraph <string>, string> bipartiteGraph; // The status for checking bipartiteness bool initBipartiteStatus; // Prepare the graph for the first case of testing _initializeFirstCaseGraph(ref graph); // Test initializing the bipartite // This initialization must fail. The graph contains an odd cycle initBipartiteStatus = false; bipartiteGraph = null; try { bipartiteGraph = new BipartiteColoring <UndirectedSparseGraph <string>, string>(graph); initBipartiteStatus = bipartiteGraph.IsBipartite(); } catch { initBipartiteStatus = false; } Debug.Assert(initBipartiteStatus == false, "Graph should not be bipartite."); /************************************************************/ // // Prepare the graph for the second case of testing _initializeSecondCaseGraph(ref graph); // // Test initializing the bipartite // This initialization must fail. The graph contains an odd cycle bipartiteGraph = null; initBipartiteStatus = false; try { bipartiteGraph = new BipartiteColoring <UndirectedSparseGraph <string>, string>(graph); initBipartiteStatus = bipartiteGraph.IsBipartite(); } catch { initBipartiteStatus = false; } Debug.Assert(initBipartiteStatus == false, "Graph should not be bipartite."); // // Remove Odd Cycle and try to initialize again. initBipartiteStatus = true; graph.RemoveEdge("c", "v"); graph.RemoveEdge("f", "b"); // // This initialization must pass. The graph doesn't contain any odd cycle try { bipartiteGraph = new BipartiteColoring <UndirectedSparseGraph <string>, string>(graph); initBipartiteStatus = bipartiteGraph.IsBipartite(); } catch { initBipartiteStatus = false; } Debug.Assert(initBipartiteStatus == true, "Graph should be bipartite."); Debug.Assert(bipartiteGraph.ColorOf("a") == BipartiteColor.Red); Debug.Assert(bipartiteGraph.ColorOf("s") == BipartiteColor.Blue); Debug.Assert(bipartiteGraph.ColorOf("b") == BipartiteColor.Red); Debug.Assert(bipartiteGraph.ColorOf("f") == BipartiteColor.Red); Debug.Assert(bipartiteGraph.ColorOf("z") == BipartiteColor.Blue); }
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(); Assert.True(graph.VerticesCount == 8, "Wrong vertices count."); Assert.True(graph.EdgesCount == 10, "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 == 2, "Wrong outgoing edges from 's'."); Assert.True(graph.OutgoingEdges("x").ToList().Count == 3, "Wrong outgoing edges from 'x'."); Assert.True(graph.OutgoingEdges("d").ToList().Count == 3, "Wrong outgoing edges from 'd'."); Assert.True(graph.OutgoingEdges("c").ToList().Count == 4, "Wrong outgoing edges from 'c'."); Assert.True(graph.OutgoingEdges("v").ToList().Count == 2, "Wrong outgoing edges from 'v'."); Assert.True(graph.OutgoingEdges("f").ToList().Count == 3, "Wrong outgoing edges from 'f'."); Assert.True(graph.OutgoingEdges("z").ToList().Count == 1, "Wrong outgoing edges from 'z'."); Assert.True(graph.IncomingEdges("a").ToList().Count == 2, "Wrong incoming edges from 'a'."); Assert.True(graph.IncomingEdges("s").ToList().Count == 2, "Wrong incoming edges from 's'."); Assert.True(graph.IncomingEdges("x").ToList().Count == 3, "Wrong incoming edges from 'x'."); Assert.True(graph.IncomingEdges("d").ToList().Count == 3, "Wrong incoming edges from 'd'."); Assert.True(graph.IncomingEdges("c").ToList().Count == 4, "Wrong incoming edges from 'c'."); Assert.True(graph.IncomingEdges("v").ToList().Count == 2, "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 == 7, "Wrong edges count."); graph.RemoveVertex("x"); Assert.True(graph.VerticesCount == 7, "Wrong vertices count."); Assert.True(graph.EdgesCount == 4, "Wrong edges count."); 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"); Assert.True(graph.BreadthFirstWalk("s").SequenceEqual(new string[] { "s", "a", "x", "z", "d", "c", "f", "v" })); graph.AddVertices(new string[] { "a", "b", "c", "d", "e", "f" }); 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 == 10, "Wrong vertices count."); Assert.True(graph.EdgesCount == 17, "Wrong edges count."); Assert.True(graph.DepthFirstWalk().SequenceEqual(new string[] { "a", "d", "e", "c", "v", "f", "x", "s", "b", "z" })); }
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 void TestUndirectedSparseGraph() { IVertexList <IVertex> vertices = new VertexList <Vertex>(); IUndirectedSparseGraph <IVertexList <IVertex>, int, IVertex, IEdge <IVertex> > graph = new UndirectedSparseGraph <IVertexList <IVertex>, int, IVertex, IEdge <IVertex> >(vertices); Assert.IsNotNull(graph.EdgeSet); Assert.IsNotNull(graph.VertexSet); Assert.IsNotNull(((IMutableGraph <IVertexList <IVertex>, int, IVertex, IUndirectedAdjacencyList <int, IVertex, IEdge <Vertex> >, IEdge <Vertex> >)graph).EdgeSet); Assert.IsNotNull(((IMutableGraph <IVertexList <IVertex>, int, IVertex, IUndirectedAdjacencyList <int, IVertex, IEdge <Vertex> >, IEdge <Vertex> >)graph).VertexSet); Assert.IsNotNull(((IReadonlyGraph <IVertexList <IVertex>, int, IVertex, IUndirectedAdjacencyList <int, IVertex, IEdge <Vertex> >, IEdge <Vertex> >)graph).EdgeSet); Assert.IsNotNull(((IReadonlyGraph <IVertexList <IVertex>, int, IVertex, IUndirectedAdjacencyList <int, IVertex, IEdge <Vertex> >, IEdge <Vertex> >)graph).VertexSet); }
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() { // The graph IGraph<string> graph = new UndirectedSparseGraph<string>(); // The bipartite wrapper BipartiteColoring<UndirectedSparseGraph<string>, string> bipartiteGraph; // The status for checking bipartiteness bool initBipartiteStatus; // // Prepare the graph for the first case of testing _initializeFirstCaseGraph(ref graph); // // Test initializing the bipartite // This initialization must fail. The graph contains an odd cycle initBipartiteStatus = false; bipartiteGraph = null; try { bipartiteGraph = new BipartiteColoring<UndirectedSparseGraph<string>, string>(graph); initBipartiteStatus = bipartiteGraph.IsBipartite(); } catch { initBipartiteStatus = false; } Debug.Assert(initBipartiteStatus == false, "Graph should not be bipartite."); /************************************************************/ // // Prepare the graph for the second case of testing _initializeSecondCaseGraph(ref graph); // // Test initializing the bipartite // This initialization must fail. The graph contains an odd cycle bipartiteGraph = null; initBipartiteStatus = false; try { bipartiteGraph = new BipartiteColoring<UndirectedSparseGraph<string>, string>(graph); initBipartiteStatus = bipartiteGraph.IsBipartite(); } catch { initBipartiteStatus = false; } Debug.Assert(initBipartiteStatus == false, "Graph should not be bipartite."); // // Remove Odd Cycle and try to initialize again. initBipartiteStatus = true; graph.RemoveEdge("c", "v"); graph.RemoveEdge("f", "b"); // // This initialization must pass. The graph doesn't contain any odd cycle try { bipartiteGraph = new BipartiteColoring<UndirectedSparseGraph<string>, string>(graph); initBipartiteStatus = bipartiteGraph.IsBipartite(); } catch { initBipartiteStatus = false; } Debug.Assert(initBipartiteStatus == true, "Graph should be bipartite."); Debug.Assert(bipartiteGraph.ColorOf("a") == BipartiteColor.Red); Debug.Assert(bipartiteGraph.ColorOf("s") == BipartiteColor.Blue); Debug.Assert(bipartiteGraph.ColorOf("b") == BipartiteColor.Red); Debug.Assert(bipartiteGraph.ColorOf("f") == BipartiteColor.Red); Debug.Assert(bipartiteGraph.ColorOf("z") == BipartiteColor.Blue); }
// GET: /<controller>/ public IActionResult Index() { string result = string.Empty; // The graph IGraph <string> graph = new UndirectedSparseGraph <string>(); // The bipartite wrapper BipartiteColoring <UndirectedSparseGraph <string>, string> bipartiteGraph; // The status for checking bipartiteness bool initBipartiteStatus; // // Prepare the graph for the second case of testing _initializeSecondCaseGraph(ref graph, ref result); // // Test initializing the bipartite // This initialization must fail. The graph contains an odd cycle bipartiteGraph = null; initBipartiteStatus = false; try { bipartiteGraph = new BipartiteColoring <UndirectedSparseGraph <string>, string>(graph); initBipartiteStatus = bipartiteGraph.IsBipartite(); } catch (Exception ex) { string err = ex.Message; initBipartiteStatus = false; } if (!initBipartiteStatus) { Console.WriteLine("Graph should not be bipartite."); } // // Remove Odd Cycle and try to initialize again. initBipartiteStatus = true; graph.RemoveEdge("c", "v"); graph.RemoveEdge("f", "b"); // // This initialization must pass. The graph doesn't contain any odd cycle try { bipartiteGraph = new BipartiteColoring <UndirectedSparseGraph <string>, string>(graph); initBipartiteStatus = bipartiteGraph.IsBipartite(); } catch { initBipartiteStatus = false; } if (!initBipartiteStatus) { Console.WriteLine("Graph should not be bipartite."); } result = result + " 'a' color is " + bipartiteGraph.ColorOf("a") + "\n"; result = result + " 's' color is " + bipartiteGraph.ColorOf("s") + "\n"; result = result + " 'b' color is " + bipartiteGraph.ColorOf("b") + "\n"; result = result + " 'f' color is " + bipartiteGraph.ColorOf("f") + "\n"; result = result + " 'z' color is " + bipartiteGraph.ColorOf("z") + "\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 <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 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(); }
// 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() { // 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."); }