objectives
play

Objectives Graphs Graph Connectivity, Traversal BFS & DFS - PDF document

1/28/19 Objectives Graphs Graph Connectivity, Traversal BFS & DFS Implementations, Analysis Jan 28, 2019 CSCI211 - Sprenkle 1 Review What is a heap? When is it useful? What is a graph? What are two ways to


  1. 1/28/19 Objectives • Graphs • Graph Connectivity, Traversal • BFS & DFS Implementations, Analysis Jan 28, 2019 CSCI211 - Sprenkle 1 Review • What is a heap? Ø When is it useful? • What is a graph? Ø What are two ways to implement a graph? Ø What are their space costs? Ø What are the operations that can be performed on them? Ø What is the [time] cost of those operations? Jan 28, 2019 CSCI211 - Sprenkle 2 1

  2. 1/28/19 Review: Graph Representation: Adjacency Matrix • n � n matrix with A uv = 1 if (u, v) is an edge Ø Two representations of each edge (symmetric matrix) Ø Space: Q (n 2 ) Ø Checking if (u, v) is an edge: Q (1) time Ø Identifying all edges: Q (n 2 ) time 1 2 3 4 5 6 7 8 1 0 1 1 0 0 0 0 0 2 1 0 1 1 1 0 0 0 3 1 1 0 0 1 0 1 1 4 0 1 0 0 1 0 0 0 5 0 1 1 1 0 1 0 0 6 0 0 0 0 1 0 0 0 7 0 0 1 0 0 0 0 1 8 0 0 1 0 0 0 1 0 Jan 28, 2019 CSCI211 - Sprenkle 3 Graph Representation: Adjacency List • Node indexed array of lists Ø Two representations of each edge What are the Ø Space? extremes? Ø Checking if (u, v) is an edge? Ø Identifying all edges? edges 1 2 3 2 1 3 4 5 1 2 5 7 8 3 node 4 2 5 5 2 3 4 6 5 6 7 3 8 8 3 7 Jan 28, 2019 CSCI211 - Sprenkle 4 2

  3. 1/28/19 Graph Representation: Adjacency List • Node indexed array of lists Ø Two representations of each edge degree = number of neighbors of u Ø Space = 2m + n = O(m + n) Ø Checking if (u, v) is an edge takes O(deg(u)) time Ø Identifying all edges takes Q (m + n) time edges 1 2 3 2 1 3 4 5 1 2 5 7 8 3 node 4 2 5 5 2 3 4 6 5 6 7 3 8 8 3 7 Jan 28, 2019 CSCI211 - Sprenkle 5 Paths and Connectivity • Def. A path in an undirected graph G = (V, E) is a sequence P of nodes v 1 , v 2 , …, v k-1 , v k Ø Each consecutive pair v i , v i+1 is joined by an edge in E • Def. A path is simple if all nodes are distinct • Def. An undirected graph is connected if ∀ pair of nodes u and v, there is a path between u and v • Short path • Distance Jan 28, 2019 CSCI211 - Sprenkle 6 3

  4. 1/28/19 Cycles • Def. A cycle is a path v 1 , v 2 , …, v k-1 , v k in which v 1 = v k , k > 3, and the first k-1 nodes are all distinct cycle C = 1-2-4-5-3-1 Jan 28, 2019 CSCI211 - Sprenkle 7 TREES Jan 28, 2019 CSCI211 - Sprenkle 8 4

  5. 1/28/19 Trees • Def. An undirected graph is a tree if it is connected and does not contain a cycle • Simplest connected graph Ø Deleting any edge from a tree will disconnect it Jan 28, 2019 CSCI211 - Sprenkle 9 Rooted Trees • Given a tree T, choose a root node r and orient each edge away from r • Models hierarchical structure root r parent of v v child of v a tree the same tree, rooted at 1 Why n-1 edges? Jan 28, 2019 CSCI211 - Sprenkle 10 5

  6. 1/28/19 Rooted Trees • Why n-1 edges? Ø Each non-root node has an edge to its parent root r parent of v v child of v a tree the same tree, rooted at 1 Jan 28, 2019 CSCI211 - Sprenkle 11 Trees • Theorem. Let G be an undirected graph on n nodes. Any two of the following statements imply the third: Ø G is connected Ø G does not contain a cycle Ø G has n -1 edges Jan 28, 2019 CSCI211 - Sprenkle 12 6

  7. 1/28/19 Phylogeny Trees • Describe evolutionary history of species Ø mammals and birds share a common ancestor that they animals do not share with other species Ø all animals are descended from an ancestor not shared with mushrooms, trees, and bacteria Jan 28, 2019 CSCI211 - Sprenkle 13 GRAPH CONNECTIVITY & TRAVERSAL Jan 28, 2019 CSCI211 - Sprenkle 14 7

  8. 1/28/19 Connectivity • s-t connectivity problem. Given nodes s and t , is there a path between s and t ? • s-t shortest path problem. Given nodes s and t , what is the length of the shortest path between s and t ? • Applications Ø Facebook Ø Maze traversal Ø Kevin Bacon number Ø Spidering the web Ø Fewest number of hops in a communication network Jan 28, 2019 CSCI211 - Sprenkle 15 Application: Connected Component • Find all nodes reachable from s • Connected component containing node 1 is { 1, 2, 3, 4, 5, 6, 7, 8 } Jan 28, 2019 CSCI211 - Sprenkle 16 8

  9. 1/28/19 Application: Flood Fill • Given lime green pixel in an image, change color of entire blob of neighboring lime pixels to blue Ø Node: pixel Ø Edge: two neighboring lime pixels Ø Blob: connected component of lime pixels recolor lime green blob to blue Jan 28, 2019 CSCI211 - Sprenkle 17 Application: Flood Fill • Given lime green pixel in an image, change color of entire blob of neighboring lime pixels to blue Ø Node: pixel Ø Edge: two neighboring lime pixels Ø Blob: connected component of lime pixels recolor lime green blob to blue Jan 28, 2019 CSCI211 - Sprenkle 18 9

  10. 1/28/19 My Facebook Friends Created with Social Graph UDel HS Family Gburg Duke Extreme Blue Jan 28, 2019 CSCI211 - Sprenkle 19 A General Algorithm R will consist of nodes to which s has a path R = {s} while there is an edge (u,v) where u ∈ R and v ∉ R add v to R it's safe R s to add v u v • R will be the connected component containing s • Algorithm is underspecified In what order should we consider the edges? Jan 28, 2019 CSCI211 - Sprenkle 20 10

  11. 1/28/19 Possible Orders • Breadth-first • Depth-first Jan 28, 2019 CSCI211 - Sprenkle 21 Breadth-First Search • Intuition . Explore outward from s in all possible directions (edges), adding nodes one “layer” at a time L 0 • Algorithm s L 1 L 2 L n-1 Ø L 0 = { s } Ø L 1 = all neighbors of L 0 Ø L 2 = all nodes that have an edge to a node in L 1 and do not belong to L 0 or L 1 Ø L i+1 = all nodes that have an edge to a node in L i and do not belong to an earlier layer Jan 28, 2019 CSCI211 - Sprenkle 22 11

  12. 1/28/19 Run BFS on This Graph s = 1 Jan 28, 2019 CSCI211 - Sprenkle 23 Example of Breadth-First Search s = 1 L 0 L 1 L 2 L 3 Creates a tree -- is a node in the graph that is not in the tree Jan 28, 2019 CSCI211 - Sprenkle 24 12

  13. 1/28/19 Breadth-First Search • Theorem. For each i , L i consists of all nodes at distance exactly i from s . There is a path from s to t iff t appears in some layer. s L 1 L 2 L n-1 • What does this theorem mean? • Can we determine the distance between s and t? Jan 28, 2019 CSCI211 - Sprenkle 25 Breadth-First Search • Theorem. For each i , L i consists of all nodes at distance exactly i from s . There is a path from s to t iff t appears in some layer. Ø Length of shortest path to t from s , is the i from L i Ø All nodes reachable from s are in L 1 , L 2 , …, L n-1 s L 1 L 2 L n-1 Jan 28, 2019 CSCI211 - Sprenkle 26 13

  14. 1/28/19 Breadth-First Search • Property . Let T be a BFS tree of G = (V, E), and let (x, y) be an edge of G. Then the level of x and y differ by at most 1. If x is in L i , G: then y must be in ??? Jan 28, 2019 CSCI211 - Sprenkle 27 Breadth-First Search • Property . Let T be a BFS tree of G = (V, E), and let (x, y) be an edge of G. Then the level of x and y differ by at most 1. If x is in L i , then y must be in • L i-1 : y was reached before x G: • L i : a common parent of x and y was reached first • L i+1 : y will be added in the next layer Jan 30, 2019 CSCI211 - Sprenkle 28 14

  15. 1/28/19 Connected Component: BFS • Find all nodes reachable from s In general…. R will consist of nodes to which s has a path R = {s} while there is an edge (u,v) where u ∈ R and v ∉ R add v to R In what order does BFS consider edges? Jan 28, 2019 CSCI211 - Sprenkle 29 Connected Component: BFS vs DFS • Find all nodes reachable from s In general…. R will consist of nodes to which s has a path R = {s} while there is an edge (u,v) where u ∈ R and v ∉ R add v to R • Theorem. Upon termination, R is the connected component containing s Ø BFS = explore in order of distance from s Ø DFS = explore until hit “deadend” Jan 28, 2019 CSCI211 - Sprenkle 30 15

  16. 1/28/19 Depth-First Search • Need to keep track of where you’ve been • When reach a “dead-end” (already explored all neighbors), backtrack to node with unexplored neighbor • Algorithm : DFS(u): Mark u as “Explored” and add u to R For each edge (u, v) incident to u If v is not marked “Explored” then DFS(v) Jan 28, 2019 CSCI211 - Sprenkle 31 Depth-First Search • How does DFS work on this graph? Ø Starting from node 1 Jan 28, 2019 CSCI211 - Sprenkle 32 16

  17. 1/28/19 Looking Ahead • Monday, 11:59 p.m.: journal - Chapter 2.5, 3.1 • Friday: Problem Set 3 due Jan 28, 2019 CSCI211 - Sprenkle 33 17

Recommend


More recommend