Change the TSP Held-Karp algorithm so we don't need to go back to the source
I need to solve a problem where I had to find the shortest way to connect all points starting from the distance matrix. It's almost like a traveling salesman problem, except that I don't have to close my path back to where I started. I found Held-Karp (Python) algorithm that solves TSP very well, but always calculates distances back to the starting point. So now it leaves me 3 questions:
- Could at least one situation have a different result if I change my function to not go back to the starting point?
- If the answer to 1 is yes, how can I modify my held_karp () function to fit my needs?
- In 2 no, what should I look for next?
I have translated the function held_karp()
from Python to PHP and I would be happy to use any language for my solution.
function held_karp($matrix) {
$nb_nodes = count($matrix);
# Maps each subset of the nodes to the cost to reach that subset, as well
# as what node it passed before reaching this subset.
# Node subsets are represented as set bits.
$c = [];
# Set transition cost from initial state
for($k = 1; $k < $nb_nodes; $k++) $c["(".(1 << $k).",$k)"] = [$matrix[0][$k], 0];
# Iterate subsets of increasing length and store intermediate results
# in classic dynamic programming manner
for($subset_size = 2; $subset_size < $nb_nodes; $subset_size++) {
$combinaisons = every_combinations(range(1, $nb_nodes - 1), $subset_size, false);
foreach($combinaisons AS $subset) {
# Set bits for all nodes in this subset
$bits = 0;
foreach($subset AS $bit) $bits |= 1 << $bit;
# Find the lowest cost to get to this subset
foreach($subset AS $bk) {
$prev = $bits & ~(1 << $bk);
$res = [];
foreach($subset AS $m) {
if(($m == 0)||($m == $bk)) continue;
$res[] = [$c["($prev,$m)"][0] + $matrix[$m][$bk], $m];
}
$c["($bits,$bk)"] = min($res);
}
}
}
# We're interested in all bits but the least significant (the start state)
$bits = (2**$nb_nodes - 1) - 1;
# Calculate optimal cost
$res = [];
for($k = 1; $k < $nb_nodes; $k++) $res[] = [$c["($bits,$k)"][0] + $matrix[$k][0], $k];
list($opt, $parent) = min($res);
# Backtrack to find full path
$path = [];
for($i = 0; $i < $nb_nodes - 1; $i++) {
$path[] = $parent;
$new_bits = $bits & ~(1 << $parent);
list($scrap, $parent) = $c["($bits,$parent)"];
$bits = $new_bits;
}
# Add implicit start state
$path[] = 0;
return [$opt, array_reverse($path)];
}
If you need to know how every_combinations () function works
function every_combinations($set, $n = NULL, $order_matters = true) {
if($n == NULL) $n = count($set);
$combinations = [];
foreach($set AS $k => $e) {
$subset = $set;
unset($subset[$k]);
if($n == 1) $combinations[] = [$e];
else {
$subcomb = every_combinations($subset, $n - 1, $order_matters);
foreach($subcomb AS $s) {
$comb = array_merge([$e], $s);
if($order_matters) $combinations[] = $comb;
else {
$needle = $comb;
sort($needle);
if(!in_array($needle, $combinations)) $combinations[] = $comb;
}
}
}
}
return $combinations;
}
source to share
Yes, the answer may be different. For example, if the graph has 4 vertices and the following undirected edges:
1-2 1 2-3 1 3-4 1 1-4 100 1-3 2 2-4 2
The optimal path 1-2-3-4
with weight 1 + 1 + 1 = 3, but the weight of the same cycle is 1 + 1 + 1 + 100 = 103. However, the weight of the cycle 1-3-4-2
is 2 + 1 + 2 + 1 = 6, and the weight of this path is 2 + 1 + 2 = 5, so the optimal cycle and optimal path are different.
If you are looking for a path, not a cycle, you can use the same algorithm, but you do not need to add the weight of the last edge to the start vertex, i.e.
for($k = 1; $k < $nb_nodes; $k++) $res[] = [$c["($bits,$k)"][0] + $matrix[$k][0], $k];
it should be for($k = 1; $k < $nb_nodes; $k++) $res[] = [$c["($bits,$k)"][0], $k];
source to share