-
Notifications
You must be signed in to change notification settings - Fork 0
/
dijkstra.c
69 lines (56 loc) · 1.55 KB
/
dijkstra.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
#include <stdio.h>
#include <limits.h>
#define MAX 100
#define INF INT_MAX
int graph[MAX][MAX], dist[MAX], visited[MAX], n;
int minDistance() {
int min = INF, minIndex;
for (int v = 0; v < n; v++) {
if (!visited[v] && dist[v] <= min) {
min = dist[v];
minIndex = v;
}
}
return minIndex;
}
void dijkstra(int src) {
for (int i = 0; i < n; i++) {
dist[i] = INF;
visited[i] = 0;
}
dist[src] = 0;
for (int count = 0; count < n - 1; count++) {
int u = minDistance();
visited[u] = 1;
for (int v = 0; v < n; v++) {
if (!visited[v] && graph[u][v] && dist[u] != INF && dist[u] + graph[u][v] < dist[v]) {
dist[v] = dist[u] + graph[u][v];
}
}
}
}
void printSolution(int src) {
printf("Vertex\t Distance from Source %d\n", src);
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\n", i, dist[i]);
}
}
int main() {
int src;
printf("Enter the number of vertices: ");
scanf("%d", &n);
printf("Enter the adjacency matrix (enter 0 for no direct edge):\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &graph[i][j]);
if (graph[i][j] == 0 && i != j) {
graph[i][j] = INF; // For non-existing edges
}
}
}
printf("Enter the source vertex: ");
scanf("%d", &src);
dijkstra(src);
printSolution(src);
return 0;
}