protected static void AddVertex_Clusters_Test <TEdge>( ClusteredAdjacencyGraph <TestVertex, TEdge> graph1, ClusteredAdjacencyGraph <TestVertex, TEdge> parent2, ClusteredAdjacencyGraph <TestVertex, TEdge> graph2) where TEdge : IEdge <TestVertex> { AssertNoVertex(graph1); // Graph without parent // Vertex 1 var vertex1 = new TestVertex("1"); Assert.IsTrue(graph1.AddVertex(vertex1)); AssertHasVertices(graph1, new[] { vertex1 }); // Vertex 2 var vertex2 = new TestVertex("2"); Assert.IsTrue(graph1.AddVertex(vertex2)); AssertHasVertices(graph1, new[] { vertex1, vertex2 }); // Vertex 1 bis Assert.IsFalse(graph1.AddVertex(vertex1)); AssertHasVertices(graph1, new[] { vertex1, vertex2 }); // Other "Vertex 1" var otherVertex1 = new TestVertex("1"); Assert.IsTrue(graph1.AddVertex(otherVertex1)); AssertHasVertices(graph1, new[] { vertex1, vertex2, otherVertex1 }); // Graph with parent AssertNoVertex(parent2); AssertNoVertex(graph2); Assert.IsTrue(graph2.AddVertex(vertex1)); AssertHasVertices(parent2, new[] { vertex1 }); AssertHasVertices(graph2, new[] { vertex1 }); // Vertex 2 Assert.IsTrue(parent2.AddVertex(vertex2)); AssertHasVertices(parent2, new[] { vertex1, vertex2 }); AssertHasVertices(graph2, new[] { vertex1 }); Assert.IsTrue(graph2.AddVertex(vertex2)); AssertHasVertices(parent2, new[] { vertex1, vertex2 }); AssertHasVertices(graph2, new[] { vertex1, vertex2 }); // Vertex 1 bis Assert.IsFalse(graph2.AddVertex(vertex1)); AssertHasVertices(parent2, new[] { vertex1, vertex2 }); AssertHasVertices(graph2, new[] { vertex1, vertex2 }); // Other "Vertex 1" Assert.IsTrue(graph2.AddVertex(otherVertex1)); AssertHasVertices(parent2, new[] { vertex1, vertex2, otherVertex1 }); AssertHasVertices(graph2, new[] { vertex1, vertex2, otherVertex1 }); }
public void AddingClusterEdge() { var graph = new AdjacencyGraph <int, IEdge <int> >(); var clusteredGraph = new ClusteredAdjacencyGraph <int, IEdge <int> >(graph); ClusteredAdjacencyGraph <int, IEdge <int> > cluster1 = clusteredGraph.AddCluster(); cluster1.AddVertex(5); cluster1.AddVertex(6); var edge = new TaggedEdge <int, int>(5, 6, 1); cluster1.AddEdge(edge); Assert.IsTrue(ContainsEdgeParent(clusteredGraph, edge)); }
protected static void AddEdgeRange_Clusters_Test( ClusteredAdjacencyGraph <int, Edge <int> > graph1, ClusteredAdjacencyGraph <int, Edge <int> > parent2, ClusteredAdjacencyGraph <int, Edge <int> > graph2) { // Graph without parent graph1.AddVertex(1); graph1.AddVertex(2); graph1.AddVertex(3); AssertNoEdge(graph1); // Edge 1, 2, 3 var edge1 = new Edge <int>(1, 2); var edge2 = new Edge <int>(1, 3); var edge3 = new Edge <int>(2, 3); Assert.AreEqual(3, graph1.AddEdgeRange(new[] { edge1, edge2, edge3 })); AssertHasEdges(graph1, new[] { edge1, edge2, edge3 }); // Edge 1, 4 var edge4 = new Edge <int>(2, 2); Assert.AreEqual(1, graph1.AddEdgeRange(new[] { edge1, edge4 })); // Showcase the add of only one edge AssertHasEdges(graph1, new[] { edge1, edge2, edge3, edge4 }); // Graph with parent graph2.AddVertex(1); graph2.AddVertex(2); graph2.AddVertex(3); AssertNoEdge(parent2); AssertNoEdge(graph2); // Edge 1, 2, 3 Assert.AreEqual(3, graph2.AddEdgeRange(new[] { edge1, edge2, edge3 })); AssertHasEdges(parent2, new[] { edge1, edge2, edge3 }); AssertHasEdges(graph2, new[] { edge1, edge2, edge3 }); // Edge 1, 4 Assert.AreEqual(1, parent2.AddEdgeRange(new[] { edge1, edge4 })); // Showcase the add of only one edge AssertHasEdges(parent2, new[] { edge1, edge2, edge3, edge4 }); AssertHasEdges(graph2, new[] { edge1, edge2, edge3 }); Assert.AreEqual(1, graph2.AddEdgeRange(new[] { edge1, edge4 })); // Showcase the add of only one edge AssertHasEdges(parent2, new[] { edge1, edge2, edge3, edge4 }); AssertHasEdges(graph2, new[] { edge1, edge2, edge3, edge4 }); }
protected static void AddVertex_Throws_Clusters_Test <TVertex, TEdge>( ClusteredAdjacencyGraph <TVertex, TEdge> graph) where TVertex : class where TEdge : IEdge <TVertex> { // ReSharper disable once AssignNullToNotNullAttribute Assert.Throws <ArgumentNullException>(() => graph.AddVertex(null)); AssertNoVertex(graph); }
public void OutEdges() { var wrappedGraph = new AdjacencyGraph <int, Edge <int> >(); var graph = new ClusteredAdjacencyGraph <int, Edge <int> >(wrappedGraph); OutEdges_Test( graph, vertex => graph.AddVertex(vertex), edges => graph.AddVerticesAndEdgeRange(edges)); }
public void AddingClusterVertex() { var graph = new AdjacencyGraph <int, IEdge <int> >(); var clusteredGraph = new ClusteredAdjacencyGraph <int, IEdge <int> >(graph); ClusteredAdjacencyGraph <int, IEdge <int> > cluster = clusteredGraph.AddCluster(); cluster.AddVertex(5); Assert.IsTrue(ContainsVertexParent(clusteredGraph, 5)); }
protected static void AddEdgeRange_Throws_Clusters_Test( ClusteredAdjacencyGraph <int, Edge <int> > graph) { graph.AddVertex(1); graph.AddVertex(2); graph.AddVertex(3); AssertNoEdge(graph); // ReSharper disable once AssignNullToNotNullAttribute Assert.Throws <ArgumentNullException>(() => graph.AddEdgeRange(null)); AssertNoEdge(graph); // Edge 1, 2, 3 var edge1 = new Edge <int>(1, 2); var edge3 = new Edge <int>(2, 3); Assert.Throws <ArgumentNullException>(() => graph.AddEdgeRange(new[] { edge1, null, edge3 })); AssertNoEdge(graph); }
public void RemovingClusterVertex() { var graph = new AdjacencyGraph <int, IEdge <int> >(); var clusteredGraph = new ClusteredAdjacencyGraph <int, IEdge <int> >(graph); ClusteredAdjacencyGraph <int, IEdge <int> > cluster1 = clusteredGraph.AddCluster(); ClusteredAdjacencyGraph <int, IEdge <int> > cluster2 = cluster1.AddCluster(); ClusteredAdjacencyGraph <int, IEdge <int> > cluster3 = cluster2.AddCluster(); cluster3.AddVertex(5); cluster2.RemoveVertex(5); Assert.IsFalse(ContainsVertexParent(cluster3, 5)); }
public void OutEdge_Throws() { var wrappedGraph1 = new AdjacencyGraph <TestVertex, Edge <TestVertex> >(); var graph1 = new ClusteredAdjacencyGraph <TestVertex, Edge <TestVertex> >(wrappedGraph1); OutEdge_NullThrows_Test(graph1); var wrappedGraph2 = new AdjacencyGraph <int, Edge <int> >(); var graph2 = new ClusteredAdjacencyGraph <int, Edge <int> >(wrappedGraph2); OutEdge_Throws_Test( graph2, vertex => graph2.AddVertex(vertex), edge => graph2.AddEdge(edge)); }
protected static void AddEdge_Throws_Clusters_Test( ClusteredAdjacencyGraph <int, Edge <int> > graph) { // ReSharper disable once AssignNullToNotNullAttribute Assert.Throws <ArgumentNullException>(() => graph.AddEdge(null)); AssertNoEdge(graph); // Both vertices not in graph Assert.Throws <VertexNotFoundException>(() => graph.AddEdge(new Edge <int>(0, 1))); AssertNoEdge(graph); // Source not in graph graph.AddVertex(1); Assert.Throws <VertexNotFoundException>(() => graph.AddEdge(new Edge <int>(0, 1))); AssertNoEdge(graph); // Target not in graph Assert.Throws <VertexNotFoundException>(() => graph.AddEdge(new Edge <int>(1, 0))); AssertNoEdge(graph); }
public void ToGraphvizWithInit() { var wrappedGraph = new AdjacencyGraph <int, Edge <int> >(); wrappedGraph.AddVerticesAndEdgeRange(new[] { new Edge <int>(1, 2), new Edge <int>(1, 3), new Edge <int>(2, 4) }); wrappedGraph.AddVertex(5); var clusteredGraph = new ClusteredAdjacencyGraph <int, Edge <int> >(wrappedGraph); ClusteredAdjacencyGraph <int, Edge <int> > subGraph1 = clusteredGraph.AddCluster(); subGraph1.AddVerticesAndEdgeRange(new[] { new Edge <int>(6, 7), new Edge <int>(7, 8) }); ClusteredAdjacencyGraph <int, Edge <int> > subGraph2 = clusteredGraph.AddCluster(); subGraph2.AddVerticesAndEdge(new Edge <int>(9, 10)); subGraph2.AddVertex(11); string expectedDot = "digraph G {" + Environment.NewLine + "node [shape=diamond];" + Environment.NewLine + "edge [tooltip=\"Test Edge\"];" + Environment.NewLine + "subgraph cluster1 {" + Environment.NewLine + "5 [label=\"Test Vertex 6\"];" + Environment.NewLine + "6 [label=\"Test Vertex 7\"];" + Environment.NewLine + "7 [label=\"Test Vertex 8\"];" + Environment.NewLine + "5 -> 6;" + Environment.NewLine + "6 -> 7;" + Environment.NewLine + "}" + Environment.NewLine + "subgraph cluster2 {" + Environment.NewLine + "8 [label=\"Test Vertex 9\"];" + Environment.NewLine + "9 [label=\"Test Vertex 10\"];" + Environment.NewLine + "10 [label=\"Test Vertex 11\"];" + Environment.NewLine + "8 -> 9;" + Environment.NewLine + "}" + Environment.NewLine + "0 [label=\"Test Vertex 1\"];" + Environment.NewLine + "1 [label=\"Test Vertex 2\"];" + Environment.NewLine + "2 [label=\"Test Vertex 3\"];" + Environment.NewLine + "3 [label=\"Test Vertex 4\"];" + Environment.NewLine + "4 [label=\"Test Vertex 5\"];" + Environment.NewLine + "0 -> 1;" + Environment.NewLine + "0 -> 2;" + Environment.NewLine + "1 -> 3;" + Environment.NewLine + "}"; string dotGraph = clusteredGraph.ToGraphviz(algorithm => { algorithm.CommonVertexFormat.Shape = GraphvizVertexShape.Diamond; algorithm.CommonEdgeFormat.ToolTip = "Test Edge"; algorithm.FormatVertex += (sender, args) => { args.VertexFormat.Label = $"Test Vertex {args.Vertex}"; }; }); Assert.AreEqual(expectedDot, dotGraph); }
protected static void RemoveVertex_Clusters_Test( [NotNull] ClusteredAdjacencyGraph <int, Edge <int> > graph) { var edge12 = new Edge <int>(1, 2); var edge13 = new Edge <int>(1, 3); var edge14 = new Edge <int>(1, 4); var edge24 = new Edge <int>(2, 4); var edge31 = new Edge <int>(3, 1); var edge33 = new Edge <int>(3, 3); graph.AddVerticesAndEdgeRange(new[] { edge12, edge13, edge14, edge24, edge31, edge33 }); Assert.IsFalse(graph.RemoveVertex(5)); Assert.IsTrue(graph.RemoveVertex(3)); AssertHasVertices(graph, new[] { 1, 2, 4 }); AssertHasEdges(graph, new[] { edge12, edge14, edge24 }); Assert.IsTrue(graph.RemoveVertex(1)); AssertHasVertices(graph, new[] { 2, 4 }); AssertHasEdges(graph, new[] { edge24 }); Assert.IsTrue(graph.RemoveVertex(2)); AssertHasVertices(graph, new[] { 4 }); AssertNoEdge(graph); Assert.IsTrue(graph.RemoveVertex(4)); AssertEmptyGraph(graph); // With cluster ClusteredAdjacencyGraph <int, Edge <int> > cluster1 = graph.AddCluster(); ClusteredAdjacencyGraph <int, Edge <int> > cluster2 = graph.AddCluster(); ClusteredAdjacencyGraph <int, Edge <int> > cluster3 = graph.AddCluster(); cluster1.AddVertexRange(new[] { 1, 2 }); AssertHasVertices(cluster1, new[] { 1, 2 }); cluster2.AddVertexRange(new[] { 1, 2, 4 }); AssertHasVertices(cluster2, new[] { 1, 2, 4 }); cluster3.AddVertex(2); AssertHasVertices(cluster3, new[] { 2 }); graph.AddVertexRange(new[] { 1, 2, 3, 4 }); AssertHasVertices(graph, new[] { 1, 2, 3, 4 }); graph.RemoveVertex(2); AssertHasVertices(graph, new[] { 1, 3, 4 }); AssertHasVertices(cluster1, new[] { 1 }); AssertHasVertices(cluster2, new[] { 1, 4 }); AssertNoVertex(cluster3); graph.RemoveVertex(1); AssertHasVertices(graph, new[] { 3, 4 }); AssertNoVertex(cluster1); AssertHasVertices(cluster2, new[] { 4 }); AssertNoVertex(cluster3); graph.RemoveVertex(3); AssertHasVertices(graph, new[] { 4 }); AssertNoVertex(cluster1); AssertHasVertices(cluster2, new[] { 4 }); AssertNoVertex(cluster3); graph.RemoveVertex(4); AssertNoVertex(graph); AssertNoVertex(cluster1); AssertNoVertex(cluster2); AssertNoVertex(cluster3); }
protected static void AddEdge_ParallelEdges_Clusters_Test( ClusteredAdjacencyGraph <int, Edge <int> > graph1, ClusteredAdjacencyGraph <int, Edge <int> > parent2, ClusteredAdjacencyGraph <int, Edge <int> > graph2) { // Graph without parent graph1.AddVertex(1); graph1.AddVertex(2); AssertNoEdge(graph1); // Edge 1 var edge1 = new Edge <int>(1, 2); Assert.IsTrue(graph1.AddEdge(edge1)); AssertHasEdges(graph1, new[] { edge1 }); // Edge 2 var edge2 = new Edge <int>(1, 2); Assert.IsTrue(graph1.AddEdge(edge2)); AssertHasEdges(graph1, new[] { edge1, edge2 }); // Edge 3 var edge3 = new Edge <int>(2, 1); Assert.IsTrue(graph1.AddEdge(edge3)); AssertHasEdges(graph1, new[] { edge1, edge2, edge3 }); // Edge 1 bis Assert.IsTrue(graph1.AddEdge(edge1)); AssertHasEdges(graph1, new[] { edge1, edge2, edge3, edge1 }); // Edge 4 self edge var edge4 = new Edge <int>(2, 2); Assert.IsTrue(graph1.AddEdge(edge4)); AssertHasEdges(graph1, new[] { edge1, edge2, edge3, edge1, edge4 }); // Graph with parent graph2.AddVertex(1); graph2.AddVertex(2); AssertNoEdge(parent2); AssertNoEdge(graph2); // Edge 1 Assert.IsTrue(graph2.AddEdge(edge1)); AssertHasEdges(parent2, new[] { edge1 }); AssertHasEdges(graph2, new[] { edge1 }); // Edge 2 Assert.IsTrue(parent2.AddEdge(edge2)); AssertHasEdges(parent2, new[] { edge1, edge2 }); AssertHasEdges(graph2, new[] { edge1 }); Assert.IsTrue(graph2.AddEdge(edge2)); AssertHasEdges(parent2, new[] { edge1, edge2 }); AssertHasEdges(graph2, new[] { edge1, edge2 }); // Edge 3 Assert.IsTrue(graph2.AddEdge(edge3)); AssertHasEdges(parent2, new[] { edge1, edge2, edge3 }); AssertHasEdges(graph2, new[] { edge1, edge2, edge3 }); // Edge 1 bis Assert.IsTrue(graph2.AddEdge(edge1)); AssertHasEdges(parent2, new[] { edge1, edge2, edge3 }); AssertHasEdges(graph2, new[] { edge1, edge2, edge3, edge1 }); // Edge 4 self edge Assert.IsTrue(graph2.AddEdge(edge4)); AssertHasEdges(parent2, new[] { edge1, edge2, edge3, edge4 }); AssertHasEdges(graph2, new[] { edge1, edge2, edge3, edge1, edge4 }); }
public void Run() { // create an adjacency graph AdjacencyGraph g = new AdjacencyGraph( new NamedVertexProvider(), new NamedEdgeProvider(), true ); // create a clustered graph ClusteredAdjacencyGraph cg = new ClusteredAdjacencyGraph(g); // adding some vertices to the main graph NamedVertex a = cg.AddVertex() as NamedVertex; a.Name = "a"; NamedVertex b = cg.AddVertex() as NamedVertex; b.Name = "b"; NamedVertex c = cg.AddVertex() as NamedVertex; c.Name = "c"; NamedEdge ab = cg.AddEdge(a, b) as NamedEdge; ab.Name = "ab"; NamedEdge ac = cg.AddEdge(a, c) as NamedEdge; ac.Name = "ac"; // adding a cluster ClusteredAdjacencyGraph cg1 = cg.AddCluster(); // adding vertices and edges to the cluster NamedVertex d = cg1.AddVertex() as NamedVertex; d.Name = "d"; NamedVertex e = cg1.AddVertex() as NamedVertex; e.Name = "e"; NamedVertex f = cg1.AddVertex() as NamedVertex; f.Name = "f"; NamedEdge de = cg1.AddEdge(d, e) as NamedEdge; de.Name = "de"; NamedEdge df = cg1.AddEdge(d, f) as NamedEdge; df.Name = "df"; // adding a second cluster ClusteredAdjacencyGraph cg2 = cg.AddCluster(); // adding vertices NamedVertex h = cg2.AddVertex() as NamedVertex; h.Name = "h"; NamedVertex i = cg2.AddVertex() as NamedVertex; i.Name = "i"; NamedEdge hi = cg2.AddEdge(h, i) as NamedEdge; hi.Name = "hi"; // adding a sub-sub-cluster ClusteredAdjacencyGraph cg21 = cg2.AddCluster(); // adding vertices NamedVertex k = cg21.AddVertex() as NamedVertex; k.Name = "k"; NamedVertex l = cg21.AddVertex() as NamedVertex; l.Name = "l"; NamedEdge ak = cg.AddEdge(a, k) as NamedEdge; ak.Name = "ak"; NamedEdge kl = cg21.AddEdge(k, l) as NamedEdge; kl.Name = "kl"; // interconnecting NamedEdge cd = cg.AddEdge(c, d) as NamedEdge; cd.Name = "cd"; NamedEdge bh = cg.AddEdge(b, h) as NamedEdge; bh.Name = "bh"; NamedEdge ei = cg.AddEdge(e, i) as NamedEdge; ei.Name = "ei"; GraphvizAlgorithm gw = new GraphvizAlgorithm( cg, "../cluster", GraphvizImageType.Svgz ); gw.FormatVertex += new FormatVertexEventHandler(this.FormatVertex); gw.FormatEdge += new FormatEdgeEventHandler(this.FormatEdge); gw.Write("cluster"); cg2.Colapsed = true; gw.Write("cluster_collapsed"); }