-
-
Notifications
You must be signed in to change notification settings - Fork 439
feat: add tarjan algorithm for strongly connected components #220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* @function tarjan | ||
* @description Given a graph, find the strongly connected components(SCC). A set of nodes form a SCC if there is a path between all pairs of points within that set. | ||
* @Complexity_Analysis | ||
* Time complexity: O(V + E). We perform a DFS of (V + E) | ||
* Space Complexity: O(V). We hold numerous structures all of which at worst holds O(V) nodes. | ||
* @param {[number, number][][]} graph - The graph in adjacency list form | ||
* @return {number[][]} - An array of SCCs, where an SCC is an array with the indices of each node within that SCC. | ||
* @see https://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm | ||
*/ | ||
export const tarjan = (graph: number[][]): number[][] => { | ||
const dfs = (node: number) => { | ||
discovery[node] = index; | ||
low[node] = index; | ||
++index; | ||
stack.push(node); | ||
stackContains[node] = true; | ||
|
||
for (const child of graph[node]) { | ||
if (low[child] === -1) { | ||
dfs(child); | ||
if (low[child] < low[node]) { | ||
// Child node loops back to this node's ancestor. Update the low node. | ||
low[node] = low[child]; | ||
} | ||
} else if (stackContains[child] && low[node] > discovery[child]) { | ||
// Found a backedge. Update the low for this node if needed. | ||
low[node] = discovery[child]; | ||
} | ||
} | ||
|
||
if (discovery[node] == low[node]) { | ||
// node is the root of a SCC. Gather the SCC's nodes from the stack. | ||
let scc: number[] = []; | ||
let i = stack.length - 1; | ||
while (stack[i] != node) { | ||
appgurueu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
scc.push(stack[i]); | ||
stackContains[stack[i]] = false; | ||
stack.pop(); | ||
--i; | ||
} | ||
scc.push(stack[i]); | ||
stack.pop(); | ||
stackContains[stack[i]] = false; | ||
sccs.push(scc); | ||
} | ||
} | ||
|
||
if (graph.length === 0) { | ||
return []; | ||
} | ||
|
||
let index = 0; | ||
// The order in which we discover nodes | ||
let discovery: number[] = Array(graph.length).fill(-1); | ||
// For each node, holds the furthest ancestor it can reach | ||
let low: number[] = Array(graph.length).fill(-1); | ||
appgurueu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Holds the nodes we have visited in a DFS traversal and are considering to group into a SCC | ||
let stack: number[] = []; | ||
// Holds the elements in the stack. | ||
let stackContains = Array(graph.length).fill(false); | ||
let sccs: number[][] = []; | ||
|
||
appgurueu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for (let i = 0; i < graph.length; ++i) { | ||
if (low[i] === -1) { | ||
dfs(i); | ||
} | ||
} | ||
return sccs; | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { tarjan } from "../tarjan"; | ||
|
||
describe("tarjan", () => { | ||
appgurueu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
it("it should return no sccs for empty graph", () => { | ||
expect(tarjan([])).toStrictEqual([]); | ||
}); | ||
|
||
it("it should return one scc for graph with one element", () => { | ||
expect(tarjan([[]])).toStrictEqual([[0]]); | ||
}); | ||
|
||
it("it should return one scc for graph with element that points to itself", () => { | ||
expect(tarjan([[0]])).toStrictEqual([[0]]); | ||
}); | ||
|
||
it("it should return one scc for two element graph with cycle", () => { | ||
expect(tarjan([[1], [0]])).toStrictEqual([[1, 0]]); | ||
}); | ||
|
||
it("should return one scc for each element for straight line", () => { | ||
expect(tarjan([[1], [2], [3], []])).toStrictEqual([[3], [2], [1], [0]]); | ||
}); | ||
|
||
it("should return sccs for straight line with backedge in middle", () => { | ||
expect(tarjan([[1], [2], [3, 0], []])).toStrictEqual([[3], [2, 1, 0]]); | ||
}); | ||
|
||
it("should return sccs for straight line with backedge from end to middle", () => { | ||
expect(tarjan([[1], [2], [3], [1]])).toStrictEqual([[3, 2, 1], [0]]); | ||
}); | ||
|
||
it("should return scc for each element for graph with no edges", () => { | ||
expect(tarjan([[], [], [], []])).toStrictEqual([[0], [1], [2], [3]]); | ||
}); | ||
|
||
it("should return sccs disconnected graph", () => { | ||
expect(tarjan([[1, 2], [0, 2], [0, 1], []])).toStrictEqual([[2, 1, 0], [3]]); | ||
}); | ||
|
||
it("should return sccs disconnected graph", () => { | ||
expect(tarjan([[1, 2], [0, 2], [0, 1], [4], [5], [3]])).toStrictEqual([[2, 1, 0], [5, 4, 3]]); | ||
}); | ||
|
||
it("should return single scc", () => { | ||
expect(tarjan([[1], [2], [3], [0, 4], [3]])).toStrictEqual([[4, 3, 2, 1, 0]]); | ||
}); | ||
|
||
it("should return one scc for complete connected graph", () => { | ||
const input = [[1, 2, 3, 4], [0, 2, 3, 4], [0, 1, 3, 4], [0, 1, 2, 4], [0, 1, 2, 3]]; | ||
expect(tarjan(input)).toStrictEqual([[4, 3, 2, 1, 0]]); | ||
}); | ||
|
||
it("should return sccs", () => { | ||
const input = [[1], [2], [0, 3], [4], []]; | ||
expect(tarjan(input)).toStrictEqual([[4], [3], [2, 1, 0]]); | ||
}); | ||
|
||
it("should return sccs", () => { | ||
const input = [[1], [2], [0, 3, 4], [0], [5], [6, 7], [2, 4], [8], [5, 9], [5]]; | ||
const expected = [[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]]; | ||
expect(tarjan(input)).toStrictEqual(expected); | ||
}); | ||
|
||
it("should return sccs", () => { | ||
const input = [[1], [0, 2], [0, 3], [4], [5, 7], [6], [4, 7], []]; | ||
const expected = [[7], [6, 5, 4], [3], [2, 1, 0]]; | ||
expect(tarjan(input)).toStrictEqual(expected); | ||
}); | ||
|
||
it("should return sccs where first scc cannot reach second scc", () => { | ||
const input = [[1], [2], [0], [4], [5], [2, 3]]; | ||
const expected = [[2, 1, 0], [5, 4, 3]]; | ||
expect(tarjan(input)).toStrictEqual(expected); | ||
}); | ||
|
||
}) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.