{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

View Full Document Right Arrow Icon
Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 18 March 2010 Reading for today: Chapter 14.4
Background image of page 1

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

View Full Document Right Arrow Icon
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.
Background image of page 2
3 Today: More graph algorithms Reachability (continued) Breadth-first and depth-first search Cycles Topological sort for a DAG
Background image of page 3

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

View Full Document Right Arrow Icon
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
Background image of page 4
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;
Background image of page 5

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

View Full Document Right Arrow Icon
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 =
Background image of page 6
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.
Background image of page 7

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

View Full Document Right Arrow Icon
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.
Background image of page 8
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.
Background image of page 9

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

View Full Document Right Arrow Icon
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 0 ,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 .
Background image of page 10
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;
Background image of page 11

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

View Full Document Right Arrow Icon
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!
Background image of page 12
13 Today: More graph algorithms Reachability Breadth-first and depth-first search Cycles Topological sort for a DAG
Background image of page 13

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

View Full Document Right Arrow Icon
14 Breadth First Search (BFS)
Background image of page 14
Image of page 15
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}