-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdijkstras.c3
74 lines (63 loc) · 1.71 KB
/
dijkstras.c3
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
module dijkstras(<Type>);
import std::collections;
def Tree = HashMap(<Type, Type>);
struct Graph {
List(<Type>) verts;
HashMap(<Type, List(<Type>)>) edges;
HashMap(<Type[2], int>) weights;
}
fn Tree tree(Graph g, Type start)
{
List(<Type>) traversed = *traversed.temp_init();
HashMap(<Type, int>) delta;
Tree prev = *prev.temp_init();
foreach(vert: g.verts) {
delta[vert] = int.max;
prev[vert] = MapResult.NOT_FOUND?;
}
delta[start]! = 0;
while (!traversed.cmp(&graph.verts)) {
Type vert;
int min = int.max;
delta.@each(; Type t, int i) {
if (!traversed.contains(t)) {
if (i < min) {
vert = t;
}
}
};
if(g.edges.has_key(vert)) {
List(<Type>) edges = g.edges[vert]!;
foreach(edge: edges) {
if (!traversed.contains(edge)) {
int newPath = delta[vert] + g.weights[Type[2]{vert, edge}];
if (newPath < delta[edge]) {
delta[edge] = newPath;
prev[edge] = vert;
}
}
}
}
traversed.push(vert);
}
return prev;
}
fn bool List(<Type>).cmp(&self, List(<Type>)* other)
{
foreach (t: self) {
if (!other.contains(&t)) return false;
}
return true;
}
fn List(<Type>) search(HashMap(<Type, Type>) tree, Type start, Type end)
{
// change to try
if (!tree[end]!) {
List(<Type>) list = *list.temp_init();
list.push(end);
return list;
}
List(<Type>) next = search(tree, start, tree[end]!)!;
next.push(end);
return next;
}