Graph Traversal

COMP2521 20T2 ♢ Graph Traversal [0/22]
❖ Problems on Graphs

What kind of problems do we want to solve on graphs?

While these problems are expressed as problems on graphs, they are interesting because many real world problems can be mapped onto graphs, and the solutions to the above could then be applied in solving them.

COMP2521 20T2 ♢ Graph Traversal [1/22]
❖ Graph Traversal

Many of the above problems can be solved by

Algorithms for this typically require us to remember Since many graph search algorithms are recursive Systematic exploration like this is called traversal or search.
COMP2521 20T2 ♢ Graph Traversal [2/22]
❖ ... Graph Traversal


Consider two related problems on graphs ...

An approach to solving this problem: The above summarises one form of graph traversal.
COMP2521 20T2 ♢ Graph Traversal [3/22]
❖ ... Graph Traversal

There are two strategies for graph traversal/search ...

Depth-first search  (DFS)

Breadth-first search  (BFS) The method on the previous slide is effectively breadth-first traversal.
COMP2521 20T2 ♢ Graph Traversal [4/22]
❖ ... Graph Traversal

Comparison of BFS/DFS search for checking hasPath(a,h)?

[Diagram:Pics/graph-search-bfs-dfs.png]

Both approaches ignore some edges by remembering previously visited vertices.

COMP2521 20T2 ♢ Graph Traversal [5/22]
❖ ... Graph Traversal

A spanning tree of a graph

Consider the following graph:

[Diagram:Pics/graph0.png]

Consider how DFS and BFS could produce its spanning tree

COMP2521 20T2 ♢ Graph Traversal [6/22]
❖ ... Graph Traversal

Spanning tree resulting from DFS ...

[Diagram:Pics/dfs-tree.png]


Note: choose neighbours in ascending order

COMP2521 20T2 ♢ Graph Traversal [7/22]
❖ ... Graph Traversal

Spanning tree resulting from BFS ...

[Diagram:Pics/bfs-tree.png]


Note: choose neighbours in ascending order

COMP2521 20T2 ♢ Graph Traversal [8/22]
❖ Depth-first Search


Depth-first search can be described recursively as

visited = {}

depthFirst(G,v):
|  visited = visited ∪ {v}
|  for all (v,w) ∈ edges(G) do
|  |  if w ∉ visited then
|  |  |  depthFirst(G,w)
|  |  end if
|  end for

The recursion induces backtracking

COMP2521 20T2 ♢ Graph Traversal [9/22]
❖ ... Depth-first Search


Recursive DFS path checking

visited = {}

hasPath(G,src,dest):
|  Input  graph G, vertices src,dest
|  Output true if there is a path from src to dest,
|         false otherwise
|
|  return dfsPathCheck(G,src,dest)


Requires wrapper around recursive function  dfsPathCheck()

COMP2521 20T2 ♢ Graph Traversal [10/22]
❖ ... Depth-first Search


Recursive function for path checking

dfsPathCheck(G,v,dest):
|  visited = visited ∪ {v}
|  for all (v,w) ∈ edges(G) do
|  |  if w=dest then   // found edge to dest
|  |     return true
|  |  else if w ∉ visited then
|  |     if dfsPathCheck(G,w,dest) then
|  |        return true  // found path via w to dest
|  |     end if
|  |  end if
|  end for
|  return false  // no path from v to dest

COMP2521 20T2 ♢ Graph Traversal [11/22]
❖ Depth-first Traversal Example


Tracing the execution of dfsPathCheck(G,0,5) on:

[Diagram:Pics/dfsPathCheck.png]


Reminder: we consider neighbours in ascending order

Clearly does not find the shortest path

COMP2521 20T2 ♢ Graph Traversal [12/22]
❖ DFS Cost Analysis


Cost analysis:


Time complexity of DFS:   O(V+E)   (adjacency list representation)
COMP2521 20T2 ♢ Graph Traversal [13/22]
❖ Path Finding


Knowing whether a path exists can be useful

Knowing what the path is, is even more useful

