williamfiset / Algorithms

A collection of algorithms and data structures
MIT License
17.18k stars 4.34k forks source link

Update the for loop to run V-1 times #289

Open meetme2meat opened 2 years ago

meetme2meat commented 2 years ago

The following code change in the following dist array from this

[0.0, 1.0, -Infinity, -Infinity, -Infinity, 5.0, 5.0, 8.0, Infinity]

to this

[0.0, 1.0, -Infinity, -Infinity, -Infinity, 5.0, 5.0, 8.0]

williamfiset commented 2 years ago

Supposed the number of vertices is V = 3, then we want to iterate V-1 = 2 times for bellman ford.

With the clause i=0; i <= V-1; i++ that would include indeces i=0, i=1, and i=2 for a total of 3 iterations, not 2.