-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Feature: Add-on of Held-karp algorithm in backtracking folder #1744
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
Held-karp algorithm (https://en.wikipedia.org/wiki/Held-Karp_algorithm) | ||
|
||
- Held-karp algorithm solve TSP problem using dynamic programming paradigm. | ||
- It computes the minimum cost to visit each city exactly once & return to start point. | ||
considering all subsets of cities & using memoization. | ||
- Comparing it with hamiltonian algo. vs Held-karp algo. ( n! vs ~2^n ) this is more efficient. | ||
|
||
*/ | ||
|
||
function heldKarp(dist) { | ||
const n = dist.length; | ||
const memo = Array.from({ length: n }, () => Array(1 << n).fill(null)); | ||
|
||
// Base case: distance from the starting point to itself is 0 | ||
for (let i = 0; i < n; i++) { | ||
memo[i][1 << i] = dist[i][0]; | ||
} | ||
|
||
// Iterate through all subsets of vertices | ||
for (let mask = 0; mask < (1 << n); mask++) { | ||
for (let u = 0; u < n; u++) { | ||
if (!(mask & (1 << u))) continue; // u must be in the subset | ||
// Iterate through all vertices to find the minimum cost | ||
for (let v = 0; v < n; v++) { | ||
if (mask & (1 << v) || u === v) continue; // v must not be in the subset | ||
const newMask = mask | (1 << v); | ||
const newCost = memo[u][mask] + dist[u][v]; | ||
|
||
if (memo[v][newMask] === null || newCost < memo[v][newMask]) { | ||
memo[v][newMask] = newCost; | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Get the minimum cost to complete the tour | ||
let minCost = Infinity; | ||
for (let u = 1; u < n; u++) { | ||
const cost = memo[u][(1 << n) - 1] + dist[u][0]; | ||
minCost = Math.min(minCost, cost); | ||
} | ||
|
||
return minCost; | ||
} | ||
|
||
module.exports = { heldKarp }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
|
||
|
||
const { heldKarp } = require('../held_karp'); | ||
|
||
const distanceMatrix1 = [ | ||
[0, 10, 15, 20], | ||
[10, 0, 35, 25], | ||
[15, 35, 0, 30], | ||
[20, 25, 30, 0] | ||
]; | ||
console.log("Test Case 1 - Minimum cost of visiting all cities:", heldKarp(distanceMatrix1)); | ||
|
||
const distanceMatrix2 = [ | ||
[0, 5, 10], | ||
[5, 0, 15], | ||
[10, 15, 0] | ||
]; | ||
console.log("Test Case 2 - Minimum cost of visiting all cities:", heldKarp(distanceMatrix2)); | ||
|
||
const distanceMatrix3 = [ | ||
[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] | ||
]; | ||
console.log("Test Case 3 - Minimum cost of visiting all cities:", heldKarp(distanceMatrix3)); | ||
|
||
const distanceMatrix4 = [ | ||
[0] | ||
]; | ||
console.log("Test Case 4 - Minimum cost of visiting all cities:", heldKarp(distanceMatrix4)); | ||
|
||
const distanceMatrix5 = [ | ||
[0, 5], | ||
[5, 0] | ||
]; | ||
console.log("Test Case 5 - Minimum cost of visiting all cities:", heldKarp(distanceMatrix5)); | ||
|
||
const distanceMatrix6 = [ | ||
[0, 1, 1, 1], | ||
[1, 0, 1, 1], | ||
[1, 1, 0, 1], | ||
[1, 1, 1, 0] | ||
]; | ||
console.log("Test Case 6 - Minimum cost of visiting all cities:", heldKarp(distanceMatrix6)); | ||
|
||
const distanceMatrix7 = [ | ||
[0, 10, 20, 30], | ||
[5, 0, 15, 25], | ||
[10, 5, 0, 20], | ||
[20, 15, 10, 0] | ||
]; | ||
console.log("Test Case 7 - Minimum cost of visiting all cities:", heldKarp(distanceMatrix7)); | ||
|
||
const distanceMatrix8 = [ | ||
[0, 1000, 2000, 3000], | ||
[1000, 0, 1500, 2500], | ||
[2000, 1500, 0, 3500], | ||
[3000, 2500, 3500, 0] | ||
]; | ||
console.log("Test Case 8 - Minimum cost of visiting all cities:", heldKarp(distanceMatrix8)); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.