Esempio n. 1
0
        public virtual void AddEdge(Edge e)
        {
            //Check to see if the vertices belong to a different graph
            //Not necessarily null
            if (e.GetFromVertex().GetGraph() != this)
            {
                AddVertex(e.GetFromVertex());
            }

            if (e.GetToVertex().GetGraph() != this)
            {
                AddVertex(e.GetToVertex());
            }

            int fromIndex = vertices.IndexOf(e.GetFromVertex());
            int toIndex = vertices.IndexOf(e.GetToVertex());

            float val = matrix.GetValueAt(fromIndex, toIndex);

            Edge oppositeEdge = new Edge(e.GetToVertex(), e.GetFromVertex(), e.Weight);
            if (!float.IsInfinity(val))
            {
                //update existing edge
                matrix.SetValueAt(fromIndex, toIndex, e.Weight);
                if (!Directed)
                {
                    matrix.SetValueAt(toIndex, fromIndex, e.Weight);
                }
                Edge existing = FindEdge(e);
                existing.Weight = e.Weight;
                Edge otherEdge = FindEdge(new Edge(e.GetToVertex(), e.GetFromVertex()));
                if (otherEdge == null)
                {
                    return;
                }
                otherEdge.Weight = e.Weight;
                return;
            }

            vertices[fromIndex].AddOutEdge(e);
            vertices[toIndex].AddInEdge(e);

            if (!Directed)
            {
                vertices[toIndex].AddOutEdge(oppositeEdge);
                vertices[fromIndex].AddInEdge(oppositeEdge);
            }

            edges.Add(e);

            if (!Directed)
            {
                edges.Add(oppositeEdge);
                edgeCount++;
            }

            edgeCount++;

            float edgeWeight = e.Weight;

            matrix.SetValueAt(fromIndex, toIndex, edgeWeight);
            if (!Directed)
            {
                matrix.SetValueAt(toIndex, fromIndex, edgeWeight);
            }

            e.Directed = directed;
            oppositeEdge.Directed = directed;
            e.ShowLabel = showEdgeLabels;
            oppositeEdge.ShowLabel = showEdgeLabels;
        }
Esempio n. 2
0
        public virtual void RemoveEdge(Edge e)
        {
            bool success = edges.Remove(e);
            if (success)
            {
                edgeCount--;

                int fromIndex = vertices.IndexOf(e.GetFromVertex());
                int toIndex = vertices.IndexOf(e.GetToVertex());
                //If fromIndex or toIndex is -1, the vertex has already been removed, so don't worry about updating
                //their information
                if (fromIndex >= 0)
                {
                    vertices[fromIndex].RemoveOutEdge(e);
                }
                if (toIndex >= 0)
                {
                    vertices[toIndex].RemoveInEdge(e);
                }
                if (fromIndex < 0 || toIndex < 0)
                {
                    return;
                }

                matrix.SetValueAt(fromIndex, toIndex, float.PositiveInfinity);
            }
        }