{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

2010 goodrich tamassia depth first search 6 depth

Info iconThis preview shows pages 6–9. Sign up to view the full content.

View Full Document Right Arrow Icon
©  2010 Goodrich, Tamassia Depth-First Search 6 Depth-First Search Depth-first search  (DFS) is a general  technique for traversing  a graph A DFS traversal of a  graph G  Visits all the vertices and  edges of G Determines whether G is  connected Computes the connected  components of G Computes a spanning  forest of G DFS on a graph with  n   vertices and  m  edges  takes  O ( n + m )  time DFS can be further  extended to solve other  graph problems Find and report a path  between two given  vertices Find a cycle in the graph Depth-first search is to  graphs what Euler tour  is to binary trees
Background image of page 6

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
©  2010 Goodrich, Tamassia Depth-First Search 7 DFS Algorithm The algorithm uses a mechanism  for setting and getting “labels” of  vertices and edges Algorithm DFS ( G, v ) Input graph G and a start vertex v of G Output labeling of the edges of G in the connected component of v as discovery edges and back edges v.setLabel ( VISITED ) for all e G.incidentEdges ( v ) if e.getLabel () = UNEXPLORED w e.opposite ( v ) if w.getLabel () = UNEXPLORED e.setLabel ( DISCOVERY ) DFS ( G, w ) else e.setLabel ( BACK ) Algorithm DFS ( G ) Input graph G Output labeling of the edges of G as discovery edges and back edges for all u G.vertices () u.setLabel ( UNEXPLORED ) for all e G.edges () e.setLabel ( UNEXPLORED ) for all v G.vertices () if v.getLabel () = UNEXPLORED DFS ( G, v )
Background image of page 7
©  2010 Goodrich, Tamassia Depth-First Search 8 Example D B A C E D B A C E D B A C E discovery edge back edge A visited vertex A unexplored vertex unexplored edge
Background image of page 8

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}