Strategy:

Requires a global array (not a set):

COMP2521 20T2 ♢ Graph Traversal [14/22]
❖ ... Path Finding

Function to find path src→dest and print it

visited[] // store previously visited node
          // for each vertex 0..nV-1

findPath(G,src,dest):
|  Input graph G, vertices src,dest
|
|  for all vertices v∈G do
|     visited[v]=-1
|  end for
|  visited[src]=src  // starting node of the path
|  if dfsPathCheck(G,src,dest) then
|  |  // show path in dest..src order
|  |  v=dest
|  |  while v≠src do
|  |     print v"-"
|  |     v=visited[v]
|  |  end while
|  |  print src
|  end if

COMP2521 20T2 ♢ Graph Traversal [15/22]
❖ ... Path Finding


Recursive function to build path in visited[]

dfsPathCheck(G,v,dest):
|  for all (v,w) ∈ edges(G) do
|  |  if visited[w] = -1 then
|  |     visited[w] = v
|  |     if w = dest then  // found edge from v to dest
|  |        return true
|  |     else if dfsPathCheck(G,w,dest) then
|  |        return true  // found path via w to dest
|  |     end if
|  |  end if
|  end for
|  return false  // no path from v to dest

COMP2521 20T2 ♢ Graph Traversal [16/22]
❖ ... Path Finding


The visited[] array after dfsPathCheck(G,0,7) succeeds

[Diagram:Pics/dfsFindPath.png]

COMP2521 20T2 ♢ Graph Traversal [17/22]
❖ ... Path Finding

DFS can also be described non-recursively (via a stack):

visited[] // store previously visited node
          // for each vertex 0..nV-1

findPathDFS(G,src,dest):
|  Input graph G, vertices src,dest
|
|  for all vertices v∈G do
|     visited[v]=-1
|  end for
|  found=false
|  visited[src]=src
|  push src onto new stack S
|  while not found ∧ S is not empty do
|  |  pop v from S
|  |  if v=dest then
|  |     found=true
|  |  else
|  |  |  for each (v,w)∈edges(G) with visited[w]=-1 do
|  |  |     visited[w]=v
|  |  |     push w onto S
|  |  |  end for
|  |  end if
|  end while
|  if found then
|     display path in dest..src order
|  end if

Uses standard stack operations ... Time complexity is still O(V+E)

COMP2521 20T2 ♢ Graph Traversal [18/22]
❖ Breadth-first Search


Basic approach to breadth-first search (BFS):

Notes:
COMP2521 20T2 ♢ Graph Traversal [19/22]
❖ ... Breadth-first Search

BFS path finding algorithm:

visited[] // store previously visited node
          // for each vertex 0..nV-1

findPathBFS(G,src,dest):
|  Input  graph G, vertices src,dest
|
|  for all vertices v∈G do
|     visited[v]=-1
|  end for
|  found=false
|  visited[src]=src
|  enqueue src into queue Q
|  while not found ∧ Q is not empty do
|  |  dequeue v from Q
|  |  if v=dest then
|  |     found=true
|  |  else
|  |  |  for each (v,w) ∈ edges(G) with visited[w]=-1 do
|  |  |     visited[w]=v
|  |  |     enqueue w into Q
|  |  |  end for
|  |  end if
|  end while
|  if found then
|     display path in dest..src order
|  end if

Uses standard queue operations   (enqueue, dequeue, check if empty)

COMP2521 20T2 ♢ Graph Traversal [20/22]
❖ ... Breadth-first Search

The visited[] array after findPathBFS(G,0,7) succeeds

[Diagram:Pics/findPathBFS.png]

COMP2521 20T2 ♢ Graph Traversal [21/22]
❖ ... Breadth-first Search


Time complexity of BFS: O(V+E)   (same as DFS)

BFS finds a "shortest" path


In many applications, edges are weighted and we want path

We discuss weighted/directed graphs later.
COMP2521 20T2 ♢ Graph Traversal [22/22]


Produced: 22 Jun 2020