Computer Networks · M2 · mocks

Q3 · Bellman–Ford shortest path

M2 mock B

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

Question. Apply Bellman–Ford from A to D for directed edges A→B=5, A→C=8, B→C=−3, C→D=4, B→D=7. Show all passes using this edge order. [3 practice marks]

Answer

Given

Directed edgeWeight
A→B5
A→C8
B→C-3
C→D4
B→D7

To find

The shortest route and cost, with complete passes and a negative-cycle check.

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))

Bellman–Ford permits negative edges. We use the listed order with in-place updates; do not mix this with a table that reads only the previous pass.

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

Step-by-step solution

Step 1. Initialize

4 vertices → at most 3 main passes. d[A]=0; every other distance=∞. Use the listed edge order and immediate (in-place) updates.

Step 2. Pass 1: relax every edge

EdgeOld d[target]Candidate via sourceNew d[target]Decision
A→B∞0+(5)=55Update; predecessor=A
A→C∞0+(8)=88Update; predecessor=A
B→C85+(-3)=22Update; predecessor=B
C→D∞2+(4)=66Update; predecessor=C
B→D65+(7)=126No improvement
VertexABCD
End-of-pass distance0526
Predecessor—ABC

Step 3. Pass 2: relax every edge

EdgeOld d[target]Candidate via sourceNew d[target]Decision
A→B50+(5)=55No improvement
A→C20+(8)=82No improvement
B→C25+(-3)=22No improvement
C→D62+(4)=66No improvement
B→D65+(7)=126No improvement
VertexABCD
End-of-pass distance0526
Predecessor—ABC

Step 4. Stop after an unchanged pass

This entire pass changed no distance. Further identical relaxation passes will not improve anything.

Step 5. Recover and total the path

Backward predecessors: D ← C ← B ← A. Forward route: A → B → C → D.

Cost = (5) + (-3) + (4) = 6. No edge can now improve its destination label.

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