-
Notifications
You must be signed in to change notification settings - Fork 1
/
Dijkstra_algorithm_implementation.cpp
114 lines (93 loc) · 2.09 KB
/
Dijkstra_algorithm_implementation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <limits.h>
using namespace std;
int main() {
int sum=0,i,j,G[50][50],D[50],V[50],P[50],current,s,no_of_nodes;
cout<<"Enter number of nodes\n";
cin>>no_of_nodes;
int dest,source;
for(i=1;i<=no_of_nodes;i++)
{
D[i]=INT_MAX;
V[i]=0;
P[i]=0;
}
cout<<"\n\nEnter the values of edges for all vertices";
for(i=1;i<=no_of_nodes;i++)
{
for(j=1;j<=no_of_nodes;j++)
{
cout<<"\nEnter the value for G["<<i<<"]["<<j<<"] ";
cin>>G[i][j];
}
}
cout<<"\n\nEnter source and destination\n ";
cin>>source>>dest;
current=source;
D[current]=0;
V[current]=1;
int c=1;
while(c<=no_of_nodes)
{
int dc = D[current];
for(i=1;i<=no_of_nodes;i++)
{
if(G[current][i]!=0 && V[i]!=1)
{
if(G[current][i]+dc < D[i])
{
D[i]=G[current][i]+dc;
P[i]= current;
}
}
}
s=D[2];
for(i=3;i<=no_of_nodes;i++)
{
if(s>D[i] && V[i]!=1)
{
s=D[i];
current=i;
}
}
V[current]=1;
c++;
}
cout<<"\n The cost is "<<D[dest];
return 0;
}
/*
Input:
Enter number of nodes
5
Enter the values of edges for all vertices
Enter the value for G[1][1] 0
Enter the value for G[1][2] 22
Enter the value for G[1][3] 18
Enter the value for G[1][4] 3
Enter the value for G[1][5] 9
Enter the value for G[2][1] 22
Enter the value for G[2][2] 0
Enter the value for G[2][3] 1
Enter the value for G[2][4] 5
Enter the value for G[2][5] 8
Enter the value for G[3][1] 18
Enter the value for G[3][2] 1
Enter the value for G[3][3] 0
Enter the value for G[3][4] 0
Enter the value for G[3][5] 4
Enter the value for G[4][1] 3
Enter the value for G[4][2] 5
Enter the value for G[4][3] 0
Enter the value for G[4][4] 0
Enter the value for G[4][5] 2
Enter the value for G[5][1] 9
Enter the value for G[5][2] 8
Enter the value for G[5][3] 4
Enter the value for G[5][4] 2
Enter the value for G[5][5] 0
Enter source and destination
1
5
Output:
The cost is 8