-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathp83.re
34 lines (31 loc) · 792 Bytes
/
p83.re
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
/* Construct all spanning trees. */
type graph('a) = {
nodes: list('a),
edges: list(('a, 'a))
};
let split = (v, tvs, es) =>
List.partition(((x, y)) => List.mem(x, tvs) && y == v || List.mem(y, tvs) && x == v, es);
let add_edge = (e, list) =>
switch list {
| [] => [[e]]
| es => List.rev_map((x) => [e, ...x], es)
};
let s_tree = (g) => {
let rec collect = (acc, tvs, es, list) =>
switch list {
| [] => acc
| [v, ...tl] =>
let (ps, qs) = split(v, tvs, es);
if (ps == []) {
[]
} else {
collect(
List.fold_left((new_acc, p) => List.rev_append(add_edge(p, acc), new_acc), [], ps),
[v, ...tvs],
qs,
tl
)
}
};
collect([], [List.hd(g.nodes)], g.edges, List.tl(g.nodes))
};