Bird
Raised Fist0
Data Structures Theoryknowledge~10 mins

Dijkstra's algorithm in Data Structures Theory - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Dijkstra's algorithm
Start at source node
↓
Initialize distances: source=0, others=∞
↓
Mark all nodes unvisited
↓
Select unvisited node with smallest distance
↓
For each neighbor of selected node
↓
Calculate new distance via selected node
↓
If new distance < current distance, update it
↓
Mark selected node as visited
↓
Repeat until all nodes visited or smallest distance is ∞
↓
End: shortest distances found
Dijkstra's algorithm finds the shortest path from a start node to all others by repeatedly selecting the closest unvisited node and updating distances.
Execution Sample
Data Structures Theory
Graph:
A--1--B--2--C
|     |
4     3
|     |
D-----E

Start: A
Find shortest distances from node A to all others in a weighted graph.
Analysis Table
StepCurrent NodeDistancesVisited NodesActionNotes
1A{"A":0, "B":∞, "C":∞, "D":∞, "E":∞}[]Initialize distances and start at ASet distance A=0, others ∞
2A{"A":0, "B":1, "C":∞, "D":4, "E":∞}[]Update neighbors B and DB=1 via A, D=4 via A
3A{"A":0, "B":1, "C":∞, "D":4, "E":∞}['A']Mark A visitedA done
4B{"A":0, "B":1, "C":3, "D":4, "E":4}['A']Update neighbors C and EC=3 via B, E=4 via B
5B{"A":0, "B":1, "C":3, "D":4, "E":4}['A', 'B']Mark B visitedB done
6C{"A":0, "B":1, "C":3, "D":4, "E":4}['A', 'B']No neighbors to updateC neighbors visited or no shorter path
7C{"A":0, "B":1, "C":3, "D":4, "E":4}['A', 'B', 'C']Mark C visitedC done
8D{"A":0, "B":1, "C":3, "D":4, "E":4}['A', 'B', 'C']Update neighbor EE current=4, new via D=8 (no update)
9D{"A":0, "B":1, "C":3, "D":4, "E":4}['A', 'B', 'C', 'D']Mark D visitedD done
10E{"A":0, "B":1, "C":3, "D":4, "E":4}['A', 'B', 'C', 'D']No neighbors to updateAll neighbors visited or no shorter path
11E{"A":0, "B":1, "C":3, "D":4, "E":4}['A', 'B', 'C', 'D', 'E']Mark E visitedE done
12-{"A":0, "B":1, "C":3, "D":4, "E":4}['A', 'B', 'C', 'D', 'E']All nodes visitedAlgorithm ends
💡 All nodes visited; shortest distances finalized
State Tracker
VariableStartAfter Step 2After Step 4After Step 8Final
Distances{"A":0, "B":∞, "C":∞, "D":∞, "E":∞}{"A":0, "B":1, "C":∞, "D":4, "E":∞}{"A":0, "B":1, "C":3, "D":4, "E":4}{"A":0, "B":1, "C":3, "D":4, "E":4}{"A":0, "B":1, "C":3, "D":4, "E":4}
Visited Nodes[][]["A", "B"]["A", "B", "C", "D"]["A", "B", "C", "D", "E"]
Current NodeABCDE
Key Insights - 3 Insights
Why do we update the distance to a neighbor only if the new distance is smaller?
Because Dijkstra's algorithm always keeps the shortest known distance. Updating only when the new path is shorter ensures we find the minimum distance. See execution_table rows 2 and 4 where distances are updated only if smaller.
Why do we mark nodes as visited and never revisit them?
Once a node is visited, its shortest distance is finalized. Revisiting could cause incorrect updates. This is shown in execution_table rows 3, 5, 7, 9, and 11 where nodes are marked visited and not reconsidered.
What happens if a node is unreachable from the source?
Its distance remains infinity (∞) because no path exists. The algorithm stops when all reachable nodes are visited. This is implied in the exit condition in execution_table row 12.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4. What is the distance to node C?
A∞
B3
C1
D4
💡 Hint
Check the Distances column in row with Step 4.
At which step is node D marked as visited?
AStep 7
BStep 8
CStep 9
DStep 10
💡 Hint
Look at the Visited Nodes column to see when D appears.
If the edge from B to E had weight 1 instead of 3, how would the distance to E change after step 4?
AIt would be 2
BIt would remain 4
CIt would be 3
DIt would be ∞
💡 Hint
Calculate distance from A to E via B: A->B=1 plus B->E=1 equals 2.
Concept Snapshot
Dijkstra's algorithm finds shortest paths from a start node to all others.
Initialize distances (start=0, others=∞).
Repeatedly pick unvisited node with smallest distance.
Update neighbors' distances if shorter path found.
Mark nodes visited to finalize distances.
Stops when all nodes visited or unreachable.
Full Transcript
Dijkstra's algorithm starts at a source node, setting its distance to zero and all others to infinity. It keeps track of visited nodes and distances. At each step, it selects the unvisited node with the smallest current distance. It then updates the distances to its neighbors if a shorter path is found through this node. After updating, it marks the current node as visited, meaning its shortest distance is finalized. This process repeats until all nodes are visited or no reachable nodes remain. The final distances represent the shortest paths from the source to every other node in the graph.

