Skip to content

Commit

Permalink
Fix for shortest Hamiltonian Path Problem, array access
Browse files Browse the repository at this point in the history
  • Loading branch information
leo848 committed Apr 2, 2024
1 parent 3bdf563 commit 975b6b6
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/path/create/held_karp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ pub fn solve<C: CreateContext>(ctx: C) -> C::Path {
ctx.send_edges(empty(), Some(0.0));
let mut dp_memo: Box<[Box<[f32]>]> =
vec![vec![f32::INFINITY; size].into_boxed_slice(); 1 << size].into_boxed_slice();
dp_memo[0][1] = 0.0;

for i in 0..size {
dp_memo[1 << i][i] = 0.0;
}

for mask in 1..1 << size {
if mask & ((1 << (size.saturating_sub(5))) - 1) == 0 {
Expand Down Expand Up @@ -63,7 +66,7 @@ pub fn solve<C: CreateContext>(ctx: C) -> C::Path {
let mut min_chain_length = f32::INFINITY;
for j in 0..size {
if j != new_last_node && (mask & (1 << j)) != 0 {
let cost = dp_memo[j][mask] + matrix[(j, new_last_node)];
let cost = dp_memo[mask][j] + matrix[(j, new_last_node)];
if cost < min_chain_length {
min_chain_length = cost;
next_node = j;
Expand Down

0 comments on commit 975b6b6

Please sign in to comment.