forked from Pranav016/Hacktoberfest-PR-for-beginners
-
Notifications
You must be signed in to change notification settings - Fork 11
/
dijkstra.cpp
123 lines (110 loc) · 2.62 KB
/
dijkstra.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
113
114
115
116
117
118
119
120
121
122
123
#include<iostream>
using namespace std;
#define infinity 100000
////////////////
void create(int *a,int n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>*(a+i*n+j);
}
}
}
//////////////
void print(int *a,int n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<*(a+i*n+j)<<" ";
}
cout<<endl;
}
}
///////////////////////
void dijkstra(int *a,int n,int s)
{
int i,j;
int cost[n][n];
for(i=0;i<n;i++)//assigning value to cost to each edge of a graph
{
for(j=0;j<n;j++)
{
if(*(a+i*n+j)==0)
cost[i][j]=infinity;
else
{
cost[i][j]=*(a+i*n+j);
}
}
}
int dist[n],visit[n]={0},prev[n];
for(i=0;i<n;i++)//finding edge that are directly connected with source vertex
{
dist[i]=cost[s][i];
prev[i]=s;
visit[i]=0;
}
dist[s]=0;//assigning distance of source vertex 0
visit[s]=1;//declaring we have already visited source vertex
int coun=1;
int nextnode,mindist;
while(coun<n-1)
{
mindist=infinity;//assigning value to min distance infinity
for(i=0;i<n;i++)//finding min distance of vertex and assigning it as next source vertex
{
if(mindist>dist[i]&&visit[i]!=1)
{
mindist=dist[i];
nextnode=i;
}
}
visit[nextnode]=1;//declaring we have already visited next vertex
for(i=0;i<n;i++)
{
if(mindist+cost[nextnode][i]<dist[i]&&visit[i]!=1)//relaxing each vertex which are relaxable
{
dist[i]=mindist+cost[nextnode][i];
prev[i]=nextnode;//assigning value of next node which show that using next node vertex given vertex is relaxed
}
}
coun++;
}
cout<<"************graph traversal with minimum weight*************"<<endl;
for(i=0;i<n;i++)
{
if(i!=s)
{
cout<<"distnce of node "<<i<<" "<<dist[i];
cout<<" path "<<i;
int j=i;
do
{
j=prev[j];
cout<<"-> "<<j;
}while(j!=s);
}
cout<<endl;
}
}
////////////////////
int main()
{
int n;
cout<<"enter vertices"<<endl;
cin>>n;
int a[n][n];
create((int *)a,n);
print((int *)a,n);
int s;
cout<<"enter source vertices"<<endl;
cin>>s;
dijkstra((int *)a,n,s);
return 0;
}