-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphs.fold
More file actions
33 lines (22 loc) · 854 Bytes
/
Graphs.fold
File metadata and controls
33 lines (22 loc) · 854 Bytes
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
|| Inductive Graph
type Graph A = Empty | Edge A & Graph A
let neighbors g a cond =
let edge l (b,c) = if b = a && cond c then c :: l
else if c = a && cond b then b :: l
else l in
List.fold_left edge [] g.edges
let rec list_path g a to_b = match to_b with
| [] -> assert false (* [to_b] contains the path to [b]. *)
| a' :: _ ->
if a' = a then [to_b]
else
let n = neighbors g a' (fun c -> not(List.mem c to_b)) in
List.concat(List.map (fun c -> list_path g a (c :: to_b)) n)
let paths g a b =
assert(a <> b);
list_path g a [b];;
neighbors g a cond =
fold edge [] g.edges where
edge l (b, c) | b == a and cond c = [c, l...]
| c == a and cond b = [b, l...]
| otherwise = l