Example #1
0
        public virtual void RemoveVertex(Vertex v)
        {
            int  index   = vertices.IndexOf(v);
            bool success = vertices.Remove(v);

            if (success)
            {
                vertexCount--;
                v.SetGraph(null);
                //Delete all edges going to and from this vertex
                foreach (Edge e in v.GetInEdges())
                {
                    RemoveEdge(e);
                }
                foreach (Edge e in v.GetOutEdges())
                {
                    RemoveEdge(e);
                }

                matrix.RemoveVertex(index);
            }
        }
Example #2
0
        public virtual void RemoveVertex(Vertex v)
        {
            int index = vertices.IndexOf(v);
            bool success = vertices.Remove(v);
            if (success)
            {
                vertexCount--;
                v.SetGraph(null);
                //Delete all edges going to and from this vertex
                foreach (Edge e in v.GetInEdges())
                {
                    RemoveEdge(e);
                }
                foreach (Edge e in v.GetOutEdges())
                {
                    RemoveEdge(e);
                }

                matrix.RemoveVertex(index);
            }
        }
Example #3
0
        public static List <Edge> BreadthFirstSearch(Graph g, int source, int goal)
        {
            //Does a breadth-first search and finds the shortest path between the source and the goal
            //Returns null if no path exists
            bool found    = false;
            bool directed = g.Directed;

            List <object> tags = new List <object>();
            List <Edge>   path = new List <Edge>();

            foreach (Vertex v in g.GetVertices())
            {
                tags.Add(v.Tag);
                v.Tag = null;
            }

            Queue <Vertex> q          = new Queue <Vertex>();
            Vertex         sourceVert = g.GetVertices()[source];
            Vertex         goalVert   = g.GetVertices()[goal];

            q.Enqueue(sourceVert);
            sourceVert.Tag = new BFSData(null);
            while (q.Count > 0)
            {
                Vertex next = q.Dequeue();

                if (next.Equals(goalVert))
                {
                    found = true;
                    break;
                }

                foreach (Edge e in next.GetOutEdges())
                {
                    Vertex toVert = e.GetToVertex();
                    if (toVert.Tag != null)
                    {
                        continue;
                    }
                    else
                    {
                        toVert.Tag = e;
                        q.Enqueue(toVert);
                    }
                }
                if (!directed)
                {
                    foreach (Edge e in next.GetInEdges())
                    {
                        Vertex toVert = e.GetFromVertex();
                        if (toVert.Tag != null)
                        {
                            continue;
                        }
                        else
                        {
                            toVert.Tag = e;
                            q.Enqueue(toVert);
                        }
                    }
                }
            }

            if (!found)
            {
                return(null);
            }

            Edge currentEdge = goalVert.Tag as Edge;

            path.Add(currentEdge);
            while (!currentEdge.GetFromVertex().Equals(sourceVert))
            {
                Vertex nextVert = currentEdge.GetFromVertex();
                currentEdge = nextVert.Tag as Edge;
                path.Add(currentEdge);
            }

            path.Reverse();

            for (int i = 0; i < tags.Count; i++)
            {
                g.GetVertices()[i].Tag = tags[i];
            }

            return(path);
        }