-
Notifications
You must be signed in to change notification settings - Fork 0
/
DIJKATRA.C
executable file
·154 lines (119 loc) · 2.61 KB
/
DIJKATRA.C
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
/*
Program Purpose : To Perform Dikastra's Shortest Path Algorithm.
Coded by : Devendra Walanj
Subject : Analysis of Algorithms
Semister : IV
*/
#include<stdio.h>
#include<conio.h>
int all(int x[],int v)
{
int i;
for(i=1;i<=v;i++)
if(x[i]==0)
return 0;
return 1;
}
void admat (int a[][10])
{
int i,j,e,w;
printf("\nEnter an Edge ?(0:no/1:yes) : ");
scanf("%d",&e);
while(e)
{
printf("\nEnter node number from & to :");
scanf("%d%d",&i,&j);
printf("\nEnter Weight of Edge : ");
scanf("%d",&w);
a[i][j] = w;
a[j][i] = w;
printf("Enter an Edge ?(0:no/1:yes) : ");
scanf("%d",&e);
}
}
void dijkastras(int a[][10],int v)
{
int d[10],p[10]={0},visit[10]={0};
int s,dest,curr,dc,i,x,l,min;
char ch = 'y';
for(i=0;i<10;i++)
d[i] = 32767;
printf("\nEnter source & destination node : ");
scanf("%d %d",&s,&dest);
curr = s;
d[s] = 0;
visit[s] = 1;
dc = d[curr];
while(!all(visit,v))
{
for(i=1;i<=v;i++)
{
if(a[curr][i]!=0)
if(visit[i]==0)
if(d[i]>a[curr][i]+dc)
{
d[i]=a[curr][i]+dc;
p[i]=curr;
}
}
min = 32767;
for(i=1;i<=v;i++)
{
if(visit[i]==0)
if(d[i]<min)
{
min = d[i];
curr = i;
}
}
dc = d[curr];
visit[curr] = 1;
}
do
{
printf("\nShortest Distance from %d to %d : ",s,dest);
printf("%d\n",d[dest]);
printf("Path = \n");
l = dest;
do
{
x = p[l];
printf("\nVertex %d connected to %d\n",l,x);
l = x;
}while(l!=s && s!=dest);
printf("Do you want to continue(y/n))? : ");
scanf("%c",&ch);
printf("\nEnter source and destination");
scanf("%d %d",&s,&dest);
}while(ch=='y');
}
void main()
{
int mat[10][10]={0};
int n = 0;
system("cls");
printf("Enter Total No. of Nodes : ");
scanf("%d",&n);
admat(mat);
dijkastras(mat,n);
getch();
}
OUTPUT :
Enter Total No. of Nodes : 4
Enter an Edge ?(0:no/1:yes) : 1
Enter node number from & to :1 4
Enter Weight of Edge : 100
Enter an Edge ?(0:no/1:yes) : 1
Enter node number from & to :1 3
Enter Weight of Edge : 100
Enter an Edge ?(0:no/1:yes) : 1
Enter node number from & to :2 3
Enter Weight of Edge : 56
Enter an Edge ?(0:no/1:yes) : 0
Enter source & destination node : 2 4
Shortest Distance from 2 to 4 : 256
Path =
Vertex 4 connected to 1
Vertex 1 connected to 3
Vertex 3 connected to 2
Do you want to continue(y/n))? : n