package owl-base

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file owl_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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# 1 "src/base/core/owl_graph.ml"
(*
 * OWL - OCaml Scientific and Engineering Computing
 * Copyright (c) 2016-2019 Liang Wang <liang.wang@cl.cam.ac.uk>
 *)


type 'a node = {
  mutable id   : int;            (* unique identifier *)
  mutable name : string;         (* name of the node *)
  mutable prev : 'a node array;  (* parents of the node *)
  mutable next : 'a node array;  (* children of the node *)
  mutable attr : 'a;             (* indicate the validity *)
}

type order = BFS | DFS

type traversal = PreOrder | PostOrder

type dir = Ancestor | Descendant


let _global_id = ref 0


let id x = x.id


let name x = x.name


let set_name x s = x.name <- s


let parents x = x.prev


let set_parents x parents = x.prev <- parents


let children x = x.next


let set_children x children = x.next <- children


let indegree x = Array.length x.prev


let outdegree x = Array.length x.next


let degree x = Array.(length x.prev + length x.next)


let attr x = x.attr


let set_attr x a = x.attr <- a


let node ?id ?(name="") ?(prev=[||]) ?(next=[||]) attr =
  let id = match id with
    | Some i -> i
    | None   -> (
        _global_id := !_global_id + 1;
        !_global_id
      )
  in
  { id; name; prev; next; attr }


let connect parents children =
  Array.iter (fun parent ->
    parent.next <- Array.append parent.next children;
    Array.iter (fun child ->
      child.prev <- Array.append child.prev parents
    ) children
  ) parents


let connect_descendants parents children =
  Array.iter (fun parent ->
    parent.next <- Array.append parent.next children
  ) parents


let connect_ancestors parents children =
  Array.iter (fun child ->
    child.prev <- Array.append child.prev parents
  ) children


let remove_node x =
  let f = fun y -> y.id <> x.id in
  Array.iter (fun parent ->
    parent.next <- Owl_utils.Array.filter f parent.next
  ) x.prev;
  Array.iter (fun child ->
    child.prev <- Owl_utils.Array.filter f child.prev
  ) x.next


let remove_edge src dst =
  src.next <- Owl_utils.Array.filter (fun x -> x.id <> dst.id) src.next;
  dst.prev <- Owl_utils.Array.filter (fun x -> x.id <> src.id) dst.prev


let replace_child child_0 child_1 =
  Array.iter (fun parent ->
    let next = Array.map (fun v ->
      if v.id = child_0.id then child_1 else v
    ) parent.next
    in
    parent.next <- next;
  ) child_0.prev


let replace_parent parent_0 parent_1 =
  Array.iter (fun child ->
    let prev = Array.map (fun v ->
      if v.id = parent_0.id then parent_1 else v
    ) child.prev
    in
    child.prev <- prev;
  ) parent_0.next


(* depth-first search from [x]; [f : node -> unit] is applied to each node;
   [next node -> node array] returns the next set of nodes to iterate;
*)
let dfs_iter traversal f x next =
  let h = Hashtbl.create 512 in
  let rec _dfs_iter y =
    if not (Hashtbl.mem h y.id) then (
      Hashtbl.add h y.id None;
      update y;
    )
  and relax y =
    Array.iter (fun z -> _dfs_iter z) (next y)
  and update y = match traversal with
    | PreOrder -> f y; relax y
    | PostOrder -> relax y; f y
  in
  Array.iter _dfs_iter x


(* breadth-first search from [x]; [f : node -> unit] is applied to each node;
   [next node -> node array] returns the next set of nodes to iterate.
*)
let bfs_iter traversal f x next =
  match traversal with
  | PostOrder -> Owl_log.warn "PostOrder BFS not implemented. PreOrder is used."
  | PreOrder  -> ();
  let h = Hashtbl.create 512 in
  let q = Queue.create () in
  let relax y =
    Array.iter (fun z ->
      if not (Hashtbl.mem h z.id) then (
        Hashtbl.add h z.id None;
        Queue.push z q
      )
    ) (next y)
  in
  let update y = f y; relax y in

  Array.iter (fun y -> Queue.push y q) x;
  Array.iter (fun y -> Hashtbl.add h y.id None) x;
  while not (Queue.is_empty q) do
    let y = Queue.pop q in
    update y
  done


