This repository has been archived by the owner on Jun 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 204
/
ograph_extended.ml
322 lines (264 loc) · 8.91 KB
/
ograph_extended.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
open Common
open Ocollection
open Oset
open Oassoc
(* open Ograph *)
open Oassocb
open Osetb
(*****************************************************************************)
(* Prelude *)
(*****************************************************************************)
(*
* An imperative directed polymorphic graph.
*
* todo?: prendre en parametre le type de finitemap et set?
* todo?: add_arc doit ramer, car del la key, puis add. Better to
* have a ref to a set?
*
* opti: graph with pointers and a tag visited => need keep global value
* visited_counter. check(that node is in, ...), display.
* opti: when the graph structure is stable, have a method compact, that
* transforms that in a matrix (assert that all number between 0 and
* free_index are used, or do some defrag-like-move/renaming).
*)
(*****************************************************************************)
(* Types *)
(*****************************************************************************)
type nodei = int
(*****************************************************************************)
(* Pure version *)
(*****************************************************************************)
class ['a,'b] ograph_extended =
let build_assoc () = new oassocb [] in (* opti?: = oassoch *)
let build_set () = new osetb Set_poly.empty in
object(o)
(* inherit ['a] ograph *)
val free_index = 0
val succ = build_assoc()
val pred = build_assoc()
val nods = build_assoc()
method add_node (e: 'a) =
let i = free_index in
({<
nods = nods#add (i, e);
pred = pred#add (i, build_set() );
succ = succ#add (i, build_set() );
free_index = i + 1;
>}, i)
method add_nodei i (e: 'a) =
({<
nods = nods#add (i, e);
pred = pred#add (i, build_set() );
succ = succ#add (i, build_set() );
free_index = (max free_index i) + 1;
>}, i)
method del_node (i) =
{<
(* check: e is effectively the index associated with e,
and check that already in *)
(* todo: assert that have no pred and succ, otherwise
* will have some dangling pointers
*)
nods = nods#delkey i;
pred = pred#delkey i;
succ = succ#delkey i;
>}
method replace_node (i, (e: 'a)) =
assert (nods#haskey i);
{<
nods = nods#replkey (i, e);
>}
method add_arc ((a,b),(v: 'b)) =
{<
succ = succ#replkey (a, (succ#find a)#add (b, v));
pred = pred#replkey (b, (pred#find b)#add (a, v));
>}
method del_arc ((a,b),v) =
{<
succ = succ#replkey (a, (succ#find a)#del (b,v));
pred = pred#replkey (b, (pred#find b)#del (a,v));
>}
method successors e = succ#find e
method predecessors e = pred#find e
method nodes = nods
method allsuccessors = succ
(*
method ancestors xs =
let rec aux xs acc =
match xs#view with (* could be done with an iter *)
| Empty -> acc
| Cons(x, xs) -> (acc#add x)
+> (fun newacc -> aux (o#predecessors x) newacc)
+> (fun newacc -> aux xs newacc)
in aux xs (f2()) (* (new osetb []) *)
method children xs =
let rec aux xs acc =
match xs#view with (* could be done with an iter *)
| Empty -> acc
| Cons(x, xs) -> (acc#add x)
+> (fun newacc -> aux (o#successors x) newacc)
+> (fun newacc -> aux xs newacc)
in aux xs (f2()) (* (new osetb []) *)
method brothers x =
let parents = o#predecessors x in
(parents#fold (fun acc e -> acc $++$ o#successors e) (f2()))#del x
*)
end
(*****************************************************************************)
(* Mutable version *)
(*****************************************************************************)
class ['a,'b] ograph_mutable =
let build_assoc () = new oassocb [] in
let build_set () = new osetb Set_poly.empty in
object(o)
val mutable free_index = 0
val mutable succ = build_assoc()
val mutable pred = build_assoc()
val mutable nods = build_assoc()
method add_node (e: 'a) =
let i = free_index in
nods <- nods#add (i, e);
pred <- pred#add (i, build_set() );
succ <- succ#add (i, build_set() );
free_index <- i + 1;
i
method add_nodei i (e: 'a) =
nods <- nods#add (i, e);
pred <- pred#add (i, build_set() );
succ <- succ#add (i, build_set() );
free_index <- (max free_index i) + 1;
method del_node (i) =
(* check: e is effectively the index associated with e,
and check that already in *)
(* todo: assert that have no pred and succ, otherwise
* will have some dangling pointers
*)
nods <- nods#delkey i;
pred <- pred#delkey i;
succ <- succ#delkey i;
method replace_node (i, (e: 'a)) =
assert (nods#haskey i);
nods <- nods#replkey (i, e);
method add_arc ((a,b),(v: 'b)) =
succ <- succ#replkey (a, (succ#find a)#add (b, v));
pred <- pred#replkey (b, (pred#find b)#add (a, v));
method del_arc ((a,b),v) =
succ <- succ#replkey (a, (succ#find a)#del (b,v));
pred <- pred#replkey (b, (pred#find b)#del (a,v));
method successors e = succ#find e
method predecessors e = pred#find e
method nodes = nods
method allsuccessors = succ
method nb_nodes =
nods#length
method nb_edges =
nods#fold (fun acc (i, e) ->
let children = o#successors i in
acc + children#cardinal
) 0
end
(*****************************************************************************)
(* API *)
(*****************************************************************************)
(* depth first search *)
let dfs_iter xi f g =
let already = Hashtbl.create 101 in
let rec aux_dfs xs =
xs +> List.iter (fun xi ->
if Hashtbl.mem already xi then ()
else begin
Hashtbl.add already xi true;
f xi;
let succ = g#successors xi in
aux_dfs (succ#tolist +> List.map fst);
end
) in
aux_dfs [xi]
let dfs_iter_with_path xi f g =
let already = Hashtbl.create 101 in
let rec aux_dfs path xi =
if Hashtbl.mem already xi then ()
else begin
Hashtbl.add already xi true;
f xi path;
let succ = g#successors xi in
let succ' = succ#tolist +> List.map fst in
succ' +> List.iter (fun yi ->
aux_dfs (xi::path) yi
);
end
in
aux_dfs [] xi
let generate_ograph_generic g label fnode filename =
Common.with_open_outfile filename (fun (pr,_) ->
pr "digraph misc {\n" ;
pr "size = \"10,10\";\n" ;
(match label with
None -> ()
| Some x -> pr (Printf.sprintf "label = \"%s\";\n" x));
let nodes = g#nodes in
nodes#iter (fun (k,node) ->
let (str,border_color,inner_color) = fnode (k, node) in
let color =
match inner_color with
None ->
(match border_color with
None -> ""
| Some x -> Printf.sprintf ", style=\"setlinewidth(3)\", color = %s" x)
| Some x ->
(match border_color with
None -> Printf.sprintf ", style=\"setlinewidth(3),filled\", fillcolor = %s" x
| Some x' -> Printf.sprintf ", style=\"setlinewidth(3),filled\", fillcolor = %s, color = %s" x x') in
(* so can see if nodes without arcs were created *)
pr (sprintf "%d [label=\"%s [%d]\"%s];\n" k str k color)
);
nodes#iter (fun (k,node) ->
let succ = g#successors k in
succ#iter (fun (j,edge) ->
pr (sprintf "%d -> %d;\n" k j);
);
);
pr "}\n" ;
);
()
let generate_ograph_xxx g filename =
with_open_outfile filename (fun (pr,_) ->
pr "digraph misc {\n" ;
pr "size = \"10,10\";\n" ;
let nodes = g#nodes in
nodes#iter (fun (k,(node, s)) ->
(* so can see if nodes without arcs were created *)
pr (sprintf "%d [label=\"%s [%d]\"];\n" k s k)
);
nodes#iter (fun (k,node) ->
let succ = g#successors k in
succ#iter (fun (j,edge) ->
pr (sprintf "%d -> %d;\n" k j);
);
);
pr "}\n" ;
);
()
let launch_gv_cmd filename =
let _status =
Unix.system ("dot " ^ filename ^ " -Tps -o " ^ filename ^ ".ps;") in
let _status = Unix.system ("gv " ^ filename ^ ".ps &")
in
(* zarb: I need this when I launch the program via eshell, otherwise gv
do not get the chance to be launched *)
Unix.sleep 1;
()
let print_ograph_extended g filename launchgv =
generate_ograph_xxx g filename;
if launchgv then launch_gv_cmd filename
let print_ograph_mutable g filename launchgv =
generate_ograph_xxx g filename;
if launchgv then launch_gv_cmd filename
let print_ograph_mutable_generic
?(title=None)
?(launch_gv = true)
?(output_file = "/tmp/ograph.dot")
~s_of_node
g =
generate_ograph_generic g title s_of_node output_file;
if launch_gv then launch_gv_cmd output_file