-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Copy pathHeldKarpAlgorithm.test.js
87 lines (78 loc) · 2.31 KB
/
HeldKarpAlgorithm.test.js
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { heldKarp } from '../HeldKarpAlgorithm'
describe('Held-Karp Algorithm - TSP Solver', () => {
it('Test Case 1 - 4 cities (Simple)', () => {
const distanceMatrix = [
[0, 10, 15, 20],
[10, 0, 35, 25],
[15, 35, 0, 30],
[20, 25, 30, 0]
]
const result = heldKarp(distanceMatrix)
expect(result).toBe(80)
})
it('Test Case 2 - 3 cities', () => {
const distanceMatrix = [
[0, 5, 10],
[5, 0, 15],
[10, 15, 0]
]
const result = heldKarp(distanceMatrix)
expect(result).toBe(30)
})
it('Test Case 3 - 5 cities', () => {
const distanceMatrix = [
[0, 10, 15, 20, 25],
[10, 0, 35, 25, 30],
[15, 35, 0, 30, 20],
[20, 25, 30, 0, 10],
[25, 30, 20, 10, 0]
]
const result = heldKarp(distanceMatrix)
expect(result).toBe(95)
})
it('Test Case 4 - 1 city', () => {
const distanceMatrix = [
[0]
]
const result = heldKarp(distanceMatrix)
expect(result).toBe(0)
})
it('Test Case 5 - 2 cities', () => {
const distanceMatrix = [
[0, 5],
[5, 0]
]
const result = heldKarp(distanceMatrix)
expect(result).toBe(10)
})
it('Test Case 6 - Equal costs (4 cities)', () => {
const distanceMatrix = [
[0, 1, 1, 1],
[1, 0, 1, 1],
[1, 1, 0, 1],
[1, 1, 1, 0]
]
const result = heldKarp(distanceMatrix)
expect(result).toBe(4)
})
it('Test Case 7 - Custom distances', () => {
const distanceMatrix = [
[0, 10, 20, 30],
[5, 0, 15, 25],
[10, 5, 0, 20],
[20, 15, 10, 0]
]
const result = heldKarp(distanceMatrix)
expect(result).toBe(55)
})
it('Test Case 8 - Large distances', () => {
const distanceMatrix = [
[0, 1000, 2000, 3000],
[1000, 0, 1500, 2500],
[2000, 1500, 0, 3500],
[3000, 2500, 3500, 0]
]
const result = heldKarp(distanceMatrix)
expect(result).toBe(8000)
})
})