Practice

(1/5)
1. What is the main purpose of Dijkstra's algorithm in graph theory?
easy
A. To detect cycles in a graph
B. To sort nodes in alphabetical order
C. To find the maximum flow in a network
D. To find the shortest path from one node to all other nodes in a weighted graph

Solution

  1. Step 1: Understand the goal of Dijkstra's algorithm

    Dijkstra's algorithm is designed to find the shortest distances from a starting node to all other nodes in a graph where edges have weights.
  2. Step 2: Compare with other graph algorithms

    Sorting nodes or detecting cycles are different tasks handled by other algorithms, not Dijkstra's.
  3. Final Answer:

    To find the shortest path from one node to all other nodes in a weighted graph -> Option D
  4. Quick Check:

    Shortest path finding = To find the shortest path from one node to all other nodes in a weighted graph [OK]
Hint: Remember: Dijkstra finds shortest paths from one node [OK]
Common Mistakes:
  • Confusing shortest path with sorting or cycle detection
  • Thinking it finds maximum flow
  • Assuming it works on unweighted graphs only
2. Which of the following is the correct step in Dijkstra's algorithm?
easy
A. Select the unvisited node with the smallest tentative distance
B. Select the visited node with the largest distance
C. Update distances by adding the maximum edge weight
D. Remove nodes with the highest degree first

Solution

  1. Step 1: Identify the selection criteria in Dijkstra's algorithm

    The algorithm always picks the unvisited node with the smallest tentative distance to explore next.
  2. Step 2: Eliminate incorrect options

    Choosing visited nodes or using maximum edge weights contradicts the algorithm's logic.
  3. Final Answer:

    Select the unvisited node with the smallest tentative distance -> Option A
  4. Quick Check:

    Pick closest unvisited node = Select the unvisited node with the smallest tentative distance [OK]
Hint: Always pick closest unvisited node next [OK]
Common Mistakes:
  • Picking visited nodes again
  • Adding maximum instead of minimum edge weights
  • Removing nodes based on degree
3. Consider the graph with nodes A, B, C and edges: A-B (4), A-C (2), C-B (1). Starting from A, what is the shortest distance to B using Dijkstra's algorithm?
medium
A. 3
B. 4
C. 2
D. 1

Solution

  1. Step 1: Initialize distances from A

    Distance to A is 0, to B is infinity, to C is infinity initially.
  2. Step 2: Update distances via neighbors

    From A, distance to C is 2, to B is 4. Next, from C, distance to B is 2 + 1 = 3, which is less than 4, so update B's distance to 3.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    Shortest path A->C->B = 3 [OK]
Hint: Check all paths, pick minimum sum of edges [OK]
Common Mistakes:
  • Ignoring the path through C
  • Choosing direct edge without comparison
  • Mixing up edge weights
4. In the following pseudocode for Dijkstra's algorithm, what is the error?
1. Initialize distances to infinity
2. Set distance to start node as 0
3. While unvisited nodes remain:
4.    Pick any unvisited node
5.    Update distances to neighbors
6.    Mark node as visited
medium
A. Not initializing distances to zero
B. Picking any unvisited node instead of the one with smallest distance
C. Marking nodes as visited before updating distances
D. Setting start node distance to infinity

Solution

  1. Step 1: Identify the node selection step

    The algorithm must pick the unvisited node with the smallest tentative distance, not just any node.
  2. Step 2: Understand the impact of wrong selection

    Picking any node can lead to incorrect shortest paths because it ignores the priority of closer nodes.
  3. Final Answer:

    Picking any unvisited node instead of the one with smallest distance -> Option B
  4. Quick Check:

    Must pick closest unvisited node [OK]
Hint: Always pick node with smallest tentative distance [OK]
Common Mistakes:
  • Choosing nodes randomly
  • Setting wrong initial distances
  • Marking nodes visited too early
5. You have a weighted graph with some edges having zero weight. How does Dijkstra's algorithm handle zero-weight edges when finding shortest paths?
hard
A. It treats zero-weight edges as infinite weight edges
B. It ignores zero-weight edges completely
C. It treats zero-weight edges like any other edge and updates distances accordingly
D. It causes the algorithm to fail or loop infinitely

Solution

  1. Step 1: Understand edge weights in Dijkstra's algorithm

    Dijkstra's algorithm works with non-negative weights, including zero, treating them as valid edges.
  2. Step 2: Effect of zero-weight edges

    Zero-weight edges allow the algorithm to update distances without adding cost, effectively moving to connected nodes at no extra distance.
  3. Final Answer:

    It treats zero-weight edges like any other edge and updates distances accordingly -> Option C
  4. Quick Check:

    Zero weight edges are valid and processed [OK]
Hint: Zero weight edges count as normal edges with no added cost [OK]
Common Mistakes:
  • Ignoring zero-weight edges thinking they cause errors
  • Assuming zero weight means infinite cost
  • Believing algorithm loops infinitely with zero weights
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard