Skip to content

Commit 1f26c22

Browse files
committed
3 graph algos
1 parent 1e3de1c commit 1f26c22

File tree

3 files changed

+257
-0
lines changed

3 files changed

+257
-0
lines changed

graph/c++/bellman.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include<iostream>
2+
#include<bits/stdc++.h>
3+
using namespace std;
4+
int e,v;
5+
struct edge
6+
{
7+
char v1,v2;
8+
int w;
9+
}*E;
10+
struct vertex
11+
{
12+
char v,P;
13+
int d;
14+
}*V;
15+
bool BellmanFord(char s)
16+
{
17+
V[s-97].d=0;
18+
for(int j=1;j<v;j++)
19+
{
20+
for(int i=0;i<e;i++)
21+
{
22+
if(E[i].w+V[(E[i].v1)-97].d<V[(E[i].v2)-97].d)
23+
{
24+
V[(E[i].v2)-97].P=E[i].v1;
25+
V[(E[i].v2)-97].d=E[i].w+V[(E[i].v1)-97].d;
26+
}
27+
}
28+
}
29+
for(int i=0;i<e;i++)
30+
{
31+
if(E[i].w+V[(E[i].v1)-97].d<V[(E[i].v2)-97].d)
32+
return false;
33+
}
34+
return true;
35+
}
36+
int main()
37+
{
38+
char s;
39+
cout<<"Enter no. of vertices: ";
40+
cin>>v;
41+
V=new vertex[v];
42+
cout<<"Enter no. of edges: ";
43+
cin>>e;
44+
E=new edge[e];
45+
cout<<"Enter edges and their weights: \n";
46+
for(int i=0;i<e;i++)
47+
cin>>E[i].v1>>E[i].v2>>E[i].w;
48+
for(int i=0;i<v;i++)
49+
{
50+
V[i].v=i+97;
51+
V[i].d=INT_MAX;
52+
V[i].P=' ';
53+
}
54+
cout<<"Enter the souce vertex: ";
55+
cin>>s;
56+
bool res=BellmanFord(s);
57+
if(res==false)
58+
{
59+
cout<<"Negative weight cycle exists in graph!!!\n";
60+
cout<<"Bellman Ford Algorithm Fails!!!";
61+
}
62+
else
63+
{
64+
cout<<"Shortest Distance from "<<s<<":\n";
65+
for(int i=0;i<v;i++)
66+
cout<<V[i].v<<" = "<<" \t "<<V[i].d<<endl;
67+
}
68+
return 0;
69+
}

graph/c++/dijikstra.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#include<iostream>
2+
using namespace std;
3+
int e,v;
4+
char *Q;
5+
struct edge
6+
{
7+
char v1,v2;
8+
int w;
9+
}*E;
10+
struct vertex
11+
{
12+
char v,P;
13+
int d;
14+
}*V;
15+
char Extract_Min()
16+
{
17+
char min;
18+
int k=INT_MAX;
19+
int pos;
20+
for(int i=0;i<v;i++)
21+
{
22+
if(k>V[i].d&&Q[i]!=' ')
23+
{
24+
min=Q[i];
25+
pos=i;
26+
k=V[i].d;
27+
}
28+
}
29+
Q[pos]=' ';
30+
return min;
31+
}
32+
void dijkstra(char s)
33+
{
34+
int qs=v;
35+
char u,r=Q[s-97];
36+
V[s-97].d=0;
37+
while(qs--)
38+
{
39+
u=Extract_Min();
40+
//cout<<u<<" ";
41+
for(int i=0;i<e;i++)
42+
{
43+
if(E[i].v1==u)
44+
{
45+
if((Q[(E[i].v2)-97]!=' ')&&((E[i].w+V[u-97].d)<V[(E[i].v2)-97].d))
46+
{
47+
V[(E[i].v2)-97].P=E[i].v1;
48+
V[(E[i].v2)-97].d=E[i].w+V[u-97].d;
49+
}
50+
}
51+
else if(E[i].v2==u)
52+
{
53+
if((Q[(E[i].v1)-97]!=' ')&&((E[i].w+V[u-97].d)<V[(E[i].v1)-97].d))
54+
{
55+
V[(E[i].v1)-97].P=E[i].v2;
56+
V[(E[i].v1)-97].d=E[i].w+V[u-97].d;
57+
}
58+
}
59+
}
60+
}
61+
}
62+
int main()
63+
{
64+
char s;
65+
cout<<"Enter no. of vertices: ";
66+
cin>>v;
67+
V=new vertex[v];
68+
Q=new char[v];
69+
cout<<"Enter no. of edges: ";
70+
cin>>e;
71+
E=new edge[e];
72+
cout<<"Enter edges and their weights: \n";
73+
for(int i=0;i<e;i++)
74+
cin>>E[i].v1>>E[i].v2>>E[i].w;
75+
for(int i=0;i<v;i++)
76+
{
77+
V[i].v=i+97;
78+
V[i].d=INT_MAX;
79+
V[i].P=' ';
80+
Q[i]=i+97;
81+
}
82+
cout<<"Enter the souce vertex: ";
83+
cin>>s;
84+
dijkstra(s);
85+
cout<<"Shortest Distance from "<<s<<":\n";
86+
for(int i=0;i<v;i++)
87+
cout<<V[i].v<<" = "<<" \t "<<V[i].d<<endl;
88+
return 0;
89+
}

