예제 #1
0
 public static bool DFS(this Graph g, VisitVertexFunction preVisit, VisitVertexFunction postVisit, VisitEdgeFunction visitEdge,
                        int startingIndex, out bool[] visited)
 {
     if (startingIndex < 0 || startingIndex >= g.VerticesCount)
     {
         throw new IndexOutOfRangeException("startingIndex must be between (inclusive) 0 and VerticesCount-1");
     }
     bool[] visitedTab = new bool[g.VerticesCount];
     visited = visitedTab;
     return(RecursiveDFS(g, preVisit, postVisit, visitEdge, startingIndex, visitedTab));
 }
예제 #2
0
        public static bool BFS(this Graph g, VisitVertexFunction preVisit, VisitVertexFunction postVisit, VisitEdgeFunction visitEdge,
                               int startingIndex, out bool[] visited)
        {
            if (startingIndex < 0 || startingIndex >= g.VerticesCount)
            {
                throw new IndexOutOfRangeException("startingIndex must be between (inclusive) 0 and VerticesCount-1");
            }
            bool[] visitedTab = new bool[g.VerticesCount];
            visited = visitedTab;
            List <Edge> edges = new List <Edge>();
            Edge        e     = null;

            do
            {
                e = null;
                if (!visitedTab[startingIndex])
                {
                    visitedTab[startingIndex] = true;
                    if (null != preVisit)
                    {
                        if (!preVisit(startingIndex))
                        {
                            return(false);
                        }
                    }
                    edges.AddRange(g.GetEdgesFrom(startingIndex));
                    if (null != postVisit)
                    {
                        if (!postVisit(startingIndex))
                        {
                            return(false);
                        }
                    }
                }
                if (edges.Count > 0)
                {
                    e = edges[0];
                    edges.RemoveAt(0);
                    if (null != visitEdge)
                    {
                        if (!visitEdge(e))
                        {
                            return(false);
                        }
                    }
                    startingIndex = e.To;
                }
            }while (null != e);

            return(true);
        }
예제 #3
0
 private static bool RecursiveDFS(this Graph g, VisitVertexFunction preVisit, VisitVertexFunction postVisit, VisitEdgeFunction visitEdge,
                                  int startingIndex, bool[] visited)
 {
     if (visited[startingIndex])
     {
         return(true);
     }
     visited[startingIndex] = true;
     if (null != preVisit)
     {
         bool preVisitResult = preVisit(startingIndex);
         if (false == preVisitResult)
         {
             return(false);
         }
     }
     foreach (Edge e in g.GetEdgesFrom(startingIndex))
     {
         if (null != visitEdge)
         {
             bool visitEdgeResult = visitEdge(e);
             if (false == visitEdgeResult)
             {
                 continue;
             }
         }
         if (false == g.RecursiveDFS(preVisit, postVisit, visitEdge, e.To, visited))
         {
             return(false);
         }
     }
     if (null != postVisit)
     {
         bool postVisitResult = postVisit(startingIndex);
         if (false == postVisitResult)
         {
             return(false);
         }
     }
     return(true);
 }
예제 #4
0
        public static bool GeneralSearch <T>(this Graph g, VisitVertexFunction preVisit, VisitVertexFunction postVisit, VisitEdgeFunction visitEdge,
                                             int startingIndex, out bool[] visited)
            where T : IEdgeCollection, new()
        {
            if (startingIndex < 0 || startingIndex >= g.VerticesCount)
            {
                throw new IndexOutOfRangeException("startingIndex must be between (inclusive) 0 and VerticesCount-1");
            }

            bool[] visitedTab = new bool[g.VerticesCount];
            int[]  post       = new int[g.VerticesCount];

            visited = visitedTab;

            T collection = new T();

            foreach (var e in g.GetEdgesFrom(startingIndex))
            {
                collection.Push(e);
            }

            post[startingIndex] = g.GetOutDegree(startingIndex);

            visitedTab[startingIndex] = true;

            if (null != preVisit)
            {
                if (!preVisit(startingIndex))
                {
                    return(false);
                }
            }
            post[startingIndex] = g.GetOutDegree(startingIndex);

            while (!collection.IsEmpty())
            {
                Edge e = collection.Pop();

                post[e.From]--;

                if (null != visitEdge)
                {
                    if (!visitEdge(e))
                    {
                        return(false);
                    }
                }



                if (!visitedTab[e.To])
                {
                    visitedTab[e.To] = true;
                    if (null != preVisit)
                    {
                        if (!preVisit(startingIndex))
                        {
                            return(false);
                        }
                    }

                    foreach (var f in g.GetEdgesFrom(e.To))
                    {
                        collection.Push(f);
                    }
                    post[e.To] = g.GetOutDegree(e.To);
                }

                if (0 == post[e.From])
                {
                    if (null != postVisit)
                    {
                        if (!postVisit(e.From))
                        {
                            return(false);
                        }
                    }
                }
            }
            return(true);
        }