CPSC 221

Graph Traversal: Whatever-First Search

Today

Announcements

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


Today: Graph traversal

  • One algorithm, two bags: BFS and DFS
  • What a traversal tells us: components, cycles
  • Running time

The Maze

Imagine yourself in a complex maze.

 

How do you explore systematically?

 

Ariadne and the Minotaur (video)

What Did You Notice?

 

A Maze Is a Graph

V:   rooms

E:   \((u,v)\) if there is a doorway between \(u\) and \(v\)

 

The shape of the rooms is irrelevant.

 

Each step uses one operation:
list the neighbours of the current room.

Graph Traversal

Objective: visit every vertex and every edge.

Purpose: find interesting structure in the graph.

 

Compared with a tree traversal:

Tree Graph
Ordered
Obvious start
Acyclic

BFS, by Example

ABC DEF GH

Queue:   ________________

Visit order:   ____________

d 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
WFS(G, s):
  mark s VISITED

  bag.put(s)
  while bag is not empty:
    v = bag.take()
    for each w adjacent to v:
      if w is UNVISITED:
        mark w VISITED

        bag.put(w)

BFS

BFS(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
      BFS(G, v)
BFS(G, v):
  queue q
  setLabel(v, VISITED)
  q.enqueue(v)
  while !q.isEmpty()
    v = q.dequeue()
    for all w in G.adjacentVertices(v)
      if getLabel(w) == UNEXPLORED
        setLabel((v,w), DISCOVERY)
        setLabel(w, VISITED)
        q.enqueue(w)
      else if getLabel((v,w)) == UNEXPLORED
        setLabel((v,w), CROSS)

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

BFS: Observations

The discovery edges form a ________________________.

\(d(v)\) is ______________________________________.

If \((u, w)\) is a cross edge, then \(|d(u) - d(w)| \le\) ________.

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

 

Number of connected components: __________________________

\(G\) has a cycle exactly when ______________________________

DFS, by Example

ABC DEF GH

Call stack:   ________________

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 ______________________________

Counting Edges

\(n\) vertices, \(m\) edges, \(c\) connected components.

 

Discovery edges:   __________

Non-discovery edges:   __________

 

The graph is acyclic exactly when ____________________.

 

Our example: \(n = 8\), \(m = 13\), \(c = 1\).

Running Time

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

Labelling vertices:   ________

Labelling edges:   ________

Each adjacentVertices(v):   ________

  summed over all \(v\):   ________

 

Total:   ____________

Adjacency matrix?

 

________________

Next

Wednesday: Dijkstra’s shortest-path algorithm.

 

Whatever-first search, with a priority queue as the bag.