-
Notifications
You must be signed in to change notification settings - Fork 0
/
dijkastra.c
59 lines (59 loc) · 1.36 KB
/
dijkastra.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
/*dijkastra*/
#include <stdio.h>
void dijkstra(int G[100][100], int n, int source);
void relax(int v, int u, int w[100][100], int dist[]);
int extract_min(int q[], int dist[], int n);
void dijkstra(int G[100][100], int n, int source) {
int distance[100];
int visited[100];
for (int i = 0; i < n; i++) {
distance[i] = 99999;
visited[i] = 0;
}
distance[source] = 0;
for (int i = 0; i < n; i++) {
int u = extract_min(visited, distance, n);
visited[u] = 1;
for (int v = 0; v < n; v++) {
if (!visited[v] && G[u][v] != -1) {
relax(v, u, G, distance);
}
}
}
printf("Vertex \t Distance from Source\n");
for (int i = 0; i < n; i++) {
printf("%d \t %d\n", i, distance[i]);
}
}
void relax(int v, int u, int w[100][100], int dist[]) {
if (w[u][v] + dist[u] < dist[v]) {
dist[v] = w[u][v] + dist[u];
}
}
int extract_min(int visited[], int dist[], int n) {
int min = 99999;
int min_index = -1;
for (int v = 0; v < n; v++) {
if (!visited[v] && dist[v] < min) {
min = dist[v];
min_index = v;
}
}
return min_index;
}
int main() {
int G[100][100];
int n, source;
printf("Enter the number of vertices: ");
scanf("%d", &n);
printf("Enter the adjacency matrix (%d x %d):\n", n, n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &G[i][j]);
}
}
printf("Enter the source vertex: ");
scanf("%d", &source);
dijkstra(G, n, source);
return 0;
}