示例#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 IVertex <Vertex> addVertex(Vertex element)
        {
            IVertex <Vertex> v = new AdjMapGraphVertex(element);

            vertices.Add(v);
            return(v);
        }
示例#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
        private AdjMapGraphVertex validate(IVertex <Vertex> vertex)
        {
            if (!(vertex is AdjMapGraphVertex))
            {
                throw new ArgumentException("Invalid Vertex");
            }

            AdjMapGraphVertex vert = (AdjMapGraphVertex)vertex;

            if (vertices.Contains(vert))
            {
                return(vert);
            }
            throw new ArgumentException("Vertex does not belong to this graph");
        }
示例#5
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);
        }
示例#6
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);
                }
            }
        }