Skip to content

Commit e123b19

Browse files
committed
[FEATURE]: Add Kahn's Algorithm in Graphs #1795
1 parent a360b19 commit e123b19

File tree

2 files changed

+16
-45
lines changed

2 files changed

+16
-45
lines changed

Graphs/KahnsAlgorithm.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,14 @@
2323
*/
2424

2525
function kahnTopologicalSort(V, edges) {
26-
// Build adjacency list and indegree array
2726
const adj = Array.from({ length: V }, () => []);
2827
const indegree = new Array(V).fill(0);
2928

3029
for (const [u, v] of edges) {
31-
if (u < 0 || u >= V || v < 0 || v >= V) {
32-
throw new Error('Edge contains vertex outside range 0..V-1');
33-
}
3430
adj[u].push(v);
3531
indegree[v]++;
3632
}
3733

38-
// Initialize queue with nodes of indegree 0
3934
const queue = [];
4035
for (let i = 0; i < V; i++) {
4136
if (indegree[i] === 0) queue.push(i);
@@ -44,7 +39,7 @@ function kahnTopologicalSort(V, edges) {
4439
const topoOrder = [];
4540
let idx = 0;
4641
while (idx < queue.length) {
47-
const node = queue[idx++]; // treat array as queue
42+
const node = queue[idx++];
4843
topoOrder.push(node);
4944

5045
for (const nei of adj[node]) {
@@ -53,10 +48,10 @@ function kahnTopologicalSort(V, edges) {
5348
}
5449
}
5550

56-
// If topoOrder size != V, graph has a cycle
51+
// Return empty array if cycle detected
5752
if (topoOrder.length !== V) return [];
58-
5953
return topoOrder;
6054
}
6155

6256
module.exports = { kahnTopologicalSort };
57+

Graphs/test/KahnsAlgorithm.test.js

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,29 @@
11
const { kahnTopologicalSort } = require('../KahnsAlgorithm');
22

3-
describe("Kahn's Algorithm - Topological Sort", () => {
4-
test('returns a valid topological order for a DAG', () => {
3+
describe("Kahn's Algorithm", () => {
4+
test('DAG returns valid topological order', () => {
55
const V = 6;
6-
const edges = [
7-
[5, 2],
8-
[5, 0],
9-
[4, 0],
10-
[4, 1],
11-
[2, 3],
12-
[3, 1],
13-
];
14-
6+
const edges = [[5,2],[5,0],[4,0],[4,1],[2,3],[3,1]];
157
const order = kahnTopologicalSort(V, edges);
168
expect(order.length).toBe(V);
17-
18-
// verify topological property
19-
const pos = new Array(V);
9+
const pos = Array(V);
2010
for (let i = 0; i < order.length; i++) pos[order[i]] = i;
21-
22-
for (const [u, v] of edges) {
23-
expect(pos[u]).toBeLessThan(pos[v]);
24-
}
11+
for (const [u,v] of edges) expect(pos[u]).toBeLessThan(pos[v]);
2512
});
2613

27-
test('returns empty array when graph contains a cycle', () => {
14+
test('Cycle returns empty array', () => {
2815
const V = 3;
29-
const edges = [
30-
[0, 1],
31-
[1, 2],
32-
[2, 0] // cycle
33-
];
34-
const order = kahnTopologicalSort(V, edges);
35-
expect(order).toEqual([]);
16+
const edges = [[0,1],[1,2],[2,0]];
17+
expect(kahnTopologicalSort(V, edges)).toEqual([]);
3618
});
3719

38-
test('handles isolated nodes', () => {
20+
test('Includes isolated nodes', () => {
3921
const V = 4;
40-
const edges = [
41-
[0, 1],
42-
[2, 3]
43-
];
22+
const edges = [[0,1],[2,3]];
4423
const order = kahnTopologicalSort(V, edges);
45-
expect(order.length).toBe(4);
46-
47-
const pos = new Array(V);
24+
expect(order.length).toBe(V);
25+
const pos = Array(V);
4826
for (let i = 0; i < order.length; i++) pos[order[i]] = i;
49-
for (const [u, v] of edges) {
50-
expect(pos[u]).toBeLessThan(pos[v]);
51-
}
27+
for (const [u,v] of edges) expect(pos[u]).toBeLessThan(pos[v]);
5228
});
5329
});

0 commit comments

Comments
 (0)