CPSC 221

Depth-First Search; Dijkstra’s Shortest Paths

Today

Announcements

  • Examlet 9 is Thursday to Sunday: graphs and disjoint sets
  • Final Practice opens tonight
  • Lab Graphs is due Sunday 12/06
  • HW3 is due Monday 12/07


Today:

  • Depth-first search
  • Single-source shortest paths: Dijkstra’s algorithm

DFS, by Example

ABC DEF GH

Visit order:   ____________

p
A C B D
B A C E
C B A D E F
D A C F H
E B C G
F C D G
G E F H
H D G
DFS(G, v):
  mark v VISITED
  for each w adjacent to v:
    if w is UNVISITED:

      DFS(G, w)

DFS

DFS(G):
  for all u in G.vertices()
    setLabel(u, UNEXPLORED)
  for all e in G.edges()
    setLabel(e, UNEXPLORED)
  for all v in G.vertices()
    if getLabel(v) == UNEXPLORED
      DFS(G, v)
DFS(G, v):
  setLabel(v, VISITED)
  for all w in G.adjacentVertices(v)
    if getLabel(w) == UNEXPLORED
      setLabel((v,w), DISCOVERY)
      DFS(G, w)
    else if getLabel((v,w)) == UNEXPLORED
      setLabel((v,w), BACK)

 

Where’s the stack?
____________________

Labels every vertex, and labels every edge discovery or back.

DFS: Observations

The discovery edges form a ________________________.

If \((u, w)\) is a back edge, then \(w\) is __________________________ of \(u\).

To find a path from \(A\) to \(v\): _____________________________.

Is it a shortest path? ____________________

 

Number of connected components: __________________________

\(G\) has a cycle exactly when ______________________________

 

For directed graphs, a timer before and after the for loop (pre and post)
gives more: topological sort, strongly connected components, and cycle detection.

Your Turn: Counting Edges

\(n\) vertices, \(m\) edges, \(c\) connected components.
Traverse every component (BFS or DFS).

 

  1. How many discovery edges are there?

  2. How many non-discovery edges?

  3. Using only \(n\), \(m\), and \(c\): when is the graph acyclic?

Try it: \(n = 12\), \(m = 13\), \(c = 3\).

Traversal: Running Time

\(n\) vertices, \(m\) edges.

Labelling vertices:   ________

Labelling edges:   ________

 

for all w in G.adjacentVertices(v):

Adjacency list Adjacency matrix
one vertex v
summed over all v
Total

Single-Source Shortest Paths

Given a start vertex (source) \(s\), find the path of least total cost
from \(s\) to every vertex in the graph.

Single-Source Shortest Paths

  • Input: a directed graph \(G\) with non-negative edge weights, and a start vertex \(s\).
  • Output: a subgraph \(G'\) made of the shortest (least total cost) paths from \(s\) to every other vertex.

Dijkstra’s Algorithm (1959)

 

Note:

Dijkstra, by Example

Labelled:   ________________

d p
s
b
c
d
e
f
g
Dijkstra(G, s):
  for all v: d[v] = INF, p[v] = null
  d[s] = 0
  PQ = all vertices, keyed by d
  repeat n times:
    v = PQ.removeMin()
    label v
    for each unlabelled w adjacent to v:
      if ________________ < d[w]:
        d[w] = ________________
        p[w] = v
  return p

Three Observations

  • Once a vertex is labelled, its distance is final.

  • d[] values only decrease, never increase.

  • The predecessors p[] form a shortest-path tree.

Dijkstra’s Correctness

  1. Assume Dijkstra’s is correct for the first \(k\) vertices it labels: the cloud.
  2. Suppose it fails on the \((k+1)\)st vertex, \(u\).
  3. Then some other path from \(s\) to \(u\) is shorter. Call it ______.
  4. Call the first vertex where it leaves the cloud \(y\).
  5. \(s\) to \(y\) is at least as long as \(Q\), since \(u\) came out of the PQ first.
  6. So \(P\) is even longer! But that’s a ________________.
  7. So Dijkstra’s does not fail on the \((k+1)\)st vertex.

Dijkstra: Running Time

Dijkstra(G, s):
  for all v: d[v] = INF, p[v] = null
  d[s] = 0
  PQ = all vertices, keyed by d          // ⭐
  repeat n times:
    v = PQ.removeMin()                   // ⭐
    label v
    for each unlabelled w adjacent to v: // 💜
      if d[v] + cost(v,w) < d[w]:
        d[w] = d[v] + cost(v,w)          // ⭐
        p[w] = v
Adj matrix Adj list
heap O(n2 + m log n) O(n log n + m log n)
unsorted array O(n2) O(n2)

 

Which is best? Depends on the graph’s density:

  • Sparse (\(m = O(n)\)):
  • Dense (\(m = O(n^2)\)):

Next

Friday: minimum spanning trees.

Monday: Kruskal’s algorithm.

 

Examlet 9 Thursday to Sunday.

Practice: Dijkstra

After class: run Dijkstra’s algorithm from A.