notes.pdf

# Algorithm 32 wrapper for traverse 1 procedure

• Notes
• 104
• 100% (2) 2 out of 2 people found this document helpful

This preview shows page 70 - 74 out of 104 pages.

Algorithm 32 Wrapper for Traverse 1: procedure TraverseAll 2: for all vertices v do 3: if v is unmarked then 4: Traverse ( v ) 5: end if 6: end for 7: end procedure Lemma 9.2.2. TraverseAll () marks every vertex in disconnected graph exactly once. Set of pair ( parent ( v ) , v ) with parent 6 = defines spanning forest. 9.3 Standard Graph Algorithm Step 1 Maintain set S of visited vertices, and T \ S of unvisited. Step 2 Maintain tree over S . Step 3 Repeat: (a) Pick vertex w in T , which is adjacent to S . (b) Add to S , remove from T , update tree on S . 9.4 Depth First Search 9.4.1 Formal Definition The Formal definition of DFS is shown in algorithm 33. 61
Algorithm 33 Formal Definition of DFS 1: procedure DFS ( v ) 2: Mark v 3: for each edge vw do 4: if w is unmarked then 5: DFS ( w ) 6: end if 7: end for 8: end procedure Observation: The procedure create a similar stack of that in algorithm 30, but it’s the program’s stack. When DFS ( w ) makes recursive call, it stores current program on program stack, i.e. when DFS ( v ) calls DFS ( w ), DFS ( v ) is on stack. Given v in undirected G , DFS ( v ) visits all vertices in v ’s component. Algorithm 34 DFS All the Component 1: procedure DFSAll ( G ) 2: for all v V do 3: if v is unmarked then 4: DFS ( v ) 5: end if 6: end for 7: end procedure 9.4.2 Count and Label Components DFS Algorithm can be modified to count and label all the components. 62
Algorithm 35 Count and Label the Components 1: procedure CountAndLabel ( G ) 2: count = 0 3: for all v V do 4: if v is unmarked. then 5: count = count + 1 6: LabelComponent ( v, count ) 7: end if 8: end for 9: return count 10: end procedure 11: procedure LabelComponent ( v, count ) 12: Mark v 13: component ( v ) = count 14: for each vw do 15: if w is unmarked then 16: LabelComponent ( w, count ) 17: end if 18: end for 19: end procedure 9.4.3 Pre/Post Order Traverse of Binary Tree Originally, the definition are: Pre-order: visit, recursive call on left, recursive call on right. Post-order: recursive call on left, recursive call on right, visit. Consider the problem in the view of DFS: Pre-order: vertices put on stack; Post-order: vertices taken off from stack. A modified algorithm is shown in algorithm 36 63
Algorithm 36 Pre/Post Order Based on DFS 1: procedure DFSAll ( G ) 2: clock = 0 3: for all v V do 4: if v is unmarked then 5: DFS ( v ) 6: end if 7: end for 8: end procedure 9: procedure DFS ( v ) 10: Mark v 11: Previsit ( v ) 12: for each edge vw do 13: if w is unmarked then 14: PreVisit ( w ) 15: end if 16: end for 17: PostVisit ( v ) 18: end procedure 19: procedure PreVisit ( v ) 20: pre ( v ) = clock 21: clock = clock + 1 22: end procedure 23: procedure PostVisit ( v ) 24: post ( v ) = clock 25: clock = clock - 1 26: end procedure 9.4.4 Directed Graphs and Reachability For undirected graph, DFS ( v ) explores the component of v . For directed graph, it is trickier. For v, u in directed graph, u is reachable from v , if directed path from v to u .

Want to read all 104 pages?

Want to read all 104 pages?

#### You've reached the end of your free preview.

Want to read all 104 pages?

• Fall '09
• SUB

{[ 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