protected static void AdjacentEdges_ImmutableGraph_Test(
            [NotNull] IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph,
            [NotNull, InstantHandle] Func <IImplicitUndirectedGraph <int, Edge <int> > > createGraph)
        {
            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);

            wrappedGraph.AddVertex(1);
            IImplicitUndirectedGraph <int, Edge <int> > graph = createGraph();

            AssertNoAdjacentEdge(graph, 1);

            wrappedGraph.AddVertex(5);
            wrappedGraph.AddVerticesAndEdgeRange(new[] { edge12, edge13, edge14, edge24, edge31, edge33 });
            graph = createGraph();

            AssertHasAdjacentEdges(graph, 1, new[] { edge12, edge13, edge14, edge31 });
            AssertHasAdjacentEdges(graph, 2, new[] { edge12, edge24 });
            AssertHasAdjacentEdges(graph, 3, new[] { edge13, edge31, edge33 }, 4);  // Has self edge counting twice
            AssertHasAdjacentEdges(graph, 4, new[] { edge14, edge24 });
            AssertNoAdjacentEdge(graph, 5);
        }
        protected static void InEdges_ImmutableGraph_ReversedTest(
            IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph,
            Func <IBidirectionalIncidenceGraph <int, SReversedEdge <int, Edge <int> > > > createGraph)
        {
            var edge12 = new Edge <int>(1, 2);
            var edge13 = new Edge <int>(1, 3);
            var edge14 = new Edge <int>(1, 4);
            var edge33 = new Edge <int>(3, 3);
            var edge34 = new Edge <int>(3, 4);
            var edge43 = new Edge <int>(4, 3);

            wrappedGraph.AddVertex(1);
            IBidirectionalIncidenceGraph <int, SReversedEdge <int, Edge <int> > > graph = createGraph();

            AssertNoInEdge(graph, 1);
            AssertNoOutEdge(graph, 1);

            wrappedGraph.AddVerticesAndEdgeRange(new[] { edge12, edge13, edge14, edge33, edge34, edge43 });
            graph = createGraph();

            AssertNoOutEdge(graph, 1);
            AssertHasReversedOutEdges(graph, 2, new[] { edge12 });
            AssertHasReversedOutEdges(graph, 3, new[] { edge13, edge33, edge43 });
            AssertHasReversedOutEdges(graph, 4, new[] { edge14, edge34 });

            AssertHasReversedInEdges(graph, 1, new[] { edge12, edge13, edge14 });
            AssertNoInEdge(graph, 2);
            AssertHasReversedInEdges(graph, 3, new[] { edge33, edge34 });
            AssertHasReversedInEdges(graph, 4, new[] { edge43 });
        }
        /// <summary>
        /// Clones a graph to another graph
        /// </summary>
        /// <typeparam name="TVertex">type of the vertices</typeparam>
        /// <typeparam name="TEdge">type of the edges</typeparam>
        /// <param name="g"></param>
        /// <param name="vertexCloner"></param>
        /// <param name="edgeCloner"></param>
        /// <param name="clone"></param>
        public static void Clone <TVertex, TEdge>(
#if !NET20
            this
#endif
            IVertexAndEdgeListGraph <TVertex, TEdge> g,
            Func <TVertex, TVertex> vertexCloner,
            Func <TEdge, TVertex, TVertex, TEdge> edgeCloner,
            IMutableVertexAndEdgeSet <TVertex, TEdge> clone)
            where TEdge : IEdge <TVertex>
        {
            Contract.Requires(g != null);
            Contract.Requires(vertexCloner != null);
            Contract.Requires(edgeCloner != null);
            Contract.Requires(clone != null);

            var vertexClones = new Dictionary <TVertex, TVertex>(g.VertexCount);

            foreach (var v in g.Vertices)
            {
                var vc = vertexCloner(v);
                clone.AddVertex(vc);
                vertexClones.Add(v, vc);
            }

            foreach (var edge in g.Edges)
            {
                var ec = edgeCloner(
                    edge,
                    vertexClones[edge.Source],
                    vertexClones[edge.Target]);
                clone.AddEdge(ec);
            }
        }
