Skip to content

Added Kosaraju's algorithm for finding SCCs and program for removal of dupes from a unsorted link list. #91

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

Closed
wants to merge 2 commits into from
Closed
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
106 changes: 106 additions & 0 deletions graph_problems/kosaraju.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
A strongly connected component (SCC) of a directed graph is a maximal strongly connected subgraph.
We can find all strongly connected components of a directed graph in O(V+E) time using Kosaraju’s algorithm. (V:vertices, E:edges)
Kosaraju's Algorithm:
1) Create an empty stack ‘S’ and do DFS traversal of a graph.
In DFS traversal, after calling recursive DFS for adjacent vertices of a vertex, push the vertex to stack.
2) Reverse directions of all arcs to obtain the transpose graph.
3) One by one pop a vertex from S while S is not empty.
Let the popped vertex be ‘v’. Take v as source and do DFS (call DFSUtil(v)).
The DFS starting from v prints strongly connected component of v.
*/
//Code:

#include<bits/stdc++.h>
using namespace std;
void print(vector<int> a[],int V)
{
for(int i=0;i<V;i++)
{
if(!a[i].empty())
cout<<"i="<<i<<"-->";
for(int j=0;j<a[i].size();j++)
cout<<a[i][j]<<" ";
if(!a[i].empty())
cout<<endl;
}
}
void calc_time(int v,stack<int> &st,bool vis[],vector<int> adj[])
{
vis[v]=true;
for(auto i=adj[v].begin();i!=adj[v].end();i++)
{
if(vis[*i]==false)
calc_time(*i,st,vis,adj);
}
st.push(v);
}
void dfs(int v,bool vis[],vector<int> grev[])
{
vis[v]=true;
// cout<<v<<" ";
for(auto i=grev[v].begin();i!=grev[v].end();i++)
{
if(vis[*i]==false)
dfs(*i,vis,grev);
}
}
int kosaraju(int V, vector<int> adj[])
{
// print(adj,V);
bool vis[V]={};
stack<int> st;
for(int v=0;v<V;v++)
{
if(vis[v]==false)
calc_time(v,st,vis,adj);
}
// cout<<"stack_size="<<st.size()<<endl;
//making new graph (grev) with reverse edges as in adj[]
vector<int> grev[V];
for(int i=0;i<V+1;i++)
{
for(auto j=adj[i].begin();j!=adj[i].end();j++)
{
grev[*j].push_back(i);
}
}
// cout<<"grev="<<endl;
// print(grev,V);
//reinitialise vis to 0
for(int i=0;i<V;i++)
vis[i]=false;
int cnt_scc=0;
while(!st.empty())
{
int t=st.top();
st.pop();
if(vis[t]==false)
{
dfs(t,vis,grev);
cnt_scc++;
}
}
// cout<<"cnt_scc="<<cnt_scc<<endl;
return cnt_scc;

}
int main()
{
int t;
cin>>t;
while(t--)
{
int a,b ;
cin>>a>>b;
int m,n;
vector<int> adj[a+1];
for(int i=0;i<b;i++){
cin>>m>>n;
adj[m].push_back(n);
}
// exit(0);
cout<<kosaraju(a, adj)<<endl;
}
return 0;
}
75 changes: 75 additions & 0 deletions linked_list_problems/removeDuplicatesFromUnsortedList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* Given an unsorted linkedlist, remove duplicates from it.
* Input: 8->9->12->1->8->11->1->8->9->33
* Output: 8->9->12->1->11->33
*/

#include <iostream>

struct Node {
int data;
Node * next;
Node( int d ) : data{ d }, next{ nullptr } { }
};

void insert( Node * & head, int data )
{
Node * newNode = new Node(data);
if ( head == nullptr ) {
head = newNode;
} else {
Node * temp = head;
while( temp->next != nullptr ) {
temp = temp->next;
}
temp->next = newNode;
}
}

void printList( Node * head )
{
while( head ) {
std::cout << head->data << "-->";
head = head->next;
}
std::cout << "NULL" << std::endl;
}

Node *removeDuplicates(Node *head)
{
Node *curr = head, *temp = NULL;
while(curr != NULL)
{
temp = curr;
while(temp->next != NULL)
{
if(temp->next->data == curr->data)
temp->next = temp->next->next;
else
temp = temp->next;
}
curr = curr->next;
}
return head;
}

int main()
{
Node * head = nullptr;
// 8->9->12->1->8->11->1->8->9->33
insert(head, 8);
insert(head, 9);
insert(head, 12);
insert(head, 1);
insert(head, 8);
insert(head, 11);
insert(head, 1);
insert(head, 8);
insert(head, 9);
insert(head, 33);
printList(head);

Node *duplicateremovedlist=removeDuplicates(head);
printList(duplicateremovedlist);
return 0;
}