Lecture 19 Traversal Algorithm - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Lecture 19 Traversal Algorithm

Description:

Graph traversal , a process to retrace arcs so that each node ... prolog, when processing a query based on a recursive definition, purses a depth-first search. ... – PowerPoint PPT presentation

Number of Views:84
Avg rating:3.0/5.0
Slides: 13
Provided by: Defa67
Category:

less

Transcript and Presenter's Notes

Title: Lecture 19 Traversal Algorithm


1
Lecture 19 Traversal Algorithm
  • July 28th, 2003

2
What is Traversal
  • Graph traversal , a process to retrace arcs so
    that each node of the graph is visited.
  • Recall in Section 5.2, We have several tree
    traversal algorithms
  • Two general traversal methods
  • Depth first search
  • Breadth first search
  • Application of traversal
  • Solve the path questions about a graph. Is there
    a path from node x to node y? Is there a path
    through G that use each arc once? What is the
    minimum weight path between x and y?

3
Traversal --Example
  • Graph G How to order the nodes?

4
Algorithm Depth-first search
  • DepthFirst (graph G node a)
  • mark a visited
  • write(a)
  • for each node n adjacent to a do
  • if n not visited then
  • DepthFirst(G,n)
  • end if
  • end for
  • end DepthFirst

5
Depth-first search
  • What idea have your seen before? Recursion
  • In the depth-first search, we begin at an
    arbitrary node a of the graph, marked it visited,
    and write it down. We then strike out on a path
    away from a, visiting and writing down nodes,
    proceeding as far as possible until there are no
    more unvisited nodes on that path. We then back
    up the path, at each node exploring any new side
    paths, until finally we retreat back to a.

6
Depth first search (cont.)
  • What is the traversal of the example?
  • a,b,c,d,f,g,e,h,I,k,j
  • What is the traversal of the Practice 14?
  • Correction, the right most node is l.
  • a,e,d,b,c,I,f,g,h,l,k

7
Algorithm Breadth-first search
  • BreadthFirst (graph G node a)
  • Local variable queue of nodes Q
  • initialize Q to be empty
  • mark a visited
  • write(a)
  • enqueue(a,Q)
  • while Q is not empty do
  • for each node n adjacent to front(Q) do
  • if n not visited then
  • mark n visited
  • write(n)
  • enqueue(n,Q)
  • end if
  • end for
  • dequeue(Q)
  • end while
  • end BreadthFirst

8
Breadth-first search Example
  • In breadth-first search, befinning at an
    arbitrary node a , we first fan out from node a
    to visit nodes that are adjacent to a, then we
    fan out from those nodes.
  • A queue structure is required here.

9
Breadth-first search Example
  • What is in queue?
  • a
  • b,e,h,i
  • e,h,i,c
  • h,i,c,j
  • j,k,d,f
  • f,g

10
Time complexity?
  • Given a graph with n nodes and m arcs.
  • What kind of graph representation is good?
    Adjacent list or adjacent matrix?
  • What is the time complexity? max(m,n)

11
Some more applications
  • Provide a basis for performing other
    graph-related tasks.
  • Provide an efficient algorithm for reachability
    problem for directed graph
  • Sparse graph, O(n2)
  • Non sparse graph, O(n3)
  • Topological sort
  • prolog, when processing a query based on a
    recursive definition, purses a depth-first
    search.

12
Exercise
  • Exercise 6.4
  • 1,2,3,11,12,13
Write a Comment
User Comments (0)
About PowerShow.com