diff --git a/CP/TopologicalSort/UsingDFS.cpp b/CP/TopologicalSort/UsingDFS.cpp new file mode 100644 index 0000000..afcfa75 --- /dev/null +++ b/CP/TopologicalSort/UsingDFS.cpp @@ -0,0 +1,72 @@ +#include +#include +#include + +using namespace std; + +// Function to perform DFS and store the topological sort in a stack +void dfs(int node, vector>& adj, vector& visited, stack& topoStack) { + visited[node] = true; + + // Visit all the neighbors of the current node + for (int neighbor : adj[node]) { + if (!visited[neighbor]) { + dfs(neighbor, adj, visited, topoStack); + } + } + + // After visiting all neighbors, push the current node to the stack + topoStack.push(node); +} + +// Function to perform topological sort using DFS +void topologicalSortDFS(int V, vector>& adj) { + vector visited(V, false); + stack topoStack; + + // Perform DFS from all unvisited nodes + for (int i = 0; i < V; i++) { + if (!visited[i]) { + dfs(i, adj, visited, topoStack); + } + } + + // Print topological sort (stack will give reverse order) + cout << "Topological Sort: "; + while (!topoStack.empty()) { + cout << topoStack.top() << " "; + topoStack.pop(); + } + cout << endl; +} + +int main() { + int V, E; + cout << "Enter the number of vertices and edges: "; + cin >> V >> E; + + vector> adj(V); + + cout << "Enter the edges (u v) where u -> v:\n"; + for (int i = 0; i < E; i++) { + int u, v; + cin >> u >> v; + adj[u].push_back(v); + } + + topologicalSortDFS(V, adj); + + return 0; +} + +// Input: +// 6 6 +// 5 2 +// 5 0 +// 4 0 +// 4 1 +// 2 3 +// 3 1 + +// Output: +// Topological Sort: 5 4 2 3 1 0 diff --git a/CP/TopologicalSort/UsingKahnAlgo.cpp b/CP/TopologicalSort/UsingKahnAlgo.cpp new file mode 100644 index 0000000..c2dc192 --- /dev/null +++ b/CP/TopologicalSort/UsingKahnAlgo.cpp @@ -0,0 +1,92 @@ +#include +#include +#include +#include + +using namespace std; + +// Function to perform topological sort using Kahn's algorithm +void topologicalSortKahn(int V, vector>& adj) { + vector inDegree(V, 0); + + // Step 1: Compute in-degree of all vertices + for (int i = 0; i < V; i++) { + for (int u : adj[i]) { + inDegree[u]++; + } + } + + // Step 2: Initialize the queue with all vertices with 0 in-degree + queue q; + for (int i = 0; i < V; i++) { + if (inDegree[i] == 0) { + q.push(i); + } + } + + // Step 3: Perform BFS and collect topological order using a stack + stack topoStack; + int count = 0; // Counter to check if the graph is a DAG (no cycle) + + while (!q.empty()) { + int node = q.front(); + q.pop(); + topoStack.push(node); // Store node in topological sort + + // Reduce in-degree of neighboring nodes + for (int neighbor : adj[node]) { + inDegree[neighbor]--; + if (inDegree[neighbor] == 0) { + q.push(neighbor); + } + } + + count++; + } + + // If count != V, the graph has a cycle + if (count != V) { + cout << "Graph contains a cycle. Topological sort not possible." << endl; + return; + } + + // Step 4: Print topological sort (stack will give reverse order) + cout << "Topological Sort: "; + while (!topoStack.empty()) { + cout << topoStack.top() << " "; + topoStack.pop(); + } + cout << endl; +} + +int main() { + int V, E; + cout << "Enter the number of vertices and edges: "; + cin >> V >> E; + + vector> adj(V); + + cout << "Enter the edges (u v) where u -> v:\n"; + for (int i = 0; i < E; i++) { + int u, v; + cin >> u >> v; + adj[u].push_back(v); + } + + topologicalSortKahn(V, adj); + + return 0; +} + + +// Input: +// 6 6 +// 5 2 +// 5 0 +// 4 0 +// 4 1 +// 2 3 +// 3 1 + +// Output: +// Topological Sort: 4 5 2 3 1 0