objec ves
play

Objec&ves Graphs Graph Connec&vity, Traversal BFS & - PDF document

1/29/18 Objec&ves Graphs Graph Connec&vity, Traversal BFS & DFS Implementa&ons, Analysis Jan 29, 2018 CSCI211 - Sprenkle 1 Review What is a heap? When is it useful? What is a graph? What is one way to


  1. 1/29/18 Objec&ves • Graphs • Graph Connec&vity, Traversal • BFS & DFS Implementa&ons, Analysis Jan 29, 2018 CSCI211 - Sprenkle 1 Review • What is a heap? Ø When is it useful? • What is a graph? Ø What is one way to implement a graph? Jan 29, 2018 CSCI211 - Sprenkle 2 1

  2. 1/29/18 Graph Representa&on: Adjacency Matrix • n×n matrix with A uv = 1 if (u, v) is an edge Ø Two representa&ons of each edge (symmetric matrix) Ø Space? Ø Checking if (u, v) is an edge? Ø Iden&fying all edges? 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 29, 2018 CSCI211 - Sprenkle 3 Graph Representa&on: Adjacency Matrix • n×n matrix with A uv = 1 if (u, v) is an edge Ø Two representa&ons of each edge (symmetric matrix) Ø Space: Θ (n 2 ) Ø Checking if (u, v) is an edge: Θ (1) &me Ø Iden&fying all edges: Θ (n 2 ) &me 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 29, 2018 CSCI211 - Sprenkle 4 2

  3. 1/29/18 Graph Representa&on: Adjacency List • Node indexed array of lists Ø Two representa&ons of each edge What are the Ø Space? extremes? Ø Checking if (u, v) is an edge? Ø Iden&fying 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 29, 2018 CSCI211 - Sprenkle 5 Graph Representa&on: Adjacency List • Node indexed array of lists degree = number of Ø Two representa&ons of each edge neighbors of u Ø Space = 2m + n = O(m + n) Ø Checking if (u, v) is an edge takes O(deg(u)) &me Ø Iden&fying all edges takes Θ (m + n) &me edges 1 2 3 2 1 3 4 5 3 1 2 5 7 8 node 4 2 5 5 2 3 4 6 6 5 7 3 8 8 3 7 Jan 29, 2018 CSCI211 - Sprenkle 6 3

  4. 1/29/18 Paths and Connec&vity • 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 consecu&ve pair v i , v i+1 is joined by an edge in E • Def. A path is simple if all nodes are dis%nct • 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 29, 2018 CSCI211 - Sprenkle 7 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 dis&nct cycle C = 1-2-4-5-3-1 Jan 29, 2018 CSCI211 - Sprenkle 8 4

  5. 1/29/18 TREES Jan 29, 2018 CSCI211 - Sprenkle 9 Trees • Def. An undirected graph is a tree if it is connected and does not contain a cycle • Simplest connected graph Ø Dele&ng any edge from a tree will disconnect it Jan 29, 2018 CSCI211 - Sprenkle 10 5

  6. 1/29/18 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 29, 2018 CSCI211 - Sprenkle 11 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 29, 2018 CSCI211 - Sprenkle 12 6

  7. 1/29/18 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 29, 2018 CSCI211 - Sprenkle 13 Phylogeny Trees • Describe evolu&onary 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 29, 2018 CSCI211 - Sprenkle 14 7

  8. 1/29/18 GRAPH CONNECTIVITY & TRAVERSAL Jan 29, 2018 CSCI211 - Sprenkle 15 Connec&vity • s-t connec9vity 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 ? • Applica&ons Ø Facebook Ø Maze traversal Ø Kevin Bacon number Ø Spidering the web Ø Fewest number of hops in a communica&on network Jan 29, 2018 CSCI211 - Sprenkle 16 8

  9. 1/29/18 Applica&on: Connected Component • Find all nodes reachable from s • Connected component containing node 1 is { 1, 2, 3, 4, 5, 6, 7, 8 } Jan 29, 2018 CSCI211 - Sprenkle 17 Applica&on: Flood Fill • Given lime green pixel in an image, change color of en&re 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 29, 2018 CSCI211 - Sprenkle 18 9

  10. 1/29/18 Applica&on: Flood Fill • Given lime green pixel in an image, change color of en&re 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 29, 2018 CSCI211 - Sprenkle 19 My Facebook Friends Created with Social Graph UDel HS Family Gburg Duke Extreme Blue Jan 29, 2018 CSCI211 - Sprenkle 20 10

  11. 1/29/18 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 to R add v s u v • R will be the connected component containing s • Algorithm is underspecified In what order should we consider the edges? Jan 29, 2018 CSCI211 - Sprenkle 21 Possible Orders • Breadth-first • Depth-first Jan 29, 2018 CSCI211 - Sprenkle 22 11

  12. 1/29/18 Breadth-First Search • Intui9on . Explore outward from s in all possible direc&ons (edges), adding nodes one "layer" at a &me 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 29, 2018 CSCI211 - Sprenkle 23 Run BFS on This Graph s = 1 Jan 29, 2018 CSCI211 - Sprenkle 24 12

  13. 1/29/18 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 29, 2018 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. s L 1 L 2 L n-1 • What does this theorem mean? • Can we determine the distance between s and t? Jan 29, 2018 CSCI211 - Sprenkle 26 13

  14. 1/29/18 Looking Ahead • Monday, 11:59 p.m.: journal - Chapter 2.4, 2.5, 3.1 • Friday: Problem Set 3 due Jan 29, 2018 CSCI211 - Sprenkle 27 14

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend