-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathDijkstraSmallestPath.test.js
More file actions
30 lines (25 loc) · 823 Bytes
/
DijkstraSmallestPath.test.js
File metadata and controls
30 lines (25 loc) · 823 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { solve } from '../DijkstraSmallestPath.js'
// Graph represented as adjacency object where graph[u][v] = weight
// Example graph:
// A --1-- B --2-- C
// \3 \
// \ 5
// \--4-- D -----
// Shortest distances from A: A:0, B:1, C:3 (A->B->C), D:4 (A->D)
test('DijkstraSmallestPath returns shortest distances and paths', () => {
const graph = {
A: { B: 1, D: 4 },
B: { A: 1, C: 2 },
C: { B: 2, D: 5 },
D: { A: 4, C: 5 }
}
const res = solve(graph, 'A')
expect(res.A.dist).toBe(0)
expect(res.B.dist).toBe(1)
expect(res.C.dist).toBe(3)
expect(res.D.dist).toBe(4)
// Path arrays exclude the source by implementation (solutions[s] = [])
expect([...res.B]).toEqual(['B'])
expect([...res.C]).toEqual(['B', 'C'])
expect([...res.D]).toEqual(['D'])
})