protected static void AddEdgeRange_ForbiddenParallelEdges_Test() { int edgeAdded = 0; var graph = new BidirectionalMatrixGraph <Edge <int> >(3); AssertNoEdge(graph); graph.EdgeAdded += e => { Assert.IsNotNull(e); ++edgeAdded; }; // Edge 1, 2, 3 var edge1 = new Edge <int>(0, 1); var edge2 = new Edge <int>(0, 2); var edge3 = new Edge <int>(1, 2); Assert.AreEqual(3, graph.AddEdgeRange(new[] { edge1, edge2, edge3 })); Assert.AreEqual(3, edgeAdded); AssertHasEdges(graph, new[] { edge1, edge2, edge3 }); // Edge 4 var edge4 = new Edge <int>(2, 2); Assert.AreEqual(1, graph.AddEdgeRange(new[] { edge4 })); Assert.AreEqual(4, edgeAdded); AssertHasEdges(graph, new[] { edge1, edge2, edge3, edge4 }); }
public BidirectionalMatrixGraph<Edge<int>> TwoDisjointEdges() { BidirectionalMatrixGraph<Edge<int>> g = new BidirectionalMatrixGraph<Edge<int>>(10); g.AddEdge(new Edge<int>(0, 1)); g.AddEdge(new Edge<int>(5, 6)); return g; }
public BidirectionalMatrixGraph<Edge<int>> TwoConnectedEdges() { BidirectionalMatrixGraph<Edge<int>> g = new BidirectionalMatrixGraph<Edge<int>>(10); g.AddEdge(new Edge<int>(0, 1)); g.AddEdge(new Edge<int>(1, 2)); return g; }
protected static void ContainsEdge_SourceTarget_ForbiddenParallelEdges_Test( [NotNull] BidirectionalMatrixGraph <Edge <int> > graph) { var edge1 = new Edge <int>(1, 2); var edge2 = new Edge <int>(1, 3); var edge3 = new Edge <int>(2, 2); Assert.IsFalse(graph.ContainsEdge(1, 2)); Assert.IsFalse(graph.ContainsEdge(2, 1)); graph.AddEdge(edge1); Assert.IsTrue(graph.ContainsEdge(1, 2)); Assert.IsFalse(graph.ContainsEdge(2, 1)); graph.AddEdge(edge2); Assert.IsTrue(graph.ContainsEdge(1, 3)); Assert.IsFalse(graph.ContainsEdge(3, 1)); graph.AddEdge(edge3); Assert.IsTrue(graph.ContainsEdge(2, 2)); // Vertices is not present in the graph Assert.IsFalse(graph.ContainsEdge(4, 5)); Assert.IsFalse(graph.ContainsEdge(1, 4)); Assert.IsFalse(graph.ContainsEdge(4, 1)); }
public BidirectionalMatrixGraph<Edge<int>> Loop() { BidirectionalMatrixGraph<Edge<int>> g = new BidirectionalMatrixGraph<Edge<int>>(10); g.AddEdge(new Edge<int>(0, 1)); g.AddEdge(new Edge<int>(1, 0)); return g; }
protected static void TryGetEdges_ImmutableVertices_Test( [NotNull] BidirectionalMatrixGraph <Edge <int> > graph) { var edge1 = new Edge <int>(1, 2); var edge2 = new Edge <int>(1, 3); var edge3 = new Edge <int>(2, 2); var edge4 = new Edge <int>(2, 4); var edge5 = new Edge <int>(3, 1); graph.AddEdgeRange(new[] { edge1, edge2, edge3, edge4, edge5 }); Assert.IsFalse(graph.TryGetEdges(6, 10, out _)); Assert.IsFalse(graph.TryGetEdges(6, 1, out _)); Assert.IsTrue(graph.TryGetEdges(2, 2, out IEnumerable <Edge <int> > gotEdges)); CollectionAssert.AreEqual(new[] { edge3 }, gotEdges); Assert.IsTrue(graph.TryGetEdges(2, 4, out gotEdges)); CollectionAssert.AreEqual(new[] { edge4 }, gotEdges); Assert.IsTrue(graph.TryGetEdges(1, 2, out gotEdges)); CollectionAssert.AreEqual(new[] { edge1 }, gotEdges); Assert.IsTrue(graph.TryGetEdges(2, 1, out gotEdges)); CollectionAssert.IsEmpty(gotEdges); }
protected static void TryGetEdge_ImmutableVertices_Test( [NotNull] BidirectionalMatrixGraph <Edge <int> > graph) { var edge1 = new Edge <int>(1, 2); var edge2 = new Edge <int>(1, 3); var edge3 = new Edge <int>(2, 2); var edge4 = new Edge <int>(2, 4); var edge5 = new Edge <int>(3, 1); graph.AddEdgeRange(new[] { edge1, edge2, edge3, edge4, edge5 }); Assert.IsFalse(graph.TryGetEdge(6, 10, out _)); Assert.IsFalse(graph.TryGetEdge(6, 1, out _)); Assert.IsTrue(graph.TryGetEdge(2, 4, out Edge <int> gotEdge)); Assert.AreSame(edge4, gotEdge); Assert.IsTrue(graph.TryGetEdge(2, 2, out gotEdge)); Assert.AreSame(edge3, gotEdge); Assert.IsTrue(graph.TryGetEdge(1, 2, out gotEdge)); Assert.AreSame(edge1, gotEdge); Assert.IsFalse(graph.TryGetEdge(2, 1, out _)); }
protected static void AddEdge_EquatableEdge_ForbiddenParallelEdges_Test( [NotNull] BidirectionalMatrixGraph <EquatableEdge <int> > graph) { int edgeAdded = 0; AssertNoEdge(graph); // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local graph.EdgeAdded += e => { Assert.IsNotNull(e); ++edgeAdded; }; // Edge 1 var edge1 = new EquatableEdge <int>(1, 2); Assert.IsTrue(graph.AddEdge(edge1)); Assert.AreEqual(1, edgeAdded); AssertHasEdges(graph, new[] { edge1 }); // Edge 2 var edge2 = new EquatableEdge <int>(2, 1); Assert.IsTrue(graph.AddEdge(edge2)); Assert.AreEqual(2, edgeAdded); AssertHasEdges(graph, new[] { edge1, edge2 }); // Edge 3 self edge var edge3 = new EquatableEdge <int>(2, 2); Assert.IsTrue(graph.AddEdge(edge3)); Assert.AreEqual(3, edgeAdded); AssertHasEdges(graph, new[] { edge1, edge2, edge3 }); }
public void GraphMLSerialization_BidirectionalMatrixGraph([NotNull] BidirectionalMatrixGraph <EquatableEdge <int> > graph) { AdjacencyGraph <int, EquatableEdge <int> > deserializedGraph = SerializeDeserialize <BidirectionalMatrixGraph <EquatableEdge <int> >, AdjacencyGraph <int, EquatableEdge <int> > >(graph); Assert.IsTrue(EquateGraphs.Equate(graph, deserializedGraph)); }
public (BidirectionalMatrixGraph <Edge <int> >, Func <Edge <int>, double>, int) GetResult() { if (this.graph != null) { return(this.graph, this.edgeWeights, vertexCount); } var graph = new BidirectionalMatrixGraph <Edge <int> >(vertexCount); var edges = new List <Edge <int> >(); for (int i = 0; i < this.vertexCount; i++) { for (int j = 0; j < this.vertexCount; j++) { if (i != j && adjacencyMatrix[i][j] > 0) { edges.Add(new Edge <int>(i, j)); } } } graph.AddEdgeRange(edges); this.edgeWeights = edge => adjacencyMatrix[edge.Source][edge.Target]; this.graph = graph; return(GetResult()); }
protected static void AddEdge_EquatableEdge_ForbiddenParallelEdges_Test( BidirectionalMatrixGraph <EquatableEdge <int> > graph) { int edgeAdded = 0; AssertNoEdge(graph); graph.EdgeAdded += e => { Assert.IsNotNull(e); ++edgeAdded; }; // Edge 1 var edge1 = new EquatableEdge <int>(1, 2); Assert.IsTrue(graph.AddEdge(edge1)); Assert.AreEqual(1, edgeAdded); AssertHasEdges(graph, new[] { edge1 }); // Edge 2 var edge2 = new EquatableEdge <int>(2, 1); Assert.IsTrue(graph.AddEdge(edge2)); Assert.AreEqual(2, edgeAdded); AssertHasEdges(graph, new[] { edge1, edge2 }); // Edge 3 self edge var edge3 = new EquatableEdge <int>(2, 2); Assert.IsTrue(graph.AddEdge(edge3)); Assert.AreEqual(3, edgeAdded); AssertHasEdges(graph, new[] { edge1, edge2, edge3 }); }
public void BinarySerialization_BidirectionalMatrixGraph([NotNull] BidirectionalMatrixGraph <EquatableEdge <int> > graph) { BidirectionalMatrixGraph <EquatableEdge <int> > deserializedGraph = SerializeDeserialize <int, EquatableEdge <int>, BidirectionalMatrixGraph <EquatableEdge <int> > >(graph); Assert.IsTrue(EquateGraphs.Equate(graph, deserializedGraph)); }
protected static void Degree_Throws_Matrix_Test <TEdge>( [NotNull] BidirectionalMatrixGraph <TEdge> graph) where TEdge : class, IEdge <int> { // ReSharper disable ReturnValueOfPureMethodIsNotUsed Assert.Throws <VertexNotFoundException>(() => graph.Degree(-1)); Assert.Throws <VertexNotFoundException>(() => graph.Degree(10)); // ReSharper restore ReturnValueOfPureMethodIsNotUsed }
public Graph(Matrix matrix) { if (matrix.m != matrix.n) { throw new ArgumentException("This is not a square matrix!"); } adjacencyMatrix = matrix.GetContent(); graph = null; edgeWeights = null; vertexCount = matrix.n; }
protected static void InEdge_Throws_ImmutableVertices_Test( BidirectionalMatrixGraph <Edge <int> > graph) { // ReSharper disable ReturnValueOfPureMethodIsNotUsed Assert.Throws <VertexNotFoundException>(() => graph.InEdge(-1, 0)); Assert.Throws <VertexNotFoundException>(() => graph.InEdge(4, 0)); graph.AddEdge(new Edge <int>(2, 1)); AssertIndexOutOfRange(() => graph.InEdge(1, 5)); // ReSharper restore ReturnValueOfPureMethodIsNotUsed }
private BidirectionalMatrixGraph <IEdge <int> > getTestGraph() { var graph = new BidirectionalMatrixGraph <IEdge <int> >(4); var list = new List <IEdge <int> >(); list.Add(new EquatableEdge <int>(0, 1)); list.Add(new EquatableEdge <int>(1, 2)); list.Add(new EquatableEdge <int>(1, 0)); list.Add(new EquatableEdge <int>(2, 3)); graph.AddEdgeRange(list); return(graph); }
protected static void InEdges_Throws_Matrix_Test <TEdge>( BidirectionalMatrixGraph <TEdge> graph) where TEdge : class, IEdge <int> { const int vertex = 10; // ReSharper disable ReturnValueOfPureMethodIsNotUsed // ReSharper disable AssignNullToNotNullAttribute Assert.Throws <VertexNotFoundException>(() => graph.IsInEdgesEmpty(vertex)); Assert.Throws <VertexNotFoundException>(() => graph.InDegree(vertex)); Assert.Throws <VertexNotFoundException>(() => graph.InEdges(vertex).ToArray()); // ReSharper restore AssignNullToNotNullAttribute // ReSharper restore ReturnValueOfPureMethodIsNotUsed }
public void Construction() { var graph = new BidirectionalMatrixGraph<Edge<int>>(1); AssertGraphProperties(graph, 1); graph = new BidirectionalMatrixGraph<Edge<int>>(12); AssertGraphProperties(graph, 12); #region Local function void AssertGraphProperties<TEdge>( BidirectionalMatrixGraph<TEdge> g, int nbVertices) where TEdge : class, IEdge<int>
protected static void RemoveOutEdgeIf_ImmutableVertices_Test( [NotNull] BidirectionalMatrixGraph <Edge <int> > graph) { int edgesRemoved = 0; // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local graph.EdgeRemoved += e => { Assert.IsNotNull(e); // ReSharper disable once AccessToModifiedClosure ++edgesRemoved; }; Assert.AreEqual(0, graph.RemoveOutEdgeIf(6, _ => true)); CheckCounter(0); AssertNoEdge(graph); var edge01 = new Edge <int>(0, 1); var edge02 = new Edge <int>(0, 2); var edge03 = new Edge <int>(0, 3); var edge13 = new Edge <int>(1, 3); var edge20 = new Edge <int>(2, 0); var edge22 = new Edge <int>(2, 2); graph.AddEdgeRange(new[] { edge01, edge02, edge03, edge13, edge20, edge22 }); Assert.AreEqual(2, graph.RemoveOutEdgeIf(0, edge => edge.Target >= 2)); CheckCounter(2); AssertHasEdges(graph, new[] { edge01, edge13, edge20, edge22 }); Assert.AreEqual(0, graph.RemoveOutEdgeIf(2, edge => edge.Target > 4)); CheckCounter(0); AssertHasEdges(graph, new[] { edge01, edge13, edge20, edge22 }); Assert.AreEqual(2, graph.RemoveOutEdgeIf(2, _ => true)); CheckCounter(2); AssertHasEdges(graph, new[] { edge01, edge13 }); #region Local function void CheckCounter(int expectedRemovedEdges) { Assert.AreEqual(expectedRemovedEdges, edgesRemoved); edgesRemoved = 0; } #endregion }
protected static void OutEdge_ImmutableVertices_Test( BidirectionalMatrixGraph <Edge <int> > graph) { var edge11 = new Edge <int>(1, 1); var edge12 = new Edge <int>(1, 2); var edge13 = new Edge <int>(1, 3); var edge24 = new Edge <int>(2, 4); var edge33 = new Edge <int>(3, 3); var edge41 = new Edge <int>(4, 1); graph.AddEdgeRange(new[] { edge11, edge12, edge13, edge24, edge33, edge41 }); Assert.AreSame(edge11, graph.OutEdge(1, 0)); Assert.AreSame(edge13, graph.OutEdge(1, 2)); Assert.AreSame(edge24, graph.OutEdge(2, 0)); Assert.AreSame(edge33, graph.OutEdge(3, 0)); Assert.AreSame(edge41, graph.OutEdge(4, 0)); }
protected static void Degree_ImmutableVertices_Test( [NotNull] BidirectionalMatrixGraph <Edge <int> > graph) { var edge1 = new Edge <int>(1, 2); var edge2 = new Edge <int>(1, 3); var edge3 = new Edge <int>(1, 4); var edge4 = new Edge <int>(2, 4); var edge5 = new Edge <int>(3, 2); var edge6 = new Edge <int>(3, 3); graph.AddEdgeRange(new[] { edge1, edge2, edge3, edge4, edge5, edge6 }); Assert.AreEqual(0, graph.Degree(0)); Assert.AreEqual(3, graph.Degree(1)); Assert.AreEqual(3, graph.Degree(2)); Assert.AreEqual(4, graph.Degree(3)); // Self edge Assert.AreEqual(2, graph.Degree(4)); }
protected static void InEdge_ImmutableVertices_Test( BidirectionalMatrixGraph <Edge <int> > graph) { var edge11 = new Edge <int>(1, 1); var edge14 = new Edge <int>(1, 4); var edge21 = new Edge <int>(2, 1); var edge31 = new Edge <int>(3, 1); var edge33 = new Edge <int>(3, 3); var edge42 = new Edge <int>(4, 2); graph.AddEdgeRange(new[] { edge11, edge14, edge21, edge31, edge33, edge42 }); Assert.AreSame(edge11, graph.InEdge(1, 0)); Assert.AreSame(edge31, graph.InEdge(1, 2)); Assert.AreSame(edge42, graph.InEdge(2, 0)); Assert.AreSame(edge33, graph.InEdge(3, 0)); Assert.AreSame(edge14, graph.InEdge(4, 0)); }
protected static void OutEdges_ImmutableVertices_Test( BidirectionalMatrixGraph <Edge <int> > graph) { var edge01 = new Edge <int>(0, 1); var edge02 = new Edge <int>(0, 2); var edge03 = new Edge <int>(0, 3); var edge13 = new Edge <int>(1, 3); var edge20 = new Edge <int>(2, 0); var edge22 = new Edge <int>(2, 2); AssertNoOutEdge(graph, 1); graph.AddEdgeRange(new[] { edge01, edge02, edge03, edge13, edge20, edge22 }); AssertHasOutEdges(graph, 0, new[] { edge01, edge02, edge03 }); AssertHasOutEdges(graph, 1, new[] { edge13 }); AssertHasOutEdges(graph, 2, new[] { edge20, edge22 }); AssertNoOutEdge(graph, 3); }
protected static void InEdges_ImmutableVertices_Test( BidirectionalMatrixGraph <Edge <int> > graph) { var edge02 = new Edge <int>(0, 2); var edge10 = new Edge <int>(1, 0); var edge20 = new Edge <int>(2, 0); var edge22 = new Edge <int>(2, 2); var edge30 = new Edge <int>(3, 0); var edge31 = new Edge <int>(3, 1); AssertNoInEdge(graph, 1); graph.AddEdgeRange(new[] { edge02, edge10, edge20, edge22, edge30, edge31 }); AssertHasInEdges(graph, 0, new[] { edge10, edge20, edge30 }); AssertHasInEdges(graph, 1, new[] { edge31 }); AssertHasInEdges(graph, 2, new[] { edge02, edge22 }); AssertNoInEdge(graph, 3); }
protected static void AddEdgeRange_ForbiddenParallelEdges_Throws_Test( [NotNull] BidirectionalMatrixGraph <Edge <int> > graph) { int edgeAdded = 0; AssertNoEdge(graph); // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local graph.EdgeAdded += e => { Assert.IsNotNull(e); ++edgeAdded; }; // ReSharper disable once AssignNullToNotNullAttribute Assert.Throws <ArgumentNullException>(() => graph.AddEdgeRange(null)); AssertNoEdge(graph); Assert.AreEqual(0, edgeAdded); // Edge 1, 2, 3 var edge1 = new Edge <int>(0, 1); var edge3 = new Edge <int>(1, 2); Assert.Throws <ArgumentNullException>(() => graph.AddEdgeRange(new[] { edge1, null, edge3 })); Assert.AreEqual(0, edgeAdded); AssertNoEdge(graph); // Edge 1, 3, 4 var edge4 = new Edge <int>(0, 1); Assert.Throws <ParallelEdgeNotAllowedException>(() => graph.AddEdgeRange(new[] { edge1, edge3, edge4 })); Assert.AreEqual(2, edgeAdded); AssertHasEdges(graph, new[] { edge1, edge3 }); // Out of range => vertex not found Assert.Throws <VertexNotFoundException>(() => graph.AddEdgeRange(new[] { new Edge <int>(4, 5), })); Assert.AreEqual(2, edgeAdded); AssertHasEdges(graph, new[] { edge1, edge3 }); }
protected static void RemoveEdge_EquatableEdge_ImmutableVertices_Test( [NotNull] BidirectionalMatrixGraph <EquatableEdge <int> > graph) { int edgesRemoved = 0; graph.EdgeRemoved += e => { Assert.IsNotNull(e); // ReSharper disable once AccessToModifiedClosure ++edgesRemoved; }; var edge01 = new EquatableEdge <int>(0, 1); var edge02 = new EquatableEdge <int>(0, 2); var edge03 = new EquatableEdge <int>(0, 3); var edge13 = new EquatableEdge <int>(1, 3); var edge20 = new EquatableEdge <int>(2, 0); var edge22 = new EquatableEdge <int>(2, 2); var edgeNotInGraph = new EquatableEdge <int>(2, 3); var edgeWithVertexNotInGraph1 = new EquatableEdge <int>(2, 10); var edgeWithVertexNotInGraph2 = new EquatableEdge <int>(10, 2); var edgeWithVerticesNotInGraph = new EquatableEdge <int>(10, 11); var edgeNotEquatable = new EquatableEdge <int>(0, 1); graph.AddEdgeRange(new[] { edge01, edge02, edge03, edge13, edge20, edge22 }); Assert.IsFalse(graph.RemoveEdge(edgeNotInGraph)); CheckCounter(0); Assert.IsFalse(graph.RemoveEdge(edgeWithVertexNotInGraph1)); CheckCounter(0); Assert.IsFalse(graph.RemoveEdge(edgeWithVertexNotInGraph2)); CheckCounter(0); Assert.IsFalse(graph.RemoveEdge(edgeWithVerticesNotInGraph)); CheckCounter(0); Assert.IsTrue(graph.RemoveEdge(edgeNotEquatable)); CheckCounter(1); AssertHasVertices(graph, new[] { 0, 1, 2, 3 }); AssertHasEdges(graph, new[] { edge02, edge03, edge13, edge20, edge22 }); Assert.IsTrue(graph.RemoveEdge(edge02)); CheckCounter(1); AssertHasVertices(graph, new[] { 0, 1, 2, 3 }); AssertHasEdges(graph, new[] { edge03, edge13, edge20, edge22 }); Assert.IsTrue(graph.RemoveEdge(edge20)); CheckCounter(1); AssertHasVertices(graph, new[] { 0, 1, 2, 3 }); AssertHasEdges(graph, new[] { edge03, edge13, edge22 }); Assert.IsTrue(graph.RemoveEdge(edge03)); Assert.IsTrue(graph.RemoveEdge(edge13)); Assert.IsTrue(graph.RemoveEdge(edge22)); CheckCounter(3); AssertHasVertices(graph, new[] { 0, 1, 2, 3 }); AssertNoEdge(graph); #region Local function void CheckCounter(int expectedRemovedEdges) { Assert.AreEqual(expectedRemovedEdges, edgesRemoved); edgesRemoved = 0; } #endregion }
static void Main(string[] args) { String path = args[0], output = args[1]; var adjacencyMatrix = MatrixReader.Reader(path); (BidirectionalMatrixGraph <Edge <int> > graph, Func <Edge <int>, double> edgeWeights, int vertexCount) = new Graph(adjacencyMatrix).GetResult(); int root = 0; TryFunc <int, IEnumerable <Edge <int> > > tryGetPaths = graph.ShortestPathsDijkstra(edgeWeights, root); var graphShort = new BidirectionalMatrixGraph <Edge <int> >(vertexCount); // query path for given vertices foreach (var target in graph.Vertices) { IEnumerable <Edge <int> > pathShort; if (tryGetPaths(target, out pathShort)) { foreach (var edge in pathShort) { if (!graphShort.ContainsEdge(edge)) { graphShort.AddEdge(edge); } } } } // render String GetDotCode(BidirectionalMatrixGraph <Edge <int> > shortestTree, BidirectionalMatrixGraph <Edge <int> > graph) { StringBuilder builder = new StringBuilder(); builder.AppendLine("digraph G {"); foreach (var vertex in graph.Vertices) { builder.Append(vertex.ToString()); builder.AppendLine(";"); } foreach (var edge in graph.Edges) { builder.Append(edge.ToString()); if (shortestTree.ContainsEdge(edge)) { builder.AppendLine(" [color = red];"); } else { builder.AppendLine(" [];"); } } builder.AppendLine("}"); return(builder.ToString()); } void GeneratePDF(String output, String dot) { String dotFile = output + ".dot"; using (StreamWriter writer = File.CreateText(dotFile)) writer.Write(dot); using (Process process = new Process()) { process.StartInfo.FileName = "dot"; process.StartInfo.Arguments = "-Tpdf -o" + output + " " + dotFile; process.Start(); while (!process.HasExited) { process.Refresh(); } } File.Delete(dotFile); } string outputDot = GetDotCode(graphShort, graph); GeneratePDF(output, outputDot); }
protected static void RemoveOutEdgeIf_Throws_Test <TEdge>( [NotNull] BidirectionalMatrixGraph <TEdge> graph) where TEdge : class, IEdge <int> { // ReSharper disable once AssignNullToNotNullAttribute Assert.Throws <ArgumentNullException>(() => graph.RemoveOutEdgeIf(default, null));