-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8960 - Dijkstra's Algorithm.c
116 lines (94 loc) · 3.57 KB
/
8960 - Dijkstra's Algorithm.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
/**********************************************************************************************************
NAME: CANDIDA RUTH NORONHA
CLASS: SE COMPS B
ROLL NO. : 8960
BATCH: C
TITLE: DIJKSTRA'S ALGORITHM
SUBMISSION DATE : 25th MARCH, 2021
**********************************************************************************************************/
#include <stdio.h>
#define INFINITY 9999
#define MAX 10
void Dijkstra(int Graph[MAX][MAX], int n, int start)
{
int cost[MAX][MAX], distance[MAX], pred[MAX];
int visited[MAX], count, mindistance, nextnode, i, j;
// Creating cost matrix
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
if (Graph[i][j] == 0)
cost[i][j] = INFINITY;
else
cost[i][j] = Graph[i][j];
for (i = 0; i < n; i++)
{
distance[i] = cost[start][i];//initially distance is cost of the edge from source to that node
pred[i] = start;//predecessor is the source node
visited[i] = 0;//node not visited
}
//beginning from source node
distance[start] = 0;//distance of source node from itself is 0
visited[start] = 1;//mark source node as visited
count = 1;
while (count < n - 1)
{
mindistance = INFINITY;
for (i = 0; i < n; i++)
if (distance[i] < mindistance && !visited[i])
{
mindistance = distance[i];
nextnode = i;
}
visited[nextnode] = 1;
for (i = 0; i < n; i++)
if (!visited[i])
if (mindistance + cost[nextnode][i] < distance[i])
{
distance[i] = mindistance + cost[nextnode][i];
pred[i] = nextnode;
}
count++;
}
// Printing the distance
for (i = 0; i < n; i++)
if (i != start) {
printf("\nDistance from source to %d: %d", i, distance[i]);
}
printf("\n");
}
int main() {
int Graph[MAX][MAX], i, j, n, u;
printf("\n------------------------------------------------------------------------------------------------\n");
printf("\n Enter the number of nodes in the Graph : ");
scanf("%d",&n);
printf("\n------------------------------------------------------------------------------------------------\n");
printf("\n Enter the adjacency matrix of the Graph : \n");
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
scanf("%d",&Graph[i][j]);
printf("\n------------------------------------------------------------------------------------------------\n");
u = 0;
Dijkstra(Graph, n, u);
printf("\n------------------------------------------------------------------------------------------------\n");
return 0;
}
/**********************************************************************************************************
OUTPUT :
------------------------------------------------------------------------------------------------
Enter the number of nodes in the Graph : 6
------------------------------------------------------------------------------------------------
Enter the adjacency matrix of the Graph :
0 20 15 0 0 0
2 0 0 0 10 30
0 0 0 4 0 10
0 0 0 0 0 0
0 0 0 15 0 0
0 10 0 4 10 0
------------------------------------------------------------------------------------------------
Distance from source to 1: 20
Distance from source to 2: 15
Distance from source to 3: 19
Distance from source to 4: 30
Distance from source to 5: 25
------------------------------------------------------------------------------------------------
**********************************************************************************************************/