Skip to content

Commit

Permalink
Added:Topo sort using Kahn Algo and DFS
Browse files Browse the repository at this point in the history
  • Loading branch information
pravin435916 committed Oct 3, 2024
1 parent 67ae9a2 commit 9af0c82
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 0 deletions.
72 changes: 72 additions & 0 deletions CP/TopologicalSort/UsingDFS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include <iostream>
#include <vector>
#include <stack>

using namespace std;

// Function to perform DFS and store the topological sort in a stack
void dfs(int node, vector<vector<int>>& adj, vector<bool>& visited, stack<int>& 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<vector<int>>& adj) {
vector<bool> visited(V, false);
stack<int> 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<vector<int>> 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
92 changes: 92 additions & 0 deletions CP/TopologicalSort/UsingKahnAlgo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include <iostream>
#include <vector>
#include <queue>
#include <stack>

using namespace std;

// Function to perform topological sort using Kahn's algorithm
void topologicalSortKahn(int V, vector<vector<int>>& adj) {
vector<int> 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<int> 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<int> 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<vector<int>> 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

0 comments on commit 9af0c82

Please sign in to comment.