Exemplo n.º 1
0
        public static IEnumerable <T> TopologicalSortReverse <T>(this ICollection <T> list, NodeLinksResolver <T> linkResolver)
        {
            TopologicalSortState <T> state = new TopologicalSortState <T>
            {
                Unmarked = new HashSet <T>(list),
                GetLinks = linkResolver,
            };

            while (state.Unmarked.Count > 0)
            {
                Visit(state.Unmarked.First(), state);
            }

            return(state.SortedNodes);
        }
Exemplo n.º 2
0
        internal static void Visit <T>(T node, TopologicalSortState <T> state)
        {
            if (state.TemporaryMarked.Contains(node))
            {
                throw new InvalidOperationException("Not a DAG");
            }

            if (!state.Unmarked.Contains(node))
            {
                return;
            }

            state.TemporaryMarked.Add(node);
            foreach (T link in state.GetLinks(node))
            {
                Visit(link, state);
            }

            state.SortedNodes.Add(node);
            state.TemporaryMarked.Remove(node);
            state.Unmarked.Remove(node);
        }