-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDIJ.CPP
More file actions
181 lines (145 loc) · 2.83 KB
/
DIJ.CPP
File metadata and controls
181 lines (145 loc) · 2.83 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include<stdio.h>
#include<stdlib.h>
/*
1.array u maintain the list of all visited vertices
2.array prev maintains the immediate parent of each vertex
3.array tag maintains the list of all the vertices that are appended in the spanning tree
4.dist matrix maintains the CURRENT minimum distance of all the vertices from the given source
5.vertices numbering start with 0
*/
int v,e,s,**d,infinity=999,*dist,*u,*prev,k,*tag;
/*function to find the least cost vertex among all the visited vertices*/
int minimum()
{
int min,i,index=-1;
min=999;
for(i=0;i<v;i++)
{
if(dist[i]< min && tag[i]!=1)
{
min=dist[i];
index=i;
}
}
return(index);
}
/*recursive function to update the distance matrix*/
void dijkstra ( int p )
{
int j=0,i,k,m=0,min;
for(i=0;i<v;i++)
{
if(i!=p && tag[i]!=1)
{
if(d[p][i] < infinity)
{
u[i]=1;
}
}
}
for(k=0; k < v; k++)
{
if(u[k]==1)
{
if(dist[p]+d[k][p] < dist[k])
{
dist[k]=dist[p]+d[k][p];
prev[k]=p;
}
}
}
min=minimum();
if(min!=-1)
{
tag[min]=1;
dijkstra(min);
}
}
void print()
{
int i=0,j;
for(i=0;i<v;i++)
{ j=i;
printf("%d<--",i+1);
while(prev[j]!=-1)
{
printf("%d<--",prev[j]+1);
j=prev[j];
}
printf("=%d\n",dist[i]);
}
}
int main()
{
int i,j,x,y;
printf("Enter the number of vertices\n");
scanf("%d",&v);
u=(int*) malloc (v*sizeof(int));
for(i=0;i<v;i++)
u[i]=-1;
prev=(int*) malloc (v*sizeof(int));
for(i=0;i<v;i++)
prev[i]=-1;
dist=(int*) malloc (v*sizeof(int));
tag=(int*) malloc (v*sizeof(int));
for(i=0;i<v;i++)
tag[i]=0;
/*adjacency matrix declaration*/
d = (int**) malloc(v*sizeof(int*));
for (i = 0; i < v; i++)
d[i] = (int*) malloc(v*sizeof(int));
/*Initialize matrix*/
for(i=0;i<v;i++)
{
for(j=0;j<v;j++)
{
if(i==j)
d[i][j]=0;
else
d[i][j]=999;
}
}
/*Enter cost for each edge present*/
printf("Enter the number of edges\n");
scanf("%d",&e);
for(i=0;i<e;i++)
{
printf("Enter edge(u,v)");
scanf("%d %d",&x,&y);
printf("Enter its cost\n");
scanf("%d",&d[x-1][y-1]);
d[y-1][x-1]=d[x-1][y-1];
}
/*read cost adjacency matrix*//*
printf("Enter the adjacency matrix, type 999 for infinity and 0 for d[i][i]\n");
for(i=0;i<v;i++)
for(j=0;j<v;j++)
scanf("%d",&d[i][j]);
/*end read*/
/*print matrix*/
for(i=0;i<v;i++)
{
for(j=0;j<v;j++)
printf("%d\t",d[i][j]);
printf("\n");
}
printf("Enter the source vertex\n");
scanf("%d",&s);
s=s-1;
/*put source in the spanning tree*/
tag[s]=1;
/*assign initial values to vertices in the dist matrix*/
for(i=0;i<v;i++)
{
if(i!=s)
dist[i]=999;
else dist[i]=0;
}
dijkstra(s);
print();
/*
for(i=0;i<v;i++)
printf("%d", dist[i]);
*/
return(0);
}