Beispiel #1
0
        public static System.Collections.Generic.IEnumerable <T> Sort <T, E>
            (IGraph <T, E> graph, IEnumerable <T> source)
            where E : IEdge <T>
        {
            Dictionary <T, bool> Visited = new Dictionary <T, bool>();
            StackQueue <T>       Stack   = new StackQueue <T>();

            foreach (T v in graph.Vertices)
            {
                Visited[v] = false;
            }

            foreach (T v in source)
            {
                Stack.Push(v);
            }

            while (Stack.Count != 0)
            {
                T u = Stack.Pop();
                Visited[u] = true;
                yield return(u);

                foreach (T v in graph.ReversePredecessors(u))
                {
                    if (!Visited[v] && !Stack.Contains(v))
                    {
                        Stack.Push(v);
                    }
                }
            }
        }