610 likes | 711 Views
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 }
E N D
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 } • 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
World Wide Web • Web graph. • Node: web page. • Edge: hyperlink from one page to another.
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.
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.
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.
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.
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
Trees • Def. An undirected graph is a tree if it is connected and does not contain a cycle.
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.
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!
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.
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
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.
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!
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.
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.
Rooted Trees • Rooted tree. Given a tree T, choose a root node r and orient each edge away from r. • Importance. Models hierarchical structure.
Rooted Trees • GUI containment hierarchy. Describe organization of GUI widgets.
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.
Breadth First Search • BFS intuition. Explore outward from s in all possible directions, adding nodes one "layer" at a time.
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.
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.
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.
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
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)
Connected Component • Connected component. Find all nodes reachable from s. • Connected component containing node 1 = { 1, 2, 3, 4, 5, 6, 7, 8 }.
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.
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.
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.
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.
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.
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.
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.
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).
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.
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.
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.
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.
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.
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
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.