# Q4 · Dijkstra shortest path

Subject: Computer Networks

Exam: M2

Reader: https://deep-learning-study-room.vercel.app/cn/#m2/mocks/mock-a/q4

Canonical: https://deep-learning-study-room.vercel.app/read/cn/m2/mocks/mock-a--q4/

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

| Edge | Cost |
| --- | --- |
| A→B | 2 |
| A→C | 6 |
| B→C | 1 |
| B→D | 7 |
| C→D | 2 |

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



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](https://deep-learning-study-room.vercel.app/cn/fig/dijkstra-mock.f988abf338.svg)

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.

| Vertex | Distance from A | Predecessor | State |
| --- | --- | --- | --- |
| A | 0 | — | 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.

| Edge | Old distance | Candidate | Decision |
| --- | --- | --- | --- |
| A→B | ∞ | 0+(2)=2 | Improve to 2; predecessor=A |
| A→C | ∞ | 0+(6)=6 | Improve to 6; predecessor=A |

| Vertex | Distance from A | Predecessor | State |
| --- | --- | --- | --- |
| A | 0 | — | Settled |
| B | 2 | A | Tentative |
| C | 6 | A | Tentative |
| 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.

| Edge | Old distance | Candidate | Decision |
| --- | --- | --- | --- |
| B→C | 6 | 2+(1)=3 | Improve to 3; predecessor=B |
| B→D | ∞ | 2+(7)=9 | Improve to 9; predecessor=B |

| Vertex | Distance from A | Predecessor | State |
| --- | --- | --- | --- |
| A | 0 | — | Settled |
| B | 2 | A | Settled |
| C | 3 | B | Tentative |
| D | 9 | B | Tentative |

#### Step 4. Settle C at distance 3

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

| Edge | Old distance | Candidate | Decision |
| --- | --- | --- | --- |
| C→D | 9 | 3+(2)=5 | Improve to 5; predecessor=C |

| Vertex | Distance from A | Predecessor | State |
| --- | --- | --- | --- |
| A | 0 | — | Settled |
| B | 2 | A | Settled |
| C | 3 | B | Settled |
| D | 5 | C | Tentative |

#### 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.

| Vertex | Distance from A | Predecessor | State |
| --- | --- | --- | --- |
| A | 0 | — | Settled |
| B | 2 | A | Settled |
| C | 3 | B | Settled |
| D | 5 | C | Settled |

#### 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.

#### What this result means

The predecessor table proves which edges make the route. A table containing only the final cost is not the complete working requested.
