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); }