Example #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");
            }
        }
Example #2
0
        private AdjMapGraphEdge validate(IEdge <Edge> edge)
        {
            if (!(edge is AdjMapGraphEdge))
            {
                throw new ArgumentException("Invalid Edge");
            }

            AdjMapGraphEdge e = (AdjMapGraphEdge)edge;

            if (edges.Contains(e))
            {
                return(e);
            }
            throw new ArgumentException("Edge does not belong to this graph");
        }
Example #3
0
        public IVertex <Vertex> getOpposite(IVertex <Vertex> source, IEdge <Edge> edge)
        {
            AdjMapGraphEdge          e    = validate(edge);
            List <IVertex <Vertex> > endp = e.getEndpoints();

            if (source == endp[0])
            {
                return(endp[1]);
            }
            else if (source == endp[1])
            {
                return(endp[0]);
            }
            else
            {
                throw new ArgumentException("The Vertex does not belong to this edge");
            }
        }
Example #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);
                }
            }
        }
Example #5
0
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("VERTICES:" + Environment.NewLine);
            foreach (IVertex <Vertex> v in vertices)
            {
                sb.Append("Vertex " + v.getElement() + Environment.NewLine);
            }

            sb.Append("EDGES:" + Environment.NewLine);
            foreach (IEdge <Edge> e in edges)
            {
                if (e is AdjMapGraphEdge)
                {
                    AdjMapGraphEdge edge = validate(e);
                    sb.Append(edge.getEndpoints()[0].ToString() + "\t\t ----[" + e.getElement() + "]---- \t\t" + edge.getEndpoints()[1].ToString());
                }
            }


            return(sb.ToString());
        }
Example #6
0
        public List <IVertex <Vertex> > getEndpoints(IEdge <Edge> edge)
        {
            AdjMapGraphEdge e = validate(edge);

            return(e.getEndpoints());
        }