Skip to content
This repository was archived by the owner on Sep 7, 2025. It is now read-only.
Merged
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
83 changes: 83 additions & 0 deletions graphs/kosaraju.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include<bits/stdc++.h>
using namespace std;
void print(vector < list <int> > adjlist,int i,bool vis[])
{ //print strongly connected components
vis[i]=true;
cout<<i<<"--> ";
list<int>::iterator it;
for(it=adjlist[i].begin();it!=adjlist[i].end();it++)
{
if(!vis[*it])
print(adjlist,*it,vis);
}
cout<<endl;

}
stack <int> fillstack(vector < list <int> > adjlist,int i, stack <int> order,bool vis[])
{
vis[i]=true;
list<int>::iterator it;
for(it=adjlist[i].begin();it!=adjlist[i].end();it++)
{
if(vis[*it]==false)
fillstack(adjlist,*it,order,vis);
}
order.push(i);
//cout<<order.top()<<" ";
return order;
}
int kosaraju( vector < list <int> > adjlist, int v)
{ int count=0;
bool vis[v];
int i;
//mark all nodes as unvisited
for(i=0;i<=v;i++)
vis[i]=false;
stack <int> order;
list<int>::iterator it;
for(i=1;i<=v;i++)
{
if(vis[i]==false)
//fill stack in order of finish times using DFS
order = fillstack(adjlist,i,order,vis);
}
/*while(!order.empty())
{
cout<<order.top()<<" ";
order.pop();
}*/
for(i=1;i<=v;i++)
vis[i]=false;

for(i=0;i<v;i++)
{ //reverse the graph
for(it=adjlist[i].begin();it!=adjlist[i].end();it++)
adjlist[*it].push_back(i);
}
while(order.empty()==false)
{ //check connectivity by performing DFS on reverse graph
i= order.top();
order.pop();
if(vis[i]==false)
{//print(adjlist,i,vis);
count++;
}

}
return count;
}
int main()
{
int v,e,v1,v2,i;
cin>>v>>e;
vector < list <int> > adjlist(v+1);
for(i=0;i<e;i++)
{
cin>>v1>>v2;
adjlist[v1].push_back(v2);
adjlist[v2].push_back(v1); //for undirected graph
}

int ans=kosaraju(adjlist,v);
cout<<ans<<endl;
}
96 changes: 96 additions & 0 deletions graphs/kruskal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include<bits/stdc++.h>
using namespace std;
class graph
{
int v,adj[100][100];
public:
void read()
{
int i,j,e,v1,v2,c;

cout<<"vertex,edges"<<endl;
cin>>v>>e;
for(i=0;i<v;i++)
{
for(j=0;j<v;j++)
{
adj[i][j]=999;

}
}

for(i=0;i<e;i++)
{ cout<<"Enter edge, cost"<<endl;
cin>>v1>>v2>>c;
adj[v1][v2]=adj[v2][v1]=c;
}
return;
}
int find(int v,int p[])
{
while(p[v]!=v)
v=p[v];
return v;
}
void unionij(int i,int j,int p[])
{
if(i<j)
p[j]=i;
else
p[i]=j;
}
void kruskal()
{
int i,j,t[100][2],k=0,min,sum=0,p[v],count=0,v1,v2;
for(i=0;i<v;i++)
p[i]=i;
while(count<v)
{
min=999;
for(i=0;i<v;i++)
{
for(j=0;j<v;j++)
{
if(adj[i][j]<min)
{
min=adj[i][j];
v1=i;
v2=j;
}
}
}
if(min==999)break;
i=find(v1,p);
j=find(v2,p);
if(i!=j)
{
t[k][0]=i;
t[k][1]=j;
k++;

sum+=min;
count++;
unionij(i,j,p);

}
adj[v1][v2]=adj[v2][v1]=999;
}
if(count==v-1)
{
cout<<"Spanning tree is:"<<endl;
for(i=0;i<v-1;i++)
cout<<"Edge "<<t[i][0]<<"-->"<<t[i][1]<<endl;
cout<<endl<<"Cost : "<<sum<<endl;
return;
}

cout<<"Spanning tree doesn't exist"<<endl;

}
};
int main()
{
graph g;
g.read();
g.kruskal();
}
61 changes: 61 additions & 0 deletions graphs/topological sort.(matrix).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include<bits/stdc++.h>
using namespace std;
class graph
{
int v,adj[100][100],st[100],top;
public:
void read()
{
int i,j;

cout<<"vertex"<<endl;
cin>>v;
for(i=0;i<v;i++)
{
for(j=0;j<v;j++)
{
cin>>adj[i][j];
// adj[j][i]=adj[i][j];
}
}
}
void topo(int u,int vis[])
{
vis[u]=1;
int i;
for(i=0;i<v;i++)
{
if(adj[u][i]==1&&vis[i]==0)
topo(i,vis);

}
top++;
st[top]=u;
}
void toposort()
{
int i,vis[v];
top=-1;
for(i=0;i<v;i++)
vis[i]=0;
for(i=0;i<v;i++)
{

if(vis[i]==0)
topo(i,vis);
}
while(top>=0)
{
cout<<st[top]<<" ";
top--;
}
cout<<endl;
}

};
int main()
{
graph g;
g.read();
g.toposort();
}
48 changes: 48 additions & 0 deletions graphs/topological sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include<bits/stdc++.h>
using namespace std;
void sortit(vector<list <int> > adjlist,int i, stack <int> sorted,int vis[])
{
vis[i]= 1;
list <int> :: iterator it;

for(it= adjlist[i].begin();it!=adjlist[i].end();it++)
{
if(vis[*it]==0)
{
sortit(adjlist,*it,sorted,vis);
}

}
cout<<i<<endl;
sorted.push(i);

}
void topsort(vector<list <int> > adjlist,int v)
{
int i,vis[v];
for(i=1;i<=v;i++)
vis[i]=0;
stack <int> sorted;
for(i=1;i<=v;i++)
{
if(vis[i]==0)
sortit(adjlist,i,sorted,vis);
}
while(!sorted.empty())
{
cout<< sorted.top();
sorted.pop();
}
}
int main()
{
int v,e,v1,v2;
cin>>v>>e;
vector<list <int> > adjlist(v+1);
for(int i=1;i<=v;i++)
{
cin>>v1>>v2;
adjlist[v1].push_back(v2);
}
topsort(adjlist,v);
}
Binary file added graphs/tsp.o
Binary file not shown.