Note that there are some explanatory texts on larger screens.

plurals
  1. PODifficulty Wrapping My Head Around the Iterations of Bellman Ford
    primarykey
    data
    text
    <p>The way the Bellman Ford algorithm has been explained to me is that it goes through n-1 (with n being the number of nodes) iterations, updating the distance between nodes on each iteration. All graphical examples show the nodes initialized to infinity, and the ones closest to the source node being updated on the first iteration, but the rest remaining at infinity until an iteration occurs that reaches them.</p> <p>However, looking at code for the algorithm, such as that provided <a href="https://gist.github.com/701720" rel="nofollow">here</a>, I'm finding it hard to understand why all of the nodes that are more steps away than the number of iterations are not updated with the algorithm. For instance, if I'm on my second iteration and I have a node d that is only reachable through the path a-b-c-d, the examples I've read seem to indicate that d will not be updated until the 4th iteration. </p> <p>But the main relaxation function of the code:</p> <pre><code>def relax(node, neighbour, graph, d, p): # If the distance between the node and the neighbour is lower than the one I have now if d[neighbour] &gt; d[node] + graph[node][neighbour]: # Record this lower distance d[neighbour] = d[node] + graph[node][neighbour] p[neighbour] = node </code></pre> <p>updates based on information giving the weight and distance from the source of the predecessor node. If the algorithm iterates over each node in every iteration, what's to stop d from being properly updated in the FIRST iteration? For instance, if the algorithm iterated over the nodes in order a-b-c-d, I don't see why the algorithm wouldn't store the distance information for node b, move on to node c, store distance information for that, and finally reach d with enough information to calculate the shortest path IN THE FIRST ITERATION. </p> <p>I hope that made some kind of sense. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload