c++ - Find the shortest path with Floyd algorithm -


i have adjacency matrix contains number 0s , 1s. if there no edge 1 node another, field 0, otherwise field marked 1.

then, if field in adjacency matrix 0, there no edge between nodes, otherwise there edge weight of 1.

now, have applied floyd algorithm find out shortest path node each other node. don't right solution.

here floyd algorithm implementation.

void floyd_warshal(int graph[nodes][nodes], int d[nodes][nodes]) {     (int = 0; < nodes; i++)     {         (int j = 0; j < nodes; j++)         {             if (graph[i][j] == 0) { graph[i][j] = int_max; }             d[i][j] = graph[i][j];         }     }     (int k = 0; k < nodes; k++) {         (int = 0; < nodes; i++)         {             (int j = 0; j < nodes; j++)             {                 if (d[i][j] > d[i][k] + d[k][j]) {                     d[i][j] = d[i][k] + d[k][j];                 }             }         }     } } 

i have set 0's int_max in order build standard matrix algorithm didn't right solution.

also, here example of matrix:

  b c d 0 0 1 0 b 1 1 0 1 c 0 0 1 0 d 1 0 0 0 

after applying matrix algorithm, 0 in matrix converted int_max. expect weight 2's or 1's unexpected values such -2712323...

the reason why big negative values integer overflow.

if there no edge, set d[i][j]=int_max. then

            if (d[i][j] > d[i][k] + d[k][j]) {                 d[i][j] = d[i][k] + d[k][j]; 

if there no edge i k , k j, sum overflow , result negative. afterwards, algorithm think there short (large negative) path i j, , poison other paths.

i suggest using int_max/2 instead if int_max.


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -