public void TryGetOutEdges_Throws() { var wrappedGraph = new AdjacencyGraph <TestVertex, Edge <TestVertex> >(); var graph = new ArrayAdjacencyGraph <TestVertex, Edge <TestVertex> >(wrappedGraph); TryGetOutEdges_Throws_Test(graph); }
private static void SameEdges <TVertex, TEdge>([NotNull] IVertexAndEdgeListGraph <TVertex, TEdge> graph) where TEdge : IEdge <TVertex> { ArrayAdjacencyGraph <TVertex, TEdge> adjacencyGraph = graph.ToArrayAdjacencyGraph(); CollectionAssert.AreEqual(graph.Edges, adjacencyGraph.Edges); }
public void ContainsVertex_Throws() { var wrappedGraph = new AdjacencyGraph <TestVertex, Edge <TestVertex> >(); var graph = new ArrayAdjacencyGraph <TestVertex, Edge <TestVertex> >(wrappedGraph); ContainsVertex_Throws_Test(graph); }
private static void SameVertexCount <TVertex, TEdge>([NotNull] IVertexAndEdgeListGraph <TVertex, TEdge> graph) where TEdge : IEdge <TVertex> { ArrayAdjacencyGraph <TVertex, TEdge> adjacencyGraph = graph.ToArrayAdjacencyGraph(); Assert.AreEqual(graph.VertexCount, adjacencyGraph.VertexCount); }
public void ContainsEdge_Throws() { var wrappedGraph = new AdjacencyGraph <TestVertex, Edge <TestVertex> >(); var graph = new ArrayAdjacencyGraph <TestVertex, Edge <TestVertex> >(wrappedGraph); ContainsEdge_NullThrows_Test(graph); ContainsEdge_SourceTarget_Throws_Test(graph); }
public GraphCreator(Matrix matrix) { if (matrix.m != matrix.n) { throw new ArgumentException("Adjacency matrix should be square."); } this.matrix = matrix.GetContent(); this.graph = null; this.edgeCost = null; }
public void OutEdges_Throws() { var wrappedGraph1 = new AdjacencyGraph <TestVertex, Edge <TestVertex> >(); var graph1 = new ArrayAdjacencyGraph <TestVertex, Edge <TestVertex> >(wrappedGraph1); OutEdges_NullThrows_Test(graph1); var wrappedGraph2 = new AdjacencyGraph <EquatableTestVertex, Edge <EquatableTestVertex> >(); var graph2 = new ArrayAdjacencyGraph <EquatableTestVertex, Edge <EquatableTestVertex> >(wrappedGraph2); OutEdges_Throws_Test(graph2); }
public void OutEdge_Throws() { var wrappedGraph1 = new AdjacencyGraph <TestVertex, Edge <TestVertex> >(); var graph1 = new ArrayAdjacencyGraph <TestVertex, Edge <TestVertex> >(wrappedGraph1); OutEdge_NullThrows_Test(graph1); var wrappedGraph2 = new AdjacencyGraph <int, Edge <int> >(); OutEdge_Throws_ImmutableGraph_Test( wrappedGraph2, () => new ArrayAdjacencyGraph <int, Edge <int> >(wrappedGraph2)); }
public void BinarySerialization_AdjacencyGraph([NotNull] AdjacencyGraph <int, EquatableEdge <int> > graph) { AdjacencyGraph <int, EquatableEdge <int> > deserializedGraph1 = SerializeDeserialize <int, EquatableEdge <int>, AdjacencyGraph <int, EquatableEdge <int> > >(graph); Assert.IsTrue(EquateGraphs.Equate(graph, deserializedGraph1)); var arrayGraph = new ArrayAdjacencyGraph <int, EquatableEdge <int> >(graph); ArrayAdjacencyGraph <int, EquatableEdge <int> > deserializedGraph2 = SerializeDeserialize <int, EquatableEdge <int>, ArrayAdjacencyGraph <int, EquatableEdge <int> > >(arrayGraph); Assert.IsTrue(EquateGraphs.Equate(arrayGraph, deserializedGraph2)); }
public void BinarySerialization_AdjacencyGraph_Complex([NotNull] AdjacencyGraph <EquatableTestVertex, EquatableTestEdge> graph) { AdjacencyGraph <EquatableTestVertex, EquatableTestEdge> deserializedGraph1 = SerializeDeserialize <EquatableTestVertex, EquatableTestEdge, AdjacencyGraph <EquatableTestVertex, EquatableTestEdge> >(graph); Assert.IsTrue(EquateGraphs.Equate(graph, deserializedGraph1)); var arrayGraph = new ArrayAdjacencyGraph <EquatableTestVertex, EquatableTestEdge>(graph); ArrayAdjacencyGraph <EquatableTestVertex, EquatableTestEdge> deserializedGraph2 = SerializeDeserialize <EquatableTestVertex, EquatableTestEdge, ArrayAdjacencyGraph <EquatableTestVertex, EquatableTestEdge> >(arrayGraph); Assert.IsTrue(EquateGraphs.Equate(arrayGraph, deserializedGraph2)); }
private static void AssertSameProperties <TVertex, TEdge>(IVertexAndEdgeListGraph <TVertex, TEdge> graph) where TEdge : IEdge <TVertex> { ArrayAdjacencyGraph <TVertex, TEdge> adjacencyGraph = graph.ToArrayAdjacencyGraph(); Assert.AreEqual(graph.VertexCount, adjacencyGraph.VertexCount); CollectionAssert.AreEqual(graph.Vertices, adjacencyGraph.Vertices); Assert.AreEqual(graph.EdgeCount, adjacencyGraph.EdgeCount); CollectionAssert.AreEqual(graph.Edges, adjacencyGraph.Edges); foreach (TVertex vertex in graph.Vertices) { CollectionAssert.AreEqual(graph.OutEdges(vertex), adjacencyGraph.OutEdges(vertex)); } }
public void Construction() { var wrappedGraph = new AdjacencyGraph <int, Edge <int> >(); var graph = new ArrayAdjacencyGraph <int, Edge <int> >(wrappedGraph); AssertGraphProperties(graph); AssertEmptyGraph(graph); wrappedGraph.AddVertexRange(new[] { 2, 3, 1 }); graph = new ArrayAdjacencyGraph <int, Edge <int> >(wrappedGraph); AssertGraphProperties(graph); AssertHasVertices(graph, new[] { 1, 2, 3 }); AssertNoEdge(graph); var edge1 = new Edge <int>(1, 2); var edge2 = new Edge <int>(2, 2); var edge3 = new Edge <int>(3, 4); var edge4 = new Edge <int>(1, 4); wrappedGraph.AddVerticesAndEdgeRange(new[] { edge1, edge2, edge3, edge4 }); graph = new ArrayAdjacencyGraph <int, Edge <int> >(wrappedGraph); AssertGraphProperties(graph); AssertHasVertices(graph, new[] { 1, 2, 3, 4 }); AssertHasEdges(graph, new[] { edge1, edge2, edge3, edge4 }); wrappedGraph = new AdjacencyGraph <int, Edge <int> >(false); wrappedGraph.AddVerticesAndEdgeRange(new[] { edge1, edge1, edge2, edge3, edge4 }); graph = new ArrayAdjacencyGraph <int, Edge <int> >(wrappedGraph); AssertGraphProperties(graph, false); AssertHasVertices(graph, new[] { 1, 2, 3, 4 }); AssertHasEdges(graph, new[] { edge1, edge2, edge3, edge4 }); #region Local function void AssertGraphProperties <TVertex, TEdge>( ArrayAdjacencyGraph <TVertex, TEdge> g, bool allowParallelEdges = true) where TEdge : IEdge <TVertex> { Assert.IsTrue(g.IsDirected); Assert.AreEqual(allowParallelEdges, g.AllowParallelEdges); } #endregion }
public (IVertexAndEdgeListGraph <int, Edge <int> >, Func <Edge <int>, double>) GetResult() { if (this.graph != null) { return(this.graph, this.edgeCost); } AdjacencyGraph <int, Edge <int> > graph = new AdjacencyGraph <int, Edge <int> >(); int i, j; for (i = 0; i < this.matrix.Length; ++i) { for (j = 0; j < this.matrix[i].Length; ++j) { if (matrix[i][j] >= 0) { graph.AddVerticesAndEdge(new Edge <int>(i, j)); } } } this.edgeCost = edge => matrix[edge.Source][edge.Target]; this.graph = new ArrayAdjacencyGraph <int, Edge <int> >(graph); return(this.GetResult()); }
public void Clone() { var wrappedGraph = new AdjacencyGraph <int, Edge <int> >(); var graph = new ArrayAdjacencyGraph <int, Edge <int> >(wrappedGraph); AssertEmptyGraph(graph); var clonedGraph = graph.Clone(); Assert.IsNotNull(clonedGraph); AssertEmptyGraph(clonedGraph); clonedGraph = (ArrayAdjacencyGraph <int, Edge <int> >)((ICloneable)graph).Clone(); Assert.IsNotNull(clonedGraph); AssertEmptyGraph(clonedGraph); var edge1 = new Edge <int>(1, 2); var edge2 = new Edge <int>(1, 3); var edge3 = new Edge <int>(2, 3); wrappedGraph.AddVerticesAndEdgeRange(new[] { edge1, edge2, edge3 }); graph = new ArrayAdjacencyGraph <int, Edge <int> >(wrappedGraph); AssertHasVertices(graph, new[] { 1, 2, 3 }); AssertHasEdges(graph, new[] { edge1, edge2, edge3 }); clonedGraph = graph.Clone(); Assert.IsNotNull(clonedGraph); AssertHasVertices(clonedGraph, new[] { 1, 2, 3 }); AssertHasEdges(clonedGraph, new[] { edge1, edge2, edge3 }); clonedGraph = (ArrayAdjacencyGraph <int, Edge <int> >)((ICloneable)graph).Clone(); Assert.IsNotNull(clonedGraph); AssertHasVertices(clonedGraph, new[] { 1, 2, 3 }); AssertHasEdges(clonedGraph, new[] { edge1, edge2, edge3 }); }