Graph Traversals - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Graph Traversals

Description:

The iterative preorder depth-first algorithm is: push the starting vertex, v, onto the stack; ... Recursive preorder Depth-First Traversal Implementation ... – PowerPoint PPT presentation

Number of Views:603
Avg rating:3.0/5.0
Slides: 15
Provided by: Nul82
Category:

less

Transcript and Presenter's Notes

Title: Graph Traversals


1
Graph Traversals
  • General Traversal Algorithm
  • Depth-First Traversals.
  • Algorithms.
  • Example.
  • Implementation.
  • Breadth-First Traversal.
  • The Algorithm.
  • Example.
  • Implementation.
  • Review Questions.

2
General Traversal Algorithm
  • This algorithm ensures that all vertices are
    visited in the graph.
  • This is certainly important in the case where the
    graph is disconnected (in the case of undirected
    graphs) or not strongly connected (in the case of
    directed graphs)
  • The method call doTraverse is replaced with all
    the subsequent traversal methods discussed.
  • dfsPreorder, dfsPostorder, BreadthFirst.
  • GraphTraversal (Graph G)
  • for each vertex v ? G do
  • mark v as unvisited
  • for each vertex v ? G do
  • if v is marked as unvisited
  • doTraverse(G, v)

3
Depth-First Traversal Algorithm
  • In this method, After visiting a vertex v, which
    is adjacent to w1, w2, w3, ... Next we visit
    one of v's adjacent vertices, w1 say. Next, we
    visit all vertices adjacent to w1 before coming
    back to w2, etc.
  • Must keep track of vertices already visited to
    avoid cycles.
  • The method can be implemented using recursion or
    iteration.
  • The iterative preorder depth-first algorithm is
  • push the starting vertex, v, onto the stack
  • mark v as visited
  • while(stack is not empty)
  • pop vertex v off the stack
  • visit v
  • for each vertex w adjacent to v that is not
    marked visited do
  • mark w as visited
  • push w onto the stack
  • Note Adjacent vertices can be pushed in any
    order but to obtain a unique
  • traversal, we will push them in reverse
    alphabetical order.

4
Example
  • Demonstrates depth-first traversal using an
    explicit stack.

Order of Traversal
A B C F D G H I E
Stack
5
Recursive preorder Depth-First Traversal
Implementation
dfsPreorder(G, v) visit v mark v as visited
for each adjacent vertex w of v do
if (w has not been marked as visited)
dfsPreorder(G, w)
  • The following is the code for the recursive
    preorderDepthFirstTraversal method of the
    AbstractGraph class

public void preorderDepthFirstTraversal(Visitor
visitor, Vertex start) boolean visited
new booleannumberOfVertices for(int v 0
v lt numberOfVertices v) visitedv
false preorderDepthFirstTraversal(visitor,
start, visited)
6
Recursive preorder Depth-First Traversal
Implementation (contd)
  • private void preorderDepthFirstTraversal(Visitor
    visitor,
  • Vertex v, boolean
    visited)
  • if(visitor.isDone())
  • return
  • visitor.visit(v)
  • visitedgetIndex(v) true
  • Iterator p v.getSuccessors()
  • while(p.hasNext())
  • Vertex to (Vertex) p.next()
  • if(! visitedgetIndex(to))
  • preorderDepthFirstTraversal(visitor, to,
    visited)

7
Recursive preorder Depth-First Traversal
Implementation (contd)
The Preorder Depth First Traversal Tree is shown
below
A
B
C
D
E
F
G
H
I
8
Recursive postorder Depth-First Traversal
Implementation
dfsPostorder(G, v) mark v as visited
for(each neighbour w of v) if(w is not
marked visited) dfsPostorder(G, w)
visit v
  • The following is the code for the recursive
    postorderDepthFirstTraversal method of the
    AbstractGraph class
  • public void postorderDepthFirstTraversal(Visitor
    visitor,
  • Vertex
    start)
  • boolean visited new booleannumberOfVertice
    s
  • for(int v 0 v lt numberOfVertices v)
  • visitedv false
  • postorderDepthFirstTraversal(visitor, start,
    visited)

9
Recursive postorder Depth-First Traversal
Implementation (contd)
  • private void postorderDepthFirstTraversal(
  • Visitor visitor, Vertex v, boolean
    visited)
  • if(visitor.isDone())
  • return
  • // mark v
  • visitedgetIndex(v) true
  • Iterator p v.getSuccessors()
  • while(p.hasNext())
  • Vertex to (Vertex) p.next()
  • if(! visitedgetIndex(to))
  • postorderDepthFirstTraversal(visitor,
    to, visited)
  • // visit v
  • visitor.visit(v)

10
Recursive postorder Depth-First Traversal
Implementation (contd)

The Postorder Depth First Traversal Tree is shown
below
11
Breadth-First Traversal Algorithm
  • In this method, After visiting a vertex v, we
    must visit all its adjacent vertices w1, w2, w3,
    ..., before going down next level to visit
    vertices adjacent to w1 etc.
  • The method can be implemented using a queue.
  • A boolean array is used to ensure that a vertex
    is enqueued only once.
  • BreadthFirst(G, v)
  • 1 enqueue the starting vertex v mark it as
    visited
  • 2 while(queue is not empty)
  • 3 dequeue a vertex v from the queue
  • 4 visit v.
  • enqueue vertices adjacent to v that are not
    marked visited
  • Note Adjacent vertices can be enqueued in any
    order but to obtain a unique
  • traversal, we will enqueue them in
    alphabetical order.

12
Example
  • Demonstrating breadth-first traversal using a
    queue.

Queue front
The Breadth-firstTraversal Tree is shown below
A
B
C
D
E
F
Order of Traversal
G
H
I
A B D E C G F H I
Queue rear
13
Breadth-First Traversal Implementation
  • public void breadthFirstTraversal(Visitor
    visitor, Vertex start)
  • boolean enqueued new booleannumberOfVertic
    es
  • for(int i 0 i lt numberOfVertices i)
    enqueuedi false
  • Queue queue new QueueAsLinkedList()
  • enqueuedgetIndex(start) true
  • queue.enqueue(start)
  • while(!queue.isEmpty() !visitor.isDone())
  • Vertex v (Vertex) queue.dequeue()
  • visitor.visit(v)
  • Iterator it v.getSuccessors()
  • while(it.hasNext())
  • Vertex to (Vertex) it.next()
  • int index getIndex(to)
  • if(!enqueuedindex)
  • enqueuedindex true
  • queue.enqueue(to)

14
Review Questions
  • 1. Considera depth-first traversal of the
    undirected graph GA shown above, starting from
    vertex a.
  • List the order in which the nodes are visited in
    a preorder traversal showing the depth-first
    traversal tree.
  • List the order in which the nodes are visited in
    a postorder traversal
  • 2. Repeat exercise 1 above for a depth-first
    traversal starting from vertex d.
  • 3. List the order in which the nodes of the
    undirected graph GA shown above are visited by a
    breadth first traversal that starts from vertex
    a, showing the breadth-first traversal tree.
    Repeat this exercise for a breadth-first
    traversal starting from vertex d.
  • 4. Repeat Exercises 1 and 3 for the directed
    graph GB.
Write a Comment
User Comments (0)
About PowerShow.com