コード例 #1
0
        public IEdge <Edge> addEdge(IVertex <Vertex> source, IVertex <Vertex> destination, Edge element, double weight, bool addInverse = false)
        {
            if (getEdge(source, destination) == null)
            {
                if (addInverse && getEdge(destination, source) != null)
                {
                    throw new ArgumentException("Destination and source are already connected");
                }
                else
                {
                    AdjMapGraphEdge edge = new AdjMapGraphEdge(source, destination, element, weight);
                    edges.Add(edge);
                    AdjMapGraphVertex s = validate(source);
                    AdjMapGraphVertex d = validate(destination);
                    s.getOutgoing().Add(destination, edge);

                    if (addInverse)
                    {
                        d.getOutgoing().Add(source, edge);
                    }

                    return(edge);
                }
            }
            else
            {
                throw new ArgumentException("Source and destination are already connected");
            }
        }
コード例 #2
0
        public IEdge <Edge> getEdge(IVertex <Vertex> source, IVertex <Vertex> destination, bool undirectedSearch = false)
        {
            AdjMapGraphVertex src = validate(source);

            if (src.getOutgoing().ContainsKey(destination))
            {
                return(src.getOutgoing()[destination]);
            }

            if (undirectedSearch)
            {
                AdjMapGraphVertex dst = validate(destination);
                if (dst.getOutgoing().ContainsKey(source))
                {
                    return(dst.getOutgoing()[source]);
                }
            }

            return(null);
        }
コード例 #3
0
        public void removeVertex(IVertex <Vertex> vertex)
        {
            AdjMapGraphVertex v = validate(vertex);

            foreach (IEdge <Edge> e in v.getOutgoing().Values)
            {
                removeEdge(e);
            }
            foreach (IEdge <Edge> e in v.getIncoming().Values)
            {
                removeEdge(e);
            }
            vertices.Remove(v);
        }
コード例 #4
0
        public void removeEdge(IEdge <Edge> edge, bool directed = true)
        {
            AdjMapGraphEdge   e  = validate(edge);
            AdjMapGraphVertex v0 = validate(e.getEndpoints()[0]);
            AdjMapGraphVertex v1 = validate(e.getEndpoints()[1]);

            v0.getOutgoing().Remove(v1);
            v1.getIncoming().Remove(v0);

            if (!directed)
            {
                if (getEdge(v1, v0) == null)
                {
                    throw new ArgumentException("Inverse edge does not exist");
                }
                else
                {
                    v1.getOutgoing().Remove(v0);
                    v0.getIncoming().Remove(v1);
                }
            }
        }