Alex1 group2

Download Alex1 group2

Post on 12-Jul-2015

314 views

Category:

Technology

0 download

Embed Size (px)

TRANSCRIPT

<ul><li><p>Advanced Algorithms </p><p>Exercise 1 </p><p>Q1, Q8 </p><p>Group 2 </p><p>BINAYA KAR </p><p> 2013/10/20 1 Q1 Q8 </p></li><li><p>Q1 </p><p> Recall the recursive program (discussed in the class) that computes the n-th Fibonacci </p><p>number. Compute the number of additions </p><p>used by the program and design a more </p><p>efficient program. </p><p>f(n) { </p><p> if(n </p></li><li><p>Q1- the number of additions used </p><p> Definition : F1 = F2 = 1, Fn = Fn-1 + Fn-2 n 3 </p><p> Definition : Gn : the number of addition used by above </p><p>program for f(n). </p><p>f(n) { </p><p> if(n </p></li><li><p>Q1- the number of additions used </p><p> G1 = G2 = 0, Gn = Gn-1 + Gn-2 + 1, n 3 </p><p> Observe the program, f(n) calls f(n-1) and f(n-2) express that it used </p><p>Gn-1 + Gn-2 times of addition, and finally used </p><p>one addition to calculate result. </p><p>f(n) { </p><p> if(n </p></li><li><p>Q1- the number of additions used </p><p> G1 = G2 = 0, Gn = Gn-1 + Gn-2 + 1, n 3 </p><p> Goal : n 1, Gn = Fn 1 </p><p> Basis : G1 = F1 1 = 0, G2 = F2 1 = 0 </p><p> Assume that Gn = Fn 1 Gn = Gn-1 + Gn-2 + 1 = (Fn-1 1)+(Fn-2 1)+1 = (Fn-1 + Fn-2) 1 </p><p> = Fn 1 </p><p> 2013/10/20 5 Q1 Q8 </p></li><li><p>Q1-more efficient program(1) </p><p> This way depends on what MAX_QUERY you known. </p><p> Time Complexity for building Table : O(n) </p><p> Space Complexity : O(n) </p><p> Query : O(1) 2013/10/20 6 Q1 Q8 </p><p>Integer Array : F[] </p><p>F[1] = F[2] = 0 </p><p>For i = 0 to MAX_QUERY </p><p> F[i] = F[i-1] + F[i-2] </p></li><li><p>Q1-more efficient program(2) </p><p> Companion Matrix and Divide and Conquer. Time complexity : O(log n) </p><p>Space Complexity : O(1) </p><p>2013/10/20 7 Q1 Q8 </p><p>1-nn</p><p>n1n</p><p>FF</p><p>FF A</p><p>01</p><p>11</p><p>...AAA A b b b n 421</p><p>n1n</p><p>1nn</p><p>n1n</p><p>1-nnn1n</p><p>1-nn</p><p>n1n</p><p>FF</p><p>FF</p><p>FF</p><p>FFFF</p><p>FF</p><p>FF </p><p>2</p><p>01</p><p>11</p></li><li><p>Q8 </p><p> Let G=(V, E) be a directed graph. </p><p> Design an efficient algorithm to label the vertices of the graph with distinct labels from 1 </p><p>to |V| such that the label of each vertex v is </p><p>greater than the label of at least one of vs </p><p>predecessors, or determine that no such </p><p>labeling is possible. (A predecessor of v is a </p><p>vertex w such that wv E.). </p><p>2013/10/20 8 Q1 Q8 </p></li><li><p>Q8 </p><p> vertex v vertex w, wv E and v.label &gt; w.label </p><p>2013/10/20 9 Q1 Q8 </p><p>v </p><p>w </p></li><li><p>Q8 </p><p> Lemma 1. For the smallest label of vertex v, its indegree equals zero. (indeg(v) = 0) </p><p>2013/10/20 10 Q1 Q8 </p><p>This node has </p><p>the smallest label. </p></li><li><p>Q8 </p><p> Lemma 2. The neighbor of the smallest label vertex mark 2-nd smallest label. </p><p>2013/10/20 11 Q1 Q8 </p><p>the smallest label. </p><p>2-nd smallest label. </p><p>2-nd smallest label. </p></li><li><p>Q8 efficient algorithm </p><p>2013/10/20 12 Q1 Q8 </p><p>visited[]:initialize false. </p><p>indeg[]:indegree of vertex </p><p>label[]:label of vertex </p><p>foundIdx = 1 </p><p>for(each indeg[v] = 0) </p><p> dfs(v); </p><p>dfs(v) { </p><p> visited[v] = true; </p><p> label[v] = foundIdx++; </p><p> for(neighbor u, vu in E) { </p><p> if(!visited[u]) </p><p> dfs(u); </p><p> } </p><p>} </p></li><li><p>Q8 efficient algorithm </p><p> Finally, check whether all vertice have marked. return possible or impossible. </p><p> Time complexity : O(V+E) </p><p> Space complexity : O(V) </p><p>2013/10/20 13 Q1 Q8 </p></li><li><p> ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ </p><p>2013/10/20 14 Q1 Q8 </p></li><li><p> ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ </p><p>2013/10/20 15 Q1 Q8 </p></li><li><p> ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ </p><p>2013/10/20 16 Q1 Q8 </p></li><li><p>Q2 </p><p> Determine the space complexity of the quicksort algorithm. </p><p>2013/10/20 17 Q1 Q8 </p></li><li><p>Q2 Answer. </p><p> Iterative Quicksort still used stack structure. </p><p> Don't care about that memory of input used. </p><p> Quicksort used 3 variable in split function, O(1) space complexity. </p><p> Discuss about stack memory each stack frame use O(1) space. </p><p> Maximum recursion depth log(N) </p><p> Space complexity : O(logN) </p><p> 2013/10/20 18 Q1 Q8 </p></li><li><p>Q3 </p><p> Derive a closed formula of T(n) that satisfies the following recurrence relation: </p><p>T(n) = T(n/2) + 1, T(1) = 0. </p><p>2013/10/20 19 Q1 Q8 </p></li><li><p>Q3 Answer. </p><p> T(n) = T(n/2) + 1, T(1) = 0. </p><p> Let n = 2k, T(k) = T(k-1) + 1, T(0) = 0 T(k) = k T(n) = log2(n) </p><p>2013/10/20 20 Q1 Q8 </p></li><li><p>Q4 </p><p> Given n 1 numbers x1, x2, , xn, show that the function f(x) = sigma(|x-xi|) takes its </p><p>minimum value at the median of these n </p><p>number. </p><p>2013/10/20 21 Q1 Q8 </p></li><li><p>Q4 Proof.(1.1) </p><p> M is the median number. </p><p> (1) if n is even, A M and t = n/2 </p><p> x1 x2 ... xs A xs+1 ... xt M xt+1 ... xn </p><p> f(A) </p><p>2013/10/20 22 Q1 Q8 </p><p>t</p><p>s</p><p>n</p><p>t</p><p>ii</p><p>t t</p><p>s</p><p>ii</p><p>n</p><p>s</p><p>i</p><p>s</p><p>i</p><p>AMMxAx)xA(xMMA</p><p>AxxA</p><p>1 11 1</p><p>11</p><p>)()()(</p><p>)()(</p></li><li><p>Q4 Proof. (1.1) </p><p>______ ______ ______ __________ </p><p> 0 ? 0 = 0 (t = n/2) </p><p>when A = M, f(A) has minimum value. </p><p>2013/10/20 23 Q1 Q8 </p><p>)()2()()(2)(</p><p>)()()()(2)()(</p><p>)()()(</p><p>111</p><p>111</p><p>1 11 1</p><p>AMtnMxAxxM</p><p>AMtnMxAxxMMAt</p><p>AMMxAx)xA(xMMA</p><p>n</p><p>t</p><p>i</p><p>t</p><p>s</p><p>i</p><p>t</p><p>i</p><p>n</p><p>t</p><p>i</p><p>t</p><p>s</p><p>i</p><p>t</p><p>i</p><p>t</p><p>s</p><p>n</p><p>t</p><p>ii</p><p>t t</p><p>s</p><p>ii</p></li><li><p>Q4 Proof.(1.2) </p><p> M is the median number. </p><p> (1) if n is even, A M and s = n/2 </p><p> x1 x2 ... xs M xs+1 ... xt A xt+1 ... xn </p><p> f(A) </p><p>2013/10/20 24 Q1 Q8 </p><p>t</p><p>s</p><p>n</p><p>t</p><p>ii</p><p>s t</p><p>s</p><p>ii</p><p>n</p><p>t</p><p>i</p><p>t</p><p>i</p><p>AMMxAx)xA(xMMA</p><p>AxxA</p><p>1 11 1</p><p>11</p><p>)()()(</p><p>)()(</p></li><li><p>Q4 Proof. (1.2) </p><p>______ ______ ___________ </p><p> 0 0 ? </p><p>when A = M, f(A) has minimum value. </p><p>2013/10/20 25 Q1 Q8 </p><p>)()()()(</p><p>)()()()()(</p><p>)()()(</p><p>11</p><p>11</p><p>1 11 1</p><p>AMstnMxxM</p><p>AMtnMxxMMAs</p><p>AMMxAx)xA(xMMA</p><p>n</p><p>t</p><p>i</p><p>t</p><p>i</p><p>n</p><p>t</p><p>i</p><p>s</p><p>i</p><p>t</p><p>s</p><p>n</p><p>t</p><p>ii</p><p>s t</p><p>s</p><p>ii</p></li><li><p>Q4 Proof.(2.1) </p><p> (2) if n is odd, same as proof(1.1)&amp;proof(1.2) </p><p> f(x) = sigma(|x-xi|) takes its minimum value at the median of these n number. </p><p>2013/10/20 26 Q1 Q8 </p></li><li><p>Q5 </p><p> Given a positive integer n, find a way to partition n into one or more positive integers </p><p>j1, j2, , jk (i.e. j1 + j2 + + jk = n) such that </p><p>the product of these k integers is maximized. </p><p>2013/10/20 27 Q1 Q8 </p></li><li><p>Q5 Observe </p><p> f(n): maximum product of these k integers. </p><p> Observe these, f(2) = 2, </p><p>f(3) = 3, </p><p>f(4) = 4 = 2*2, </p><p>f(5) = 6 = 2*3, </p><p>f(6) = 9 = 3*3, </p><p>2013/10/20 28 Q1 Q8 </p></li><li><p>Q5 Observe </p><p> Get recusion function : f(2) = 2, f(3) = 3, </p><p> f(n) = max( f(a) * f(b) ) n 4 where a + b = n, a&amp;b are positive integer. </p><p> Draw call tree jk {2, 3} </p><p> 2013/10/20 29 Q1 Q8 </p></li><li><p>Q5 Observe </p><p> Let x is numbers of 2, y is numbers of 3 x 0, y 0, 2x + 3y = n, </p><p> Goal : 2x 3y has maximize value. f(n) = 2x 3y </p><p>log(f(n)) = x log2 + y log3 </p><p> = x log2 [(n - 2x)/3] log3 </p><p> = x(log2 - log(3(2/3))) + n/3 log3 </p><p> Because (log2 - log(3(2/3))) &lt; 0, get x 0 </p><p> 2013/10/20 30 Q1 Q8 </p></li><li><p>Q5 Observe </p><p> 3 </p><p>2013/10/20 31 Q1 Q8 </p></li><li><p>Q6 </p><p> Determine the correct closed formula for An (see slide 5 in unit 2 ) and prove its </p><p>correctness. </p><p> Problem: Maximal number of regions obtained by joining n points around a circle by straight </p><p>lines. </p><p>2013/10/20 32 Q1 Q8 </p></li><li><p>Q6 Observe </p><p> f(n) = f(n-1) + C(n-1, 3) + n-1 </p><p> f(n) = 1 + C(n,2) + C(n,4) </p><p> f(2) = 2, f(3) = 4, f(4) = 8, f(5) = 16, ... </p><p>2013/10/20 33 Q1 Q8 </p></li><li><p>Q6 Proof </p><p> Eulers Formula : V E + F = 2 </p><p> V = n + C(n, 4) </p><p> E = 4 * C(n,4)/2 + C(n, 2) = 2 * C(n, 4) + C(n, 2) </p><p> F = 2 + E V = 2 + C(n, 2) + C(n, 4) - n </p><p> 2013/10/20 34 Q1 Q8 </p></li><li><p>Q6 Proof </p><p> Eulers Formula : V E + F = 2 </p><p> V = n + C(n, 4) </p><p> E = 4 * C(n,4)/2 + C(n, 2) = 2 * C(n, 4) + C(n, 2) </p><p> F = 2 + E V = 2 + C(n, 2) + C(n, 4) - n </p><p> f(n) = F 1 + n = 1 + C(n,2) + C(n,4) </p><p>2013/10/20 35 Q1 Q8 </p></li><li><p>Q7 </p><p> Let d1, d2, , dn, n 2, be positive integers. Prove that if d1 + d2 + ... + dn= 2n-2, then there </p><p>exists a tree with n vertices of degrees exactly </p><p>d1, d2, , dn. Based on your proof, design an </p><p>efficient algorithm to construct such a tree. </p><p>2013/10/20 36 Q1 Q8 </p></li><li><p>Q7 Proof Goal : </p><p> if d1 + d2 + ... + dn= 2n-2, there exists a tree. </p><p> Basis : n = 1 and n = 2 is true. </p><p> Assume that d1, d2, , dn, dn+1 of n+1 vertices given, </p><p>with d1 + d2 + ... + dn+1 = 2(n+1) - 2 </p><p> exists a degree &gt; 1 (pigeonhole principle) </p><p> Let dn+1 &gt; 1, d1, d2, , dn + dn+1 - 2 satisfy the conditions. </p><p>2013/10/20 37 Q1 Q8 </p></li><li><p>Q7 Proof </p><p> For |V| = n and d1, d2, , dn + dn+1 2, </p><p>Add a (n+1)-th vertex, remove dn+1 1 from </p><p>n-th vertex, then add these in neighbors of </p><p>(n+1)-th vertex. Finally, add an edge between </p><p>n-th and (n+1)-th vertex. </p><p>2013/10/20 38 Q1 Q8 </p><p>s </p></li><li><p>Q7 efficient algorithm </p><p> O(|V|) </p><p>2013/10/20 39 Q1 Q8 </p><p>Build Queue Q0, Q1 </p><p>Q0 : if d[i] != 1, Q0.push(i) </p><p>Q1 : if d[i] == 1, Q1.push(i) </p><p>while(!Q1.empty()) { </p><p> x = Q1.front(), Q1.pop(); </p><p> y = Q0.front(), Q0.pop(); </p><p> show have edge between x and y. </p><p> if(--d[y] == 1) Q1.push(y); </p><p> else Q0.push(y); </p><p>} </p></li><li><p>Q9 </p><p> For an undirected graph G=(V, E) and a vertex v in V let G\v denote the subgraph of G obtained by </p><p>removing v and all the edges incident to v from G. If </p><p>G is connected, then G\v can be disconnected or </p><p>connected. As a matter of fact, for any connected </p><p>graph G, we can always find a vertex v in G such that </p><p>G\v is connected. Prove this claim by mathematical </p><p>induction on the numbers of vertices and edges, </p><p>respectively. Discuss whether these proving </p><p>procedures imply algorithms for finding such a </p><p>vertex? 2013/10/20 40 Q1 Q8 </p></li><li><p>Q9 </p><p> Problem : Give an undirected graph G, find a non-cut </p><p>vertex v satisfy G &amp; G\v must be a connected </p><p>graph. </p><p> If G is a tree, each leaf node is non-cut vertex. Else non-cut vertex must be in a cycle. </p><p> 2013/10/20 41 Q1 Q8 </p></li><li><p>Q9 efficient algorithm </p><p> Worst case O(|V|+|E|) without cut operation. </p><p> Worst case O(|V|) with cut operation. </p><p>2013/10/20 42 Q1 Q8 </p><p>dfs(node) { </p><p> visited[node] = true; </p><p> for i in node's neighbors </p><p> if(visited[i]) </p><p> cut-inverseEdge --- O(1) </p><p> dfs(i); </p><p> else </p><p> i is a non-cut vertex. </p><p> end process. </p><p>} </p></li><li><p>Q10 </p><p> Give a linear-time algorithm that takes as input a tree and determines whether it </p><p>has a perfect matching: a set of edges </p><p>that touches each node exactly once. </p><p>2013/10/20 43 Q1 Q8 </p></li><li><p>Q10 Proof ? </p><p> Lemma. </p><p>If exists a perfect matching in a rooted </p><p>tree, each leaf node must match its </p><p>parent. </p><p>2013/10/20 44 Q1 Q8 </p></li><li><p>Q10 Proof ? </p><p>2013/10/20 45 Q1 Q8 </p><p>implement by depth-first search, </p><p>mx[] : label of node with matched. </p><p>dfs(node, parent) { </p><p> visited[node] = true; </p><p> for i in node's neighbors </p><p> if(visited[i] == false) </p><p> dfs(i, node); </p><p> if(mx[node] == null&amp;&amp;mx[parent] == null) </p><p> mx[node] = parent </p><p> mx[parent] = node </p><p>} </p><p>check all nodes have matched. </p></li><li><p> 2013/10/20 Q1 Q8 46 </p></li></ul>