DepthFirst Traversal Implementation - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

DepthFirst Traversal Implementation

Description:

Depth-first traversal depth-first tree ... depth first traversal Vector. Excercise - Vector returned by depthFirstTraversal. Graph G ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 11
Provided by: csUi
Category:

less

Transcript and Presenter's Notes

Title: DepthFirst Traversal Implementation


1
Depth-First Traversal - Implementation
  • Depth-first traversal
  • Depth-first tree
  • Graph with gt1 connected components
  • Construct the Vector return by depthFirstTraversal
  • Methods related with depthFirstTraversal

2
Depth-first traversal(Implemented in
MyAbstractGraph)
  • The wrapper recursive depthFirstTraversal
    method

// this should be //public Vector
depthFirstTraversal() public void
depthFirstTraversal() if( numberOfVertices()
gt 0) // Creates a boolean array to
keep track of //vertices that have
already been visited boolean visited
new booleansize for(int i 0 i lt
size i) visitedi false
//Get the first unvisited vertex
Object start firstVertex (visited)
// Call the recursive depthFirstTraversal.
depthFirstTraversal (start, visited)
public void depthFirstTraversal( Object start,
boolean visited) // Mark the current
vertex as visited visited getIndex(start)
true // Get the neighbors of the current
vertex Vector nbrs getNeighbors(start)
int numNeighbors nbrs.size() Object
currentNbr // Process each neighbor of the
current vertex and // proceed to search from
each unvisited neighbor for(int i 0 i lt
numNeighbors i) currentNbr
nbrs.get(i) if(!visitedgetIndex(currentNbr
)) depthFirstTraversal(currentNbr,
visited)
Depth-first tree the recursion tree of
depthFirstTraversal
3
Depth-first traversal depth-first tree
  • Assume neighboring vertices are scanned in
    alphabetical order by depth-first traversal
  • First call to recursive d-f traversal with start
    a.

Draw the depth-first tree
4
Graph with gt1 connected components
  • Graph G
  • Depth-first trees connected components

start a
start f
5
Process graphs with gt1 connected components
  • Modify the wrapper

// this should be //public Vector
depthFirstTraversal() public void
depthFirstTraversal() if( numberOfVertices()
gt 0) // Creates a boolean array to
keep track of //vertices that have
already been visited boolean visited
new booleansize for(int i 0 i lt
size i) visitedi false
//Get the first unvisited vertex
Object start firstVertex (visited)
// Call the recursive depthFirstTraversal.
depthFirstTraversal (start, visited)

public void depthFirstTraversal( Object start,
boolean visited) // Mark the current
vertex as visited visited getIndex(start)
true // Get the neighbors of the current
vertex Vector nbrs getNeighbors(start)
int numNeighbors nbrs.size() Object
currentNbr // Process each neighbor of the
current vertex and // proceed to search from
each unvisited neighbor for(int i 0 i lt
numNeighbors i) currentNbr
nbrs.get(i) if(!visitedgetIndex(currentNbr
)) depthFirstTraversal(currentNbr,
visited)
public void depthFirstTraversal( Object start,
boolean visited) .
while (there exist unvisited vertices)
//Get the first unvisited vertex Object start
firstVertex (visited) // Call the
recursive depthFirstTraversal.
depthFirstTraversal (start, visited)
6
Construct the Vector returned by
depthFirstTraversal
  • public Vector depthFirstTraversal ()
  • Return a Vector of SinglyLinkedListElement (SLLE)
  • Store the traversal path to each vertex

SSLE.value()
SSLE.next()
parent in the depth-first tree
size numberOfVertices()
7
Example - Vector returned by depthFirstTraversal
  • map Chicago, Denver, Buffalo, New
    York, Houston, Atlanta

null
depth first traversal Vector
Graph G
depth first tree of G
8
Excercise- Vector returned by
depthFirstTraversal
  • Graph G
  • Depth-first trees
  • Write the Vector returned by depthFirstTraversal

9

Methods related with depthFirstTraversal
public interface MyGraph public Vector
depthFirstTraversal ()
more public methods to be implemented in
MyMatrixGraph and MyListGraph
public Object firstVertex (boolean visited)
public int getIndex (Object v)
Access vertices in map through firstVertex
(boolean v) and getIndex (Object v )
implements
public abstract class MyGraph implements
MyGraph public Vector depthFirstTraversal ()
//wrapper of d-f public void depthFirstTraversal
() //recursive d-f
extends
public class MyListGraph extends
MyAbstractGraph protected Vector map
protected int numVertices, numEdges protected
DoublyLinkedList L
public class MyMatrixGraph extends
MyAbstractGraph protected Vector map
protected int numVertices, numEdges protected
Matrix M
//implement firstVertex and getIndex
//implement firstVertex and getIndex
10
Next step - Construct Vector returned by
depthFristTraversal
public Vector depthFirstTraversal() if(
numberOfVertices() gt 0) // Creates a
boolean array to keep track of //vertices
that have already been visited boolean
visited new booleansize for(int i
0 i lt size i) visitedi
false Initialize returning Vector
while (there exist unvisited vertices)
//Get the first unvisited vertex
Object start firstVertex (visited)
//Call the recursive depthFirstTraversal.
depthFirstTraversal () //more
arguments needed ?
public void depthFirstTraversal() // Mark
the current vertex as visited visited
getIndex(start) true Add a new
SinglyLinkedListElement ( who is the
parent ? ) . // Get the neighbors of the
current vertex Vector nbrs
getNeighbors(start) int numNeighbors
nbrs.size() Object currentNbr // Process
each neighbor of the current vertex and //
proceed to search from each unvisited neighbor
for(int i 0 i lt numNeighbors i)
currentNbr nbrs.get(i)
if(!visitedgetIndex(currentNbr))
depthFirstTraversal(currentNbr, visited)
Write a Comment
User Comments (0)
About PowerShow.com