16 Graphs Part 2

16 Graphs Part 2 - Introduction to Graphs 15-211...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 18 March 2010 Reading for today: Chapter 14.4 2 Announcements Homework 4: Programming due 11:59 pm Monday Programming is substantial – don’t delay! Homework 5: Coming soon Pi Day: Friday (tomorrow) at 4-5 pm, Wean 5 th floor atrium. 3 Today: More graph algorithms Reachability (continued) Breadth-first and depth-first search Cycles Topological sort for a DAG Given s, compute the set of all reachable vertices: R(s) = { v 2 V | exists a path from s to v } Think inductively: s is in R(s). If u is in R(s) and (u, v) 2 E then v is in R(s). 4 Last time… Reachability Maintain S, the reachable nodes found so far Also maintain a set C, nodes we’ve added to S but still need to explore 5 Reachability S = C = { s }; while( C not empty ) u = pick and remove from C; foreach( v: (u,v) in E & v not in S ) //needs attention add v to S, C; Return S; 6 An example, with C being a stack: s a b c d e f g h i j k l m n t S = C = 7 An example, with C being a stack: Suppose s = Spain S = { C = { Poland Germany Denmark Nether. Belgium Austria France Switz. Czech R. Spain Italy Luxem. 8 Correctness: Proposition 1: At any time during the execution S ˧ R(s), the reachable nodes. That is, anything in S is also in R(s) Need to show that all vertices in S are reachable. Proposition 2: Upon completion, R(s) ˧ S. That is, everything in R(s) is also in S Need to show that if v is in R(s) then it is in S. 9 Reachability: Correctness 1 Proposition 1: At any time during the execution S ˧ R(s), the reachable nodes. Proof : Check that S ˧ R(s) is a loop invariant: Initially S = {s} ˧ R(s). Assume S ˧ R(s) and (u, v) requires attention. Then there is a path from s to u, since u 2 S. But then there also is a path from s to v. 10 Reachability: Correctness 2 Proposition 2: Upon completion, R(s) ˧ S. Proof: By induction on the length k of a shortest path from s to v, v 2 R(s). We will show that v is also in S. k = 0: s 2 S by initialization. k > 0: Consider path s = x ,x 1 , . . . ,x k − 1 ,x k = v. By IH, x k − 1 is placed into S and C at some point because it has a path length < k. Either v is already in S (because there is a shorter path to v), or the edge (x k − 1 , v) requires attention and v is added to S and C . 11 What is the running time of this? T(n, m) = θ (n+m) C contains each node at most once: n total time For each node, the for-loop inspects each edge at most once: m total foreach executions Running time of the inner loop is θ (1) S = C = { s }; while( C not empty ) u = pick and remove from C; foreach( v: (u,v) in E & v not in S ) //needs attention add v to S, C; Return S; 12 The choice of container C Different data structures for C affect the order we explore nodes, but not the algorithm’s correctness A stack: depth-first search (DFS) A queue: breadth-first search (BFS) Note: the order of the edge adjacency lists also affects the order! 13...
View Full Document

This document was uploaded on 05/10/2011.

Page1 / 87

16 Graphs Part 2 - Introduction to Graphs 15-211...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online