Computer Networks · TEE · papers

Q5(c) · Bellman-Ford example

2024-12-06 · Final 2024-25 / Re-exam 2023-24

Actual paper; 100 marks attempted. Current-syllabus alignment is stated per question.

Q5(c). Explain Bellman Ford algorithm for finding shortest path from source node to destination node with the help of an example. [10 marks]

Answer

Definition. Bellman–Ford computes single-source shortest paths by repeatedly relaxing every edge. It supports negative edge weights and detects reachable negative cycles.

Simple meaning. Har edge ko baar-baar relax; negative edge chalega, negative cycle problem hai.

Understand it step by step

  1. Initialize source 0 and all other distances ∞.
  2. Relax every directed edge, in a stated order, for at most V−1 full passes, where V is the number of vertices. A shortest simple path uses at most V−1 edges when no relevant negative cycle exists.
  3. In the in-place convention used here, an update is immediately available to later edges in the same pass. Different edge orders can therefore produce different intermediate tables while converging to the same valid final distances.
  4. If a full pass makes no change, all distances have stabilized and the algorithm may stop early.
  5. A further improvement on a reachable edge after V−1 passes signals a reachable negative cycle. Destinations reachable from such a cycle need not have a finite shortest distance.

Bellman–Ford repeatedly relaxes all edges and can handle negative weights. It uses up to V−1 passes and an additional improvement check for reachable negative cycles. Dijkstra settles the smallest tentative vertex and requires nonnegative weights for its standard correctness guarantee.

Yaad rakho: Pass = all edges once. Negative edge ≠ negative cycle.

Given

Directed edgeWeight
A→B4
A→C5
B→C-2
C→D3

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 | 4 | C | C | A->C | 5 | B->C | -2 | D | D | C->D | 3
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+(4)=44Update; predecessor=A
A→C∞0+(5)=55Update; predecessor=A
B→C54+(-2)=22Update; predecessor=B
C→D∞2+(3)=55Update; predecessor=C
VertexABCD
End-of-pass distance0425
Predecessor—ABC

Step 3. Pass 2: relax every edge

EdgeOld d[target]Candidate via sourceNew d[target]Decision
A→B40+(4)=44No improvement
A→C20+(5)=52No improvement
B→C24+(-2)=22No improvement
C→D52+(3)=55No improvement

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 = (4) + (-2) + (3) = 5. No edge can now improve its destination label.

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