-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDijkstras.c
119 lines (97 loc) · 1.82 KB
/
Dijkstras.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
#include<stdio.h>
#include<conio.h>
struct Node
{
int point, dis, next_point, label;
}node[50];
int n, temp, dis_mat[100][100], row, row1, min, last, path[100], first;
void input();
int main()
{
int i, j, k;
input();
for(i=0; i<n; i++)
{
if (node[i].point == temp)
row = i;
}
for(j=0; j<n; j++)
{
node[j].dis = dis_mat[row][j];
node[j].next_point = node[row].point;
}
node[row].label = 1;
do
{
for(i=0; i<n; i++)
{
if(node[i].label != 1)
{
if (node[i].dis <= node[row1].dis + dis_mat[row][i])
node[i].dis = node[i].dis;
else
{
node[i].dis = node[row].dis + dis_mat[row][i];
node[i].next_point = node[row1].point;
}
}
}
min = 999;
for(i=0; i<n; i++)
{
if (min > node[i].dis && node[i].label != 1)
{
min = node[i].dis;
row1 = i;
}
}
node[row1].label = 1;
temp = node[row1].point;
for(j=0; j<n; j++)
{
if (node[j].point == temp)
row = j;
}
}while(temp != last);
printf("\nShortest distance = %d", node[row1].dis);
i = 0;
do
{
path[i] = last;
for(j=0; j<n; j++)
{
if (node[j].point == last)
row = j;
}
last = node[row].next_point;
i++;
}while(last != first);
printf("\nFirst ");
i--;
for(j=i; j>=0; j--)
printf("\n%d", path[j]);
getch();
return 0;
}
void input()
{
int i, j;
printf("\nEnter no. of nodes: ");
scanf("%d", &n);
printf("\nEnter Label of nodes: ");
for(i=0; i<n; i++)
scanf("%d", &node[i].point);
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
printf("\nDistance between nodes %d %d : ", i+1, j+1);
scanf("%d", &dis_mat[i][j]);
}
}
printf("\nFirst Node: ");
scanf("%d", &first);
temp = first;
printf("\nLast Node: ");
scanf("%d", &last);
}