# 12-5 Notes - Depth First Search o Traverse all verticies of...

This preview shows pages 1–2. Sign up to view the full content.

Depth First Search o Traverse all verticies of G o Mark each vertex in G w/ “visit times” Every vertex in G has 2 times S(v) – start time > 0 once set F(v) – finishing time > 0 once set o A vertex has 3 states Undiscovered s(v) = 0 In progress s(v) > 0, f(v) = 0 Finished f(v) >0 DFS Code DFS(G) For each u in V{ S(u) = f(u) = 0 Parent(u) = null } Time = 1 For each u in V{ If s(u)=0 DFS-VISIT(G, u) DFS-Visit Code DFS-VISIT(G,u) S(u) = time Time++ For each v in Adj(u){ If s(v) =0{ Parent(v) = u DFS-VISIT(G,v) } } F(u) = time Time++ EXAMPLE ON HW Cost: (n+m) Can do unconnected graphs o Just will complete chunk a, then start again in chunk b Applications o Application #1: Cycle detector Given directed graph G, does G have a cycle? Proposal: run DFS on G If we ever encounter an “in progress” vertex while checking adjacencies, then say “cyle” Claim: G has a sycle iff DFS discovers an in progress vertex Pf: ( )if DFS discovers an in progress vertex then G has a cycle

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 2

12-5 Notes - Depth First Search o Traverse all verticies of...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online