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