Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[FEATURE]: Add Kahn's Algorithm in Graphs #1795#1796

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
Virus2hell wants to merge1 commit intoTheAlgorithms:master
base:master
Choose a base branch
Loading
fromVirus2hell:feat/add-kahns-algorithm
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletionsGraphs/KahnsAlgorithm.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
export function kahnTopologicalSort(V, edges) {
const adj = Array.from({ length: V }, () => [])
const indegree = new Array(V).fill(0)

for (const [u, v] of edges) {
adj[u].push(v)
indegree[v]++
}

const queue = []
for (let i = 0; i < V; i++) {
if (indegree[i] === 0) queue.push(i)
}

const topoOrder = []
let idx = 0
while (idx < queue.length) {
const node = queue[idx++]
topoOrder.push(node)

for (const nei of adj[node]) {
indegree[nei]--
if (indegree[nei] === 0) queue.push(nei)
}
}

if (topoOrder.length !== V) return []
return topoOrder
}
62 changes: 62 additions & 0 deletionsGraphs/__tests__/KahnsAlgorithm.test.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
import { kahnTopologicalSort } from '../KahnsAlgorithm.js'

describe("Kahn's Algorithm - Topological Sort", () => {
test('returns a valid topological order for a DAG', () => {
const V = 6
const edges = [
[5, 2],
[5, 0],
[4, 0],
[4, 1],
[2, 3],
[3, 1]
]

const order = kahnTopologicalSort(V, edges)
expect(order.length).toBe(V)

// verify topological property
const pos = new Array(V)
for (let i = 0; i < order.length; i++) pos[order[i]] = i

for (const [u, v] of edges) {
expect(pos[u]).toBeLessThan(pos[v])
}
})

test('returns empty array when graph contains a cycle', () => {
const V = 3
const edges = [
[0, 1],
[1, 2],
[2, 0] // cycle
]
const order = kahnTopologicalSort(V, edges)
expect(order).toEqual([])
})

test('includes isolated nodes', () => {
const V = 4
const edges = [
[0, 1],
[2, 3]
]

const order = kahnTopologicalSort(V, edges)
expect(order.length).toBe(V)

const pos = new Array(V)
for (let i = 0; i < order.length; i++) pos[order[i]] = i

for (const [u, v] of edges) {
expect(pos[u]).toBeLessThan(pos[v])
}
})

test('works with empty graph', () => {
const V = 0
const edges = []
const order = kahnTopologicalSort(V, edges)
expect(order).toEqual([])
})
})

[8]ページ先頭

©2009-2025 Movatter.jp