|
| 1 | +import Stack from '../../../data-structures/stack/Stack'; |
| 2 | +import depthFirstSearch from '../depth-first-search/depthFirstSearch'; |
| 3 | + |
| 4 | +/** |
| 5 | + * @param {Graph} graph |
| 6 | + * @return {Stack} |
| 7 | + */ |
| 8 | +function getVerticesSortedByDfsFinishTime(graph) { |
| 9 | + // Set of all visited vertices during DFS pass. |
| 10 | + const visitedVerticesSet = {}; |
| 11 | + |
| 12 | + // Stack of vertices by finish time. |
| 13 | + // All vertices in this stack are ordered by finished time in decreasing order. |
| 14 | + // Vertex that has been finished first will be at the bottom of the stack and |
| 15 | + // vertex that has been finished last will be at the top of the stack. |
| 16 | + const verticesByDfsFinishTime = new Stack(); |
| 17 | + |
| 18 | + // Set of all vertices we're going to visit. |
| 19 | + const notVisitedVerticesSet = {}; |
| 20 | + graph.getAllVertices().forEach((vertex) => { |
| 21 | + notVisitedVerticesSet[vertex.getKey()] = vertex; |
| 22 | + }); |
| 23 | + |
| 24 | + // Specify DFS traversal callbacks. |
| 25 | + const dfsCallbacks = { |
| 26 | + enterVertex: ({ currentVertex }) => { |
| 27 | + // Add current vertex to visited set. |
| 28 | + visitedVerticesSet[currentVertex.getKey()] = currentVertex; |
| 29 | + |
| 30 | + // Delete current vertex from not visited set. |
| 31 | + delete notVisitedVerticesSet[currentVertex.getKey()]; |
| 32 | + }, |
| 33 | + leaveVertex: ({ currentVertex }) => { |
| 34 | + // Push vertex to the stack when leaving it. |
| 35 | + // This will make stack to be ordered by finish time in decreasing order. |
| 36 | + verticesByDfsFinishTime.push(currentVertex); |
| 37 | + }, |
| 38 | + allowTraversal: ({ nextVertex }) => { |
| 39 | + // Don't allow to traverse the nodes that have been already visited. |
| 40 | + return !visitedVerticesSet[nextVertex.getKey()]; |
| 41 | + }, |
| 42 | + }; |
| 43 | + |
| 44 | + // Do FIRST DFS PASS traversal for all graph vertices to fill the verticesByFinishTime stack. |
| 45 | + while (Object.values(notVisitedVerticesSet).length) { |
| 46 | + // Peek any vertex to start DFS traversal from. |
| 47 | + const startVertexKey = Object.keys(notVisitedVerticesSet)[0]; |
| 48 | + const startVertex = notVisitedVerticesSet[startVertexKey]; |
| 49 | + delete notVisitedVerticesSet[startVertexKey]; |
| 50 | + |
| 51 | + depthFirstSearch(graph, startVertex, dfsCallbacks); |
| 52 | + } |
| 53 | + |
| 54 | + return verticesByDfsFinishTime; |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * @param {Graph} graph |
| 59 | + * @param {Stack} verticesByFinishTime |
| 60 | + * @return {*[]} |
| 61 | + */ |
| 62 | +function getSCCSets(graph, verticesByFinishTime) { |
| 63 | + // Array of arrays of strongly connected vertices. |
| 64 | + const stronglyConnectedComponentsSets = []; |
| 65 | + |
| 66 | + // Array that will hold all vertices that are being visited during one DFS run. |
| 67 | + let stronglyConnectedComponentsSet = []; |
| 68 | + |
| 69 | + // Visited vertices set. |
| 70 | + const visitedVerticesSet = {}; |
| 71 | + |
| 72 | + // Callbacks for DFS traversal. |
| 73 | + const dfsCallbacks = { |
| 74 | + enterVertex: ({ currentVertex }) => { |
| 75 | + // Add current vertex to SCC set of current DFS round. |
| 76 | + stronglyConnectedComponentsSet.push(currentVertex); |
| 77 | + |
| 78 | + // Add current vertex to visited set. |
| 79 | + visitedVerticesSet[currentVertex.getKey()] = currentVertex; |
| 80 | + }, |
| 81 | + leaveVertex: ({ previousVertex }) => { |
| 82 | + // Once DFS traversal is finished push the set of found strongly connected |
| 83 | + // components during current DFS round to overall strongly connected components set. |
| 84 | + // The sign that traversal is about to be finished is that we came back to start vertex |
| 85 | + // which doesn't have parent. |
| 86 | + if (previousVertex === null) { |
| 87 | + stronglyConnectedComponentsSets.push([...stronglyConnectedComponentsSet]); |
| 88 | + } |
| 89 | + }, |
| 90 | + allowTraversal: ({ nextVertex }) => { |
| 91 | + // Don't allow traversal of already visited vertices. |
| 92 | + return !visitedVerticesSet[nextVertex.getKey()]; |
| 93 | + }, |
| 94 | + }; |
| 95 | + |
| 96 | + while (!verticesByFinishTime.isEmpty()) { |
| 97 | + /** @var {GraphVertex} startVertex */ |
| 98 | + const startVertex = verticesByFinishTime.pop(); |
| 99 | + |
| 100 | + // Reset the set of strongly connected vertices. |
| 101 | + stronglyConnectedComponentsSet = []; |
| 102 | + |
| 103 | + // Don't do DFS on already visited vertices. |
| 104 | + if (!visitedVerticesSet[startVertex.getKey()]) { |
| 105 | + // Do DFS traversal. |
| 106 | + depthFirstSearch(graph, startVertex, dfsCallbacks); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return stronglyConnectedComponentsSets; |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * Kosaraju's algorithm. |
| 115 | + * |
| 116 | + * @param {Graph} graph |
| 117 | + * @return {*[]} |
| 118 | + */ |
| 119 | +export default function stronglyConnectedComponents(graph) { |
| 120 | + // In this algorithm we will need to do TWO DFS PASSES overt the graph. |
| 121 | + |
| 122 | + // Get stack of vertices ordered by DFS finish time. |
| 123 | + // All vertices in this stack are ordered by finished time in decreasing order: |
| 124 | + // Vertex that has been finished first will be at the bottom of the stack and |
| 125 | + // vertex that has been finished last will be at the top of the stack. |
| 126 | + const verticesByFinishTime = getVerticesSortedByDfsFinishTime(graph); |
| 127 | + |
| 128 | + // Reverse the graph. |
| 129 | + graph.reverse(); |
| 130 | + |
| 131 | + // Do DFS once again on reversed graph. |
| 132 | + return getSCCSets(graph, verticesByFinishTime); |
| 133 | +} |
0 commit comments