1 / 61

Chapter 3

Chapter 3. Graphs. Undirected Graphs. Undirected graph . G = (V, E) V = nodes. E = edges between pairs of nodes. Captures pairwise relationship between objects. Graph size parameters: n = |V|, m = |E|. Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 }

arwen
Download Presentation

Chapter 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3 Graphs

  2. Undirected Graphs • Undirected graph. G = (V, E) • V = nodes. • E = edges between pairs of nodes. • Captures pairwise relationship between objects. • Graph size parameters: n = |V|, m = |E|.

  3. Undirected Graphs • V = { 1, 2, 3, 4, 5, 6, 7, 8 } • E = { (1,2), (1,3), (2,3), (2,4), (2,5), (3,5), (3,7), (3,8), (4,5), (5,6) } • n = 8 • m = 11

  4. Some Graph Applications

  5. World Wide Web • Web graph. • Node: web page. • Edge: hyperlink from one page to another.

  6. Graph Representation: Adjacency Matrix • Adjacency matrix. n-by-n matrix with Auv = 1 if (u,v) is an edge. • Two representations of each edge. • Space proportional to n2. • Checking if (u, v) is an edge takes Θ(1) time. • Identifying all edges takes Θ(n2) time.

  7. Graph Representation: Adjacency Matrix

  8. Graph Representation: Adjacency List • Adjacency list. Node indexed array of lists. • Two representations of each edge. • Space proportional to m+n. • Checking if (u,v) is an edge takes O(deg(u)) time. • Identifying all edges takes Θ(m + n) time.

  9. Graph Representation: Adjacency List

  10. Paths and Connectivity • Def. A path in an undirected graph G = (V, E) is a sequence P of nodes v1, v2, …, vk-1, vk with the property that each consecutive pair vi, vi+1 is joined by an edge in E. • Def. A path is simple if all nodes are distinct.

  11. Paths and Connectivity • Def. An undirected graph is connected if for every pair of nodes u and v, there is a path between u and v.

  12. Cycles • Def. A cycle is a path v1, v2, …, vk-1, vk in which v1 = vk, k > 2, and the first k-1 nodes are all distinct. • cycle C = 1-2-4-5-3-1

  13. Trees • Def. An undirected graph is a tree if it is connected and does not contain a cycle.

  14. Trees • Theorem. Let G be an undirected graph. The following statements are equivalent. • G is a tree: it is connected and does not contain a cycle. • Any two vertices in G are connected by a unique simple path. • G is connected, but if any edge is removed from E, the resulting graph is disconnected. • G is connected, and |E| +1= |V|. • G is acyclic, and |E| +1= |V|. • G is acyclic, but if any edge is added to E, the resulting graph contains a cycle.

  15. Trees • Proof (1)=>(2): • Tree is connected => any two vertices in G are connected by at least one simple path. • Let u and v be vertices that are connected by two distinct simple paths p1 and p2 => G has a cycle!

  16. Trees • Proof (2)=>(3): • If any two vertices in G are connected by a unique simple path => G is connected • Let (u, v) be any edge in E. This edge is a path from u to v. • If we remove (u,v) from G, there is no path from u to v => G gets disconnected.

  17. Trees • Proof (3)=>(4): By induction. • Lets assume that |V|=|E| +1. is true for any tree of k edges • Lets consider a tree G=(V,E) with |E| =k+1 • Let (u, v) be any edge in E. If we remove (u,v) from G => G gets disconnected. • Hence, we have G1=(V1,E1) and G2=(V2,E2) with 0≤ |E1|,|E2| ≤k • By induction |V| = |V1| + |V2| =(|E1| +1) + (|E2| +1) =( |E1| + |E2| +1) +1 = |E|+1

  18. Trees • Proof (4)=>(5): • Suppose G has a cycle containing k vertices v1, v2,…, vk. • Let Gk = (Vk, Ek) be such a graph. Note that |Vk|= |Ek|=k. • If k<|V|, since G is connected there must be a vertex vk+1 in V-Vk that is adjacent to some vertex vi in Vk. • Define Gk+1 = (Vk+1, Ek+1) as Vk+1= VkU{vk+1} and Ek+1 = EkU{(vi,vk+1)}. Note that |Vk+1|= |Ek+1|=k+1.

  19. Trees • If k+1<|V| we can define Gk+2,…, Gn=(Vn, En) where n=|V|, Vn = V, and |En|=|Vn|=|V|. • Gn is a subgraph of G => En E => |E|≥|V| which contradicts |E| = |V|-1!

  20. Trees • Proof (5)=>(6): • Let k be the number of connected components of G. • Each connected component is a tree by definition. • Since (1)=>(5), the sum of all edges in all connected components of G is |V|-k => k=1. • Since (1)=>(2), any two vertices in G are connected by a simple path => adding any edge to G creates a cycle.

  21. Trees • Proof (6)=>(1): • Let u and v be arbitrary vertices in G. • If u and v are not already adjacent, adding and edge (u,v) creates a cycle in which all edges but (u,v) belong to G. • Thus, there is a path from u to v and hence G is connected.

  22. Rooted Trees • Rooted tree. Given a tree T, choose a root node r and orient each edge away from r. • Importance. Models hierarchical structure.

  23. Rooted Trees • GUI containment hierarchy. Describe organization of GUI widgets.

  24. Connectivity • s-t connectivity problem. Given two nodes s and t, is there a path between s and t? • s-t shortest path problem. Given two nodes s and t, what is the length of the shortest path between s and t? • Applications. • Maze traversal. • Erdős–Bacon number (mine is 3 ). • Fewest number of hops in a communication network.

  25. Breadth First Search • BFS intuition. Explore outward from s in all possible directions, adding nodes one "layer" at a time.

  26. Breadth First Search • BFS algorithm. • L0 = { s }. • L1 = all neighbors of L0. • L2 = all nodes that do not belong to L0 or L1, and that have an edge to a node in L1. • Li+1 = all nodes that do not belong to an earlier layer, and that have an edge to a node in Li. • Claim. For each j, Lj consists of all nodes at distance exactly j from s. • Claim. There is a path from s to t iff t appears in some layer.

  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.

  28. Breadth First Search • Proof. By contradiction: • Suppose i<j-1 • Consider the point in the BFS alg. when the edges incident to x were examined. • x belongs to layer Li the only nodes discovered from x belong to layers Li+1 and earlier. • If y is a neighbor of x, it should have been discovered at this point at the least. • Hence it should belong to layer Li+1 or earlier.

  29. Breadth First Search

  30. Breadth First Search • Theorem. The above implementation of BFS runs in O(m + n) time if the graph is given by its adjacency representation. • Pf. Easy to prove O(n2) running time: • at most n lists L[i] • each node occurs on at most one list; for loop runs ≤ n times • when we consider node u, there are ≤ n incident edges (u, v), and we spend O(1) processing each edge

  31. Breadth First Search • Actually runs in O(m + n) time: • when we consider node u, there are deg(u) incident edges (u, v) • total time processing edges is • each edge (u, v) is counted exactly twice in sum: once in deg(u) and once in deg(v)

  32. Connected Component • Connected component. Find all nodes reachable from s. • Connected component containing node 1 = { 1, 2, 3, 4, 5, 6, 7, 8 }.

  33. Flood Fill • 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.

  34. Connected Component

  35. Connected Component • Theorem. Upon termination, R is the connected component containing s. • Pf. By contradiction • For any node v in R, there is a path from any other node in R. • Consider w not in R, and suppose there is a s-w path P in G. • There must be a first node v in P not in R • There is a node u immediately preceding v on P => (u,v) is in E • u is in R, which contradicts the stopping condition! • BFS = explore in order of distance from s. • DFS = explore in a different way.

  36. Depth-First Search

  37. Depth-First Search • Property. For a given recursive call to DFS(u), all nodes that are marked “Explored” between the invocation and the end of this recursive call are descendants of u in T. • Theorem. Let T be a DFS-tree, let x and y be nodes in T, and let (x,y) be an edge of G that is not an edge of T. Then one of x or y is an ancestor of the other.

  38. Depth-First Search • Pf. • Suppose that x is reached first by DFS. • When (x,y) is examined by DFS, it is not added to T because y is marked “Explored”. • Since y was not marked “Explored” when DFS(x) was first invoked => y was discovered between the invocation of DFS(x) and the end of recursive call DFS(x) => • y is a descendant of x.

  39. Bipartite Graphs • Def. An undirected graph G = (V, E) is bipartite if the nodes can be colored red or blue such that every edge has one red and one blue end. • Applications. • Stable marriage: men = red, women = blue. • Scheduling: machines = red, jobs = blue.

  40. Testing Bipartiteness • Testing bipartiteness. Given a graph G, is it bipartite? • Many graph problems become: • easier if the underlying graph is bipartite (matching) • tractable if the underlying graph is bipartite (independent set) • The size of the maximum independent set plus the size of the maximum matching is equal to the number of vertices -- König's theorem. • Before attempting to design an algorithm, we need to understand the structure of bipartite graphs.

  41. Testing Bipartiteness

  42. An Obstruction to Bipartiteness • Lemma. If a graph G is bipartite, it cannot contain an odd length cycle. • Pf. Not possible to 2-color the odd cycle, let alone G.

  43. Bipartite Graphs • Lemma. Let G be a connected graph, and let L0, …, Lk be the layers produced by BFS starting at node s. Exactly one of the following holds. • (i) No edge of G joins two nodes of the same layer, and G is bipartite. • (ii) An edge of G joins two nodes of the same layer, and G contains an odd-length cycle (and hence is not bipartite).

  44. Bipartite Graphs • Pf. (i) • Suppose no edge joins two nodes in the same layer. • This implies all edges join nodes on different levels. • Bipartition: red = nodes on odd levels, blue = nodes on even levels.

  45. Bipartite Graphs • Pf. (ii) • Suppose (x, y) is an edge with x, y in same level Lj. • Let z = lca(x, y) = lowest common ancestor. • Let Li be level containing z. • Consider cycle that takes edge from x to y, then path from y to z, then path from z to x. • Its length is 1 + (j-i) + (j-i), which is odd.

  46. Directed Graphs • Directed graph. G = (V, E) • Edge (u, v) goes from node u to node v. • Ex. Web graph - hyperlink points from one web page to another. • Directedness of graph is crucial. • Modern web search engines exploit hyperlink structure to rank web pages by importance.

  47. Graph Search • Directed reachability. Given a node s, find all nodes reachable from s. • Directed s-t shortest path problem. Given two nodes s and t, what is the length of the shortest path between s and t? • Graph search. BFS extends naturally to directed graphs. • Web crawler. Start from web page s. Find all web pages linked from s, either directly or indirectly.

  48. Strong Connectivity • Def. Nodes u and v are mutually reachable if there is a path from u to v and also a path from v to u. • Def. A graph is strongly connected if every pair of nodes is mutually reachable.

  49. Strong Connectivity • Lemma. Let s be any node. G is strongly connected iff every node is reachable from s, and s is reachable from every node. • Pf. => Follows from definition. • Pf. <= Path from u to v: concatenate u-s path with s-v path. Path from v to u: concatenate v-s path with s-u path. • ok if paths overlap

  50. Strong Connectivity: Algorithm • Theorem. Can determine if G is strongly connected in O(m + n) time. • Pf. • Pick any node s. • Run BFS from s in G. • Run BFS from s in Grev. • Grev: reverse orientation of every edge in G • Return true iff all nodes reached in both BFS executions. • Correctness follows immediately from previous lemma.

More Related