-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dijkstra.c
117 lines (90 loc) · 1.57 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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_VERTEX 10
#define MAX_INT 100000
int graph[MAX_VERTEX][MAX_VERTEX];
bool visit[MAX_VERTEX];
int dist[MAX_VERTEX];
void Dijkstra(int st, int n) // IN: st : start vertex ; IN: n: number of vertex
{
int i,j,w;
for( i = 0; i < n; i++) // init
{
dist[i] = MAX_INT;
visit[i] = false;
}
dist[st] = 0;
while(true)
{
int u = -1;
int sd = MAX_INT;
for( i = 0; i < n; i++) // greedy for the min weight
{
if( !visit[i] && dist[i] < sd)
{
sd = dist[i];
u = i;
}
}
if( -1 == u)
break;
visit[u] = true;
for ( j = 0; j < n; j++) //update new weight
{
if( u == j)
continue;
int nw = graph[u][j] + dist[u];
if( nw < dist[j])
{
dist[j] = nw;
}
}
}
}
void Test()
{
memset(graph, 0, sizeof graph);
const int n = 4;
struct {
int data[n][n];
int st;
int end;
int result ;
} test_1[] =
{
0, 2, MAX_INT, 4,
2, 0, 3, MAX_INT,
MAX_INT, 3, 0, 2,
4, MAX_INT, 2, 0,
1,
3,
4
};
for( int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
graph[i][j] = test_1[0].data[i][j];
//printf("%d ",graph[i][j]);
}
//printf("\n");
}
//printf("\n");
Dijkstra(test_1[0].st - 1, n);
if( test_1[0].result == dist[test_1[0].end ] )
{
printf("PASS! \n");
printf("end = %d, result = %d\n",test_1[0].end,dist[test_1[0].end ]);
}
else
{
printf("FAILE! \n");
printf("end = %d, result = %d\n", test_1[0].end,dist[test_1[0].end ]);
}
}
int main()
{
Test();
return 0;
}