Exemple #1
0
        public E AddEdge(V sourceVertex, V targetVertex)
        {
            AssertVertexExist(sourceVertex);
            AssertVertexExist(targetVertex);

            // TODO::review, since ContainsEdge calles GetEdge which calls ContainsVertex,
            //      and we have called AssertVertexExist already
            if (ContainsEdge(sourceVertex, targetVertex))
            {
                return(default(E));
            }

            if (sourceVertex.Equals(targetVertex))
            {
                throw new ArgumentException(ErrorMessages.LoopsNotAllowed);
            }

            var edge = _edgeFactory.CreateEdge(sourceVertex, targetVertex);

            _edges.Add(edge);

            if (_vertexEdgeMap[sourceVertex] == null)
            {
                _vertexEdgeMap[sourceVertex] = new HashSet <E>();
            }
            _vertexEdgeMap[sourceVertex].Add(edge);

            if (_vertexEdgeMap[targetVertex] == null)
            {
                _vertexEdgeMap[targetVertex] = new HashSet <E>();
            }
            _vertexEdgeMap[targetVertex].Add(edge);

            return(edge);
        }
        public void MergeVertex(TVertex v, IEdgeFactory <TVertex, TEdge> edgeFactory)
        {
            GraphContracts.AssumeInVertexSet(this, v, "v");
            GraphContracts.AssumeNotNull(edgeFactory, "edgeFactory");

            // storing edges in local array
            EdgeList inedges  = this.vertexInEdges[v];
            EdgeList outedges = this.vertexOutEdges[v];

            // remove vertex
            this.RemoveVertex(v);

            // add edges from each source to each target
            foreach (TEdge source in inedges)
            {
                //is it a self edge
                if (source.Source.Equals(v))
                {
                    continue;
                }
                foreach (TEdge target in outedges)
                {
                    if (v.Equals(target.Target))
                    {
                        continue;
                    }
                    // we add an new edge
                    this.AddEdge(edgeFactory.CreateEdge(source.Source, target.Target));
                }
            }
        }
Exemple #3
0
        public static void Create <TVertex, TEdge>(
            IMutableVertexAndEdgeListGraph <TVertex, TEdge> g,
            IVertexFactory <TVertex> vertexFactory,
            IEdgeFactory <TVertex, TEdge> edgeFactory,
            Random rnd,
            int vertexCount,
            int edgeCount,
            bool selfEdges
            ) where TEdge : IEdge <TVertex>
        {
            if (g == null)
            {
                throw new ArgumentNullException("g");
            }
            if (vertexFactory == null)
            {
                throw new ArgumentNullException("vertexFactory");
            }
            if (edgeFactory == null)
            {
                throw new ArgumentNullException("edgeFactory");
            }
            if (rnd == null)
            {
                throw new ArgumentNullException("random generator");
            }


            for (int i = 0; i < vertexCount; ++i)
            {
                g.AddVertex(vertexFactory.CreateVertex());
            }


            TVertex a;
            TVertex b;
            int     j = 0;

            while (j < edgeCount)
            {
                a = GetVertex(g, rnd);
                do
                {
                    b = GetVertex(g, rnd);
                }while (selfEdges == false && a.Equals(b));

                if (g.AddEdge(edgeFactory.CreateEdge(a, b)))
                {
                    ++j;
                }
            }
        }
        public void CreateEdgeFactory()
        {
            IEdgeFactory <Foo, Edge <Foo> > factory = FactoryCompiler.GetEdgeFactory <Foo, Edge <Foo> >();

            Assert.IsNotNull(factory);
            Foo        source = new Foo();
            Foo        target = new Foo();
            Edge <Foo> edge   = factory.CreateEdge(source, target);

            Assert.IsNotNull(edge);
            Assert.IsNotNull(edge.Source);
            Assert.IsNotNull(edge.Target);
        }
        /// <summary>
        /// Adds temporary edges to the graph to make all vertex even.
        /// </summary>
        /// <param name="g"></param>
        /// <returns></returns>
        public List <TEdge> AddTemporaryEdges(IEdgeFactory <TVertex, TEdge> edgeFactory)
        {
            // first gather odd edges.
            List <TVertex> oddVertices = AlgoUtility.OddVertices(this.VisitedGraph);

            // check that there are an even number of them
            if (oddVertices.Count % 2 != 0)
            {
                throw new Exception("number of odd vertices in not even!");
            }

            // add temporary edges to create even edges:
            this.temporaryEdges = new List <TEdge>();

            bool found, foundbe, foundadjacent;

            while (oddVertices.Count > 0)
            {
                TVertex u = oddVertices[0];
                // find adjacent odd vertex.
                found         = false;
                foundadjacent = false;
                foreach (var e in this.VisitedGraph.OutEdges(u))
                {
                    TVertex v = e.Target;
                    if (!v.Equals(u) && oddVertices.Contains(v))
                    {
                        foundadjacent = true;
                        // check that v does not have an out-edge towards u
                        foundbe = false;
                        foreach (var be in this.VisitedGraph.OutEdges(v))
                        {
                            if (be.Target.Equals(u))
                            {
                                foundbe = true;
                                break;
                            }
                        }
                        if (foundbe)
                        {
                            continue;
                        }
                        // add temporary edge
                        TEdge tempEdge = edgeFactory.CreateEdge(v, u);
                        if (!this.VisitedGraph.AddEdge(tempEdge))
                        {
                            throw new InvalidOperationException();
                        }
                        // add to collection
                        temporaryEdges.Add(tempEdge);
                        // remove u,v from oddVertices
                        oddVertices.Remove(u);
                        oddVertices.Remove(v);
                        // set u to null
                        found = true;
                        break;
                    }
                }

                if (!foundadjacent)
                {
                    // pick another vertex
                    if (oddVertices.Count < 2)
                    {
                        throw new Exception("Eulerian trail failure");
                    }
                    TVertex v        = oddVertices[1];
                    TEdge   tempEdge = edgeFactory.CreateEdge(u, v);
                    if (!this.VisitedGraph.AddEdge(tempEdge))
                    {
                        throw new InvalidOperationException();
                    }
                    // add to collection
                    temporaryEdges.Add(tempEdge);
                    // remove u,v from oddVertices
                    oddVertices.Remove(u);
                    oddVertices.Remove(v);
                    // set u to null
                    found = true;
                }

                if (!found)
                {
                    oddVertices.Remove(u);
                    oddVertices.Add(u);
                }
            }
            return(this.temporaryEdges);
        }