Skip to content

Added Shortest Path Algos for Graphs #16

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

Open
wants to merge 4 commits into
base: master
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
62 changes: 62 additions & 0 deletions Dynamic Programming/Medium/matrixchain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int **m,**s;
void MatrixChainOrder(int *P,int n)
{
int j;
long int q;
m=new int*[n];
s=new int*[n];
for(int i=0;i<n;i++)
{
m[i]=new int[n];
s[i]=new int[n];
m[i][i]=0;
}
for(int l=2;l<=n;l++)
for(int i=0;i<n-l+1;i++)
{
j=i+l-1;
m[i][j]=INT_MAX;
for(int k=i;k<j;k++)
{
q=m[i][k]+m[k+1][j]+(P[i]*P[k+1]*P[j+1]);
if(q<m[i][j])
{
m[i][j]=q;
s[i][j]=k;
}
}
}
}
int num=1;
void disp(int a,int b)
{
if(a==b)
{
cout<<"M"<<num;
num++;
return;
}
cout<<"(";
disp(a,s[a][b]);
cout<<" X ";
disp(s[a][b]+1,b);
cout<<")";
}
int main()
{
int n,*P;
cout<<"Enter the no. of matrices: ";
cin>>n;
P=new int[n+1];
cout<<"Enter the different orders: \n";
for(int i=0;i<n+1;i++)
cin>>P[i];
MatrixChainOrder(P,n);
cout<<"Order of Matrix Multiplication: \n";
disp(0,n-1);
cout<<"\nThe maximum scalar multiplications required: "<<m[0][n-1];
return 0;
}
55 changes: 55 additions & 0 deletions Graphs/Minimum Spanning Tree/kruskal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include<iostream>
#include<algorithm>
#include"disjoint.h"
using namespace std;
struct edge
{
char v1,v2;
int w;
}*E,*MST;
int v,e,t,*rank;
char *P;
bool comp(edge e1,edge e2)
{
return (e1.w<e2.w);
}
void MST_kruskal()
{
t=0;
MST=new edge[v-1];
sort(E,E+e,comp);
for(int i=0;i<e;i++)
{
if(find_set(E[i].v1,P,rank)!=find_set(E[i].v2,P,rank))
{
MST[t++]=E[i];
union_set(E[i].v1,E[i].v2,P,rank);
}
}
}
int main()
{
char a,b;
cout<<"Enter no. of vertices: ";
cin>>v;
cout<<"Enter no. of edges: ";
cin>>e;
P=new char[v];
rank=new int[v];
cout<<"Enter the end vertices of the edges and their corresponding weights: \n";
E=new edge[e];
for(int i=0;i<v;i++)
P[i]=' ';
for(int i=0;i<e;i++)
{
cin>>E[i].v1>>E[i].v2>>E[i].w;
makeset(E[i].v1,P,rank);
makeset(E[i].v2,P,rank);
}
MST_kruskal();
cout<<"Resultant Minimum Spanning Tree: \n";
cout<<"Edge \t"<<" Weight\n";
for(int i=0;i<t;i++)
cout<<MST[i].v1<<"---"<<MST[i].v2<<" \t "<<MST[i].w<<endl;
return 0;
}
88 changes: 88 additions & 0 deletions Graphs/Minimum Spanning Tree/prims.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include<iostream>
#include<climits>
using namespace std;
int e,v;
char *Q;
struct edge
{
char v1,v2;
int w;
}*E;
struct vertex
{
char v,P;
int key;
}*V;
char Extract_Min()
{
char min;
int k=INT_MAX;
int pos;
for(int i=0;i<v;i++)
{
if(k>V[i].key&&Q[i]!=' ')
{
min=Q[i];
pos=i;
k=V[i].key;
}
}
Q[pos]=' ';
return min;
}
void MST_Prim()
{
int qs=v;
char u;
V[0].key=0;
while(qs--)
{
u=Extract_Min();
//cout<<u<<" ";
for(int i=0;i<e;i++)
{
if(E[i].v1==u)
{
if((Q[(E[i].v2)-97]!=' ')&&(E[i].w<V[(E[i].v2)-97].key))
{
V[(E[i].v2)-97].P=E[i].v1;
V[(E[i].v2)-97].key=E[i].w;
}
}
else if(E[i].v2==u)
{
if((Q[(E[i].v1)-97]!=' ')&&(E[i].w<V[(E[i].v1)-97].key))
{
V[(E[i].v1)-97].P=E[i].v2;
V[(E[i].v1)-97].key=E[i].w;
}
}
}
}
}
int main()
{
cout<<"Enter no. of vertices: ";
cin>>v;
V=new vertex[v];
Q=new char[v];
cout<<"Enter no. of edges: ";
cin>>e;
E=new edge[e];
cout<<"Enter edges and their weights: \n";
for(int i=0;i<e;i++)
cin>>E[i].v1>>E[i].v2>>E[i].w;
for(int i=0;i<v;i++)
{
V[i].v=i+97;
V[i].key=INT_MAX;
V[i].P=' ';
Q[i]=i+97;
}
MST_Prim();
cout<<"Resultant Minimum Spanning Tree: \n";
cout<<"Edge \t"<<" Weight\n";
for(int i=1;i<v;i++)
cout<<V[i].v<<"---"<<V[i].P<<" \t "<<V[i].key<<endl;
return 0;
}
69 changes: 69 additions & 0 deletions Graphs/Shortest Path Algos/bellman.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int e,v;
struct edge
{
char v1,v2;
int w;
}*E;
struct vertex
{
char v,P;
int d;
}*V;
bool BellmanFord(char s)
{
V[s-97].d=0;
for(int j=1;j<v;j++)
{
for(int i=0;i<e;i++)
{
if(E[i].w+V[(E[i].v1)-97].d<V[(E[i].v2)-97].d)
{
V[(E[i].v2)-97].P=E[i].v1;
V[(E[i].v2)-97].d=E[i].w+V[(E[i].v1)-97].d;
}
}
}
for(int i=0;i<e;i++)
{
if(E[i].w+V[(E[i].v1)-97].d<V[(E[i].v2)-97].d)
return false;
}
return true;
}
int main()
{
char s;
cout<<"Enter no. of vertices: ";
cin>>v;
V=new vertex[v];
cout<<"Enter no. of edges: ";
cin>>e;
E=new edge[e];
cout<<"Enter edges and their weights: \n";
for(int i=0;i<e;i++)
cin>>E[i].v1>>E[i].v2>>E[i].w;
for(int i=0;i<v;i++)
{
V[i].v=i+97;
V[i].d=INT_MAX;
V[i].P=' ';
}
cout<<"Enter the souce vertex: ";
cin>>s;
bool res=BellmanFord(s);
if(res==false)
{
cout<<"Negative weight cycle exists in graph!!!\n";
cout<<"Bellman Ford Algorithm Fails!!!";
}
else
{
cout<<"Shortest Distance from "<<s<<":\n";
for(int i=0;i<v;i++)
cout<<V[i].v<<" = "<<" \t "<<V[i].d<<endl;
}
return 0;
}
89 changes: 89 additions & 0 deletions Graphs/Shortest Path Algos/dijikstra.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include<iostream>
using namespace std;
int e,v;
char *Q;
struct edge
{
char v1,v2;
int w;
}*E;
struct vertex
{
char v,P;
int d;
}*V;
char Extract_Min()
{
char min;
int k=INT_MAX;
int pos;
for(int i=0;i<v;i++)
{
if(k>V[i].d&&Q[i]!=' ')
{
min=Q[i];
pos=i;
k=V[i].d;
}
}
Q[pos]=' ';
return min;
}
void dijkstra(char s)
{
int qs=v;
char u,r=Q[s-97];
V[s-97].d=0;
while(qs--)
{
u=Extract_Min();
//cout<<u<<" ";
for(int i=0;i<e;i++)
{
if(E[i].v1==u)
{
if((Q[(E[i].v2)-97]!=' ')&&((E[i].w+V[u-97].d)<V[(E[i].v2)-97].d))
{
V[(E[i].v2)-97].P=E[i].v1;
V[(E[i].v2)-97].d=E[i].w+V[u-97].d;
}
}
else if(E[i].v2==u)
{
if((Q[(E[i].v1)-97]!=' ')&&((E[i].w+V[u-97].d)<V[(E[i].v1)-97].d))
{
V[(E[i].v1)-97].P=E[i].v2;
V[(E[i].v1)-97].d=E[i].w+V[u-97].d;
}
}
}
}
}
int main()
{
char s;
cout<<"Enter no. of vertices: ";
cin>>v;
V=new vertex[v];
Q=new char[v];
cout<<"Enter no. of edges: ";
cin>>e;
E=new edge[e];
cout<<"Enter edges and their weights: \n";
for(int i=0;i<e;i++)
cin>>E[i].v1>>E[i].v2>>E[i].w;
for(int i=0;i<v;i++)
{
V[i].v=i+97;
V[i].d=INT_MAX;
V[i].P=' ';
Q[i]=i+97;
}
cout<<"Enter the souce vertex: ";
cin>>s;
dijkstra(s);
cout<<"Shortest Distance from "<<s<<":\n";
for(int i=0;i<v;i++)
cout<<V[i].v<<" = "<<" \t "<<V[i].d<<endl;
return 0;
}
Loading