Computer Networks · M2 · mocks

Q4 · Dijkstra shortest path

M2 mock A

Q1(a)+Q1(b) compulsory; choose any two of Q2/Q3/Q4 · 10 marks attempted

Question. Use Dijkstra from A to D for directed edges A→B=2, A→C=6, B→C=1, B→D=7, C→D=2. Show each selected vertex and relaxation. [3 practice marks]

Answer

Given

EdgeCost
A→B2
A→C6
B→C1
B→D7
C→D2

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 | 2 | C | C | A->C | 6 | B->C | 1 | D | D | B->D | 7 | C->D | 2
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+(2)=2Improve to 2; predecessor=A
A→C∞0+(6)=6Improve to 6; predecessor=A
VertexDistance from APredecessorState
A0—Settled
B2ATentative
C6ATentative
D∞—Tentative

Step 3. Settle B at distance 2

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

EdgeOld distanceCandidateDecision
B→C62+(1)=3Improve to 3; predecessor=B
B→D∞2+(7)=9Improve to 9; predecessor=B
VertexDistance from APredecessorState
A0—Settled
B2ASettled
C3BTentative
D9BTentative

Step 4. Settle C at distance 3

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

EdgeOld distanceCandidateDecision
C→D93+(2)=5Improve to 5; predecessor=C
VertexDistance from APredecessorState
A0—Settled
B2ASettled
C3BSettled
D5CTentative

Step 5. Settle D at distance 5

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

No outgoing edges to relax.

VertexDistance from APredecessorState
A0—Settled
B2ASettled
C3BSettled
D5CSettled

Step 6. Recover the path

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

Edge sum: 2 + 1 + 2 = 5.

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