Skip to content

Commit

Permalink
Fix bug in shortest path implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyboycjr committed Jan 22, 2024
1 parent 3990f75 commit 7eeb292
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ void sssp_bfs(void) {
myself->prevedge = NULL;
myself->via = myself;
myself->distance = 0;
myself->weighted_distance = 0;
list_insert_head(todo_list, myself);

/* Loop while todo_list is filled */
Expand Down Expand Up @@ -178,14 +179,15 @@ void sssp_bfs(void) {

if(e->to->status.visited
&& (!e->to->status.indirect || indirect)
&& (e->to->distance != n->distance + 1 || e->weight >= e->to->prevedge->weight)) {
&& (e->to->distance != n->distance + 1 || e->to->weighted_distance <= n->weighted_distance + e->weight)) {
continue;
}

// Only update nexthop if it doesn't increase the path length

if(!e->to->status.visited || (e->to->distance == n->distance + 1 && e->weight < e->to->prevedge->weight)) {
if(!e->to->status.visited || (e->to->distance == n->distance + 1 && e->to->weighted_distance > n->weighted_distance + e->weight)) {
e->to->nexthop = (n->nexthop == myself) ? e->to : n->nexthop;
e->to->weighted_distance = n->weighted_distance + e->weight;
}

e->to->status.visited = true;
Expand Down
1 change: 1 addition & 0 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ typedef struct node_t {
compression_level_t outcompression; /* Compression level, 0 = no compression */

int distance;
int weighted_distance;
struct node_t *nexthop; /* nearest node from us to him */
struct edge_t *prevedge; /* nearest node from him to us */
struct node_t *via; /* next hop for UDP packets */
Expand Down

0 comments on commit 7eeb292

Please sign in to comment.