Computer Networks · TEE · papers

Q5(a) · Dijkstra algorithm

2025-12-05 · 2022-23

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

Q5(a). Explain the Dijkstra’s algorithm with an example. [10 marks]

Answer

Definition. Dijkstra’s algorithm finds shortest paths from a source in a graph whose edge weights are nonnegative.

Simple meaning. Sabse chhota unfinished distance select karo, phir uske neighbours improve karo.

Understand it step by step

  1. Initialize source distance 0, all others ∞, and no predecessors. No vertex is settled initially.
  2. Select an unsettled vertex with minimum tentative distance. With nonnegative edges, its distance can now be finalized.
  3. Relax each relevant outgoing edge: compare old distance with selected distance plus edge cost.
  4. Repeat selection and relaxation. If all remaining distances are ∞, the remaining vertices are unreachable from the source.
  5. Trace predecessors for the required destination. Always preserve arrow direction. Standard Dijkstra is not valid for graphs with negative edge costs.
  1. Set source to 0 and other distances to infinity.
  2. Repeatedly choose the unsettled vertex with minimum tentative distance.
  3. Settle it and relax its outgoing edges.
  4. Recover paths using predecessors.

The standard algorithm requires nonnegative edge weights.

Yaad rakho: Minimum tentative total choose; negative edges pe Dijkstra nahi.

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.