Example #1
0
        /// <summary>Finds strongly connected components in given directed graph.</summary>
        /// <param name="graph">The directed graph.</param>
        /// <returns>List of vertices in strongly connected components.</returns>
        public static List <HashSet <Vertex <TVertexId> > > FindScc <TVertexId, TVertexProperty, TEdgeProperty>(
            this IDirectedGraph <TVertexId, TVertexProperty, TEdgeProperty> graph)
        {
            var postOrderStrategy = new PostOrderStrategy <TVertexId>();

            Searching.DfsRecursive(graph, postOrderStrategy, graph.Vertices);

            var vertices = postOrderStrategy.postTimes
                           .OrderByDescending(kv => kv.Value)
                           .Select(kv => kv.Key)
                           .ToList();
            IDirectedGraph <TVertexId, TVertexProperty, TEdgeProperty> reversedGraph = graph.ReversedCopy();
            var sccStrategy = new SCCStrategy <TVertexId>();

            Searching.DfsRecursive(reversedGraph, sccStrategy, vertices);
            return(sccStrategy.components);
        }