graph/c++/floyd_warshall.cpp

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#include<iostream>
2+
#include<climits>
3+
#include<bits/stdc++.h>
4+
using namespace std;
5+
int v,e;
6+
int **W;
7+
struct matrix
8+
{
9+
int **D;
10+
char **P;
11+
}*R;
12+
void Floyd_Warshall(int **W,int n)
13+
{
14+
R=new matrix[n+1];
15+
for(int i=0;i<=n;i++)
16+
{
17+
R[i].D=new int*[n];
18+
R[i].P=new char*[n];
19+
for(int k=0;k<n;k++)
20+
{
21+
R[i].D[k]=new int[n];
22+
R[i].P[k]=new char[n];
23+
}
24+
}
25+
for(int i=0;i<n;i++)
26+
{
27+
for(int j=0;j<n;j++)
28+
{
29+
R[0].D[i][j]=W[i][j];
30+
if(W[i][j]==INT_MAX||W[i][j]==0)
31+
R[0].P[i][j]='N';
32+
else
33+
R[0].P[i][j]=i+97;
34+
}
35+
}
36+
for(int k=1;k<=n;k++)
37+
{
38+
for(int i=0;i<n;i++)
39+
{
40+
for(int j=0;j<n;j++)
41+
{
42+
if(R[k-1].D[i][j]>(R[k-1].D[i][k-1]+R[k-1].D[k-1][j])&&
43+
R[k-1].D[i][k-1]!=INT_MAX&&R[k-1].D[k-1][j]!=INT_MAX)
44+
{
45+
R[k].D[i][j]=R[k-1].D[i][k-1]+R[k-1].D[k-1][j];
46+
R[k].P[i][j]=R[k-1].P[k-1][j];
47+
}
48+
else
49+
{
50+
R[k].D[i][j]=R[k-1].D[i][j];
51+
R[k].P[i][j]=R[k-1].P[i][j];
52+
}
53+
}
54+
}
55+
}
56+
}
57+
int main()
58+
{
59+
char a,b;
60+
int w;
61+
cout<<"Enter the no. of vertices: ";
62+
cin>>v;
63+
W=new int*[v];
64+
cout<<"Enter the no. of edges: ";
65+
cin>>e;
66+
for(int i=0;i<v;i++)
67+
{
68+
W[i]=new int[v];
69+
for(int j=0;j<v;j++)
70+
{
71+
if(i==j)
72+
W[i][j]=0;
73+
else
74+
W[i][j]=INT_MAX;
75+
}
76+
}
77+
cout<<"Enter edges and their corresponding weights: \n";
78+
for(int i=0;i<e;i++)
79+
{
80+
cin>>a>>b>>w;
81+
W[a-97][b-97]=w;
82+
}
83+
Floyd_Warshall(W,v);
84+
cout<<"Distance Matrix: \n";
85+
for(int i=0;i<v;i++)
86+
{
87+
for(int j=0;j<v;j++)
88+
cout<<R[v].D[i][j]<<"\t";
89+
cout<<endl;
90+
}
91+
cout<<"Parent Matrix: \n";
92+
for(int i=0;i<v;i++)
93+
{
94+
for(int j=0;j<v;j++)
95+
cout<<R[v].P[i][j]<<"\t";
96+
cout<<endl;
97+
}
98+
return 0;
99+
}

0 commit comments

Comments
 (0)