author | wenzelm |
Wed, 31 Dec 2014 21:45:30 +0100 | |
changeset 59212 | ecf64bc69778 |
parent 56372 | fadb0fef09d7 |
child 59245 | be4180f3c236 |
permissions | -rw-r--r-- |
46611 | 1 |
/* Title: Pure/General/graph.scala |
2 |
Module: PIDE |
|
3 |
Author: Makarius |
|
4 |
||
5 |
Directed graphs. |
|
6 |
*/ |
|
7 |
||
8 |
package isabelle |
|
9 |
||
10 |
||
46661
d2ac78ba805e
prefer sorted Map/Set for canonical order of results -- pass ordering via fresh copy of empty;
wenzelm
parents:
46659
diff
changeset
|
11 |
import scala.collection.immutable.{SortedMap, SortedSet} |
46611 | 12 |
import scala.annotation.tailrec |
13 |
||
14 |
||
15 |
object Graph |
|
16 |
{ |
|
48348 | 17 |
class Duplicate[Key](val key: Key) extends Exception |
18 |
class Undefined[Key](val key: Key) extends Exception |
|
19 |
class Cycles[Key](val cycles: List[List[Key]]) extends Exception |
|
46611 | 20 |
|
46661
d2ac78ba805e
prefer sorted Map/Set for canonical order of results -- pass ordering via fresh copy of empty;
wenzelm
parents:
46659
diff
changeset
|
21 |
def empty[Key, A](implicit ord: Ordering[Key]): Graph[Key, A] = |
d2ac78ba805e
prefer sorted Map/Set for canonical order of results -- pass ordering via fresh copy of empty;
wenzelm
parents:
46659
diff
changeset
|
22 |
new Graph[Key, A](SortedMap.empty(ord)) |
46667 | 23 |
|
59212 | 24 |
def make[Key, A](entries: List[((Key, A), List[Key])], converse: Boolean = false)( |
25 |
implicit ord: Ordering[Key]): Graph[Key, A] = |
|
49560 | 26 |
{ |
27 |
val graph1 = |
|
28 |
(empty[Key, A](ord) /: entries) { case (graph, ((x, info), _)) => graph.new_node(x, info) } |
|
29 |
val graph2 = |
|
59212 | 30 |
(graph1 /: entries) { case (graph, ((x, _), ys)) => |
31 |
if (converse) (graph /: ys)(_.add_edge(_, x)) else (graph /: ys)(_.add_edge(x, _)) |
|
32 |
} |
|
49560 | 33 |
graph2 |
34 |
} |
|
35 |
||
46667 | 36 |
def string[A]: Graph[String, A] = empty(Ordering.String) |
37 |
def int[A]: Graph[Int, A] = empty(Ordering.Int) |
|
38 |
def long[A]: Graph[Long, A] = empty(Ordering.Long) |
|
49560 | 39 |
|
40 |
||
41 |
/* XML data representation */ |
|
42 |
||
43 |
def encode[Key, A](key: XML.Encode.T[Key], info: XML.Encode.T[A]): XML.Encode.T[Graph[Key, A]] = |
|
44 |
((graph: Graph[Key, A]) => { |
|
45 |
import XML.Encode._ |
|
46 |
list(pair(pair(key, info), list(key)))(graph.dest) |
|
47 |
}) |
|
48 |
||
59212 | 49 |
def decode[Key, A](key: XML.Decode.T[Key], info: XML.Decode.T[A], converse: Boolean = false)( |
50 |
implicit ord: Ordering[Key]): XML.Decode.T[Graph[Key, A]] = |
|
49560 | 51 |
((body: XML.Body) => { |
52 |
import XML.Decode._ |
|
59212 | 53 |
make(list(pair(pair(key, info), list(key)))(body), converse)(ord) |
49560 | 54 |
}) |
46611 | 55 |
} |
56 |
||
57 |
||
46712 | 58 |
final class Graph[Key, A] private(rep: SortedMap[Key, (A, (SortedSet[Key], SortedSet[Key]))]) |
46611 | 59 |
{ |
46661
d2ac78ba805e
prefer sorted Map/Set for canonical order of results -- pass ordering via fresh copy of empty;
wenzelm
parents:
46659
diff
changeset
|
60 |
type Keys = SortedSet[Key] |
46611 | 61 |
type Entry = (A, (Keys, Keys)) |
62 |
||
46661
d2ac78ba805e
prefer sorted Map/Set for canonical order of results -- pass ordering via fresh copy of empty;
wenzelm
parents:
46659
diff
changeset
|
63 |
def ordering: Ordering[Key] = rep.ordering |
d2ac78ba805e
prefer sorted Map/Set for canonical order of results -- pass ordering via fresh copy of empty;
wenzelm
parents:
46659
diff
changeset
|
64 |
def empty_keys: Keys = SortedSet.empty[Key](ordering) |
d2ac78ba805e
prefer sorted Map/Set for canonical order of results -- pass ordering via fresh copy of empty;
wenzelm
parents:
46659
diff
changeset
|
65 |
|
46666
b01b6977a5e8
clarified signature -- avoid oddities of Iterable like Iterator.map;
wenzelm
parents:
46661
diff
changeset
|
66 |
|
b01b6977a5e8
clarified signature -- avoid oddities of Iterable like Iterator.map;
wenzelm
parents:
46661
diff
changeset
|
67 |
/* graphs */ |
46611 | 68 |
|
69 |
def is_empty: Boolean = rep.isEmpty |
|
48507 | 70 |
def defined(x: Key): Boolean = rep.isDefinedAt(x) |
46611 | 71 |
|
56372
fadb0fef09d7
more explicit iterator terminology, in accordance to Scala 2.8 library;
wenzelm
parents:
50452
diff
changeset
|
72 |
def iterator: Iterator[(Key, Entry)] = rep.iterator |
fadb0fef09d7
more explicit iterator terminology, in accordance to Scala 2.8 library;
wenzelm
parents:
50452
diff
changeset
|
73 |
|
fadb0fef09d7
more explicit iterator terminology, in accordance to Scala 2.8 library;
wenzelm
parents:
50452
diff
changeset
|
74 |
def keys_iterator: Iterator[Key] = iterator.map(_._1) |
fadb0fef09d7
more explicit iterator terminology, in accordance to Scala 2.8 library;
wenzelm
parents:
50452
diff
changeset
|
75 |
def keys: List[Key] = keys_iterator.toList |
46611 | 76 |
|
49560 | 77 |
def dest: List[((Key, A), List[Key])] = |
56372
fadb0fef09d7
more explicit iterator terminology, in accordance to Scala 2.8 library;
wenzelm
parents:
50452
diff
changeset
|
78 |
(for ((x, (i, (_, succs))) <- iterator) yield ((x, i), succs.toList)).toList |
46611 | 79 |
|
46666
b01b6977a5e8
clarified signature -- avoid oddities of Iterable like Iterator.map;
wenzelm
parents:
46661
diff
changeset
|
80 |
override def toString: String = |
49560 | 81 |
dest.map({ case ((x, _), ys) => |
82 |
x.toString + " -> " + ys.iterator.map(_.toString).mkString("{", ", ", "}") }) |
|
46666
b01b6977a5e8
clarified signature -- avoid oddities of Iterable like Iterator.map;
wenzelm
parents:
46661
diff
changeset
|
83 |
.mkString("Graph(", ", ", ")") |
46611 | 84 |
|
85 |
private def get_entry(x: Key): Entry = |
|
86 |
rep.get(x) match { |
|
87 |
case Some(entry) => entry |
|
88 |
case None => throw new Graph.Undefined(x) |
|
89 |
} |
|
90 |
||
91 |
private def map_entry(x: Key, f: Entry => Entry): Graph[Key, A] = |
|
92 |
new Graph[Key, A](rep + (x -> f(get_entry(x)))) |
|
93 |
||
94 |
||
95 |
/* nodes */ |
|
96 |
||
97 |
def get_node(x: Key): A = get_entry(x)._1 |
|
98 |
||
99 |
def map_node(x: Key, f: A => A): Graph[Key, A] = |
|
100 |
map_entry(x, { case (i, ps) => (f(i), ps) }) |
|
101 |
||
102 |
||
103 |
/* reachability */ |
|
104 |
||
105 |
/*nodes reachable from xs -- topologically sorted for acyclic graphs*/ |
|
106 |
def reachable(next: Key => Keys, xs: List[Key]): (List[List[Key]], Keys) = |
|
107 |
{ |
|
48350
09bf3b73e446
clarified topological ordering: preserve order of adjacency via reverse fold;
wenzelm
parents:
48348
diff
changeset
|
108 |
def reach(x: Key, reached: (List[Key], Keys)): (List[Key], Keys) = |
46611 | 109 |
{ |
110 |
val (rs, r_set) = reached |
|
111 |
if (r_set(x)) reached |
|
112 |
else { |
|
48350
09bf3b73e446
clarified topological ordering: preserve order of adjacency via reverse fold;
wenzelm
parents:
48348
diff
changeset
|
113 |
val (rs1, r_set1) = (next(x) :\ (rs, r_set + x))(reach) |
46611 | 114 |
(x :: rs1, r_set1) |
115 |
} |
|
116 |
} |
|
117 |
def reachs(reached: (List[List[Key]], Keys), x: Key): (List[List[Key]], Keys) = |
|
118 |
{ |
|
119 |
val (rss, r_set) = reached |
|
48350
09bf3b73e446
clarified topological ordering: preserve order of adjacency via reverse fold;
wenzelm
parents:
48348
diff
changeset
|
120 |
val (rs, r_set1) = reach(x, (Nil, r_set)) |
46611 | 121 |
(rs :: rss, r_set1) |
122 |
} |
|
46661
d2ac78ba805e
prefer sorted Map/Set for canonical order of results -- pass ordering via fresh copy of empty;
wenzelm
parents:
46659
diff
changeset
|
123 |
((List.empty[List[Key]], empty_keys) /: xs)(reachs) |
46611 | 124 |
} |
125 |
||
126 |
/*immediate*/ |
|
127 |
def imm_preds(x: Key): Keys = get_entry(x)._2._1 |
|
128 |
def imm_succs(x: Key): Keys = get_entry(x)._2._2 |
|
129 |
||
130 |
/*transitive*/ |
|
131 |
def all_preds(xs: List[Key]): List[Key] = reachable(imm_preds, xs)._1.flatten |
|
132 |
def all_succs(xs: List[Key]): List[Key] = reachable(imm_succs, xs)._1.flatten |
|
133 |
||
46613 | 134 |
/*strongly connected components; see: David King and John Launchbury, |
135 |
"Structuring Depth First Search Algorithms in Haskell"*/ |
|
136 |
def strong_conn: List[List[Key]] = |
|
56372
fadb0fef09d7
more explicit iterator terminology, in accordance to Scala 2.8 library;
wenzelm
parents:
50452
diff
changeset
|
137 |
reachable(imm_preds, all_succs(keys))._1.filterNot(_.isEmpty).reverse |
46613 | 138 |
|
46611 | 139 |
|
140 |
/* minimal and maximal elements */ |
|
141 |
||
142 |
def minimals: List[Key] = |
|
143 |
(List.empty[Key] /: rep) { |
|
144 |
case (ms, (m, (_, (preds, _)))) => if (preds.isEmpty) m :: ms else ms } |
|
145 |
||
146 |
def maximals: List[Key] = |
|
147 |
(List.empty[Key] /: rep) { |
|
148 |
case (ms, (m, (_, (_, succs)))) => if (succs.isEmpty) m :: ms else ms } |
|
149 |
||
150 |
def is_minimal(x: Key): Boolean = imm_preds(x).isEmpty |
|
151 |
def is_maximal(x: Key): Boolean = imm_succs(x).isEmpty |
|
152 |
||
153 |
||
46668 | 154 |
/* node operations */ |
46611 | 155 |
|
156 |
def new_node(x: Key, info: A): Graph[Key, A] = |
|
157 |
{ |
|
48648 | 158 |
if (defined(x)) throw new Graph.Duplicate(x) |
46661
d2ac78ba805e
prefer sorted Map/Set for canonical order of results -- pass ordering via fresh copy of empty;
wenzelm
parents:
46659
diff
changeset
|
159 |
else new Graph[Key, A](rep + (x -> (info, (empty_keys, empty_keys)))) |
46611 | 160 |
} |
161 |
||
46613 | 162 |
def default_node(x: Key, info: A): Graph[Key, A] = |
48648 | 163 |
if (defined(x)) this else new_node(x, info) |
46613 | 164 |
|
46661
d2ac78ba805e
prefer sorted Map/Set for canonical order of results -- pass ordering via fresh copy of empty;
wenzelm
parents:
46659
diff
changeset
|
165 |
private def del_adjacent(fst: Boolean, x: Key)(map: SortedMap[Key, Entry], y: Key) |
d2ac78ba805e
prefer sorted Map/Set for canonical order of results -- pass ordering via fresh copy of empty;
wenzelm
parents:
46659
diff
changeset
|
166 |
: SortedMap[Key, Entry] = |
46611 | 167 |
map.get(y) match { |
168 |
case None => map |
|
169 |
case Some((i, (preds, succs))) => |
|
170 |
map + (y -> (i, if (fst) (preds - x, succs) else (preds, succs - x))) |
|
171 |
} |
|
172 |
||
173 |
def del_node(x: Key): Graph[Key, A] = |
|
174 |
{ |
|
175 |
val (preds, succs) = get_entry(x)._2 |
|
176 |
new Graph[Key, A]( |
|
177 |
(((rep - x) /: preds)(del_adjacent(false, x)) /: succs)(del_adjacent(true, x))) |
|
178 |
} |
|
179 |
||
46614
165886a4fe64
clarified Graph.restrict (formerly Graph.subgraph) based on public graph operations;
wenzelm
parents:
46613
diff
changeset
|
180 |
def restrict(pred: Key => Boolean): Graph[Key, A] = |
56372
fadb0fef09d7
more explicit iterator terminology, in accordance to Scala 2.8 library;
wenzelm
parents:
50452
diff
changeset
|
181 |
(this /: iterator){ case (graph, (x, _)) => if (!pred(x)) graph.del_node(x) else graph } |
46614
165886a4fe64
clarified Graph.restrict (formerly Graph.subgraph) based on public graph operations;
wenzelm
parents:
46613
diff
changeset
|
182 |
|
46611 | 183 |
|
46668 | 184 |
/* edge operations */ |
46611 | 185 |
|
186 |
def is_edge(x: Key, y: Key): Boolean = |
|
48507 | 187 |
defined(x) && defined(y) && imm_succs(x)(y) |
46611 | 188 |
|
189 |
def add_edge(x: Key, y: Key): Graph[Key, A] = |
|
190 |
if (is_edge(x, y)) this |
|
191 |
else |
|
192 |
map_entry(y, { case (i, (preds, succs)) => (i, (preds + x, succs)) }). |
|
193 |
map_entry(x, { case (i, (preds, succs)) => (i, (preds, succs + y)) }) |
|
194 |
||
195 |
def del_edge(x: Key, y: Key): Graph[Key, A] = |
|
196 |
if (is_edge(x, y)) |
|
197 |
map_entry(y, { case (i, (preds, succs)) => (i, (preds - x, succs)) }). |
|
198 |
map_entry(x, { case (i, (preds, succs)) => (i, (preds, succs - y)) }) |
|
199 |
else this |
|
200 |
||
201 |
||
202 |
/* irreducible paths -- Hasse diagram */ |
|
203 |
||
46661
d2ac78ba805e
prefer sorted Map/Set for canonical order of results -- pass ordering via fresh copy of empty;
wenzelm
parents:
46659
diff
changeset
|
204 |
private def irreducible_preds(x_set: Keys, path: List[Key], z: Key): List[Key] = |
46611 | 205 |
{ |
206 |
def red(x: Key)(x1: Key) = is_edge(x, x1) && x1 != z |
|
207 |
@tailrec def irreds(xs0: List[Key], xs1: List[Key]): List[Key] = |
|
208 |
xs0 match { |
|
209 |
case Nil => xs1 |
|
210 |
case x :: xs => |
|
211 |
if (!(x_set(x)) || x == z || path.contains(x) || |
|
212 |
xs.exists(red(x)) || xs1.exists(red(x))) |
|
213 |
irreds(xs, xs1) |
|
214 |
else irreds(xs, x :: xs1) |
|
215 |
} |
|
216 |
irreds(imm_preds(z).toList, Nil) |
|
217 |
} |
|
218 |
||
219 |
def irreducible_paths(x: Key, y: Key): List[List[Key]] = |
|
220 |
{ |
|
221 |
val (_, x_set) = reachable(imm_succs, List(x)) |
|
222 |
def paths(path: List[Key])(ps: List[List[Key]], z: Key): List[List[Key]] = |
|
223 |
if (x == z) (z :: path) :: ps |
|
224 |
else (ps /: irreducible_preds(x_set, path, z))(paths(z :: path)) |
|
225 |
if ((x == y) && !is_edge(x, x)) List(Nil) else paths(Nil)(Nil, y) |
|
226 |
} |
|
227 |
||
228 |
||
50445
68c9a6538c0e
added graph operations for transitive closure and reduction in Scala -- unproven and thus better left out of the kernel-relevant ML module;
wenzelm
parents:
49560
diff
changeset
|
229 |
/* transitive closure and reduction */ |
68c9a6538c0e
added graph operations for transitive closure and reduction in Scala -- unproven and thus better left out of the kernel-relevant ML module;
wenzelm
parents:
49560
diff
changeset
|
230 |
|
50447
2e22cdccdc38
clarified transitive_closure: proper cumulation of transitive steps, which is essential for Warshall-style algorithms;
wenzelm
parents:
50445
diff
changeset
|
231 |
private def transitive_step(z: Key): Graph[Key, A] = |
50445
68c9a6538c0e
added graph operations for transitive closure and reduction in Scala -- unproven and thus better left out of the kernel-relevant ML module;
wenzelm
parents:
49560
diff
changeset
|
232 |
{ |
50447
2e22cdccdc38
clarified transitive_closure: proper cumulation of transitive steps, which is essential for Warshall-style algorithms;
wenzelm
parents:
50445
diff
changeset
|
233 |
val (preds, succs) = get_entry(z)._2 |
50445
68c9a6538c0e
added graph operations for transitive closure and reduction in Scala -- unproven and thus better left out of the kernel-relevant ML module;
wenzelm
parents:
49560
diff
changeset
|
234 |
var graph = this |
50447
2e22cdccdc38
clarified transitive_closure: proper cumulation of transitive steps, which is essential for Warshall-style algorithms;
wenzelm
parents:
50445
diff
changeset
|
235 |
for (x <- preds; y <- succs) graph = graph.add_edge(x, y) |
50445
68c9a6538c0e
added graph operations for transitive closure and reduction in Scala -- unproven and thus better left out of the kernel-relevant ML module;
wenzelm
parents:
49560
diff
changeset
|
236 |
graph |
68c9a6538c0e
added graph operations for transitive closure and reduction in Scala -- unproven and thus better left out of the kernel-relevant ML module;
wenzelm
parents:
49560
diff
changeset
|
237 |
} |
68c9a6538c0e
added graph operations for transitive closure and reduction in Scala -- unproven and thus better left out of the kernel-relevant ML module;
wenzelm
parents:
49560
diff
changeset
|
238 |
|
56372
fadb0fef09d7
more explicit iterator terminology, in accordance to Scala 2.8 library;
wenzelm
parents:
50452
diff
changeset
|
239 |
def transitive_closure: Graph[Key, A] = (this /: keys_iterator)(_.transitive_step(_)) |
50447
2e22cdccdc38
clarified transitive_closure: proper cumulation of transitive steps, which is essential for Warshall-style algorithms;
wenzelm
parents:
50445
diff
changeset
|
240 |
|
50445
68c9a6538c0e
added graph operations for transitive closure and reduction in Scala -- unproven and thus better left out of the kernel-relevant ML module;
wenzelm
parents:
49560
diff
changeset
|
241 |
def transitive_reduction_acyclic: Graph[Key, A] = |
68c9a6538c0e
added graph operations for transitive closure and reduction in Scala -- unproven and thus better left out of the kernel-relevant ML module;
wenzelm
parents:
49560
diff
changeset
|
242 |
{ |
68c9a6538c0e
added graph operations for transitive closure and reduction in Scala -- unproven and thus better left out of the kernel-relevant ML module;
wenzelm
parents:
49560
diff
changeset
|
243 |
val trans = this.transitive_closure |
56372
fadb0fef09d7
more explicit iterator terminology, in accordance to Scala 2.8 library;
wenzelm
parents:
50452
diff
changeset
|
244 |
if (trans.iterator.exists({ case (x, (_, (_, succs))) => succs.contains(x) })) |
50452
bfb5964e3041
stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents:
50447
diff
changeset
|
245 |
error("Cyclic graph") |
50445
68c9a6538c0e
added graph operations for transitive closure and reduction in Scala -- unproven and thus better left out of the kernel-relevant ML module;
wenzelm
parents:
49560
diff
changeset
|
246 |
|
68c9a6538c0e
added graph operations for transitive closure and reduction in Scala -- unproven and thus better left out of the kernel-relevant ML module;
wenzelm
parents:
49560
diff
changeset
|
247 |
var graph = this |
68c9a6538c0e
added graph operations for transitive closure and reduction in Scala -- unproven and thus better left out of the kernel-relevant ML module;
wenzelm
parents:
49560
diff
changeset
|
248 |
for { |
56372
fadb0fef09d7
more explicit iterator terminology, in accordance to Scala 2.8 library;
wenzelm
parents:
50452
diff
changeset
|
249 |
(x, (_, (_, succs))) <- iterator |
50445
68c9a6538c0e
added graph operations for transitive closure and reduction in Scala -- unproven and thus better left out of the kernel-relevant ML module;
wenzelm
parents:
49560
diff
changeset
|
250 |
y <- succs |
68c9a6538c0e
added graph operations for transitive closure and reduction in Scala -- unproven and thus better left out of the kernel-relevant ML module;
wenzelm
parents:
49560
diff
changeset
|
251 |
if trans.imm_preds(y).exists(z => trans.is_edge(x, z)) |
68c9a6538c0e
added graph operations for transitive closure and reduction in Scala -- unproven and thus better left out of the kernel-relevant ML module;
wenzelm
parents:
49560
diff
changeset
|
252 |
} graph = graph.del_edge(x, y) |
68c9a6538c0e
added graph operations for transitive closure and reduction in Scala -- unproven and thus better left out of the kernel-relevant ML module;
wenzelm
parents:
49560
diff
changeset
|
253 |
graph |
68c9a6538c0e
added graph operations for transitive closure and reduction in Scala -- unproven and thus better left out of the kernel-relevant ML module;
wenzelm
parents:
49560
diff
changeset
|
254 |
} |
68c9a6538c0e
added graph operations for transitive closure and reduction in Scala -- unproven and thus better left out of the kernel-relevant ML module;
wenzelm
parents:
49560
diff
changeset
|
255 |
|
68c9a6538c0e
added graph operations for transitive closure and reduction in Scala -- unproven and thus better left out of the kernel-relevant ML module;
wenzelm
parents:
49560
diff
changeset
|
256 |
|
46611 | 257 |
/* maintain acyclic graphs */ |
258 |
||
259 |
def add_edge_acyclic(x: Key, y: Key): Graph[Key, A] = |
|
260 |
if (is_edge(x, y)) this |
|
261 |
else { |
|
262 |
irreducible_paths(y, x) match { |
|
263 |
case Nil => add_edge(x, y) |
|
264 |
case cycles => throw new Graph.Cycles(cycles.map(x :: _)) |
|
265 |
} |
|
266 |
} |
|
267 |
||
48348 | 268 |
def add_deps_acyclic(y: Key, xs: List[Key]): Graph[Key, A] = |
46611 | 269 |
(this /: xs)(_.add_edge_acyclic(_, y)) |
270 |
||
271 |
def topological_order: List[Key] = all_succs(minimals) |
|
272 |
} |