-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
add kosaraju algorithm in cpp #2672
Merged
+105
−0
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
//Strongly connected components(KOSARAJU ALGORITHM) | ||
#include <iostream> | ||
#include <vector> | ||
#include <stack> | ||
#include <unordered_set> | ||
using namespace std; | ||
|
||
//dfs to store vertices in a stack as per their finish time | ||
void dfs(vector<int>* edges, int start, unordered_set<int> &visited, stack<int> &finishStack) { | ||
visited.insert(start); | ||
for (int i = 0; i < edges[start].size(); i++) { | ||
int adjacent = edges[start][i]; | ||
if (visited.count(adjacent) == 0) { | ||
dfs(edges, adjacent, visited, finishStack); | ||
} | ||
} | ||
finishStack.push(start); | ||
} | ||
|
||
//dfs of a transpose graph | ||
void dfs2(vector<int>* edges, int start, unordered_set<int>* component, unordered_set<int> & visited) { | ||
visited.insert(start); | ||
component->insert(start); | ||
for (int i = 0; i < edges[start].size(); i++) { | ||
int adjacent = edges[start][i]; | ||
if (visited.count(adjacent) == 0) { | ||
dfs2(edges, adjacent, component, visited); | ||
} | ||
} | ||
} | ||
|
||
unordered_set<unordered_set<int>*>* getSCC(vector<int>* edges, vector<int>* edgesT, int n) { | ||
unordered_set<int> visited; | ||
stack<int> finishedVertices; | ||
for (int i = 0; i < n; i++) { | ||
if (visited.count(i) == 0) { | ||
dfs(edges, i, visited, finishedVertices); | ||
} | ||
} | ||
unordered_set<unordered_set<int>*>* output = new unordered_set<unordered_set<int>*>(); | ||
visited.clear(); | ||
while (finishedVertices.size() != 0) { | ||
int element = finishedVertices.top(); | ||
finishedVertices.pop(); | ||
if (visited.count(element) != 0) { | ||
continue; | ||
} | ||
unordered_set<int>* component = new unordered_set<int>(); | ||
dfs2(edgesT, element, component, visited); | ||
output->insert(component); | ||
} | ||
return output; | ||
} | ||
|
||
int main() { | ||
int n; //no.of vertices | ||
cout<<"Enter the no. of vertices : "<<endl; | ||
cin >> n; | ||
vector<int>* edges = new vector<int>[n]; //using a adjacency list to implement | ||
vector<int>* edgesT = new vector<int>[n]; //transpose of a graph | ||
int m; // no.of edges | ||
cout<<"Enter the no. of edges : "<<endl; | ||
cin >> m; | ||
|
||
for (int i = 0; i < m; i++) { | ||
int j, k; | ||
cin >> j >> k; | ||
edges[j - 1].push_back(k - 1); | ||
edgesT[k - 1].push_back(j - 1); | ||
} | ||
|
||
unordered_set<unordered_set<int>*>* components = getSCC(edges, edgesT, n); | ||
unordered_set<unordered_set<int>*>::iterator it = components->begin(); | ||
|
||
while (it != components->end()) { | ||
unordered_set<int>* component = *it; | ||
unordered_set<int>::iterator it2 = component->begin(); | ||
while (it2 != component->end()) { | ||
cout <<*it2 + 1<< " "; | ||
it2++; | ||
} | ||
cout << endl; | ||
delete component; | ||
it++; | ||
} | ||
|
||
delete components; | ||
delete [] edges; | ||
delete [] edgesT; | ||
} | ||
|
||
/*INPUTS | ||
Enter the no. of vertices : 6 | ||
Enter the no. of edges :7 | ||
1 2 | ||
2 3 | ||
3 4 | ||
4 1 | ||
3 5 | ||
5 6 | ||
6 5 | ||
|
||
OUTPUT | ||
6 5 | ||
2 3 4 1 */ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keep a line space after this