Beispiel #4
0
        protected static void InEdges_ImmutableGraph_Test(
            [NotNull] IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph,
            [NotNull, InstantHandle] Func <IBidirectionalIncidenceGraph <int, Edge <int> > > createGraph)
        {
            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 edge32 = new Edge <int>(3, 2);
            var edge33 = new Edge <int>(3, 3);

            wrappedGraph.AddVertex(1);
            IBidirectionalIncidenceGraph <int, Edge <int> > graph = createGraph();

            AssertNoInEdge(graph, 1);
            AssertNoOutEdge(graph, 1);

            wrappedGraph.AddVerticesAndEdgeRange(new[] { edge12, edge13, edge14, edge24, edge32, edge33 });
            graph = createGraph();

            AssertHasOutEdges(graph, 1, new[] { edge12, edge13, edge14 });
            AssertHasOutEdges(graph, 2, new[] { edge24 });
            AssertHasOutEdges(graph, 3, new[] { edge32, edge33 });
            AssertNoOutEdge(graph, 4);

            AssertNoInEdge(graph, 1);
            AssertHasInEdges(graph, 2, new[] { edge12, edge32 });
            AssertHasInEdges(graph, 3, new[] { edge13, edge33 });
            AssertHasInEdges(graph, 4, new[] { edge14, edge24 });
        }
        protected static void AdjacentEdge_Throws_ImmutableGraph_Test(
            [NotNull] IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph,
            [NotNull, InstantHandle] Func <IImplicitUndirectedGraph <int, Edge <int> > > createGraph)
        {
            const int vertex1 = 1;
            const int vertex2 = 2;

            IImplicitUndirectedGraph <int, Edge <int> > graph = createGraph();

            // ReSharper disable ReturnValueOfPureMethodIsNotUsed
            Assert.Throws <VertexNotFoundException>(() => graph.AdjacentEdge(vertex1, 0));

            wrappedGraph.AddVertex(vertex1);
            wrappedGraph.AddVertex(vertex2);
            graph = createGraph();
            AssertIndexOutOfRange(() => graph.AdjacentEdge(vertex1, 0));

            wrappedGraph.AddEdge(new Edge <int>(1, 2));
            graph = createGraph();
            AssertIndexOutOfRange(() => graph.AdjacentEdge(vertex1, 5));
            // ReSharper restore ReturnValueOfPureMethodIsNotUsed
        }
        protected static void InEdge_Throws_ImmutableGraph_ReversedTest(
            IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph,
            Func <IBidirectionalIncidenceGraph <int, SReversedEdge <int, Edge <int> > > > createGraph)
        {
            const int vertex1 = 1;
            const int vertex2 = 2;

            // ReSharper disable ReturnValueOfPureMethodIsNotUsed
            IBidirectionalIncidenceGraph <int, SReversedEdge <int, Edge <int> > > graph = createGraph();

            Assert.Throws <VertexNotFoundException>(() => graph.InEdge(vertex1, 0));

            wrappedGraph.AddVertex(vertex1);
            wrappedGraph.AddVertex(vertex2);
            graph = createGraph();
            AssertIndexOutOfRange(() => graph.InEdge(vertex1, 0));

            wrappedGraph.AddEdge(new Edge <int>(1, 2));
            graph = createGraph();
            AssertIndexOutOfRange(() => graph.InEdge(vertex1, 5));
            // ReSharper restore ReturnValueOfPureMethodIsNotUsed
        }
Beispiel #7
0
        protected static void OutEdge_Throws_ImmutableGraph_Test <TEdge>(
            IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph,
            Func <IImplicitGraph <int, TEdge> > createGraph)
            where TEdge : IEdge <int>
        {
            IImplicitGraph <int, TEdge> graph = createGraph();

            const int vertex1 = 1;
            const int vertex2 = 2;

            // ReSharper disable ReturnValueOfPureMethodIsNotUsed
            Assert.Throws <VertexNotFoundException>(() => graph.OutEdge(vertex1, 0));

            wrappedGraph.AddVertex(vertex1);
            wrappedGraph.AddVertex(vertex2);
            graph = createGraph();
            AssertIndexOutOfRange(() => graph.OutEdge(vertex1, 0));

            wrappedGraph.AddEdge(new Edge <int>(1, 2));
            graph = createGraph();
            AssertIndexOutOfRange(() => graph.OutEdge(vertex1, 5));
            // ReSharper restore ReturnValueOfPureMethodIsNotUsed
        }
