Prof. Swarat Chaudhuri COMP 482: Design and Analysis of Algorithms Spring 2013 Lecture 4
Q3: Analyzing an algorithm You have an array A with integer entries A[1],…, A[n] Output a 2-D array B such that B[i,j] contains the sum A[i] + A[i+1] + … + A[j] Here is an algorithm: For i=1, 2, …, n For j = i+1, 2, …, n { Add up entries A[i] through A[j] Store result in B[i,j] } Obtain an upper bound and a lower bound for the algorithm. 2
The lower bound is the more interesting one Consider the times during the execution of the algorithm when i ≤ n/4 and j ≥ 3n/4. In these cases, j − i + 1 ≥ 3n/4 − n/4 + 1 > n/2. Therefore, adding up the array entries A[i] through A[j] would require at least n/2 operations, since there are more then n/2 terms to add up. How many times during the execution of the given algorithm do we encounter such cases? There are (n/4) 2 pairs (i, j) with i ≤ n/4 and j ≥ 3n/4. the given algorithm enumerates over all of them, and as shown above, it must perform at least n/2 operations for each such pair. Therefore, the algorithm must perform at least n/2.(n/4) 2 = n 3 /32 operations. This is Ω(n 3 ), as desired. 3
Graphs 3.1 Basic Definitions and Applications
5 Undirected Graphs Undirected graph. G = (V, E) n V = nodes. n E = edges between pairs of nodes. n Captures pairwise relationship between objects. n Graph size parameters: n = |V|, m = |E|. 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
6 Some Graph Applications transportation Graph street intersections NodesEdges highways communicationcomputersfiber optic cables World Wide Webweb pageshyperlinks socialpeoplerelationships food webspeciespredator-prey software systemsfunctionsfunction calls schedulingtasksprecedence constraints circuitsgateswires
7 World Wide Web Web graph. n Node: web page. n Edge: hyperlink from one page to another. cnn.com cnnsi.com novell.comnetscape.com timewarner.com hbo.com sorpranos.com
Terrorist Network Social network graph. n Node: people. n Edge: relationship between two people. Reference: Valdis Krebs,
9 Ecological Food Web Food web graph. n Node = species. n Edge = from prey to predator. Reference:
10 Graph Representation: Adjacency Matrix Adjacency matrix. n-by-n matrix with A uv = 1 if (u, v) is an edge. n Two representations of each edge. n Space proportional to n 2. n Checking if (u, v) is an edge takes (1) time. n Identifying all edges takes (n 2 ) time
11 Graph Representation: Adjacency List Adjacency list. Node indexed array of lists. n Two representations of each edge. n Space proportional to m + n. n Checking if (u, v) is an edge takes O(deg(u)) time. n Identifying all edges takes (m + n) time degree = number of neighbors of u 37
12 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 with the property that 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 for every pair of nodes u and v, there is a path between u and v.
13 Cycles Def. A cycle is a path v 1, v 2, …, v k-1, v k in which v 1 = v k, k > 2, and the first k-1 nodes are all distinct. cycle C =
14 Trees Def. An undirected graph is a tree if it is connected and does not contain a cycle. Theorem. Let G be an undirected graph on n nodes. Any two of the following statements imply the third. n G is connected. n G does not contain a cycle. n G has n-1 edges.
15 Rooted Trees Rooted tree. Given a tree T, choose a root node r and orient each edge away from r. Importance. Models hierarchical structure. a tree the same tree, rooted at 1 v parent of v child of v root r
16 Phylogeny Trees Phylogeny trees. Describe evolutionary history of species.
17 GUI Containment Hierarchy Reference: GUI containment hierarchy. Describe organization of GUI widgets.
3.2 Graph Traversal
19 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. n Facebook. n Maze traversal. n Erdos number. n Kevin Bacon number. n Fewest number of hops in a communication network.
20 Breadth First Search BFS intuition. Explore outward from s in all possible directions, adding nodes one "layer" at a time. Effect: find “shallow” paths to nodes. BFS algorithm. n L 0 = { s }. n L 1 = all neighbors of L 0. n L 2 = all nodes that do not belong to L 0 or L 1, and that have an edge to a node in L 1. n L i+1 = all nodes that do not belong to an earlier layer, and that have an edge to a node in L i. 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 L1L1 L2L2 L n-1
Implementing BFS Q: What’s a good way to implement the above algorithm? A: Use a queue for the “frontier” 21
22 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. L0L0 L1L1 L2L2 L3L3
23 Breadth First Search: Analysis Theorem. The above implementation of BFS runs in O(m + n) time if the graph is given by its adjacency list representation. Pf. n Easy to prove O(n 2 ) 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 n 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 u V deg(u) = 2m ▪ each edge (u, v) is counted exactly twice in sum: once in deg(u) and once in deg(v)
24 Connected Component Connected component. Find all nodes reachable from s. Connected component containing node 1 = { 1, 2, 3, 4, 5, 6, 7, 8 }.
Q1: Finding connected components Give an algorithm to find the set of all connected components of an undirected graph. 25
26 Connected Component Connected component. Find all nodes reachable from s. Theorem. Upon termination, R is the connected component containing s. n BFS = explore in order of distance from s. s uv R it's safe to add v
27 Q2: Flood Fill Flood fill. Given lime green pixel in an image, change color of entire blob of neighboring lime pixels to blue. recolor lime green blob to blue
28 Flood Fill Flood fill. Given lime green pixel in an image, change color of entire blob of neighboring lime pixels to blue. recolor lime green blob to blue
29 Flood Fill Flood fill. Given lime green pixel in an image, change color of entire blob of neighboring lime pixels to blue. n Node: pixel. n Edge: two neighboring lime pixels. n Blob: connected component of lime pixels. recolor lime green blob to blue
Depth-first search Use recursion DFS intuition. Explore outward from s along one path as far as possible, and backtrack when you cannot progress. Effect: find faraway nodes. 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 Recursively call DFS(v) 30
Depth-first search Property. For a given recursive call DFS(u), all nodes marked “Explored” between the beginning and end of this recursive call are descendants of u in T. Theorem. Let T be a depth-first search 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. 31
Q3: BFS and DFS trees We have a connected graph G = (V, E) and a specific vertex u. Suppose we compute a DFS tree rooted at u, and obtain a tree T that includes all nodes of G. Suppose we then compute a BFS tree rooted at u, and obtain the same tree T. Prove that G = T. 32
Answer Suppose G has an edge e = {a, b} that does not belong to T. As T is a DFS tree, one of the two ends must be an ancestor of the other—say a is an ancestor of b. (*) Since T is a BFS tree, the distance of the two nodes from u in T can differ at most by one. But if a is an ancestor of b, and (*) holds, then a must be the direct parent of b. This means that {a, b} is an edge in T. Contradiction. 33
Q4: Finding a cycle Given a graph G, determine if it has a cycle. If so, the algorithm should output this cycle. Answer: Assume that G is connected; otherwise work on the connected components. Run BFS from an arbitrary node s, and obtain a BFS tree T. If every edge of G appears in the tree, then G = T and there is no cycle. Otherwise, there is an edge e = (v, w) that is in G but not in T. Consider the least common ancestor u of v and w in T. We get a cycle from edge e and paths u-v and u-w in T. 34