Lecture 19 Traversal Algorithm - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Lecture 19 Traversal Algorithm

Description:

Depth-First Search (Algo) DepthFirst (graph G; node a) mark as ... Depth-First Search (Algo) (Review: What idea have we seen b4 in cases like this? Recursion) ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 17
Provided by: Defa67
Category:

less

Transcript and Presenter's Notes

Title: Lecture 19 Traversal Algorithm


1
Lecture 19 Traversal Algorithm
  • July 28th, 2003

2
Euler Path Hamilton Circuit
  • Sectn 6.2 exhibits these traversals (ones of
    classic(al) status)
  • Euler Encounter all nodes BUT use each arc
    (only) one time --- do not have to return to
    starting point (see immedly below)
  • An Euler Path (p. 427) exists in a connected
    graph iff there are
  • -- no odd nodes --- (start end at
    same Node) or
  • -- two odd nodes --- (start at one
    Node end at another)
  • Hamilton A cycle (circuit) that touches all
    the nodes but is otherwise free
  • A Hamiltonian Circuit problem has no general
    solution
  • --- ie. No efficient algorithm exists
  • --- A complete graph (defd on p. 346)
    as one in which any two distinct nodes
    are adjacent) for n gt 2 represents a special
    case and
  • there are others besides!

3
Shortest Path Minimal Spanning Tree
  • Have to short-circuit in Su 04
  • Along with Euler Path Ham Circuit
  • GOT COMPANY may not have been covered in recent
    Summers?

4
What is Traversal --- Sectn 6.4 pp. 447ff
  • Graph traversal , a process to retrace arcs so
    that each node of the graph is visited.
  • Recall from Section 5.2, We have several tree
    traversal algorithms
  • Two general traversal (search) methods
  • Depth first search
  • Breadth first search
  • Traversals (also) solve path questions about a
    graph (some already seen)
  • 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 ?

5
Traversal --Example
  • Fig 6.13 p. 447B We can generalize tree
    search (We deal with Depth First Search re an
    algo and text descriptions --- in the next two
    slides)

6
Depth-First Search (Algo)
  • DepthFirst (graph G node a)
  • mark as 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

7
Depth-First Search (Algo)
  • (Review What idea have we seen b4 in cases like
    this? Recursion)
  • In depth-first search
  • --- We begin at an arbitrary node a of the
    graph --- We mark it visited, and write
    it down.
  • --- We then strike out on a path away from a,
    visitg 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 we took down and
    at each node explore any new side paths, until
    finally we retreat back to a.

8
Depth First Search (Examples)
  • Ex. 11 p. 448 What is the depth-first
    traversal (starting from a)? (You kinda have to
    trace it out with your fingers on the book
    page)
  • Ans. a, b, c, d, f, g, e, h, I, k, j NODE I
    ????
  • Prax 14 p. 449 What is the (depth-first)
    traversal starting from a? (Again, traceg by
    hand is called for.)
  • Bk Correction is needed - the graph is missg its
    right most node (it is really l).
  • Ans. a, e, d, b, c, i, f, g, h, l, k, m, j

9
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

10
Breadth-first Search
  • In breadth-first search
  • --- we begin at an arbitrary node a
  • --- we first fan out from node a to visit
    nodes that are adjacent to a --- in a
  • left to right fashion
  • --- then we fan out from those nodes.
  • A queue structure is required here.

11
Ex. 12 p. 451 Breadth-first Search Example
  • What is in the (required) queue?
  • (See the details on p. 451-52 folog the
    algo pseudocode)
  • a
  • b,e,h,I See Fig. 6.15, p. 450
  • e,h,i,c after we remove b and
    add c
  • See Fig.
    6.16, p. 452
  • h,i,c,j after we remove e and
    add j
  • j,k,d,f
  • f,g and we are now finished
  • Ans a b e h i c j k d f g
  • Prax 15 p. 452 asks us to do BREADTH 1st to
    the same case we did in Prax 14 as a Depth First
    example

12
Time complexity?
  • Given a graph with n nodes and m arcs.
  • Which (kind of) graph rep. is better?
  • adjacency list
  • or
  • adjacency matrix?
  • (The ans. is the former! --- at least in
    many cases (most applic cases?)
  • What is the time complexity (both DF BF)?
  • O(max(m,n))
  • (Note to note maker --- Should we use ? instead
    of O here (compare book closer) and continue to
    search for what seems to scant opps. to get
    symbology up to snuff?)

13
More applications
  • These provide thots about/for performg other
    graph-related tasks, such as
  • An efficient algorithm for reachability in
    directed graphs
  • Consider as cases
  • Sparse graphs, O(n2)
  • Non sparse graphs, O(n3) --- compare gen matrix
    mult

14
More applications/cont.d
  • Topological sort ---
  • In Sectn 4.2 topolog sort is related to a
    problem of determining a total order from a Poset
    --- a comment that seems only to serve to
    telegraph our startg point.
  • Prax 17 (p 454)
    a c
  • 1. Start at a do DEPTH-1st Search
    \ / \
  • to e, say, via b c (Put (1) at e) (2)
    at c b e
  • (Puts occur when we make a final
    \ /
  • stop at a node before we back-up)
    d
  • 2. Back up to b proceed to d stop a last
    time put (3) on
  • d and ditto with resp. b and, yes, a,
    too
  • 3. Unravel from high (5) to low (1)
    Ans. a b d c e
  • 4. (See possible ans. a b c d e !!!)

15
More applications/cont.d
  • Prolog, when processing a query based on a
    recursive definition, purses a depth-first
    search.
  • (See Ex 40, Ch 1)
  • Consider our natural language (syntax example)
    and realize the processing order

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