Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 221c911

Browse files
Add files via upload
1 parent ff70c30 commit 221c911

File tree

6 files changed

+341
-0
lines changed

6 files changed

+341
-0
lines changed

‎Dynamic Method/Bellma_input.txt‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
9
2+
0 4 0 0 0 0 0 8 0
3+
4 0 8 0 0 0 0 11 0
4+
0 8 0 7 0 4 0 0 2
5+
0 0 7 0 9 14 0 0 0
6+
0 0 0 9 0 10 0 0 0
7+
0 0 4 14 10 0 2 0 0
8+
0 0 0 0 0 2 0 1 6
9+
8 11 0 0 0 0 1 0 7
10+
0 0 2 0 0 0 6 7 0
11+
12+
13+
14+
15+
6
16+
0 6 4 5 0 0
17+
0 0 0 0 -1 0
18+
0 -2 0 0 3 0
19+
0 0 -2 0 0 -1
20+
0 0 0 0 0 3
21+
0 0 0 0 0 0
22+
23+
6
24+
0 1 0 0 5 0
25+
0 0 4 1 0 0
26+
0 0 0 0 0 0
27+
0 0 2 0 0 0
28+
0 0 0 4 0 1
29+
1 0 0 0 0 0
30+
31+

‎Dynamic Method/Bellman_Ford.cpp‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#include<iostream>
2+
#include<fstream>
3+
using namespace std;
4+
#define INF 9999;
5+
6+
struct node
7+
{
8+
int u , v;
9+
};
10+
11+
int pred[100] , dist[100];
12+
int nv;
13+
int graph[1000][1000];
14+
int output[1000][1000]={0};
15+
node edg[1000];
16+
17+
bool bellmanford(int n_edg, int s)
18+
{
19+
for(int i=0;i<nv;i++)
20+
{
21+
pred[i]=-1;
22+
dist[i]=INF;
23+
}
24+
25+
dist[s]=0;
26+
27+
for(int i=1;i<nv;i++)
28+
{
29+
for(int j=0;j<=n_edg;j++)
30+
{
31+
node x=edg[j];
32+
if(dist[x.u]+graph[x.u][x.v]<dist[x.v])
33+
{
34+
dist[x.v]=dist[x.u]+graph[x.u][x.v];
35+
pred[x.v]=x.u;
36+
}
37+
}
38+
}
39+
40+
for(int i=0 ; i<=n_edg ; ++i)
41+
{
42+
node x=edg[i];
43+
if(dist[x.u]+graph[x.u][x.v]<dist[x.v])
44+
return false;
45+
}
46+
47+
return true;
48+
}
49+
50+
int main()
51+
{
52+
fstream infile;
53+
infile.open("Bellma_input.txt" , ios::in);
54+
if(!infile)
55+
{
56+
cout<<"Error on opening file"<<endl;
57+
return 0;
58+
}
59+
infile>>nv;
60+
for(int i=0;i<nv;i++)
61+
{
62+
for(int j=0;j<nv;j++)
63+
infile>>graph[i][j];
64+
}
65+
66+
cout<<"Input graph is"<<endl;
67+
int n_edg=0;
68+
for(int i=0;i<nv;i++)
69+
{
70+
for(int j=0;j<nv;j++)
71+
{
72+
if(graph[i][j])
73+
{
74+
edg[n_edg].u=i;
75+
edg[n_edg].v=j;
76+
n_edg++;
77+
}
78+
cout<<graph[i][j]<<" ";
79+
}
80+
cout<<endl;
81+
}
82+
83+
cout<<"Number of edges"<<n_edg<<endl;
84+
cout<<"Edge list is"<<endl;
85+
for(int i=0;i<n_edg;i++)
86+
cout<<char(edg[i].u+'A')<<"->"<<char(edg[i].v+'A')<<endl;
87+
88+
int s;
89+
cout<<"Enter the source vertext"<<endl;
90+
cin>>s;
91+
92+
if(bellmanford(n_edg-1,s))
93+
{
94+
for(int i=0;i<nv;i++)
95+
{
96+
if(i!=s)
97+
cout<<char(s+'A')<<"->"<<dist[i]<<"->"<<char(i+'A')<<endl;
98+
}
99+
}
100+
else
101+
cout<<"Negative cycle is present"<<endl;
102+
}

‎Dynamic Method/Chain_input.txt‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
5
2+
10 20 30 10 40
3+
4+
5+
6
6+
2 3 5 2 4 3
7+
8+
9+
10+
5
11+
40 20 30 10 30
12+

