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