|
| 1 | +package io.github.acl4s.internal |
| 2 | + |
| 3 | +import scala.collection.mutable |
| 4 | +import scala.reflect.ClassTag |
| 5 | + |
| 6 | +private[internal] case class Edge(to: Int) |
| 7 | + |
| 8 | +private[internal] case class Csr[E] private (start: Array[Int], eList: Array[E]) |
| 9 | +private[internal] object Csr { |
| 10 | + def apply[E: ClassTag](n: Int, edges: collection.Seq[(Int, E)]): Csr[E] = { |
| 11 | + val csr = Csr(new Array[Int](n + 1), new Array[E](edges.size)) |
| 12 | + for ((from, _) <- edges) { |
| 13 | + csr.start(from + 1) += 1 |
| 14 | + } |
| 15 | + (1 to n).foreach(i => { |
| 16 | + csr.start(i) += csr.start(i - 1) |
| 17 | + }) |
| 18 | + val counter = csr.start.clone() |
| 19 | + for ((from, edge) <- edges) { |
| 20 | + csr.eList(counter(from)) = edge |
| 21 | + counter(from) += 1 |
| 22 | + } |
| 23 | + csr |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * Reference: |
| 29 | + * R. Tarjan, |
| 30 | + * Depth-First Search and Linear Graph Algorithms |
| 31 | + */ |
| 32 | +case class SccGraph(private val n: Int) { |
| 33 | + private val edges: mutable.Buffer[(Int, Edge)] = mutable.ListBuffer.empty |
| 34 | + |
| 35 | + def numVertices: Int = n |
| 36 | + |
| 37 | + def addEdge(from: Int, to: Int): Unit = { |
| 38 | + edges += ((from, Edge(to))) |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @return pair of (# of scc, scc id) |
| 43 | + */ |
| 44 | + def sccIds(): (Int, Array[Int]) = { |
| 45 | + val g = Csr(n, edges) |
| 46 | + var now_ord = 0 |
| 47 | + var group_num = 0 |
| 48 | + val visited = new mutable.Stack[Int](n) |
| 49 | + val ord = Array.fill(n)(-1) |
| 50 | + val low = new Array[Int](n) |
| 51 | + val ids = new Array[Int](n) |
| 52 | + |
| 53 | + def dfs(v: Int): Unit = { |
| 54 | + low(v) = now_ord |
| 55 | + ord(v) = now_ord |
| 56 | + now_ord += 1 |
| 57 | + visited.push(v) |
| 58 | + (g.start(v) until g.start(v + 1)).foreach(i => { |
| 59 | + val to = g.eList(i).to |
| 60 | + if (ord(to) == -1) { |
| 61 | + dfs(to) |
| 62 | + low(v) = low(v).min(low(to)) |
| 63 | + } else { |
| 64 | + low(v) = low(v).min(ord(to)) |
| 65 | + } |
| 66 | + }) |
| 67 | + if (low(v) == ord(v)) { |
| 68 | + while { |
| 69 | + // do |
| 70 | + val u = visited.pop() |
| 71 | + ord(u) = n |
| 72 | + ids(u) = group_num |
| 73 | + |
| 74 | + // while |
| 75 | + u != v |
| 76 | + } do {} |
| 77 | + group_num += 1 |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + (0 until n).foreach(i => { |
| 82 | + if (ord(i) == -1) { dfs(i) } |
| 83 | + }) |
| 84 | + (0 until n).foreach(i => { |
| 85 | + ids(i) = group_num - 1 - ids(i) |
| 86 | + }) |
| 87 | + |
| 88 | + (group_num, ids) |
| 89 | + } |
| 90 | + |
| 91 | + def scc(): collection.Seq[collection.Seq[Int]] = { |
| 92 | + val (group_nums, ids) = sccIds() |
| 93 | + val counts = new Array[Int](group_nums) |
| 94 | + ids.foreach(x => { counts(x) += 1 }) |
| 95 | + val groups = new mutable.ArrayBuffer[mutable.Buffer[Int]](n) |
| 96 | + (0 until group_nums).foreach(i => { |
| 97 | + groups += new mutable.ArrayBuffer[Int](counts(i)) |
| 98 | + }) |
| 99 | + (0 until n).foreach(i => { |
| 100 | + groups(ids(i)) += i |
| 101 | + }) |
| 102 | + |
| 103 | + groups |
| 104 | + } |
| 105 | +} |
0 commit comments