| author | paulson | 
| Mon, 08 Nov 2021 09:31:26 +0000 | |
| changeset 74730 | 25f5f1fa31bb | 
| parent 73360 | 4123fca23296 | 
| child 75393 | 87ebf5a50283 | 
| permissions | -rw-r--r-- | 
| 46611 | 1 | /* Title: Pure/General/graph.scala | 
| 2 | Author: Makarius | |
| 3 | ||
| 4 | Directed graphs. | |
| 5 | */ | |
| 6 | ||
| 7 | package isabelle | |
| 8 | ||
| 9 | ||
| 46661 
d2ac78ba805e
prefer sorted Map/Set for canonical order of results -- pass ordering via fresh copy of empty;
 wenzelm parents: 
46659diff
changeset | 10 | import scala.collection.immutable.{SortedMap, SortedSet}
 | 
| 46611 | 11 | import scala.annotation.tailrec | 
| 12 | ||
| 13 | ||
| 14 | object Graph | |
| 15 | {
 | |
| 48348 | 16 | class Duplicate[Key](val key: Key) extends Exception | 
| 70651 | 17 |   {
 | 
| 18 |     override def toString: String = "Graph.Duplicate(" + key.toString + ")"
 | |
| 19 | } | |
| 48348 | 20 | class Undefined[Key](val key: Key) extends Exception | 
| 70651 | 21 |   {
 | 
| 22 |     override def toString: String = "Graph.Undefined(" + key.toString + ")"
 | |
| 23 | } | |
| 48348 | 24 | class Cycles[Key](val cycles: List[List[Key]]) extends Exception | 
| 70651 | 25 |   {
 | 
| 26 |     override def toString: String = cycles.mkString("Graph.Cycles(", ",\n", ")")
 | |
| 27 | } | |
| 46611 | 28 | |
| 46661 
d2ac78ba805e
prefer sorted Map/Set for canonical order of results -- pass ordering via fresh copy of empty;
 wenzelm parents: 
46659diff
changeset | 29 | 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: 
46659diff
changeset | 30 | new Graph[Key, A](SortedMap.empty(ord)) | 
| 46667 | 31 | |
| 70636 | 32 | def make[Key, A](entries: List[((Key, A), List[Key])], | 
| 70692 | 33 | symmetric: Boolean = false)(implicit ord: Ordering[Key]): Graph[Key, A] = | 
| 49560 | 34 |   {
 | 
| 35 | val graph1 = | |
| 73359 | 36 |       entries.foldLeft(empty[Key, A](ord)) {
 | 
| 37 | case (graph, ((x, info), _)) => graph.new_node(x, info) | |
| 38 | } | |
| 49560 | 39 | val graph2 = | 
| 73359 | 40 |       entries.foldLeft(graph1) {
 | 
| 41 | case (graph, ((x, _), ys)) => | |
| 42 |           ys.foldLeft(graph) {
 | |
| 43 | case (g, y) => if (symmetric) g.add_edge(y, x) else g.add_edge(x, y) | |
| 44 | } | |
| 45 | } | |
| 49560 | 46 | graph2 | 
| 47 | } | |
| 48 | ||
| 46667 | 49 | def string[A]: Graph[String, A] = empty(Ordering.String) | 
| 50 | def int[A]: Graph[Int, A] = empty(Ordering.Int) | |
| 51 | def long[A]: Graph[Long, A] = empty(Ordering.Long) | |
| 49560 | 52 | |
| 53 | ||
| 54 | /* XML data representation */ | |
| 55 | ||
| 56 | def encode[Key, A](key: XML.Encode.T[Key], info: XML.Encode.T[A]): XML.Encode.T[Graph[Key, A]] = | |
| 60215 | 57 |     (graph: Graph[Key, A]) => {
 | 
| 49560 | 58 | import XML.Encode._ | 
| 59 | list(pair(pair(key, info), list(key)))(graph.dest) | |
| 60215 | 60 | } | 
| 49560 | 61 | |
| 59245 
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
 wenzelm parents: 
59212diff
changeset | 62 | def decode[Key, A](key: XML.Decode.T[Key], info: XML.Decode.T[A])( | 
| 59212 | 63 | implicit ord: Ordering[Key]): XML.Decode.T[Graph[Key, A]] = | 
| 60215 | 64 |     (body: XML.Body) => {
 | 
| 49560 | 65 | import XML.Decode._ | 
| 59245 
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
 wenzelm parents: 
59212diff
changeset | 66 | make(list(pair(pair(key, info), list(key)))(body))(ord) | 
| 60215 | 67 | } | 
| 46611 | 68 | } | 
| 69 | ||
| 70 | ||
| 46712 | 71 | final class Graph[Key, A] private(rep: SortedMap[Key, (A, (SortedSet[Key], SortedSet[Key]))]) | 
| 46611 | 72 | {
 | 
| 46661 
d2ac78ba805e
prefer sorted Map/Set for canonical order of results -- pass ordering via fresh copy of empty;
 wenzelm parents: 
46659diff
changeset | 73 | type Keys = SortedSet[Key] | 
| 46611 | 74 | type Entry = (A, (Keys, Keys)) | 
| 75 | ||
| 46661 
d2ac78ba805e
prefer sorted Map/Set for canonical order of results -- pass ordering via fresh copy of empty;
 wenzelm parents: 
46659diff
changeset | 76 | 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: 
46659diff
changeset | 77 | 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: 
46659diff
changeset | 78 | |
| 46666 
b01b6977a5e8
clarified signature -- avoid oddities of Iterable like Iterator.map;
 wenzelm parents: 
46661diff
changeset | 79 | |
| 
b01b6977a5e8
clarified signature -- avoid oddities of Iterable like Iterator.map;
 wenzelm parents: 
46661diff
changeset | 80 | /* graphs */ | 
| 46611 | 81 | |
| 82 | def is_empty: Boolean = rep.isEmpty | |
| 48507 | 83 | def defined(x: Key): Boolean = rep.isDefinedAt(x) | 
| 68321 | 84 | def domain: Set[Key] = rep.keySet | 
| 46611 | 85 | |
| 68496 | 86 | def size: Int = rep.size | 
| 56372 
fadb0fef09d7
more explicit iterator terminology, in accordance to Scala 2.8 library;
 wenzelm parents: 
50452diff
changeset | 87 | def iterator: Iterator[(Key, Entry)] = rep.iterator | 
| 
fadb0fef09d7
more explicit iterator terminology, in accordance to Scala 2.8 library;
 wenzelm parents: 
50452diff
changeset | 88 | |
| 
fadb0fef09d7
more explicit iterator terminology, in accordance to Scala 2.8 library;
 wenzelm parents: 
50452diff
changeset | 89 | def keys_iterator: Iterator[Key] = iterator.map(_._1) | 
| 
fadb0fef09d7
more explicit iterator terminology, in accordance to Scala 2.8 library;
 wenzelm parents: 
50452diff
changeset | 90 | def keys: List[Key] = keys_iterator.toList | 
| 46611 | 91 | |
| 49560 | 92 | def dest: List[((Key, A), List[Key])] = | 
| 56372 
fadb0fef09d7
more explicit iterator terminology, in accordance to Scala 2.8 library;
 wenzelm parents: 
50452diff
changeset | 93 | (for ((x, (i, (_, succs))) <- iterator) yield ((x, i), succs.toList)).toList | 
| 46611 | 94 | |
| 46666 
b01b6977a5e8
clarified signature -- avoid oddities of Iterable like Iterator.map;
 wenzelm parents: 
46661diff
changeset | 95 | override def toString: String = | 
| 49560 | 96 |     dest.map({ case ((x, _), ys) =>
 | 
| 97 |         x.toString + " -> " + ys.iterator.map(_.toString).mkString("{", ", ", "}") })
 | |
| 46666 
b01b6977a5e8
clarified signature -- avoid oddities of Iterable like Iterator.map;
 wenzelm parents: 
46661diff
changeset | 98 |       .mkString("Graph(", ", ", ")")
 | 
| 46611 | 99 | |
| 100 | private def get_entry(x: Key): Entry = | |
| 101 |     rep.get(x) match {
 | |
| 102 | case Some(entry) => entry | |
| 103 | case None => throw new Graph.Undefined(x) | |
| 104 | } | |
| 105 | ||
| 106 | private def map_entry(x: Key, f: Entry => Entry): Graph[Key, A] = | |
| 107 | new Graph[Key, A](rep + (x -> f(get_entry(x)))) | |
| 108 | ||
| 109 | ||
| 110 | /* nodes */ | |
| 111 | ||
| 112 | def get_node(x: Key): A = get_entry(x)._1 | |
| 113 | ||
| 114 | def map_node(x: Key, f: A => A): Graph[Key, A] = | |
| 115 |     map_entry(x, { case (i, ps) => (f(i), ps) })
 | |
| 116 | ||
| 117 | ||
| 118 | /* reachability */ | |
| 119 | ||
| 70764 
6d36b30fdd67
more operations -- incremental exploration of reachable nodes;
 wenzelm parents: 
70699diff
changeset | 120 | /*reachable nodes with length of longest path*/ | 
| 70794 | 121 | def reachable_length( | 
| 70800 | 122 | count: Key => Long, | 
| 70794 | 123 | next: Key => Keys, | 
| 70800 | 124 | xs: List[Key]): Map[Key, Long] = | 
| 70764 
6d36b30fdd67
more operations -- incremental exploration of reachable nodes;
 wenzelm parents: 
70699diff
changeset | 125 |   {
 | 
| 70800 | 126 | def reach(length: Long)(rs: Map[Key, Long], x: Key): Map[Key, Long] = | 
| 70764 
6d36b30fdd67
more operations -- incremental exploration of reachable nodes;
 wenzelm parents: 
70699diff
changeset | 127 |       if (rs.get(x) match { case Some(n) => n >= length case None => false }) rs
 | 
| 73359 | 128 | else next(x).foldLeft(rs + (x -> length))(reach(length + count(x))) | 
| 129 | xs.foldLeft(Map.empty[Key, Long])(reach(0)) | |
| 70764 
6d36b30fdd67
more operations -- incremental exploration of reachable nodes;
 wenzelm parents: 
70699diff
changeset | 130 | } | 
| 70800 | 131 | def node_height(count: Key => Long): Map[Key, Long] = | 
| 71601 | 132 | reachable_length(count, imm_preds, maximals) | 
| 70800 | 133 | def node_depth(count: Key => Long): Map[Key, Long] = | 
| 71601 | 134 | reachable_length(count, imm_succs, minimals) | 
| 70764 
6d36b30fdd67
more operations -- incremental exploration of reachable nodes;
 wenzelm parents: 
70699diff
changeset | 135 | |
| 
6d36b30fdd67
more operations -- incremental exploration of reachable nodes;
 wenzelm parents: 
70699diff
changeset | 136 | /*reachable nodes with size limit*/ | 
| 70772 
030a6baa5cb2
support headless_load_limit for more scalable load process;
 wenzelm parents: 
70764diff
changeset | 137 | def reachable_limit( | 
| 70800 | 138 | limit: Long, | 
| 139 | count: Key => Long, | |
| 70772 
030a6baa5cb2
support headless_load_limit for more scalable load process;
 wenzelm parents: 
70764diff
changeset | 140 | next: Key => Keys, | 
| 
030a6baa5cb2
support headless_load_limit for more scalable load process;
 wenzelm parents: 
70764diff
changeset | 141 | xs: List[Key]): Keys = | 
| 70764 
6d36b30fdd67
more operations -- incremental exploration of reachable nodes;
 wenzelm parents: 
70699diff
changeset | 142 |   {
 | 
| 70800 | 143 | def reach(res: (Long, Keys), x: Key): (Long, Keys) = | 
| 70764 
6d36b30fdd67
more operations -- incremental exploration of reachable nodes;
 wenzelm parents: 
70699diff
changeset | 144 |     {
 | 
| 
6d36b30fdd67
more operations -- incremental exploration of reachable nodes;
 wenzelm parents: 
70699diff
changeset | 145 | val (n, rs) = res | 
| 70772 
030a6baa5cb2
support headless_load_limit for more scalable load process;
 wenzelm parents: 
70764diff
changeset | 146 | if (rs.contains(x)) res | 
| 73359 | 147 | else next(x).foldLeft((n + count(x), rs + x))(reach) | 
| 70764 
6d36b30fdd67
more operations -- incremental exploration of reachable nodes;
 wenzelm parents: 
70699diff
changeset | 148 | } | 
| 
6d36b30fdd67
more operations -- incremental exploration of reachable nodes;
 wenzelm parents: 
70699diff
changeset | 149 | |
| 70800 | 150 | @tailrec def reachs(size: Long, rs: Keys, rest: List[Key]): Keys = | 
| 70764 
6d36b30fdd67
more operations -- incremental exploration of reachable nodes;
 wenzelm parents: 
70699diff
changeset | 151 |       rest match {
 | 
| 70772 
030a6baa5cb2
support headless_load_limit for more scalable load process;
 wenzelm parents: 
70764diff
changeset | 152 | case Nil => rs | 
| 70764 
6d36b30fdd67
more operations -- incremental exploration of reachable nodes;
 wenzelm parents: 
70699diff
changeset | 153 | case head :: tail => | 
| 
6d36b30fdd67
more operations -- incremental exploration of reachable nodes;
 wenzelm parents: 
70699diff
changeset | 154 | val (size1, rs1) = reach((size, rs), head) | 
| 70772 
030a6baa5cb2
support headless_load_limit for more scalable load process;
 wenzelm parents: 
70764diff
changeset | 155 | if (size > 0 && size1 > limit) rs | 
| 70764 
6d36b30fdd67
more operations -- incremental exploration of reachable nodes;
 wenzelm parents: 
70699diff
changeset | 156 | else reachs(size1, rs1, tail) | 
| 
6d36b30fdd67
more operations -- incremental exploration of reachable nodes;
 wenzelm parents: 
70699diff
changeset | 157 | } | 
| 
6d36b30fdd67
more operations -- incremental exploration of reachable nodes;
 wenzelm parents: 
70699diff
changeset | 158 | |
| 
6d36b30fdd67
more operations -- incremental exploration of reachable nodes;
 wenzelm parents: 
70699diff
changeset | 159 | reachs(0, empty_keys, xs) | 
| 
6d36b30fdd67
more operations -- incremental exploration of reachable nodes;
 wenzelm parents: 
70699diff
changeset | 160 | } | 
| 
6d36b30fdd67
more operations -- incremental exploration of reachable nodes;
 wenzelm parents: 
70699diff
changeset | 161 | |
| 
6d36b30fdd67
more operations -- incremental exploration of reachable nodes;
 wenzelm parents: 
70699diff
changeset | 162 | /*reachable nodes with result in topological order (for acyclic graphs)*/ | 
| 72065 
11dc8929832d
clarified order --- proper sorting of requirements;
 wenzelm parents: 
71601diff
changeset | 163 | private def reachable(next: Key => Keys, xs: List[Key], rev: Boolean = false): (List[List[Key]], Keys) = | 
| 46611 | 164 |   {
 | 
| 48350 
09bf3b73e446
clarified topological ordering: preserve order of adjacency via reverse fold;
 wenzelm parents: 
48348diff
changeset | 165 | def reach(x: Key, reached: (List[Key], Keys)): (List[Key], Keys) = | 
| 46611 | 166 |     {
 | 
| 167 | val (rs, r_set) = reached | |
| 168 | if (r_set(x)) reached | |
| 169 |       else {
 | |
| 72065 
11dc8929832d
clarified order --- proper sorting of requirements;
 wenzelm parents: 
71601diff
changeset | 170 | val (rs1, r_set1) = | 
| 73359 | 171 |           if (rev) next(x).foldLeft((rs, r_set + x)) { case (b, a) => reach(a, b) }
 | 
| 73360 | 172 | else next(x).foldRight((rs, r_set + x))(reach) | 
| 46611 | 173 | (x :: rs1, r_set1) | 
| 174 | } | |
| 175 | } | |
| 176 | def reachs(reached: (List[List[Key]], Keys), x: Key): (List[List[Key]], Keys) = | |
| 177 |     {
 | |
| 178 | val (rss, r_set) = reached | |
| 48350 
09bf3b73e446
clarified topological ordering: preserve order of adjacency via reverse fold;
 wenzelm parents: 
48348diff
changeset | 179 | val (rs, r_set1) = reach(x, (Nil, r_set)) | 
| 46611 | 180 | (rs :: rss, r_set1) | 
| 181 | } | |
| 73359 | 182 | xs.foldLeft((List.empty[List[Key]], empty_keys))(reachs) | 
| 46611 | 183 | } | 
| 184 | ||
| 185 | /*immediate*/ | |
| 186 | def imm_preds(x: Key): Keys = get_entry(x)._2._1 | |
| 187 | def imm_succs(x: Key): Keys = get_entry(x)._2._2 | |
| 188 | ||
| 189 | /*transitive*/ | |
| 72065 
11dc8929832d
clarified order --- proper sorting of requirements;
 wenzelm parents: 
71601diff
changeset | 190 | def all_preds_rev(xs: List[Key]): List[Key] = | 
| 
11dc8929832d
clarified order --- proper sorting of requirements;
 wenzelm parents: 
71601diff
changeset | 191 | reachable(imm_preds, xs, rev = true)._1.flatten.reverse | 
| 46611 | 192 | def all_preds(xs: List[Key]): List[Key] = reachable(imm_preds, xs)._1.flatten | 
| 193 | def all_succs(xs: List[Key]): List[Key] = reachable(imm_succs, xs)._1.flatten | |
| 194 | ||
| 46613 | 195 | /*strongly connected components; see: David King and John Launchbury, | 
| 196 | "Structuring Depth First Search Algorithms in Haskell"*/ | |
| 197 | def strong_conn: List[List[Key]] = | |
| 56372 
fadb0fef09d7
more explicit iterator terminology, in accordance to Scala 2.8 library;
 wenzelm parents: 
50452diff
changeset | 198 | reachable(imm_preds, all_succs(keys))._1.filterNot(_.isEmpty).reverse | 
| 46613 | 199 | |
| 46611 | 200 | |
| 201 | /* minimal and maximal elements */ | |
| 202 | ||
| 203 | def minimals: List[Key] = | |
| 73359 | 204 |     rep.foldLeft(List.empty[Key]) {
 | 
| 205 | case (ms, (m, (_, (preds, _)))) => if (preds.isEmpty) m :: ms else ms | |
| 206 | } | |
| 46611 | 207 | |
| 208 | def maximals: List[Key] = | |
| 73359 | 209 |     rep.foldLeft(List.empty[Key]) {
 | 
| 210 | case (ms, (m, (_, (_, succs)))) => if (succs.isEmpty) m :: ms else ms | |
| 211 | } | |
| 46611 | 212 | |
| 213 | def is_minimal(x: Key): Boolean = imm_preds(x).isEmpty | |
| 214 | def is_maximal(x: Key): Boolean = imm_succs(x).isEmpty | |
| 215 | ||
| 66830 | 216 | def is_isolated(x: Key): Boolean = is_minimal(x) && is_maximal(x) | 
| 217 | ||
| 46611 | 218 | |
| 46668 | 219 | /* node operations */ | 
| 46611 | 220 | |
| 221 | def new_node(x: Key, info: A): Graph[Key, A] = | |
| 222 |   {
 | |
| 48648 | 223 | 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: 
46659diff
changeset | 224 | else new Graph[Key, A](rep + (x -> (info, (empty_keys, empty_keys)))) | 
| 46611 | 225 | } | 
| 226 | ||
| 46613 | 227 | def default_node(x: Key, info: A): Graph[Key, A] = | 
| 48648 | 228 | if (defined(x)) this else new_node(x, info) | 
| 46613 | 229 | |
| 46661 
d2ac78ba805e
prefer sorted Map/Set for canonical order of results -- pass ordering via fresh copy of empty;
 wenzelm parents: 
46659diff
changeset | 230 | 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: 
46659diff
changeset | 231 | : SortedMap[Key, Entry] = | 
| 46611 | 232 |     map.get(y) match {
 | 
| 233 | case None => map | |
| 234 | case Some((i, (preds, succs))) => | |
| 235 | map + (y -> (i, if (fst) (preds - x, succs) else (preds, succs - x))) | |
| 236 | } | |
| 237 | ||
| 238 | def del_node(x: Key): Graph[Key, A] = | |
| 239 |   {
 | |
| 240 | val (preds, succs) = get_entry(x)._2 | |
| 241 | new Graph[Key, A]( | |
| 73359 | 242 | succs.foldLeft(preds.foldLeft(rep - x)(del_adjacent(false, x)))(del_adjacent(true, x))) | 
| 46611 | 243 | } | 
| 244 | ||
| 46614 
165886a4fe64
clarified Graph.restrict (formerly Graph.subgraph) based on public graph operations;
 wenzelm parents: 
46613diff
changeset | 245 | def restrict(pred: Key => Boolean): Graph[Key, A] = | 
| 73359 | 246 |     iterator.foldLeft(this) { 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: 
46613diff
changeset | 247 | |
| 70699 | 248 | def exclude(pred: Key => Boolean): Graph[Key, A] = restrict(name => !pred(name)) | 
| 249 | ||
| 46611 | 250 | |
| 46668 | 251 | /* edge operations */ | 
| 46611 | 252 | |
| 59259 
399506ee38a5
clarified static full_graph vs. dynamic visible_graph;
 wenzelm parents: 
59245diff
changeset | 253 | def edges_iterator: Iterator[(Key, Key)] = | 
| 
399506ee38a5
clarified static full_graph vs. dynamic visible_graph;
 wenzelm parents: 
59245diff
changeset | 254 |     for { x <- keys_iterator; y <- imm_succs(x).iterator } yield (x, y)
 | 
| 
399506ee38a5
clarified static full_graph vs. dynamic visible_graph;
 wenzelm parents: 
59245diff
changeset | 255 | |
| 46611 | 256 | def is_edge(x: Key, y: Key): Boolean = | 
| 48507 | 257 | defined(x) && defined(y) && imm_succs(x)(y) | 
| 46611 | 258 | |
| 259 | def add_edge(x: Key, y: Key): Graph[Key, A] = | |
| 260 | if (is_edge(x, y)) this | |
| 261 | else | |
| 262 |       map_entry(y, { case (i, (preds, succs)) => (i, (preds + x, succs)) }).
 | |
| 263 |       map_entry(x, { case (i, (preds, succs)) => (i, (preds, succs + y)) })
 | |
| 264 | ||
| 265 | def del_edge(x: Key, y: Key): Graph[Key, A] = | |
| 266 | if (is_edge(x, y)) | |
| 267 |       map_entry(y, { case (i, (preds, succs)) => (i, (preds - x, succs)) }).
 | |
| 268 |       map_entry(x, { case (i, (preds, succs)) => (i, (preds, succs - y)) })
 | |
| 269 | else this | |
| 270 | ||
| 271 | ||
| 272 | /* irreducible paths -- Hasse diagram */ | |
| 273 | ||
| 46661 
d2ac78ba805e
prefer sorted Map/Set for canonical order of results -- pass ordering via fresh copy of empty;
 wenzelm parents: 
46659diff
changeset | 274 | private def irreducible_preds(x_set: Keys, path: List[Key], z: Key): List[Key] = | 
| 46611 | 275 |   {
 | 
| 276 | def red(x: Key)(x1: Key) = is_edge(x, x1) && x1 != z | |
| 277 | @tailrec def irreds(xs0: List[Key], xs1: List[Key]): List[Key] = | |
| 278 |       xs0 match {
 | |
| 279 | case Nil => xs1 | |
| 280 | case x :: xs => | |
| 60215 | 281 | if (!x_set(x) || x == z || path.contains(x) || | 
| 46611 | 282 | xs.exists(red(x)) || xs1.exists(red(x))) | 
| 283 | irreds(xs, xs1) | |
| 284 | else irreds(xs, x :: xs1) | |
| 285 | } | |
| 286 | irreds(imm_preds(z).toList, Nil) | |
| 287 | } | |
| 288 | ||
| 289 | def irreducible_paths(x: Key, y: Key): List[List[Key]] = | |
| 290 |   {
 | |
| 291 | val (_, x_set) = reachable(imm_succs, List(x)) | |
| 292 | def paths(path: List[Key])(ps: List[List[Key]], z: Key): List[List[Key]] = | |
| 293 | if (x == z) (z :: path) :: ps | |
| 73359 | 294 | else irreducible_preds(x_set, path, z).foldLeft(ps)(paths(z :: path)) | 
| 46611 | 295 | if ((x == y) && !is_edge(x, x)) List(Nil) else paths(Nil)(Nil, y) | 
| 296 | } | |
| 297 | ||
| 298 | ||
| 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: 
49560diff
changeset | 299 | /* 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: 
49560diff
changeset | 300 | |
| 50447 
2e22cdccdc38
clarified transitive_closure: proper cumulation of transitive steps, which is essential for Warshall-style algorithms;
 wenzelm parents: 
50445diff
changeset | 301 | 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: 
49560diff
changeset | 302 |   {
 | 
| 50447 
2e22cdccdc38
clarified transitive_closure: proper cumulation of transitive steps, which is essential for Warshall-style algorithms;
 wenzelm parents: 
50445diff
changeset | 303 | 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: 
49560diff
changeset | 304 | var graph = this | 
| 50447 
2e22cdccdc38
clarified transitive_closure: proper cumulation of transitive steps, which is essential for Warshall-style algorithms;
 wenzelm parents: 
50445diff
changeset | 305 | 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: 
49560diff
changeset | 306 | 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: 
49560diff
changeset | 307 | } | 
| 
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: 
49560diff
changeset | 308 | |
| 73359 | 309 | def transitive_closure: Graph[Key, A] = keys_iterator.foldLeft(this)(_.transitive_step(_)) | 
| 50447 
2e22cdccdc38
clarified transitive_closure: proper cumulation of transitive steps, which is essential for Warshall-style algorithms;
 wenzelm parents: 
50445diff
changeset | 310 | |
| 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: 
49560diff
changeset | 311 | 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: 
49560diff
changeset | 312 |   {
 | 
| 
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: 
49560diff
changeset | 313 | val trans = this.transitive_closure | 
| 56372 
fadb0fef09d7
more explicit iterator terminology, in accordance to Scala 2.8 library;
 wenzelm parents: 
50452diff
changeset | 314 |     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: 
50447diff
changeset | 315 |       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: 
49560diff
changeset | 316 | |
| 
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: 
49560diff
changeset | 317 | 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: 
49560diff
changeset | 318 |     for {
 | 
| 56372 
fadb0fef09d7
more explicit iterator terminology, in accordance to Scala 2.8 library;
 wenzelm parents: 
50452diff
changeset | 319 | (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: 
49560diff
changeset | 320 | 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: 
49560diff
changeset | 321 | 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: 
49560diff
changeset | 322 | } 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: 
49560diff
changeset | 323 | 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: 
49560diff
changeset | 324 | } | 
| 
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: 
49560diff
changeset | 325 | |
| 
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: 
49560diff
changeset | 326 | |
| 46611 | 327 | /* maintain acyclic graphs */ | 
| 328 | ||
| 329 | def add_edge_acyclic(x: Key, y: Key): Graph[Key, A] = | |
| 330 | if (is_edge(x, y)) this | |
| 331 |     else {
 | |
| 332 |       irreducible_paths(y, x) match {
 | |
| 333 | case Nil => add_edge(x, y) | |
| 334 | case cycles => throw new Graph.Cycles(cycles.map(x :: _)) | |
| 335 | } | |
| 336 | } | |
| 337 | ||
| 48348 | 338 | def add_deps_acyclic(y: Key, xs: List[Key]): Graph[Key, A] = | 
| 73359 | 339 | xs.foldLeft(this)(_.add_edge_acyclic(_, y)) | 
| 46611 | 340 | |
| 341 | def topological_order: List[Key] = all_succs(minimals) | |
| 342 | } |