Lecture 05 - 17 September 2013 The first in-class exam is this coming Tuesday September 17. --------------------------------------------------------------------- discussion of assignment 1. Problem 7: First possible solution: assumes that length(z) remains same as length(x): n = log(x) m = log(y) T(m) = T(m-1) + O(n^2) = O(m n^2) Let n = max( log(x), log(y) ) T(n) = O(n^3) Second solution: takes into account worst case where each multiplication doubles length of z: n = log(x) m = log(y) T(m) = T(m-1) + O((2^(m-1)n)^2) = T(m-1) + O(2^m n^2) <= O(m 2^m n^2) Let n = max( log(x), log(y) ) T(n) = O(n^3 2^n) Problem 8: - eliminate check "y is even" - it always is. - simple iterative algorithm: function exp(x,y) while y & 1 == 0: y = y >> 1 x = x * x end return x Big O complexity, however, is unchanged. --------------------------------------------------------------------- Graph algorithms - [Chapters 3 and 4] Draw graph What are the two ways to represent a graph? - adjacency matrix - Size? O(|V|^2) - adjacency list - Size? O(|E|) So, when is one better? - dense: matrix - sparse: list Tasks: connected components; finding cycles in directed graphs; topological sort; strongly connected components Tool: depth first search DFS(G) for all v: visited(v) = false for all v: if not visited(v) then explore(v) end explore(G,v) visited(v) = true previsit(v) for each edge (v, u) in E if not visited(u) then explore(G,u) postvisit(v) end --------------------------------------------------------------------- Connected components: integer array ccnum[v] How modify? DFS(G) for all v in V: visited(v) = false cc = 0 for all v in V: cc = cc + 1 if not visited(v) then explore(G,v) end function previsit(v) ccnum[v] = cc end --------------------------------------------------------------------- Cycle finding in directed graphs: Claim: Followed edges in a depth first search of a directed graph form a FOREST (set of trees) Draw picture of directed graph an a DFS Pre-number function previsit(v) pre[v] = clock clock = clock + 1 end Post-number function postvisit(v) pre[v] = clock clock = clock + 1 end note: [pre[v], post[v]] == time v was on stack Claim: u is an ancester of v in the search tree (u is discovered first, and v is discovered during explore(u)) iff pre(u) < pre(v) < post(v) < post(u) Def: Given G and a DFS tree, a back edge goes from a vertice to an ancester Claim: G has a cycle IFF it has a back edge. Modify explore to find a cycle: explore(G,v) visited(v) = true previsit(v) for each edge (v, u) in E if not visited(u) then explore(G,u) postvisit(v) for each edge (v, u) in E if pre(u) < pre(v) < post(v) < post(u) then return "found cycle" end Suppose pre and post are initialized to 0, and the clock starts at 1. Then we can tell if a node is on the stack if pre(v) > 0 && post(v) == 0 Can we modify explore to find cycles immediately? explore(G,v) visited(v) = true previsit(v) for each edge (v, u) in E if visited(u) then ?? if post(u) == 0 then return "found cycle" // no need to test for pre(u)