-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.ml
80 lines (63 loc) · 2.13 KB
/
graph.ml
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
open Printf
module NeighborSet = Set.Make (String)
type neighborst = NeighborSet.t
module Graph = Map.Make (String)
type grapht = neighborst Graph.t
module StringSet = Set.Make(String)
type livet = StringSet.t
let empty : grapht = Graph.empty
let add_node (g : grapht) (name : string) : grapht =
if Graph.mem name g then g else Graph.add name NeighborSet.empty g
;;
let add_directed_edge (g : grapht) (n1 : string) (n2 : string) : grapht =
let g' = add_node (add_node g n1) n2 in
let curr_neighbors = Graph.find n1 g' in
Graph.add n1 (NeighborSet.add n2 curr_neighbors) g'
;;
let add_edge (g : grapht) (n1 : string) (n2 : string) : grapht =
let g' = add_directed_edge g n1 n2 in
add_directed_edge g' n2 n1
;;
let connect_with_rest (g : grapht) (nodes : StringSet.t) (node : string) : grapht =
StringSet.fold
(fun (subitem : string) (acc : grapht) ->
if subitem = node
then acc
else add_edge acc node subitem)
nodes
g
;;
let connect_all (g : grapht) (nodes : StringSet.t) : grapht =
StringSet.fold
(fun (item : string) (acc : grapht) ->
let acc = add_node acc item in
connect_with_rest acc nodes item
) nodes g
;;
let graph_union (g1 : grapht) (g2 : grapht) : grapht =
Graph.fold (fun (from : string) (to_ss : neighborst) (acc : grapht) ->
match Graph.find_opt from acc with
| None -> Graph.add from to_ss acc
| Some(neighbors) ->
Graph.add from (NeighborSet.union to_ss neighbors) acc)
g2
g1
;;
let get_neighbors (g : grapht) (name : string) : string list =
if Graph.mem name g
then NeighborSet.fold (fun n ns -> n :: ns) (Graph.find name g) []
else []
;;
let get_vertices (g : grapht) : string list =
let keys, _ = List.split (Graph.bindings g) in keys
;;
let string_of_stringset (s : StringSet.t) : string =
let elems = StringSet.elements s in
ExtString.String.join ", " elems
;;
let string_of_graph (g: grapht): string =
let string_of_neighbors (n: string): string =
ExtString.String.join ", " (get_neighbors g n)
in
ExtString.String.join "\n" (List.map (fun k -> sprintf "%s: %s" k (string_of_neighbors k)) (get_vertices g))
;;