Beispiel #8
0
        protected static void OutEdges_ImmutableGraph_Test(
            IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph,
            Func <IImplicitGraph <int, SEquatableEdge <int> > > createGraph)
        {
            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);

            wrappedGraph.AddVertex(1);
            IImplicitGraph <int, SEquatableEdge <int> > graph = createGraph();

            AssertNoOutEdge(graph, 1);

            wrappedGraph.AddVerticesAndEdgeRange(new[] { edge12, edge13, edge14, edge24, edge31, edge33 });
            graph = createGraph();

            AssertHasOutEdges(
                graph,
                1,
                new[]
            {
                new SEquatableEdge <int>(1, 2),
                new SEquatableEdge <int>(1, 3),
                new SEquatableEdge <int>(1, 4)
            });
            AssertHasOutEdges(
                graph,
                2,
                new[] { new SEquatableEdge <int>(2, 4) });
            AssertHasOutEdges(
                graph,
                3,
                new[]
            {
                new SEquatableEdge <int>(3, 1),
                new SEquatableEdge <int>(3, 3)
            });
            AssertNoOutEdge(graph, 4);
        }
        protected static void Degree_ImmutableGraph_ReversedTest(
            [NotNull] IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph,
            [NotNull, InstantHandle] Func <IBidirectionalIncidenceGraph <int, SReversedEdge <int, Edge <int> > > > createGraph)
        {
            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);

            wrappedGraph.AddVerticesAndEdgeRange(new[] { edge1, edge2, edge3, edge4, edge5, edge6 });
            wrappedGraph.AddVertex(5);
            IBidirectionalIncidenceGraph <int, SReversedEdge <int, Edge <int> > > graph = createGraph();

            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));
            Assert.AreEqual(0, graph.Degree(5));
        }
Beispiel #10
0
        protected static void OutEdges_ImmutableGraph_ReversedTest(
            [NotNull] IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph,
            [NotNull, InstantHandle] Func <IImplicitGraph <int, SReversedEdge <int, Edge <int> > > > createGraph)
        {
            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 edge33 = new Edge <int>(3, 3);
            var edge34 = new Edge <int>(3, 4);

            wrappedGraph.AddVertex(1);
            IImplicitGraph <int, SReversedEdge <int, Edge <int> > > graph = createGraph();

            AssertNoOutEdge(graph, 1);

            wrappedGraph.AddVerticesAndEdgeRange(new[] { edge12, edge13, edge14, edge24, edge33, edge34 });
            graph = createGraph();

            AssertNoOutEdge(graph, 1);
            AssertHasReversedOutEdges(graph, 2, new[] { edge12 });
            AssertHasReversedOutEdges(graph, 3, new[] { edge13, edge33 });
            AssertHasReversedOutEdges(graph, 4, new[] { edge14, edge24, edge34 });
        }
        private static void CreateInternal <TVertex, TEdge>(
            IMutableVertexAndEdgeSet <TVertex, TEdge> graph,
            VertexFactory <TVertex> vertexFactory,
            EdgeFactory <TVertex, TEdge> edgeFactory,
            Random rng,
            int vertexCount,
            int edgeCount,
            bool selfEdges
            )
            where TEdge : IEdge <TVertex>
        {
            if (graph is null)
            {
                throw new ArgumentNullException(nameof(graph));
            }
            if (vertexFactory is null)
            {
                throw new ArgumentNullException(nameof(vertexFactory));
            }
            if (edgeFactory is null)
            {
                throw new ArgumentNullException(nameof(edgeFactory));
            }
            if (rng is null)
            {
                throw new ArgumentNullException(nameof(rng));
            }
            if (vertexCount <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(vertexCount), "Must have at least one vertex.");
            }
            if (edgeCount <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(edgeCount), "Must not be negative.");
            }

            var vertices = new TVertex[vertexCount];

            for (int i = 0; i < vertexCount; ++i)
            {
                TVertex vertex = vertexFactory();
                vertices[i] = vertex;
                graph.AddVertex(vertex);
            }

            int j = 0;

            while (j < edgeCount)
            {
                TVertex a = vertices[rng.Next(vertexCount)];
                TVertex b;
                do
                {
                    b = vertices[rng.Next(vertexCount)];
                }while (!selfEdges && EqualityComparer <TVertex> .Default.Equals(a, b));

                if (graph.AddEdge(edgeFactory(a, b)))
                {
                    ++j;
                }
            }
        }