Пример #1
0
        public bool IsStronglyConnected(List <int>[] graph)
        {
            int v = graph.Length;

            bool[] vs = new bool[v];

            DFSUtil(graph, 0, vs);

            if (vs.Any(s => !s))
            {
                return(false);
            }

            var tGraph = GraphListHelper.GetTransposeGraph(graph);

            for (int i = 0; i < v; i++)
            {
                vs[i] = false;
            }

            DFSUtil(tGraph, 0, vs);

            if (vs.Any(s => !s))
            {
                return(false);
            }

            return(true);
        }
        /// <summary>
        /// Get strongly Connected Component, return array vetex number and componenet id
        /// </summary>
        /// <param name="graph"></param>
        /// <returns></returns>
        public static int[] GetSCC(List <int>[] graph)
        {
            var stack = new Stack <int>();
            int v     = graph.Length;

            bool[] visited = new bool[v];

            for (int i = 0; i < v; i++)
            {
                if (!visited[i])
                {
                    DFS(i, visited, stack, graph);
                }
            }

            var rGraph = GraphListHelper.GetTransposeGraph(graph);

            int[] scc = new int[v];
            for (int i = 0; i < v; i++)
            {
                scc[i] = -1;
            }

            int numCompoents = 0;

            while (stack.Count > 0)
            {
                int i = stack.Pop();
                if (scc[i] == -1)
                {
                    DFSComponents(i, scc, rGraph, numCompoents);
                }
                numCompoents++;
            }

            return(scc);
        }