public static IDirectedAcyclicGraph <T> ReduceGraph(IDirectedAcyclicGraph <T> graph)
        {
            var elementsEnumerable = graph.Elements;
            var elements           = elementsEnumerable as IList <T> ?? elementsEnumerable.ToList();
            var graphBuilder       = DirectedGraphBuilder <T> .NewBuilder();

            foreach (var element in elements)
            {
                graphBuilder.AddElement(element);
            }

            foreach (var element in elements)
            {
                foreach (var child in getOnlyChildrenWithoutIndirectPath(graph, element))
                {
                    graphBuilder.AddArrow(element, child);
                }
            }

            return(graphBuilder.CreateAcyclicGraphUnsafe());
        }
예제 #2
0
        public static DirectedGraphBuilder <T> FromExistingGraph(IDirectedGraph <T> graph)
        {
            var builder = new DirectedGraphBuilder <T>();

            foreach (var element in graph.Elements)
            {
                builder.elements.Add(element);

                var successors   = new HashSet <T>(graph.GetDirectSuccessorsOf(element));
                var predecessors = new HashSet <T>(graph.GetDirectPredecessorsOf(element));

                builder.directSuccessors.Add(element, successors);
                builder.directPredecessors.Add(element, predecessors);

                if (predecessors.Count == 0)
                {
                    builder.sources.Add(element);
                }
            }

            return(builder);
        }