{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

16 Graphs Part 2

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

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

Introduction to Graphs 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 18 March 2010 Reading for today: Chapter 14.4

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

View Full Document
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

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

View Full Document
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;

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

View Full Document
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.

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

View Full Document
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.

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

View Full Document
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 .
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;

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

View Full Document
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 Today: More graph algorithms Reachability Breadth-first and depth-first search Cycles Topological sort for a DAG

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

View Full Document