public WeightedDiAdjacencyMatrix Transpose()
        {
            WeightedDiAdjacencyMatrix graph = new WeightedDiAdjacencyMatrix(matrix.Count);

            foreach (var item in Edges)
            {
                graph.AddEdge(item.VertexB + 1, item.VertexA + 1, item.Weight);
            }
            return(graph);
        }
示例#2
0
        private void DFSUtil(int vert, bool[] visited, WeightedDiAdjacencyMatrix matrix)
        {
            visited[vert] = true;
            Console.Write((vert + 1) + " ");

            var neighbours = matrix.Neighbours(vert + 1);

            foreach (var item in neighbours)
            {
                if (!visited[item - 1])
                {
                    DFSUtil(item - 1, visited, matrix);
                }
            }
        }
示例#3
0
        private void FillOrder(int v, bool[] visited, Stack <int> stack, WeightedDiAdjacencyMatrix matrix)
        {
            // Mark the current node as visited and print it
            visited[v] = true;

            // Recur for all the vertices adjacent to this vertex
            var neighbours = matrix.Neighbours(v + 1);

            foreach (var item in neighbours)
            {
                if (!visited[item - 1])
                {
                    FillOrder(item - 1, visited, stack, matrix);
                }
            }

            // All vertices reachable from v are processed by now,
            // push v to Stack
            stack.Push(v);
        }
示例#4
0
        public static void Run()
        {
            Console.WriteLine("---------------------------------------");

            var graph = new WeightedDiAdjacencyMatrix(5);

            graph.AddEdge(2, 1, 1f);
            graph.AddEdge(1, 4, 1f);
            graph.AddEdge(4, 5, 1f);
            graph.AddEdge(1, 3, 1f);
            graph.AddEdge(3, 2, 1f);
            //graph.PrintAdjacency();


            var kosaraju = new Kosaraju();

            kosaraju.PrintSCCs(graph);


            Console.ReadKey();
        }
示例#5
0
        public void PrintSCCs(WeightedDiAdjacencyMatrix matrix)
        {
            Stack <int> stack = new Stack <int>();

            bool[] visited = new bool[matrix.Order];
            for (int i = 0; i < visited.Length; i++)
            {
                visited[i] = false;
            }

            for (int i = 0; i < matrix.Order; i++)
            {
                if (visited[i] == false)
                {
                    FillOrder(i, visited, stack, matrix);
                }
            }

            var transposed = matrix.Transpose();

            for (int i = 0; i < visited.Length; i++)
            {
                visited[i] = false;
            }


            while (stack.Count > 0)
            {
                int vert = stack.Pop();

                // Print Strongly connected component of the popped vertex
                if (visited[vert] == false)
                {
                    Console.Write("SCC: ");
                    DFSUtil(vert, visited, transposed);
                    Console.WriteLine();
                }
            }
        }