Computer Networks · TEE · read

Dijkstra · complete example 1

Routing: build the shortest path one step at a time

TEE · shared foundations and Units 3–4 · current syllabus 702AI0C023

Practice question. Use Dijkstra’s algorithm to find the shortest path from A to D in the directed graph defined below. Show every selection, relaxation and distance table. [3 practice marks]

Answer

Given

EdgeCost
A→B4
A→C2
C→B1
B→D3
C→D7

Source=A; target=D. Edges are directed; do not add reverse edges.

To find

The path A to D, its total cost and intermediate work.

Formula and why

d[v]\leftarrow\min(d[v],d[u]+w(u,v))LaTeX: d[v]\leftarrow\min(d[v],d[u]+w(u,v))

Use Dijkstra because all weights are nonnegative. Select the smallest unsettled total distance, not the cheapest isolated edge.

Swipe across the diagram to read its labels ↔
Dijkstra · given directed graph
Diagram: Dijkstra · given directed graph. Labels: Dijkstra · given directed graph | Dijkstra · given directed graph | A | A | B | B | A->B | 4 | C | C | A->C | 2 | D | D | B->D | 3 | C->B | 1 | C->D | 7
Read the labels and direction before substituting numbers.

Step-by-step solution

Step 1. Initialize

d[A]=0; every other distance=∞. No predecessors are known.

VertexDistance from APredecessorState
A0—Tentative
B∞—Tentative
C∞—Tentative
D∞—Tentative

Step 2. Settle A at distance 0

Unsettled candidates before selection: A=0, B=∞, C=∞, D=∞. Select the minimum; ties use alphabetical order.

EdgeOld distanceCandidateDecision
A→B∞0+(4)=4Improve to 4; predecessor=A
A→C∞0+(2)=2Improve to 2; predecessor=A
VertexDistance from APredecessorState
A0—Settled
B4ATentative
C2ATentative
D∞—Tentative

Step 3. Settle C at distance 2

Unsettled candidates before selection: B=4, C=2, D=∞. Select the minimum; ties use alphabetical order.

EdgeOld distanceCandidateDecision
C→B42+(1)=3Improve to 3; predecessor=C
C→D∞2+(7)=9Improve to 9; predecessor=C
VertexDistance from APredecessorState
A0—Settled
B3CTentative
C2ASettled
D9CTentative

Step 4. Settle B at distance 3

Unsettled candidates before selection: B=3, D=9. Select the minimum; ties use alphabetical order.

EdgeOld distanceCandidateDecision
B→D93+(3)=6Improve to 6; predecessor=B
VertexDistance from APredecessorState
A0—Settled
B3CSettled
C2ASettled
D6BTentative

Step 5. Settle D at distance 6

Unsettled candidates before selection: D=6. Select the minimum; ties use alphabetical order.

No outgoing edges to relax.

VertexDistance from APredecessorState
A0—Settled
B3CSettled
C2ASettled
D6BSettled

Step 6. Recover the path

Trace backwards: D ← B ← C ← A. Reverse it: A → C → B → D.

Edge sum: 2 + 1 + 3 = 6.

Final answer: A → C → B → D; cost=6.