Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Solution To Topological Sort using C++ #765

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions Topological_Sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// The topological sort algorithm takes a directed graph and returns an array of the nodes where each node appears before all the nodes it points to

#include<bits/stdc++.h>
using namespace std;

vector<int> topoSort(int V, vector<int> adj[])
{
vector<int> indegree(V,0);

for(int i = 0; i<V; i++)
{
for(auto x: adj[i])
{
indegree[x]++;
}
}

queue<int> q;
for(int i = 0; i<V; i++)
{
if(indegree[i] == 0)
{
q.push(i);
}
}

vector<int> topo;

while(!q.empty())
{
int node = q.front();
q.pop();
topo.push_back(node);

for(auto x: adj[node])
{
indegree[x]--;
if(indegree[x] == 0)
{
q.push(x);
}
}
}
return topo;
}

int check(int V, vector <int> &res, vector<int> adj[]) {

if(V!=res.size())
return 0;

vector<int> map(V, -1);
for (int i = 0; i < V; i++) {
map[res[i]] = i;
}
for (int i = 0; i < V; i++) {
for (int v : adj[i]) {
if (map[i] > map[v]) return 0;
}
}
return 1;
}

int main() {
int T;
cin >> T;
while (T--) {
int N, E;
cin >> E >> N;
int u, v;

vector<int> adj[N];

for (int i = 0; i < E; i++) {
cin >> u >> v;
adj[u].push_back(v);
}
vector <int> res = topoSort(N, adj);

cout << check(N, res, adj) << endl;
}

return 0;
}

// Time Complexity: O(V+E)
// Space Complexity: O(V)
82 changes: 82 additions & 0 deletions tempCodeRunnerFile.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include<bits/stdc++.h>
using namespace std;

vector<int> topoSort(int V, vector<int> adj[])
{
vector<int> indegree(V,0);

for(int i = 0; i<V; i++)
{
for(auto x: adj[i])
{
indegree[x]++;
}
}

queue<int> q;
for(int i = 0; i<V; i++)
{
if(indegree[i] == 0)
{
q.push(i);
}
}

vector<int> topo;

while(!q.empty())
{
int node = q.front();
q.pop();
topo.push_back(node);

for(auto x: adj[node])
{
indegree[x]--;
if(indegree[x] == 0)
{
q.push(x);
}
}
}
return topo;
}

int check(int V, vector <int> &res, vector<int> adj[]) {

if(V!=res.size())
return 0;

vector<int> map(V, -1);
for (int i = 0; i < V; i++) {
map[res[i]] = i;
}
for (int i = 0; i < V; i++) {
for (int v : adj[i]) {
if (map[i] > map[v]) return 0;
}
}
return 1;
}

int main() {
int T;
cin >> T;
while (T--) {
int N, E;
cin >> E >> N;
int u, v;

vector<int> adj[N];

for (int i = 0; i < E; i++) {
cin >> u >> v;
adj[u].push_back(v);
}
vector <int> res = topoSort(N, adj);

cout << check(N, res, adj) << endl;
}

return 0;
}
Binary file added tempCodeRunnerFile.exe
Binary file not shown.