author | wenzelm |
Thu, 09 Jul 2009 22:01:41 +0200 | |
changeset 31971 | 8c1b845ed105 |
parent 31616 | 63893e3a50a6 |
child 32709 | c5956b54a460 |
permissions | -rw-r--r-- |
6134 | 1 |
(* Title: Pure/General/graph.ML |
15759 | 2 |
Author: Markus Wenzel and Stefan Berghofer, TU Muenchen |
6134 | 3 |
|
4 |
Directed graphs. |
|
5 |
*) |
|
6 |
||
7 |
signature GRAPH = |
|
8 |
sig |
|
9 |
type key |
|
10 |
type 'a T |
|
9321 | 11 |
exception DUP of key |
19029 | 12 |
exception SAME |
13 |
exception UNDEF of key |
|
6134 | 14 |
val empty: 'a T |
28204 | 15 |
val is_empty: 'a T -> bool |
6659 | 16 |
val keys: 'a T -> key list |
14793
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents:
14161
diff
changeset
|
17 |
val dest: 'a T -> (key * key list) list |
31616 | 18 |
val get_first: key option -> (key * ('a * (key list * key list)) -> 'b option) -> |
19 |
'a T -> 'b option |
|
19615 | 20 |
val fold: (key * ('a * (key list * key list)) -> 'b -> 'b) -> 'a T -> 'b -> 'b |
14793
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents:
14161
diff
changeset
|
21 |
val minimals: 'a T -> key list |
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents:
14161
diff
changeset
|
22 |
val maximals: 'a T -> key list |
21935
4e20a5397b57
renamed project to subgraph, improved presentation, avoided unnecessary evaluation of predicate;
wenzelm
parents:
21565
diff
changeset
|
23 |
val subgraph: (key -> bool) -> 'a T -> 'a T |
6142 | 24 |
val map_nodes: ('a -> 'b) -> 'a T -> 'b T |
15759 | 25 |
val get_node: 'a T -> key -> 'a (*exception UNDEF*) |
6142 | 26 |
val map_node: key -> ('a -> 'a) -> 'a T -> 'a T |
17767 | 27 |
val map_node_yield: key -> ('a -> 'b * 'a) -> 'a T -> 'b * 'a T |
6142 | 28 |
val imm_preds: 'a T -> key -> key list |
29 |
val imm_succs: 'a T -> key -> key list |
|
6134 | 30 |
val all_preds: 'a T -> key list -> key list |
31 |
val all_succs: 'a T -> key list -> key list |
|
14161
73ad4884441f
Added function strong_conn for computing the strongly connected components
berghofe
parents:
12451
diff
changeset
|
32 |
val strong_conn: 'a T -> key list list |
15759 | 33 |
val new_node: key * 'a -> 'a T -> 'a T (*exception DUP*) |
17179 | 34 |
val default_node: key * 'a -> 'a T -> 'a T |
15759 | 35 |
val del_nodes: key list -> 'a T -> 'a T (*exception UNDEF*) |
28333
507b64f4cd2a
added del_node, which is more efficient for sparse graphs;
wenzelm
parents:
28204
diff
changeset
|
36 |
val del_node: key -> 'a T -> 'a T (*exception UNDEF*) |
14793
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents:
14161
diff
changeset
|
37 |
val is_edge: 'a T -> key * key -> bool |
6134 | 38 |
val add_edge: key * key -> 'a T -> 'a T |
6152 | 39 |
val del_edge: key * key -> 'a T -> 'a T |
23655
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents:
21935
diff
changeset
|
40 |
val merge: ('a * 'a -> bool) -> 'a T * 'a T -> 'a T (*exception DUP*) |
19029 | 41 |
val join: (key -> 'a * 'a -> 'a) (*exception DUP/SAME*) -> |
23655
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents:
21935
diff
changeset
|
42 |
'a T * 'a T -> 'a T (*exception DUP*) |
19580
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
wenzelm
parents:
19482
diff
changeset
|
43 |
val irreducible_paths: 'a T -> key * key -> key list list |
20679 | 44 |
val all_paths: 'a T -> key * key -> key list list |
6142 | 45 |
exception CYCLES of key list list |
15759 | 46 |
val add_edge_acyclic: key * key -> 'a T -> 'a T (*exception CYCLES*) |
47 |
val add_deps_acyclic: key * key list -> 'a T -> 'a T (*exception CYCLES*) |
|
48 |
val merge_acyclic: ('a * 'a -> bool) -> 'a T * 'a T -> 'a T (*exception CYCLES*) |
|
23964 | 49 |
val topological_order: 'a T -> key list |
15759 | 50 |
val add_edge_trans_acyclic: key * key -> 'a T -> 'a T (*exception CYCLES*) |
51 |
val merge_trans_acyclic: ('a * 'a -> bool) -> 'a T * 'a T -> 'a T (*exception CYCLES*) |
|
31540 | 52 |
val extend: (key -> 'a * key list) -> key -> 'a T -> 'a T |
6134 | 53 |
end; |
54 |
||
31971
8c1b845ed105
renamed functor TableFun to Table, and GraphFun to Graph;
wenzelm
parents:
31616
diff
changeset
|
55 |
functor Graph(Key: KEY): GRAPH = |
6134 | 56 |
struct |
57 |
||
58 |
(* keys *) |
|
59 |
||
60 |
type key = Key.key; |
|
61 |
||
18970 | 62 |
val eq_key = is_equal o Key.ord; |
6134 | 63 |
|
18921 | 64 |
val member_key = member eq_key; |
15759 | 65 |
val remove_key = remove eq_key; |
6152 | 66 |
|
6134 | 67 |
|
68 |
(* tables and sets of keys *) |
|
69 |
||
31971
8c1b845ed105
renamed functor TableFun to Table, and GraphFun to Graph;
wenzelm
parents:
31616
diff
changeset
|
70 |
structure Table = Table(Key); |
6134 | 71 |
type keys = unit Table.table; |
72 |
||
6142 | 73 |
val empty_keys = Table.empty: keys; |
74 |
||
18921 | 75 |
fun member_keys tab = Table.defined (tab: keys); |
76 |
fun insert_keys x tab = Table.insert (K true) (x, ()) (tab: keys); |
|
6134 | 77 |
|
78 |
||
6142 | 79 |
(* graphs *) |
6134 | 80 |
|
81 |
datatype 'a T = Graph of ('a * (key list * key list)) Table.table; |
|
82 |
||
9321 | 83 |
exception DUP = Table.DUP; |
19029 | 84 |
exception UNDEF = Table.UNDEF; |
85 |
exception SAME = Table.SAME; |
|
6134 | 86 |
|
87 |
val empty = Graph Table.empty; |
|
28204 | 88 |
fun is_empty (Graph tab) = Table.is_empty tab; |
6659 | 89 |
fun keys (Graph tab) = Table.keys tab; |
14793
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents:
14161
diff
changeset
|
90 |
fun dest (Graph tab) = map (fn (x, (_, (_, succs))) => (x, succs)) (Table.dest tab); |
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents:
14161
diff
changeset
|
91 |
|
31616 | 92 |
fun get_first b f (Graph tab) = Table.get_first b f tab; |
19615 | 93 |
fun fold_graph f (Graph tab) = Table.fold f tab; |
94 |
||
95 |
fun minimals G = fold_graph (fn (m, (_, ([], _))) => cons m | _ => I) G []; |
|
96 |
fun maximals G = fold_graph (fn (m, (_, (_, []))) => cons m | _ => I) G []; |
|
6134 | 97 |
|
21935
4e20a5397b57
renamed project to subgraph, improved presentation, avoided unnecessary evaluation of predicate;
wenzelm
parents:
21565
diff
changeset
|
98 |
fun subgraph P G = |
4e20a5397b57
renamed project to subgraph, improved presentation, avoided unnecessary evaluation of predicate;
wenzelm
parents:
21565
diff
changeset
|
99 |
let |
4e20a5397b57
renamed project to subgraph, improved presentation, avoided unnecessary evaluation of predicate;
wenzelm
parents:
21565
diff
changeset
|
100 |
fun subg (k, (i, (preds, succs))) = |
4e20a5397b57
renamed project to subgraph, improved presentation, avoided unnecessary evaluation of predicate;
wenzelm
parents:
21565
diff
changeset
|
101 |
if P k then Table.update (k, (i, (filter P preds, filter P succs))) else I; |
4e20a5397b57
renamed project to subgraph, improved presentation, avoided unnecessary evaluation of predicate;
wenzelm
parents:
21565
diff
changeset
|
102 |
in Graph (fold_graph subg G Table.empty) end; |
4e20a5397b57
renamed project to subgraph, improved presentation, avoided unnecessary evaluation of predicate;
wenzelm
parents:
21565
diff
changeset
|
103 |
|
6142 | 104 |
fun get_entry (Graph tab) x = |
17412 | 105 |
(case Table.lookup tab x of |
15531 | 106 |
SOME entry => entry |
107 |
| NONE => raise UNDEF x); |
|
6134 | 108 |
|
17412 | 109 |
fun map_entry x f (G as Graph tab) = Graph (Table.update (x, f (get_entry G x)) tab); |
19290 | 110 |
|
17767 | 111 |
fun map_entry_yield x f (G as Graph tab) = |
112 |
let val (a, node') = f (get_entry G x) |
|
113 |
in (a, Graph (Table.update (x, node') tab)) end; |
|
6134 | 114 |
|
115 |
||
6142 | 116 |
(* nodes *) |
117 |
||
118 |
fun map_nodes f (Graph tab) = Graph (Table.map (fn (i, ps) => (f i, ps)) tab); |
|
6134 | 119 |
|
6142 | 120 |
fun get_node G = #1 o get_entry G; |
18133 | 121 |
|
6142 | 122 |
fun map_node x f = map_entry x (fn (i, ps) => (f i, ps)); |
19290 | 123 |
|
17767 | 124 |
fun map_node_yield x f = map_entry_yield x (fn (i, ps) => |
125 |
let val (a, i') = f i in (a, (i', ps)) end); |
|
6142 | 126 |
|
18133 | 127 |
|
6142 | 128 |
(* reachability *) |
129 |
||
6659 | 130 |
(*nodes reachable from xs -- topologically sorted for acyclic graphs*) |
6142 | 131 |
fun reachable next xs = |
6134 | 132 |
let |
18006
535de280c812
reachable - abandoned foldl_map in favor of fold_map
haftmann
parents:
17912
diff
changeset
|
133 |
fun reach x (rs, R) = |
18921 | 134 |
if member_keys R x then (rs, R) |
135 |
else apfst (cons x) (fold reach (next x) (rs, insert_keys x R)) |
|
23964 | 136 |
in fold_map (fn x => fn X => reach x ([], X)) xs empty_keys end; |
6134 | 137 |
|
6142 | 138 |
(*immediate*) |
139 |
fun imm_preds G = #1 o #2 o get_entry G; |
|
140 |
fun imm_succs G = #2 o #2 o get_entry G; |
|
6134 | 141 |
|
6142 | 142 |
(*transitive*) |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19409
diff
changeset
|
143 |
fun all_preds G = flat o fst o reachable (imm_preds G); |
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19409
diff
changeset
|
144 |
fun all_succs G = flat o fst o reachable (imm_succs G); |
14161
73ad4884441f
Added function strong_conn for computing the strongly connected components
berghofe
parents:
12451
diff
changeset
|
145 |
|
14793
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents:
14161
diff
changeset
|
146 |
(*strongly connected components; see: David King and John Launchbury, |
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents:
14161
diff
changeset
|
147 |
"Structuring Depth First Search Algorithms in Haskell"*) |
18006
535de280c812
reachable - abandoned foldl_map in favor of fold_map
haftmann
parents:
17912
diff
changeset
|
148 |
fun strong_conn G = filter_out null (fst (reachable (imm_preds G) |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19409
diff
changeset
|
149 |
(flat (rev (fst (reachable (imm_succs G) (keys G))))))); |
6134 | 150 |
|
18133 | 151 |
|
9321 | 152 |
(* nodes *) |
6134 | 153 |
|
6152 | 154 |
fun new_node (x, info) (Graph tab) = |
17412 | 155 |
Graph (Table.update_new (x, (info, ([], []))) tab); |
6134 | 156 |
|
17179 | 157 |
fun default_node (x, info) (Graph tab) = |
158 |
Graph (Table.default (x, (info, ([], []))) tab); |
|
17140 | 159 |
|
6659 | 160 |
fun del_nodes xs (Graph tab) = |
15759 | 161 |
Graph (tab |
162 |
|> fold Table.delete xs |
|
163 |
|> Table.map (fn (i, (preds, succs)) => |
|
164 |
(i, (fold remove_key xs preds, fold remove_key xs succs)))); |
|
6659 | 165 |
|
28333
507b64f4cd2a
added del_node, which is more efficient for sparse graphs;
wenzelm
parents:
28204
diff
changeset
|
166 |
fun del_node x (G as Graph tab) = |
507b64f4cd2a
added del_node, which is more efficient for sparse graphs;
wenzelm
parents:
28204
diff
changeset
|
167 |
let |
507b64f4cd2a
added del_node, which is more efficient for sparse graphs;
wenzelm
parents:
28204
diff
changeset
|
168 |
fun del_adjacent which y = Table.map_entry y (fn (i, ps) => (i, (which (remove_key x) ps))); |
507b64f4cd2a
added del_node, which is more efficient for sparse graphs;
wenzelm
parents:
28204
diff
changeset
|
169 |
val (preds, succs) = #2 (get_entry G x); |
507b64f4cd2a
added del_node, which is more efficient for sparse graphs;
wenzelm
parents:
28204
diff
changeset
|
170 |
in |
507b64f4cd2a
added del_node, which is more efficient for sparse graphs;
wenzelm
parents:
28204
diff
changeset
|
171 |
Graph (tab |
507b64f4cd2a
added del_node, which is more efficient for sparse graphs;
wenzelm
parents:
28204
diff
changeset
|
172 |
|> Table.delete x |
507b64f4cd2a
added del_node, which is more efficient for sparse graphs;
wenzelm
parents:
28204
diff
changeset
|
173 |
|> fold (del_adjacent apsnd) preds |
507b64f4cd2a
added del_node, which is more efficient for sparse graphs;
wenzelm
parents:
28204
diff
changeset
|
174 |
|> fold (del_adjacent apfst) succs) |
507b64f4cd2a
added del_node, which is more efficient for sparse graphs;
wenzelm
parents:
28204
diff
changeset
|
175 |
end; |
507b64f4cd2a
added del_node, which is more efficient for sparse graphs;
wenzelm
parents:
28204
diff
changeset
|
176 |
|
6152 | 177 |
|
9321 | 178 |
(* edges *) |
179 |
||
18921 | 180 |
fun is_edge G (x, y) = member_key (imm_succs G x) y handle UNDEF _ => false; |
14793
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents:
14161
diff
changeset
|
181 |
|
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents:
14161
diff
changeset
|
182 |
fun add_edge (x, y) G = |
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents:
14161
diff
changeset
|
183 |
if is_edge G (x, y) then G |
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents:
14161
diff
changeset
|
184 |
else |
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents:
14161
diff
changeset
|
185 |
G |> map_entry y (fn (i, (preds, succs)) => (i, (x :: preds, succs))) |
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents:
14161
diff
changeset
|
186 |
|> map_entry x (fn (i, (preds, succs)) => (i, (preds, y :: succs))); |
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents:
14161
diff
changeset
|
187 |
|
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents:
14161
diff
changeset
|
188 |
fun del_edge (x, y) G = |
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents:
14161
diff
changeset
|
189 |
if is_edge G (x, y) then |
15759 | 190 |
G |> map_entry y (fn (i, (preds, succs)) => (i, (remove_key x preds, succs))) |
191 |
|> map_entry x (fn (i, (preds, succs)) => (i, (preds, remove_key y succs))) |
|
14793
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents:
14161
diff
changeset
|
192 |
else G; |
9321 | 193 |
|
14793
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents:
14161
diff
changeset
|
194 |
fun diff_edges G1 G2 = |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19409
diff
changeset
|
195 |
flat (dest G1 |> map (fn (x, ys) => ys |> map_filter (fn y => |
15531 | 196 |
if is_edge G2 (x, y) then NONE else SOME (x, y)))); |
14793
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents:
14161
diff
changeset
|
197 |
|
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents:
14161
diff
changeset
|
198 |
fun edges G = diff_edges G empty; |
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents:
14161
diff
changeset
|
199 |
|
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents:
14161
diff
changeset
|
200 |
|
18126 | 201 |
(* join and merge *) |
202 |
||
18133 | 203 |
fun no_edges (i, _) = (i, ([], [])); |
204 |
||
205 |
fun join f (Graph tab1, G2 as Graph tab2) = |
|
19029 | 206 |
let fun join_node key ((i1, edges1), (i2, _)) = (f key (i1, i2), edges1) |
18133 | 207 |
in fold add_edge (edges G2) (Graph (Table.join join_node (tab1, Table.map no_edges tab2))) end; |
6152 | 208 |
|
14793
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents:
14161
diff
changeset
|
209 |
fun gen_merge add eq (Graph tab1, G2 as Graph tab2) = |
18133 | 210 |
let fun eq_node ((i1, _), (i2, _)) = eq (i1, i2) |
14793
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents:
14161
diff
changeset
|
211 |
in fold add (edges G2) (Graph (Table.merge eq_node (tab1, Table.map no_edges tab2))) end; |
6152 | 212 |
|
14793
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents:
14161
diff
changeset
|
213 |
fun merge eq GG = gen_merge add_edge eq GG; |
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents:
14161
diff
changeset
|
214 |
|
18133 | 215 |
|
19580
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
wenzelm
parents:
19482
diff
changeset
|
216 |
(* irreducible paths -- Hasse diagram *) |
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
wenzelm
parents:
19482
diff
changeset
|
217 |
|
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
wenzelm
parents:
19482
diff
changeset
|
218 |
fun irreducible_preds G X path z = |
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
wenzelm
parents:
19482
diff
changeset
|
219 |
let |
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
wenzelm
parents:
19482
diff
changeset
|
220 |
fun red x x' = is_edge G (x, x') andalso not (eq_key (x', z)); |
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
wenzelm
parents:
19482
diff
changeset
|
221 |
fun irreds [] xs' = xs' |
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
wenzelm
parents:
19482
diff
changeset
|
222 |
| irreds (x :: xs) xs' = |
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
wenzelm
parents:
19482
diff
changeset
|
223 |
if not (member_keys X x) orelse eq_key (x, z) orelse member_key path x orelse |
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
wenzelm
parents:
19482
diff
changeset
|
224 |
exists (red x) xs orelse exists (red x) xs' |
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
wenzelm
parents:
19482
diff
changeset
|
225 |
then irreds xs xs' |
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
wenzelm
parents:
19482
diff
changeset
|
226 |
else irreds xs (x :: xs'); |
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
wenzelm
parents:
19482
diff
changeset
|
227 |
in irreds (imm_preds G z) [] end; |
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
wenzelm
parents:
19482
diff
changeset
|
228 |
|
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
wenzelm
parents:
19482
diff
changeset
|
229 |
fun irreducible_paths G (x, y) = |
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
wenzelm
parents:
19482
diff
changeset
|
230 |
let |
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
wenzelm
parents:
19482
diff
changeset
|
231 |
val (_, X) = reachable (imm_succs G) [x]; |
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
wenzelm
parents:
19482
diff
changeset
|
232 |
fun paths path z = |
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
wenzelm
parents:
19482
diff
changeset
|
233 |
if eq_key (x, z) then cons (z :: path) |
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
wenzelm
parents:
19482
diff
changeset
|
234 |
else fold (paths (z :: path)) (irreducible_preds G X path z); |
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
wenzelm
parents:
19482
diff
changeset
|
235 |
in if eq_key (x, y) andalso not (is_edge G (x, x)) then [[]] else paths [] y [] end; |
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
wenzelm
parents:
19482
diff
changeset
|
236 |
|
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
wenzelm
parents:
19482
diff
changeset
|
237 |
|
20736 | 238 |
(* all paths *) |
20679 | 239 |
|
240 |
fun all_paths G (x, y) = |
|
241 |
let |
|
242 |
val (_, X) = reachable (imm_succs G) [x]; |
|
20736 | 243 |
fun paths path z = |
244 |
if not (null path) andalso eq_key (x, z) then [z :: path] |
|
245 |
else if member_keys X z andalso not (member_key path z) |
|
246 |
then maps (paths (z :: path)) (imm_preds G z) |
|
20679 | 247 |
else []; |
248 |
in paths [] y end; |
|
249 |
||
250 |
||
14793
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents:
14161
diff
changeset
|
251 |
(* maintain acyclic graphs *) |
6142 | 252 |
|
253 |
exception CYCLES of key list list; |
|
6134 | 254 |
|
255 |
fun add_edge_acyclic (x, y) G = |
|
14793
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents:
14161
diff
changeset
|
256 |
if is_edge G (x, y) then G |
9347 | 257 |
else |
19580
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
wenzelm
parents:
19482
diff
changeset
|
258 |
(case irreducible_paths G (y, x) of |
9347 | 259 |
[] => add_edge (x, y) G |
260 |
| cycles => raise CYCLES (map (cons x) cycles)); |
|
6134 | 261 |
|
15759 | 262 |
fun add_deps_acyclic (y, xs) = fold (fn x => add_edge_acyclic (x, y)) xs; |
9321 | 263 |
|
14793
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents:
14161
diff
changeset
|
264 |
fun merge_acyclic eq GG = gen_merge add_edge_acyclic eq GG; |
9321 | 265 |
|
23964 | 266 |
fun topological_order G = minimals G |> all_succs G; |
267 |
||
14793
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents:
14161
diff
changeset
|
268 |
|
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents:
14161
diff
changeset
|
269 |
(* maintain transitive acyclic graphs *) |
9321 | 270 |
|
14793
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents:
14161
diff
changeset
|
271 |
fun add_edge_trans_acyclic (x, y) G = |
19290 | 272 |
add_edge_acyclic (x, y) G |
25538 | 273 |
|> fold_product (curry add_edge) (all_preds G [x]) (all_succs G [y]); |
9321 | 274 |
|
14793
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents:
14161
diff
changeset
|
275 |
fun merge_trans_acyclic eq (G1, G2) = |
19290 | 276 |
merge_acyclic eq (G1, G2) |
277 |
|> fold add_edge_trans_acyclic (diff_edges G1 G2) |
|
278 |
|> fold add_edge_trans_acyclic (diff_edges G2 G1); |
|
6134 | 279 |
|
31540 | 280 |
|
31516 | 281 |
(* constructing graphs *) |
282 |
||
283 |
fun extend explore = |
|
284 |
let |
|
31540 | 285 |
fun ext x G = |
286 |
if can (get_entry G) x then G |
|
287 |
else |
|
288 |
let val (info, ys) = explore x in |
|
289 |
G |
|
290 |
|> new_node (x, info) |
|
291 |
|> fold ext ys |
|
292 |
|> fold (fn y => add_edge (x, y)) ys |
|
293 |
end |
|
294 |
in ext end; |
|
31516 | 295 |
|
19615 | 296 |
|
297 |
(*final declarations of this structure!*) |
|
298 |
val fold = fold_graph; |
|
299 |
||
6134 | 300 |
end; |
301 |
||
31971
8c1b845ed105
renamed functor TableFun to Table, and GraphFun to Graph;
wenzelm
parents:
31616
diff
changeset
|
302 |
structure Graph = Graph(type key = string val ord = fast_string_ord); |
8c1b845ed105
renamed functor TableFun to Table, and GraphFun to Graph;
wenzelm
parents:
31616
diff
changeset
|
303 |
structure IntGraph = Graph(type key = int val ord = int_ord); |