|
| 1 | +#include <bits/stdc++.h> |
| 2 | +using namespace std; |
| 3 | + |
| 4 | +// Function to perform Topological Sort using Kahn's Algorithm |
| 5 | +void KahnsAlgorithm(vector<vector<int>>& adj, int V) { |
| 6 | + // Step 1: Initialize the indegree array with zeros |
| 7 | + // indegree[i] will store the number of incoming edges to vertex i |
| 8 | + vector<int> indegree(V, 0); |
| 9 | + |
| 10 | + // Step 2: Create a queue for processing nodes with indegree 0 |
| 11 | + queue<int> q; |
| 12 | + |
| 13 | + // Step 3: Create a vector to store the topological order of nodes |
| 14 | + vector<int> topoOrder; |
| 15 | + |
| 16 | + // Step 4: Calculate the indegree for all vertices |
| 17 | + for (int i = 0; i < V; i++) { |
| 18 | + for (int neighbour : adj[i]) { |
| 19 | + indegree[neighbour]++; // Increment indegree of each neighbor |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + // Step 5: Add all vertices with indegree 0 to the queue |
| 24 | + for (int i = 0; i < V; i++) { |
| 25 | + if (indegree[i] == 0) { |
| 26 | + q.push(i); // Push vertices with no dependencies into the queue |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + // Step 6: Process the vertices in the queue one by one |
| 31 | + while (!q.empty()) { |
| 32 | + int node = q.front(); // Get the vertex at the front of the queue |
| 33 | + q.pop(); // Remove it from the queue |
| 34 | + |
| 35 | + topoOrder.push_back(node); // Add the vertex to the topological order |
| 36 | + |
| 37 | + // Step 7: For each neighbor of the current vertex |
| 38 | + for (auto neighbour : adj[node]) { |
| 39 | + indegree[neighbour]--; // Decrease the indegree of the neighbor |
| 40 | + |
| 41 | + // If the indegree of the neighbor becomes 0, add it to the queue |
| 42 | + if (indegree[neighbour] == 0) { |
| 43 | + q.push(neighbour); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // Step 8: If the topological order contains all vertices, print it |
| 49 | + if (topoOrder.size() != V) { |
| 50 | + // If the size of the topological order is not equal to the number of vertices |
| 51 | + // it means there was a cycle in the graph, and topological sort is not possible |
| 52 | + cout << "The graph is not a DAG. Topological sort not possible.\n"; |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + // Step 9: Output the topological sort |
| 57 | + cout << "Topological sorting of the graph: "; |
| 58 | + for (int node : topoOrder) { |
| 59 | + cout << node + 1 << " "; // Print node numbers (1-based indexing) |
| 60 | + } |
| 61 | + cout << endl; |
| 62 | +} |
| 63 | + |
| 64 | +int main() { |
| 65 | + // Step 10: Number of vertices (nodes) in the graph |
| 66 | + int V = 5; |
| 67 | + |
| 68 | + // Step 11: Define the directed edges of the graph |
| 69 | + // Format: {from, to} |
| 70 | + vector<vector<int>> edges = {{1, 2}, {1, 3}, {2, 5}, {3, 5}, {5, 4}}; |
| 71 | + |
| 72 | + // Step 12: Initialize the adjacency list for the graph |
| 73 | + vector<vector<int>> adj(V); |
| 74 | + |
| 75 | + // Step 13: Convert the edge list to an adjacency list |
| 76 | + // Adjusting for 0-based indexing |
| 77 | + for (auto i : edges) { |
| 78 | + adj[i[0] - 1].push_back(i[1] - 1); |
| 79 | + } |
| 80 | + |
| 81 | + // Step 14: Call the Kahn's Algorithm function to perform topological sort |
| 82 | + KahnsAlgorithm(adj, V); |
| 83 | + |
| 84 | + return 0; |
| 85 | +} |
0 commit comments