‎Dynamic Method/Floyd_Warshall.cpp‎

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#include<iostream>
2+
#include<fstream>
3+
using namespace std;
4+
#define INF 99
5+
6+
int pred[100][100] , dist[100][100];
7+
int nv;
8+
int graph[1000][1000];
9+
int output[1000][1000]={0};
10+
11+
void Floyd_Warshall()
12+
{
13+
for(int i=0;i<nv;i++)
14+
{
15+
for(int j=0;j<nv;j++)
16+
{
17+
pred[i][j] = -1;
18+
dist[i][j] = INF;
19+
}
20+
}
21+
22+
for(int i=0;i<nv;i++)
23+
{
24+
for(int j=0;j<nv;j++)
25+
{
26+
dist[i][j]=graph[i][j];
27+
if(graph[i][j]==0 || graph[i][j]==INF)
28+
pred[i][j]=-1;
29+
else
30+
pred[i][j]=i;
31+
}
32+
}
33+
34+
for(int k=0;k<nv;k++)
35+
{
36+
for(int i=0;i<nv;i++)
37+
{
38+
for(int j=0;j<nv;j++)
39+
{
40+
if(dist[i][k]+dist[k][j] < dist[i][j])
41+
{
42+
dist[i][j]=dist[i][k]+dist[k][j];
43+
pred[i][j]=pred[k][j];
44+
}
45+
}
46+
}
47+
48+
}
49+
}
50+
51+
void shortest_path(int i, int j)
52+
{
53+
if(i==j)
54+
{
55+
cout<<char(i+'A')<<"->";
56+
return;
57+
}
58+
59+
if(pred[i][j]==-1)
60+
{
61+
cout<<"No shortest path is found"<<endl;
62+
return;
63+
}
64+
65+
else
66+
{
67+
shortest_path(i,pred[i][j]);
68+
cout<<char(j+'A')<<"->";
69+
}
70+
}
71+
72+
int main()
73+
{
74+
fstream infile;
75+
infile.open("Floyd_input.txt" , ios::in);
76+
if(!infile)
77+
{
78+
cout<<"Error!"<<endl;
79+
return 0;
80+
}
81+
infile>>nv;
82+
for(int i=0 ; i<nv ; ++i)
83+
{
84+
for(int j=0 ; j<nv ; ++j)
85+
infile>>graph[i][j];
86+
}
87+
88+
cout<<"Input graph is"<<endl;
89+
int n_edg=0;
90+
for(int i=0;i<nv;i++)
91+
{
92+
for(int j=0;j<nv;j++)
93+
cout<<graph[i][j]<<" ";
94+
cout<<endl;
95+
}
96+
97+
Floyd_Warshall();
98+
99+
for(int i=0;i<nv;i++)
100+
{
101+
for(int j=0;j<nv;j++)
102+
{
103+
if(dist[i][j]==INF)
104+
cout<<"INF"<<"\t";
105+
else
106+
cout<<dist[i][j]<<"\t";
107+
}
108+
cout<<endl;
109+
}
110+
int i , j;
111+
cout<<"Enter two vertices"<<endl;
112+
cin>>i>>j;
113+
cout<<"shortest path between "<<char(i+'A')<<"and "<<char(j+'A')<<" is"<<endl;
114+
shortest_path(i,j);
115+
return 0;
116+
}
117+

‎Dynamic Method/Floyd_input.txt‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
4
2+
0 10 5 3
3+
1 0 99 99
4+
99 2 0 99
5+
99 99 2 0
6+
7+
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include <iostream>
2+
#include <fstream>
3+
using namespace std;
4+
5+
int m[100][100];
6+
int s[100][100];
7+
8+
int Chain_Mul (int arr[],int i,int j)
9+
{
10+
if(i>=j)
11+
return 0;
12+
13+
if(m[i][j]!=0)
14+
return m[i][j];
15+
16+
int part,temp,mini = INT_MAX;
17+
18+
for(int k=i;k<j;k++)
19+
{
20+
temp = Chain_Mul(arr,i,k) + Chain_Mul(arr,k+1,j) + (arr[i-1]*arr[k]*arr[j]);
21+
if(temp < mini)
22+
{
23+
mini = temp;
24+
part = k;
25+
}
26+
}
27+
m[i][j] = mini;
28+
s[i][j] = part;
29+
30+
return m[i][j];
31+
}
32+
33+
void parenthesisation(int i,int j)
34+
{
35+
if(i==j)
36+
cout<<"A"<<i<<" ";
37+
else
38+
{
39+
cout<<"(";
40+
parenthesisation(i,s[i][j]);
41+
parenthesisation(s[i][j]+1,j);
42+
cout<<")";
43+
}
44+
}
45+
46+
int main()
47+
{
48+
int n;
49+
fstream infile;
50+
infile.open("Chain_input.txt" , ios::in);
51+
if(!infile)
52+
{
53+
cout<<"Error on opening file"<<endl;
54+
return 0;
55+
}
56+
infile>>n;
57+
58+
int arr[n];
59+
for(int i=0;i<n;i++)
60+
infile>>arr[i];
61+
62+
for (int i=1;i<=n;i++)
63+
{
64+
for (int j=1;j<=n;j++)
65+
m[i][j]=0;
66+
}
67+
68+
cout<<"Minimum No of Multiplications: "<<Chain_Mul(arr,1,n-1)<<endl;
69+
parenthesisation(1,n-1);
70+
cout<<endl;
71+
}
72+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /