Skip to content
← All questions

How do you handle multiple distance values at a single vertex during Dijkstra's algorithm?

When a vertex receives multiple distance calculations from different paths, you keep only the smallest value and cross out the larger ones. This ensures that the vertex's label always represents the shortest known distance from the source. The larger values are discarded because they cannot lead to a shorter path through that vertex.

Conditions

  • A vertex is reachable via multiple paths from different circled vertices.
  • New distance calculations are performed for an already labeled vertex.

Reasoning, step by step

  1. Calculate the new tentative distance to the vertex from the current circled vertex.
  2. Compare this new distance with the existing label at the vertex.
  3. If the new distance is smaller, replace the old label with the new one and update the predecessor arrow.
  4. If the new distance is larger or equal, cross out the new value and keep the existing smaller label.
  5. Proceed to select the next smallest uncircled vertex.

Example

In the video, vertex C initially has a label 3 from home. When processing from A, a new distance of 4 is calculated (2+22 + 2). Since 4>34 > 3, the value 4 is crossed out, and 3 remains as the valid distance for C. The speaker says: "Whenever we have two or more numbers at a vertex, we're going to cross out any number that isn't equal to the minimum."

Common misconceptions

  • Leaving multiple distance values at a single vertex, leading to confusion.
  • Updating the label with a larger value.
  • Forgetting to cross out the larger value, which might be mistakenly selected later.

Watch the explanation

Explore next

Answers are generated from source material and independently checked. Consult the original video or creator if something is unclear.