21. DFS

# 2010 goodrich tamassia depth first search 6 depth

• Notes
• 14

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

©  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

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

©  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 )
©  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

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

This is the end of the preview. Sign up to access the rest of the document.
• Fall '09
• Tamassia, depth­first search

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern