|
| 1 | +// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE.md file. |
| 4 | + |
| 5 | +abstract class Graph<T> { |
| 6 | + Iterable<T> get vertices; |
| 7 | + |
| 8 | + Iterable<T> neighborsOf(T vertex); |
| 9 | +} |
| 10 | + |
| 11 | +/// Computes the strongly connected components of [graph]. |
| 12 | +/// |
| 13 | +/// This implementation is based on [Dijkstra's path-based strong component |
| 14 | +/// algorithm] |
| 15 | +/// (https://en.wikipedia.org/wiki/Path-based_strong_component_algorithm#Description). |
| 16 | +List<List<T>> computeStrongComponents<T>(Graph<T> graph) { |
| 17 | + List<List<T>> result = <List<T>>[]; |
| 18 | + int count = 0; |
| 19 | + Map<T, int> preorderNumbers = <T, int>{}; |
| 20 | + List<T> unassigned = <T>[]; |
| 21 | + List<T> candidates = <T>[]; |
| 22 | + Set<T> assigned = new Set<T>(); |
| 23 | + |
| 24 | + void recursivelySearch(T vertex) { |
| 25 | + // Step 1: Set the preorder number of [vertex] to [count], and increment |
| 26 | + // [count]. |
| 27 | + preorderNumbers[vertex] = count++; |
| 28 | + |
| 29 | + // Step 2: Push [vertex] onto [unassigned] and also onto [candidates]. |
| 30 | + unassigned.add(vertex); |
| 31 | + candidates.add(vertex); |
| 32 | + |
| 33 | + // Step 3: For each edge from [vertex] to a neighboring vertex [neighbor]: |
| 34 | + for (T neighbor in graph.neighborsOf(vertex)) { |
| 35 | + int neighborPreorderNumber = preorderNumbers[neighbor]; |
| 36 | + if (neighborPreorderNumber == null) { |
| 37 | + // If the preorder number of [neighbor] has not yet been assigned, |
| 38 | + // recursively search [neighbor]; |
| 39 | + recursivelySearch(neighbor); |
| 40 | + } else if (!assigned.contains(neighbor)) { |
| 41 | + // Otherwise, if [neighbor] has not yet been assigned to a strongly |
| 42 | + // connected component: |
| 43 | + // |
| 44 | + // * Repeatedly pop vertices from [candidates] until the top element of |
| 45 | + // [candidates] has a preorder number less than or equal to the |
| 46 | + // preorder number of [neighbor]. |
| 47 | + while (preorderNumbers[candidates.last] > neighborPreorderNumber) { |
| 48 | + candidates.removeLast(); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + // Step 4: If [vertex] is the top element of [candidates]: |
| 53 | + if (candidates.last == vertex) { |
| 54 | + // Pop vertices from [unassigned] until [vertex] has been popped, and |
| 55 | + // assign the popped vertices to a new component. |
| 56 | + List<T> component = <T>[]; |
| 57 | + while (true) { |
| 58 | + T top = unassigned.removeLast(); |
| 59 | + component.add(top); |
| 60 | + assigned.add(top); |
| 61 | + if (top == vertex) break; |
| 62 | + } |
| 63 | + result.add(component); |
| 64 | + |
| 65 | + // Pop [vertex] from [candidates]. |
| 66 | + candidates.removeLast(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + for (T vertex in graph.vertices) { |
| 71 | + if (preorderNumbers[vertex] == null) { |
| 72 | + recursivelySearch(vertex); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return result; |
| 77 | +} |
0 commit comments