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