public GraphStorage <TVertex, TEdge> Clone() { var cloneGraph = new GraphStorage <TVertex, TEdge>(); this.Run(x => cloneGraph.Add(x)); return(cloneGraph); }
/// <summary> /// Get topological order from graph tree. This is done by processing /// tree directed edges to understand dependencies. /// </summary> /// <param name="numberOfLevels">only process n levels is specified</param> /// <param name="processedNodes">Nodes that have already been processed</param> /// <returns>list of list of vertices for each level</returns> public IReadOnlyList <IReadOnlyList <int> > GetTopologicalOrdering(int?numberOfLevels = null, IEnumerable <int> processedNodes = null) { var list = new List <IReadOnlyList <int> >(); GraphStorage <TVertex, TEdge> processGraph = base.Clone(); (processedNodes ?? Enumerable.Empty <int>()).Run(x => processGraph.Remove(x)); var nodesProcessed = new HashSet <int>(processedNodes ?? Enumerable.Empty <int>()); while (numberOfLevels != list.Count) { var freeNode = processGraph.Vertices.Values .Where(x => !processGraph.Edges.Values.Any(y => y.EdgeId.ToNodeId == x.NodeId)) .Select(x => x.NodeId) .ToList(); if (freeNode.Count == 0) { return(list); } list.Add(new List <int>(freeNode)); freeNode.Run(x => processGraph.Remove(x)); } return(list); }