let iter_ancestors ?(order=DFS) ?(traversal=PreOrder) f x =
  match order with
  | BFS -> bfs_iter traversal f x parents
  | DFS -> dfs_iter traversal f x parents


let iter_descendants ?(order=DFS) ?(traversal=PreOrder) f x =
  match order with
  | BFS -> bfs_iter traversal f x children
  | DFS -> dfs_iter traversal f x children


let _iter ?(dir=Ancestor) ?order ?traversal f x =
  match dir with
  | Ancestor   -> iter_ancestors ?order ?traversal f x
  | Descendant -> iter_descendants ?order ?traversal f x


let filter_ancestors f x =
  let s = Owl_utils.Stack.make () in
  iter_ancestors (fun n -> if f n then Owl_utils.Stack.push s n) x;
  Owl_utils.Stack.to_array s


let filter_descendants f x =
  let s = Owl_utils.Stack.make () in
  iter_descendants (fun n -> if f n then Owl_utils.Stack.push s n) x;
  Owl_utils.Stack.to_array s


let fold_ancestors f a x =
  let a = ref a in
  iter_ancestors (fun b -> a := f !a b) x;
  !a


let fold_descendants f a x =
  let a = ref a in
  iter_descendants (fun b -> a := f !a b) x;
  !a


let iter_in_edges ?order f x =
  iter_ancestors ?order (fun dst ->
    Array.iter (fun src -> f src dst) dst.prev
  ) x


let iter_out_edges ?order f x =
  iter_descendants ?order (fun src ->
    Array.iter (fun dst -> f src dst) src.next
  ) x


let fold_in_edges f a x =
  let a = ref a in
  iter_in_edges (fun b c -> a := f !a b c) x;
  !a


let fold_out_edges f a x =
  let a = ref a in
  iter_out_edges (fun b c -> a := f !a b c) x;
  !a


(* TODO *)
let _map _f _x = None


(* TODO: optimise *)
let copy ?(dir=Ancestor) x =
  let _make_if_not_exists h n =
    if Hashtbl.mem h n.id = true then Hashtbl.find h n.id
    else (
      let n' = node ~id:n.id ~name:n.name ~prev:[||] ~next:[||] n.attr in
      Hashtbl.add h n'.id n';
      n'
    )
  in
  let h = Hashtbl.create 128 in
  let _copy src dst =
    let src' = _make_if_not_exists h src in
    let dst' = _make_if_not_exists h dst in
    connect [|src'|] [|dst'|]
  in
  let _ = match dir with
    | Ancestor   -> iter_in_edges _copy x
    | Descendant -> iter_out_edges _copy x
  in
  Array.map (fun n -> Hashtbl.find h n.id) x


(* TODO *)
let _to_array = None

(* TODO *)
let _to_hashtbl = None


let num_ancestor x =
  let n = ref 0 in
  iter_ancestors (fun _ -> n := !n + 1) x;
  !n


let num_descendant x =
  let n = ref 0 in
  iter_descendants (fun _ -> n := !n + 1) x;
  !n


let length x = (num_ancestor x) + (num_descendant x) - (Array.length x)


let node_to_str x =
  Printf.sprintf "[ #%i %s in:%i out:%i ]" x.id x.name (indegree x) (outdegree x)


let pp_node formatter x =
  Format.open_box 0;
  Format.fprintf formatter "%s" (node_to_str x);
  Format.close_box ()


let to_string from_root x =
  let s = ref "" in
  let iter_fun = if from_root then iter_out_edges else iter_in_edges in
  iter_fun (fun u v -> s := Printf.sprintf "%s%i -> %i\n" !s u.id v.id) x;
  !s


let topo_sort nodes =
  let s = Owl_utils.Stack.make () in
  let f u = Owl_utils.Stack.push s u in
  iter_ancestors ~order:DFS ~traversal:PostOrder f nodes;
  Owl_utils_stack.to_array s


(* ends here *)
OCaml

Innovation. Community. Security.