Skip to content

Commit 8f45b5d

Browse files
authored
Add files via upload
1 parent 3b4653f commit 8f45b5d

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

Diff for: Backtracking/tests/HeldKarpAlgorithm.test.js

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
2+
import { heldKarp } from '../HeldKarpAlgorithm'
3+
4+
describe('Held-Karp Algorithm - TSP Solver', () => {
5+
it('Test Case 1 - 4 cities (Simple)', () => {
6+
const distanceMatrix = [
7+
[0, 10, 15, 20],
8+
[10, 0, 35, 25],
9+
[15, 35, 0, 30],
10+
[20, 25, 30, 0]
11+
]
12+
const result = heldKarp(distanceMatrix)
13+
expect(result).toBe(80)
14+
})
15+
16+
it('Test Case 2 - 3 cities', () => {
17+
const distanceMatrix = [
18+
[0, 5, 10],
19+
[5, 0, 15],
20+
[10, 15, 0]
21+
]
22+
const result = heldKarp(distanceMatrix)
23+
expect(result).toBe(30)
24+
})
25+
26+
it('Test Case 3 - 5 cities', () => {
27+
const distanceMatrix = [
28+
[0, 10, 15, 20, 25],
29+
[10, 0, 35, 25, 30],
30+
[15, 35, 0, 30, 20],
31+
[20, 25, 30, 0, 10],
32+
[25, 30, 20, 10, 0]
33+
]
34+
const result = heldKarp(distanceMatrix)
35+
expect(result).toBe(95)
36+
})
37+
38+
it('Test Case 4 - 1 city', () => {
39+
const distanceMatrix = [
40+
[0]
41+
]
42+
const result = heldKarp(distanceMatrix)
43+
expect(result).toBe(0)
44+
})
45+
46+
it('Test Case 5 - 2 cities', () => {
47+
const distanceMatrix = [
48+
[0, 5],
49+
[5, 0]
50+
]
51+
const result = heldKarp(distanceMatrix)
52+
expect(result).toBe(10)
53+
})
54+
55+
it('Test Case 6 - Equal costs (4 cities)', () => {
56+
const distanceMatrix = [
57+
[0, 1, 1, 1],
58+
[1, 0, 1, 1],
59+
[1, 1, 0, 1],
60+
[1, 1, 1, 0]
61+
]
62+
const result = heldKarp(distanceMatrix)
63+
expect(result).toBe(4)
64+
})
65+
66+
it('Test Case 7 - Custom distances', () => {
67+
const distanceMatrix = [
68+
[0, 10, 20, 30],
69+
[5, 0, 15, 25],
70+
[10, 5, 0, 20],
71+
[20, 15, 10, 0]
72+
]
73+
const result = heldKarp(distanceMatrix)
74+
expect(result).toBe(55)
75+
})
76+
77+
it('Test Case 8 - Large distances', () => {
78+
const distanceMatrix = [
79+
[0, 1000, 2000, 3000],
80+
[1000, 0, 1500, 2500],
81+
[2000, 1500, 0, 3500],
82+
[3000, 2500, 3500, 0]
83+
]
84+
const result = heldKarp(distanceMatrix)
85+
expect(result).toBe(8000)
86+
})
87+
})

0 commit comments

Comments
 (0)