21. DFS

# 2010 goodrich tamassia depth first search 9 example

• Notes
• 14

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

©  2010 Goodrich, Tamassia Depth-First Search 9 Example (cont.) D B A C E D B A C E D B A C E D B A C E

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

©  2010 Goodrich, Tamassia Depth-First Search 10 DFS and Maze Traversal  The DFS algorithm is  similar to a classic  strategy for exploring  a maze We mark each  intersection, corner  and dead end (vertex)  visited We mark each corridor  (edge ) traversed We keep track of the  path back to the  entrance (start vertex)  by means of a rope  (recursion stack)
©  2010 Goodrich, Tamassia Depth-First Search 11 Properties of DFS Property 1 DFS ( G, v ) visits all the  vertices and edges in  the connected  component of  v Property 2 The discovery edges  labeled by  DFS ( G, v ) form a spanning tree of  the connected  component of  v D B A C E

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

©  2010 Goodrich, Tamassia Depth-First Search 12 Analysis of DFS Setting/getting a vertex/edge label takes  O (1)  time Each vertex is labeled twice  once as UNEXPLORED once as  VISITED Each edge is labeled twice once as UNEXPLORED once as  DISCOVERY  or  BACK Method incidentEdges is called once for each vertex DFS runs in  O ( n + m )  time provided the graph is  represented by the adjacency list structure Recall that  Σ v deg( v ) = 2 m
©  2010 Goodrich, Tamassia Depth-First Search 13 Path Finding We can specialize the  DFS algorithm to find a  path between two given  vertices  u  and  z  using the  template method pattern We call  DFS ( G, u ) with  u   as the start vertex We use a stack  S

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