forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraverse.ml
executable file
·365 lines (322 loc) · 10.1 KB
/
traverse.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
(**************************************************************************)
(* *)
(* Ocamlgraph: a generic graph library for OCaml *)
(* Copyright (C) 2004-2010 *)
(* Sylvain Conchon, Jean-Christophe Filliatre and Julien Signoles *)
(* *)
(* This software is free software; you can redistribute it and/or *)
(* modify it under the terms of the GNU Library General Public *)
(* License version 2.1, with the special exception on linking *)
(* described in file LICENSE. *)
(* *)
(* This software is distributed in the hope that it will be useful, *)
(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *)
(* *)
(**************************************************************************)
(* Graph traversal *)
module type G = sig
val is_directed : bool
type t
module V : Sig.COMPARABLE
val iter_vertex : (V.t -> unit) -> t -> unit
val fold_vertex : (V.t -> 'a -> 'a) -> t -> 'a -> 'a
val iter_succ : (V.t -> unit) -> t -> V.t -> unit
val fold_succ : (V.t -> 'a -> 'a) -> t -> V.t -> 'a -> 'a
end
(* depth-first search *)
module Dfs(G : G) = struct
module H = Hashtbl.Make(G.V)
let iter ?(pre=fun _ -> ()) ?(post=fun _ -> ()) g =
let h = H.create 97 in
let rec visit v =
if not (H.mem h v) then begin
H.add h v ();
pre v;
G.iter_succ visit g v;
post v
end
in
G.iter_vertex visit g
let postfix post g = iter ~post g
let iter_component ?(pre=fun _ -> ()) ?(post=fun _ -> ()) g v =
let h = H.create 97 in
let rec visit v =
H.add h v ();
pre v;
G.iter_succ (fun w -> if not (H.mem h w) then visit w) g v;
post v
in
visit v
let postfix_component post g = iter_component ~post g
let has_cycle_undirected g =
let h = H.create 97 in
let father = H.create 97 in
let is_father u v = (* u is the father of v in the DFS descent *)
try G.V.equal (H.find father v) u with Not_found -> false
in
let rec visit v =
H.add h v true;
G.iter_succ
(fun w ->
try if H.find h w && not (is_father w v) then raise Exit
with Not_found -> H.add father w v; visit w)
g v;
H.remove father v;
H.replace h v false
in
try G.iter_vertex (fun v -> if not (H.mem h v) then visit v) g; false
with Exit -> true
module Tail = struct
let has_cycle g =
let h = H.create 97 in
let stack = Stack.create () in
let loop () =
while not (Stack.is_empty stack) do
let v = Stack.top stack in
if H.mem h v then begin
(* we are now done with node v *)
(* assert (H.find h v = true); *)
H.replace h v false;
ignore (Stack.pop stack)
end else begin
(* we start DFS from node v *)
H.add h v true;
G.iter_succ
(fun w ->
try if H.find h w then raise Exit
with Not_found -> Stack.push w stack)
g v;
end
done
in
try
G.iter_vertex
(fun v ->
if not (H.mem h v) then begin Stack.push v stack; loop () end)
g;
false
with Exit ->
true
let has_cycle_undirected g =
let h = H.create 97 in
let father = H.create 97 in
let is_father u v = (* u is the father of v in the DFS descent *)
try G.V.equal (H.find father v) u with Not_found -> false
in
let stack = Stack.create () in
let loop () =
while not (Stack.is_empty stack) do
let v = Stack.top stack in
if H.mem h v then begin
(* we are now done with node v *)
(* assert (H.find h v = true); *)
H.remove father v;
H.replace h v false;
ignore (Stack.pop stack)
end else begin
(* we start DFS from node v *)
H.add h v true;
G.iter_succ
(fun w ->
try if H.find h w && not (is_father w v) then raise Exit
with Not_found -> H.add father w v; Stack.push w stack)
g v;
end
done
in
try
G.iter_vertex
(fun v ->
if not (H.mem h v) then begin Stack.push v stack; loop () end)
g;
false
with Exit ->
true
let has_cycle g =
if G.is_directed then has_cycle g else has_cycle_undirected g
let iter f g =
let h = H.create 97 in
let stack = Stack.create () in
let loop () =
while not (Stack.is_empty stack) do
let v = Stack.pop stack in
if not (H.mem h v) then begin
H.add h v ();
f v;
G.iter_succ
(fun w -> if not (H.mem h w) then Stack.push w stack) g v
end
done
in
G.iter_vertex
(fun v ->
if not (H.mem h v) then begin Stack.push v stack; loop () end)
g
let iter_component f g v0 =
let h = H.create 97 in
let stack = Stack.create () in
Stack.push v0 stack;
while not (Stack.is_empty stack) do
let v = Stack.pop stack in
if not (H.mem h v) then begin
H.add h v ();
f v;
G.iter_succ (fun w -> if not (H.mem h w) then Stack.push w stack) g v
end
done
end
let prefix = Tail.iter
let has_cycle = Tail.has_cycle
let prefix_component = Tail.iter_component
(* step-by-step iterator *)
module S = Set.Make(G.V)
type iterator = S.t * G.V.t list * G.t
(** (h, st, g) where h is the set of marked vertices and st the stack
invariant: the first element of st is not in h i.e. to be visited *)
let start g =
let st = G.fold_vertex (fun v st -> v :: st) g [] in
S.empty, st, g
let get (_,st,_) = match st with
| [] -> raise Exit
| v :: _ -> v
let step (s,st,g) = match st with
| [] ->
raise Exit
| v :: st ->
let s' = S.add v s in
let st' = G.fold_succ (fun w st -> w :: st) g v st in
let rec clean = function
| w :: st when S.mem w s' -> clean st
| st -> st
in
(s', clean st', g)
end
(* breadth-first search *)
module Bfs(G : G) = struct
module H = Hashtbl.Make(G.V)
let fold f i (g : G.t) =
let h = H.create 97 in
let q = Queue.create () in
(* invariant: [h] contains exactly the vertices which have been pushed *)
let push v =
if not (H.mem h v) then begin H.add h v (); Queue.add v q end
in
let rec loop s =
if not (Queue.is_empty q) then
let v = Queue.pop q in
let ns = f v s in (* Sticking to OCamlGraph's fold conv *)
G.iter_succ push g v;
loop ns
else
s
in
G.fold_vertex (fun v s -> push v; loop s) g i
let iter f = fold (fun v () -> f v) ()
let fold_component f i g v0 =
let h = H.create 97 in
let q = Queue.create () in
(* invariant: [h] contains exactly the vertices which have been pushed *)
let push v =
if not (H.mem h v) then begin H.add h v (); Queue.add v q end
in
push v0;
let rec loop s =
if not (Queue.is_empty q) then
let v = Queue.pop q in
let ns = f v s in
G.iter_succ push g v;
loop ns
else
s
in
loop i
let iter_component f = fold_component (fun v () -> f v) ()
(* step-by-step iterator *)
(* simple, yet O(1)-amortized, persistent queues *)
module Q = struct
type 'a t = 'a list * 'a list
exception Empty
let empty = [], []
let is_empty = function [], [] -> true | _ -> false
let push x (i,o) = (x :: i, o)
let pop = function
| i, y :: o -> y, (i,o)
| [], [] -> raise Empty
| i, [] -> match List.rev i with
| x :: o -> x, ([], o)
| [] -> assert false
let peek q = fst (pop q)
end
module S = Set.Make(G.V)
(* state is [(s,q,g)] : [s] contains elements never been pushed in [q] *)
type iterator = S.t * G.V.t Q.t * G.t
let start g =
let s = G.fold_vertex S.add g S.empty in
s, Q.empty, g
let get (s,q,_) =
if Q.is_empty q then
if S.is_empty s then raise Exit else S.choose s
else
Q.peek q
let step (s,q,g) =
let push v (s,q as acc) =
if S.mem v s then
S.remove v s, Q.push v q
else
acc
in
let v,s',q' =
if Q.is_empty q then begin
if S.is_empty s then raise Exit;
let v = S.choose s in
v, S.remove v s, q
end else
let v,q' = Q.pop q in
v, s, q'
in
let s'',q'' = G.fold_succ push g v (s',q') in
(s'',q'',g)
end
(* Graph traversal with marking. *)
module type GM = sig
type t
module V : sig type t end
val iter_vertex : (V.t -> unit) -> t -> unit
val iter_succ : (V.t -> unit) -> t -> V.t -> unit
module Mark : sig
val clear : t -> unit
val get : V.t -> int
val set : V.t -> int -> unit
end
end
module Mark(G : GM) = struct
let dfs g =
G.Mark.clear g;
let n = ref 0 in
let rec visit v =
if G.Mark.get v = 0 then begin
incr n;
G.Mark.set v !n;
G.iter_succ visit g v
end
in
G.iter_vertex visit g
(* invariant: [h v = 0] means not visited at all; [h v = 1] means
already visited in the current component; [h v = 2] means
already visited in another tree *)
let has_cycle g =
G.Mark.clear g;
let rec visit v =
G.Mark.set v 1;
G.iter_succ
(fun w ->
let m = G.Mark.get w in
if m = 1 then raise Exit;
if m = 0 then visit w)
g v;
G.Mark.set v 2
in
try G.iter_vertex (fun v -> if G.Mark.get v = 0 then visit v) g; false
with Exit -> true
end