예제 #1
0
        /// <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);
            }
        }
        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
        }
예제 #4
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
        }
        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;
                }
            }
        }