public KosarajuAlgorithm(DirectedGraph Graph) { _strongConnectedComponentEdges = new List<List<DirectedEdge>>(); _strongConnectedComponentVertices = new List<List<int>>(); BuildStrongConnectedComponents(Graph); }
public TopologicalSorting(DirectedGraph Graph) { _topologicalSort = new List<int>(); bool[] mark = new bool[Graph.VerticesCount]; for (int i = 0; i < Graph.VerticesCount; ++i) if (!mark[i]) DFS(i, mark, Graph); }
public DirectedGraph(DirectedGraph g) : this(g.VerticesCount) { for (int i = 0; i < g.VerticesCount; ++i) for (int j = 0; j < g[i].Count; ++j) { Graph[i].Add(g[i, j]); ++EdgesCount; } }
void DFS(int vertex, bool[] mark, DirectedGraph Graph) { mark[vertex] = true; for (int i = 0; i < Graph.GetVertexDegree(vertex); ++i) if (!mark[Graph.GetEdge(vertex, i).End]) DFS(Graph.GetEdge(vertex, i).End, mark, Graph); _topologicalSort.Add(vertex); }
void AddStrongConnectedComponent(int vertex, int[] mark, DirectedGraph Graph) { mark[vertex] = StrongConnectedComponentCount; _strongConnectedComponentVertices[StrongConnectedComponentCount].Add(vertex); for (int i = 0; i < Graph.GetVertexDegree(vertex); ++i) if (mark[Graph.GetEdge(vertex, i).End] == StrongConnectedComponentCount || mark[Graph.GetEdge(vertex, i).End] == -1) { _strongConnectedComponentEdges[StrongConnectedComponentCount].Add(Graph.GetEdge(vertex, i).Reverse()); if (mark[Graph.GetEdge(vertex, i).End] == -1) AddStrongConnectedComponent(Graph.GetEdge(vertex, i).End, mark, Graph); } }
public static DirectedGraph ReadFromTxtFile(string fileName) { StreamReader inputFile = new StreamReader(fileName); string fileContent = inputFile.ReadToEnd(); string[] inputData = fileContent.Split(new string[] { " ", "\r\n" }, StringSplitOptions.RemoveEmptyEntries); var result = new DirectedGraph(int.Parse(inputData[0])); int edgesCount = int.Parse(inputData[1]); for (int i = 0; i < edgesCount; ++i) { DirectedEdge edge = new DirectedEdge(int.Parse(inputData[i * 2 + 2]) - 1, int.Parse(inputData[i * 2 + 3]) - 1); result[edge.Begin].Add(edge); } return result; }
public static DirectedGraph ReadFromConsole() { AllocConsole(); Console.Write("Enter a number of vertices, please: "); int verticesCount = int.Parse(Console.ReadLine()); DirectedGraph result = new DirectedGraph(verticesCount); Console.Write("Enter a number of edges, please: "); int edgesCount = int.Parse(Console.ReadLine()); for (int i = 0; i < edgesCount; ++i) { Console.Write("Enter begin and end of {0} edge, please: ", i + 1); string[] inputData = Console.ReadLine().Split(new string[]{" "}, StringSplitOptions.RemoveEmptyEntries); DirectedEdge edge = new DirectedEdge(int.Parse(inputData[0]) - 1, int.Parse(inputData[1]) - 1); result[edge.Begin].Add(edge); } FreeConsole(); return result; }
private void BuildStrongConnectedComponents(DirectedGraph Graph) { TopologicalSorting topologicalSortedVertices = new TopologicalSorting(Graph); DirectedGraph transposedGraph = Graph.Transposition(); int[] mark = new int[Graph.VerticesCount]; for (int i = 0; i < Graph.VerticesCount; ++i) mark[i] = -1; for (int i = transposedGraph.VerticesCount - 1; i >= 0; --i) if (mark[topologicalSortedVertices[i]] == -1) { List<DirectedEdge> edges = new List<DirectedEdge>(); _strongConnectedComponentEdges.Add(edges); List<int> vertices = new List<int>(); _strongConnectedComponentVertices.Add(vertices); AddStrongConnectedComponent(topologicalSortedVertices[i], mark, transposedGraph); _strongConnectedComponentVertices[StrongConnectedComponentCount].Sort(); ++StrongConnectedComponentCount; } }
public DirectedGraph Transposition() { DirectedGraph result = new DirectedGraph(VerticesCount); for (int i = 0; i < VerticesCount; ++i) for (int j = 0; j < GetVertexDegree(i); ++j) result.AddEdge(GetEdge(i, j).Reverse()); return result; }
private void ReadGraphFromATextFile_Click(object sender, EventArgs e) { openInputTxt.ShowDialog(); string fileName = openInputTxt.FileName; Graph = DirectedGraphReader.ReadFromTxtFile(fileName); }
private void ReadGraphFromAConsole_Click(object sender, EventArgs e) { Graph = DirectedGraphReader.ReadFromConsole(); }