Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.8k
feat: add condensation function using Kosaraju's algorithm for SCCs#1819
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
anurag2204-k wants to merge4 commits intoTheAlgorithms:masterChoose a base branch fromanurag2204-k:my-feature-branch
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
+161 −0
Open
Changes fromall commits
Commits
Show all changes
4 commits Select commitHold shift + click to select a range
3174c61
feat: add condensation function using Kosaraju's algorithm for SCCs
anurag2204-kff6d7f6
test: add unit tests for condensation function in SCCs
anurag2204-kee2c4b7
style: format test cases in Condensation.test.js for consistency
anurag2204-k599570d
style: standardize formatting in Condensation.test.js
anurag2204-kFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
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,131 @@ | ||
/** | ||
* Condensation via Kosaraju's algorithm | ||
* | ||
* @see https://cp-algorithms.com/graph/strongly-connected-components.html | ||
* This file computes the strongly connected components (SCCs) and the | ||
* condensation (component DAG) for a directed graph given as an edge list. | ||
* It uses the project's existing Kosaraju implementation (Kosaraju.js). | ||
*/ | ||
import { kosaraju } from './Kosaraju.js' | ||
/** | ||
* Compute SCCs and the condensation graph (component DAG) for a directed graph. | ||
* @param {Array<Array<number>>} graph edges as [u, v] | ||
* @returns {{sccs: Array<Array<number>>, condensation: Array<Array<number>>}} | ||
*/ | ||
function condensation(graph) { | ||
// Using Kosaraju implementation to compute SCCs | ||
const sccs = kosaraju(graph) | ||
// Build adjacency map to include isolated nodes | ||
const g = {} | ||
const addNode = (n) => { | ||
if (!(n in g)) g[n] = new Set() | ||
} | ||
for (const [u, v] of graph) { | ||
addNode(u) | ||
addNode(v) | ||
g[u].add(v) | ||
} | ||
// Map node -> component index (string keys) | ||
const compIndex = {} | ||
for (let i = 0; i < sccs.length; i++) { | ||
for (const node of sccs[i]) compIndex[String(node)] = i | ||
} | ||
// Build condensation adjacency list (component graph) | ||
const condensationSets = Array.from({ length: sccs.length }, () => new Set()) | ||
for (const u of Object.keys(g)) { | ||
const ci = compIndex[u] | ||
if (ci === undefined) continue | ||
for (const v of g[u]) { | ||
const cj = compIndex[String(v)] | ||
if (cj === undefined) continue | ||
if (ci !== cj) condensationSets[ci].add(cj) | ||
} | ||
} | ||
const condensation = condensationSets.map((s) => Array.from(s)) | ||
return { sccs, condensation, compIndex } | ||
} | ||
export { condensation } | ||
/** | ||
* Time and Space Complexity | ||
* | ||
* Kosaraju's algorithm (finding SCCs) and condensation graph construction: | ||
* | ||
* - Time complexity: O(V + E) | ||
* - Building the adjacency list from the input edge list: O(E) | ||
* - Running Kosaraju's two DFS passes over all vertices and edges: O(V + E) | ||
* - Building the component index and condensation edges: O(V + E) | ||
* Overall: O(V + E), where V is the number of vertices and E is the number of edges. | ||
* | ||
* - Space complexity: O(V + E) | ||
* - Adjacency lists (graph): O(V + E) | ||
* - Kosaraju bookkeeping (stacks, visited sets, topo order, sccs, compIndex): O(V) | ||
* - Condensation adjacency structures: O(C + E') where C <= V and E' <= E, so | ||
* bounded by O(V + E). | ||
* Overall: O(V + E). | ||
* | ||
* Notes: | ||
* - Recursion may use O(V) call-stack space in the worst case during DFS. | ||
* - Constants depend on the data-structures used (Sets/Arrays), but the | ||
* asymptotic bounds above hold for this implementation. | ||
*/ | ||
/** | ||
* Visual diagram and usage | ||
* | ||
* Consider the directed edge list: | ||
* | ||
* edges = [ | ||
* [1,2], [2,3], [3,1], // cycle among 1,2,3 | ||
* [2,4], // edge from the first cycle to node 4 | ||
* [4,5], [5,6], [6,4] // cycle among 4,5,6 | ||
* ] | ||
* | ||
* Original graph (conceptual): | ||
* | ||
* 1 --> 2 --> 4 --> 5 | ||
* ^ | | | ||
* | v v | ||
* 3 <-- - 6 | ||
* | ||
* Strongly connected components (SCCs) in this graph: | ||
* | ||
* SCC A: [1, 2, 3] // a cycle 1->2->3->1 | ||
* SCC B: [4, 5, 6] // a cycle 4->5->6->4 | ||
* | ||
* Condensation graph (component DAG): | ||
* | ||
* [A] ---> [B] | ||
* | ||
* Where each node in the condensation is a whole SCC from the original graph. | ||
* | ||
* Example return values from `condensation(edges)`: | ||
* | ||
* { | ||
* sccs: [[1,2,3], [4,5,6]], | ||
* condensation: [[1], []], // component 0 has an edge to component 1 | ||
* compIndex: { '1': 0, '2': 0, '3': 0, '4': 1, '5': 1, '6': 1 } | ||
* } | ||
* | ||
* Usage: | ||
* | ||
* const edges = [[1,2],[2,3],[3,1],[2,4],[4,5],[5,6],[6,4]] | ||
* const { sccs, condensation, compIndex } = condensation(edges) | ||
* | ||
* Interpretation summary: | ||
* - `sccs` is an array of components; each component is an array of original nodes. | ||
* - `condensation` is the adjacency list of the component DAG; indices refer to `sccs`. | ||
* - `compIndex` maps an original node (string key) to its component index. | ||
* | ||
* This ASCII diagram and mapping should make it easy to visualize how the | ||
* condensation function groups cycles and produces a DAG of components. | ||
*/ |
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,30 @@ | ||
import { condensation } from '../Condensation.js' | ||
test('Test Case 1', () => { | ||
const graph = [ | ||
[1, 2], | ||
[2, 3], | ||
[3, 1], | ||
[2, 4], | ||
[4, 5], | ||
[5, 6], | ||
[6, 4] | ||
] | ||
const { sccs } = condensation(graph) | ||
expect(sccs).toStrictEqual([ | ||
[1, 3, 2], | ||
[4, 6, 5] | ||
]) | ||
}) | ||
test('Test Case 2', () => { | ||
const graph = [ | ||
[1, 2], | ||
[2, 3], | ||
[3, 1], | ||
[2, 4], | ||
[4, 5] | ||
] | ||
const { sccs } = condensation(graph) | ||
expect(sccs).toStrictEqual([[1, 3, 2], [4], [5]]) | ||
}) |
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.