MCS-208 · Unit 8

Graphs

Section 3 of 8

Block 3 · Unit 8Graphs9 min · Section 3 of 8

8.3Single-Source Shortest Paths: Dijkstra's Algorithm

Covers IGNOU MCS-208 §8.3, 8.3.1

A navigation app must find the shortest route from where you are to everywhere else. In a weighted graph with non-negative weights, Dijkstra’s algorithm solves exactly this problem.

The problem and relaxation

The single-source shortest-path problem: given a weighted graph and a source vertex ss, find the length of a shortest path from ss to every vertex (and the paths themselves).

Every shortest-path algorithm in this unit keeps two arrays:

  • d[v]: the length of the shortest path to vv found so far (an upper estimate, initially ∞, and d[s] = 0);
  • pi[v]: the predecessor of vv on that path.

Dijkstra’s algorithm

  1. Set d[v] = ∞ and pi[v] = nil for all vv; set d[s] = 0; let SS (settled vertices) be empty.
  2. Repeat nn times: pick the vertex u∉Su \notin S with the smallest d[u] (Extract-Min); add uu to SS; relax every edge (u,v)(u, v) leaving uu.

This is a greedy algorithm: at each step it commits to the closest unsettled vertex. It is correct only when all edge weights are non-negative: then no later path can come back to a settled vertex more cheaply.

Running time

Implementation of Extract-MinTime
Array or unsorted list (scan all vertices)O(n2)O(n^2), best for dense graphs
Binary heap (priority queue, Unit 5)O((n+e)log⁡n)O((n + e) \log n), best for sparse graphs

Dijkstra’s algorithm works for directed and undirected graphs, as long as no weight is negative.

Key takeaways

  • Relax (u,v)(u, v): if d[u]+w<d[v]d[u] + w < d[v], update d[v]d[v] and π[v]\pi[v].
  • Dijkstra: start with S=∅S = \emptyset, d[s]=0d[s] = 0; repeatedly settle the closest unsettled vertex and relax its edges.
  • Correct only for non-negative weights; works on directed and undirected graphs.
  • O(n2)O(n^2) with an array; O((n+e)log⁡n)O((n + e) \log n) with a binary heap.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

Which problem does Dijkstra’s algorithm solve directly?

Show answer

Answer: B. Single-source shortest paths with non-negative weights

It computes distances from one source. For all pairs it must be run nn times (or use Floyd–Warshall).

Question 2 · multiple choice

Current estimates are d[a] = 4, d[b] = 7, and edge a→b has weight 2. After relaxing a→b, what is d[b]?

Show answer

Answer: C. 6

4 + 2 = 6 < 7, so d[b] becomes 6 and π[b] = a. 11 would be adding 7 + 4.

Question 3 · multiple choice

In the example, why does v’s distance change three times?

Show answer

Answer: C. Because each newly settled vertex (x, y, u) offers a shorter way to v

14 via x, 13 via y, 9 via u. Estimates only decrease, and v is settled once, at 9.

Question 4 · multiple choice

What is the running time of Dijkstra’s algorithm when Extract-Min scans an array of nn vertices?

Show answer

Answer: D. O(n2)O(n^2)

nn Extract-Min operations of O(n)O(n) each, plus O(e)≤O(n2)O(e) \le O(n^2) relaxations.

Question 5 · multiple choice

What does this program print?

C · q8-relax.c
#include <stdio.h>

int main(void)
{
    int d[4] = {0, 1000, 1000, 1000};      /* source 0, others "infinity" */
    int w[4][4] = {{0, 4, 1, 0},
                   {0, 0, 0, 1},
                   {0, 2, 0, 5},
                   {0, 0, 0, 0}};
    int done[4] = {0};
    for (int r = 0; r < 4; r++) {
        int u = -1;
        for (int v = 0; v < 4; v++)
            if (!done[v] && (u < 0 || d[v] < d[u])) u = v;
        done[u] = 1;
        for (int v = 0; v < 4; v++)
            if (w[u][v] && d[u] + w[u][v] < d[v]) d[v] = d[u] + w[u][v];
    }
    printf("%d %d %d\n", d[1], d[2], d[3]);
    return 0;
}
Show answer

Answer: A. 3 1 4

Settle 0 (d1 = 4, d2 = 1); settle 2 (d1 = 1 + 2 = 3, d3 = 6); settle 1 (d3 = 3 + 1 = 4); settle 3. “4 1 5” ignores the cheaper route 0→2→1.

Question 6 · short answer

Apply Dijkstra’s algorithm to Figure 8.5 (edges 1→2 3, 1→3 6, 1→5 9, 2→4 1, 3→4 8, 3→5 6, 4→5 6) from vertex 1, showing the distances after each step.

Show worked answer

Start: d = (0, ∞, ∞, ∞, ∞). Settle 1: d2 = 3, d3 = 6, d5 = 9. Settle 2 (3): d4 = 3 + 1 = 4. Settle 4 (4): 4 + 6 = 10 > 9, d5 stays 9. Settle 3 (6): 3→4 gives 14 > 4; 3→5 gives 12 > 9. Settle 5 (9). Final: d(1..5) = 0, 3, 6, 4, 9; paths 1→2, 1→3, 1→2→4, 1→5.

↑ ↓ move↵ openTab switch type