forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcs_m.ml
executable file
·188 lines (172 loc) · 6 KB
/
mcs_m.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
(**************************************************************************)
(* *)
(* 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. *)
(* *)
(**************************************************************************)
(* $Id: mcs_m.mli,v 1.2 2004-10-19 15:21:44 signoles Exp $ *)
module MaximalCardinalitySearch = struct
module WeightedV(V : Sig.COMPARABLE) = struct
include Util.DataV(struct type t = int end)(V)
let weight = data
let set_weight = set_data
end
module P(Gr : Sig.P) = struct
type edgelist = (Gr.V.t * Gr.V.t) list
module NewV = WeightedV(Gr.V)
module G = Persistent.Graph.Concrete(NewV)
module EdgeSet = Set.Make(G.E)
module VerticesSet = Set.Make(NewV)
module Choose = Oper.Choose(G)
module H = Hashtbl.Make(NewV)
let check_path g u v =
let h = H.create 97 in
let maxw = NewV.weight u in
let rec aux x : bool =
if H.mem h x then
false
else
if x = v then true
else
if NewV.weight x < maxw || x = u then
begin
H.add h x ();
G.fold_succ
(fun x found ->
if not found then aux x
else found)
g x false
end
else (H.add h x (); false)
in aux u
module Copy = Gmap.Vertex(Gr)(struct include G include Builder.P(G) end)
let fold f d =
let rec aux = function
(true, a) -> aux (f a)
| (false, a) -> a
in aux d
let mcsm g =
let g' = Copy.map (NewV.create 0) g in
let (_, _, ord, triang) =
fold
(fun ((i, g', a, f) as x)->
if i = 0 then (false, x)
else
let v =
G.fold_vertex
(fun x max ->
if NewV.weight x > NewV.weight max then x else max)
g' (ref 0, snd (Choose.choose_vertex g'))
in
let s =
G.fold_vertex
(fun x s ->
if x = v then s
else
if check_path g' x v then
VerticesSet.add x s
else s)
g' VerticesSet.empty
in
let f' =
VerticesSet.fold
(fun x f ->
NewV.set_weight x (succ (NewV.weight x));
if not (G.mem_edge g' x v) then
EdgeSet.add (x,v) f
else f)
s f
in
let g' = G.remove_vertex g' v in
let a' = (i, NewV.label v) :: a in
(true, (i - 1, g', a', f')))
(true, (Gr.nb_vertex g, g', [], EdgeSet.empty))
in
(List.rev ord,
EdgeSet.fold
(fun (x, y) e -> (NewV.label x, NewV.label y) :: e)
triang [])
let triangulate g =
let (_, triang) = mcsm g in
List.fold_left (fun g (x, y) -> Gr.add_edge g x y) g triang
end
module I(Gr : Sig.I) = struct
type edgelist = (Gr.V.t * Gr.V.t) list
module NewV = WeightedV(Gr.V)
module G = Imperative.Graph.Concrete(NewV)
module EdgeSet = Set.Make(G.E)
module VerticesSet = Set.Make(NewV)
module Choose = Oper.Choose(G)
module H = Hashtbl.Make(NewV)
let check_path g u v =
let h = H.create 97 in
let maxw = NewV.weight u in
let rec aux x : bool =
if H.mem h x then
false
else
if x = v then true
else
if NewV.weight x < maxw || x = u then begin
H.add h x ();
G.fold_succ
(fun x found ->
if not found then aux x
else found)
g x false
end else (H.add h x (); false)
in aux u
module Copy = Gmap.Vertex(Gr)(struct include G include Builder.I(G) end)
let mcsm g =
let f = ref EdgeSet.empty
and a = ref []
and g' = Copy.map (NewV.create 0) g in
for i = Gr.nb_vertex g downto 1 do
let v =
G.fold_vertex
(fun x max ->
if NewV.weight x > NewV.weight max then x else max)
g' (ref 0, snd (Choose.choose_vertex g'))
in
let s =
G.fold_vertex
(fun x s ->
if x = v then s
else
if check_path g' x v then
VerticesSet.add x s
else s)
g' VerticesSet.empty
in
let f' =
VerticesSet.fold
(fun x f ->
NewV.set_weight x (succ (NewV.weight x));
if not (G.mem_edge g' x v) then
EdgeSet.add (x,v) f
else f)
s !f
in
f := f';
G.remove_vertex g' v;
a := (i, NewV.label v) :: !a;
done;
(List.rev !a,
EdgeSet.fold
(fun (x, y) e -> (NewV.label x, NewV.label y) :: e)
!f [])
let triangulate g =
let (_, triang) = mcsm g in
List.iter (fun (x, y) -> Gr.add_edge g x y) triang
end
end