findGraphRoots.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const NO_MARKER = 0;
  7. const IN_PROGRESS_MARKER = 1;
  8. const DONE_MARKER = 2;
  9. const CANDIDATE_MARKER = 3;
  10. /**
  11. * Defines the nodes type used by this module.
  12. * @template T
  13. * @typedef {Set<Node<T>>} Nodes
  14. */
  15. /**
  16. * Represents the node runtime component.
  17. * @template T
  18. */
  19. class Node {
  20. /**
  21. * Creates an instance of Node.
  22. * @param {T} item the value of the node
  23. */
  24. constructor(item) {
  25. this.item = item;
  26. /** @type {Nodes<T>} */
  27. this.dependencies = new Set();
  28. /** @type {SCC<T>} */
  29. this.scc = new SCC();
  30. // Each node starts as a single-node SCC
  31. this.scc.nodes.add(this);
  32. /** @type {number} */
  33. this.incoming = 0;
  34. }
  35. }
  36. /**
  37. * SCC (strongly connected component)
  38. * @template T
  39. */
  40. class SCC {
  41. constructor() {
  42. /** @type {Nodes<T>} */
  43. this.nodes = new Set();
  44. this.marker = NO_MARKER;
  45. }
  46. }
  47. /**
  48. * Defines the stack entry type used by this module.
  49. * @template T
  50. * @typedef {object} StackEntry
  51. * @property {Node<T>} node
  52. * @property {Node<T>[]} openEdges
  53. */
  54. /**
  55. * Returns graph roots of the items.
  56. * @template T
  57. * @param {Iterable<T>} items list of items
  58. * @param {(item: T) => Iterable<T>} getDependencies function to get dependencies of an item (items that are not in list are ignored)
  59. * @returns {Iterable<T>} graph roots of the items
  60. */
  61. module.exports = (items, getDependencies) => {
  62. /** @type {Map<T, Node<T>>} */
  63. const itemToNode = new Map();
  64. for (const item of items) {
  65. const node = new Node(item);
  66. itemToNode.set(item, node);
  67. }
  68. // Early exit when there is only one node
  69. if (itemToNode.size <= 1) return items;
  70. // Build graph edges
  71. for (const node of itemToNode.values()) {
  72. for (const dep of getDependencies(node.item)) {
  73. const depNode = itemToNode.get(dep);
  74. if (depNode !== undefined) {
  75. node.dependencies.add(depNode);
  76. }
  77. }
  78. }
  79. // All candidate root SCCs, they will be removed once an incoming edge is found
  80. /** @type {Set<SCC<T>>} */
  81. const rootSCCs = new Set();
  82. for (const selectedNode of itemToNode.values()) {
  83. // DFS walk only once per unseen SCC
  84. if (selectedNode.scc.marker === NO_MARKER) {
  85. selectedNode.scc.marker = IN_PROGRESS_MARKER;
  86. // Keep a stack to avoid recursive walk
  87. /** @type {StackEntry<T>[]} */
  88. const stack = [
  89. {
  90. node: selectedNode,
  91. openEdges: [...selectedNode.dependencies]
  92. }
  93. ];
  94. while (stack.length > 0) {
  95. const topOfStack = stack[stack.length - 1];
  96. // Process one unvisited outgoing edge if available
  97. if (topOfStack.openEdges.length > 0) {
  98. const dependency =
  99. /** @type {Node<T>} */
  100. (topOfStack.openEdges.pop());
  101. const depSCC = dependency.scc;
  102. switch (depSCC.marker) {
  103. case NO_MARKER:
  104. // First time we see this SCC: enter it
  105. stack.push({
  106. node: dependency,
  107. openEdges: [...dependency.dependencies]
  108. });
  109. depSCC.marker = IN_PROGRESS_MARKER;
  110. break;
  111. case IN_PROGRESS_MARKER: {
  112. // Back-edge to an SCC that is still on the stack
  113. // Example:
  114. // A -> B -> C -> D
  115. // ^ |
  116. // |_________|
  117. // If we are at `D` and traverse `D` -> `B`, then `B/C/D` must be in one SCC
  118. /** @type {Set<SCC<T>>} */
  119. const sccsToMerge = new Set();
  120. for (
  121. let i = stack.length - 1;
  122. stack[i].node.scc !== depSCC;
  123. i--
  124. ) {
  125. sccsToMerge.add(stack[i].node.scc);
  126. }
  127. for (const sccToMerge of sccsToMerge) {
  128. for (const nodeInMergedSCC of sccToMerge.nodes) {
  129. nodeInMergedSCC.scc = depSCC;
  130. depSCC.nodes.add(nodeInMergedSCC);
  131. }
  132. }
  133. break;
  134. }
  135. case CANDIDATE_MARKER:
  136. // This finished SCC was previously considered as root SCC
  137. // We just found a new incoming edge, so it is no longer a candidate
  138. rootSCCs.delete(/** @type {SCC<T>} */ (depSCC));
  139. depSCC.marker = DONE_MARKER;
  140. break;
  141. case DONE_MARKER:
  142. // Already finalized and not a candidate
  143. break;
  144. }
  145. } else {
  146. // All dependencies of the current node have been processed
  147. // So we leave the node
  148. stack.pop();
  149. // Mark an SCC as DONE only when the popped node is the last
  150. // node from that SCC remaining on the current stack.
  151. // A -> B -> C -> D
  152. // ^ |
  153. // |_________|
  154. // If `B` is popped and the new stack top is `A`, they are in
  155. // different SCCs, so B's SCC can be finalized.
  156. if (
  157. stack.length &&
  158. topOfStack.node.scc !== stack[stack.length - 1].node.scc
  159. ) {
  160. topOfStack.node.scc.marker = DONE_MARKER;
  161. }
  162. }
  163. }
  164. const scc = selectedNode.scc;
  165. // This SCC is complete and currently has no known incoming edge
  166. scc.marker = CANDIDATE_MARKER;
  167. rootSCCs.add(scc);
  168. }
  169. }
  170. /** @type {Set<T>} */
  171. const rootNodes = new Set();
  172. // For each root SCC, we select node with the most incoming edges
  173. // from within the same SCC
  174. for (const scc of rootSCCs) {
  175. let max = 0;
  176. /** @type {Nodes<T>} */
  177. const nodes = new Set(scc.nodes);
  178. for (const node of scc.nodes) {
  179. for (const dep of node.dependencies) {
  180. if (scc.nodes.has(dep)) {
  181. dep.incoming++;
  182. if (dep.incoming < max) continue;
  183. if (dep.incoming > max) {
  184. nodes.clear();
  185. max = dep.incoming;
  186. }
  187. nodes.add(dep);
  188. }
  189. }
  190. }
  191. for (const node of nodes) {
  192. rootNodes.add(node.item);
  193. }
  194. }
  195. // When root nodes were found, return them
  196. if (rootNodes.size > 0) return rootNodes;
  197. throw new Error("Implementation of findGraphRoots is broken");
  198. };