1 / 28

Analysis & Design of Algorithms (CSCE 321)

This course covers topics such as Fibonacci numbers, computing binomial coefficients, and the sum of subset problem. Algorithms are analyzed and designed using a divide and conquer approach. Taught by Prof. Amr Goneid at AUC.

knora
Download Presentation

Analysis & Design of Algorithms (CSCE 321)

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. Analysis & Design of Algorithms(CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 9. Intermezzo Prof. Amr Goneid, AUC

  2. Prof. Amr Goneid, AUC

  3. Fibonacci Numbers • Computing Binomial Coefficients • The Sum of Subset Problem • Summary Prof. Amr Goneid, AUC

  4. 1. Fibonacci Numbers • Fibonacci numbers represent the sequence: 0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,... • Introduced by Leonardo Fibonacci (1202) • Can be computed by the recurrence relation: F(n) = F(n-2) + F(n-1) n ≥ 2 with F(0)=F(1)=1 Prof. Amr Goneid, AUC

  5. Fibonacci Numbers Fibonacci numbers are closely related to the Golden Ratio φ since: Prof. Amr Goneid, AUC

  6. The Golden Ratio Prof. Amr Goneid, AUC

  7. In Nature Prof. Amr Goneid, AUC

  8. In Nature Prof. Amr Goneid, AUC

  9. In Architecture & Design Prof. Amr Goneid, AUC

  10. In ART Prof. Amr Goneid, AUC

  11. In Art Prof. Amr Goneid, AUC

  12. Divide & Conquer Algorithm Divide & Conquer Algorithm implemented as a recursive function: int Fib(int n) { if (n < 2) return 1; else return Fib(n-2) + Fib(n-1); } Prof. Amr Goneid, AUC

  13. Divide & Conquer Algorithm Analysis Tracing for n = 5 5 3 4 1 2 2 3 0 1 0 1 1 2 0 1 Prof. Amr Goneid, AUC

  14. Divide & Conquer Algorithm This solution is an example of Overlapping Subproblems. It means that any recursive algorithm solving the problem should solve the same subproblems over and over, rather than generating new subproblems. . Prof. Amr Goneid, AUC

  15. Analysis • The number of calls T(n) = T(n-1) + T(n-2) + 1 with T(0)=T(1)=1 • The cost is very high because we keep computing the same instance over and over again. Prof. Amr Goneid, AUC

  16. 2. Computing Binomial Coefficients • A binomial is an expression like (x + y). • Algebraic expansion of powers of a binomial is done through the Binomial Theorem: Prof. Amr Goneid, AUC

  17. Computing Binomial Coefficients • The coefficients are represented by the Pascal Triangle, named after the French mathematician Blaise Pascal (1665). Prof. Amr Goneid, AUC

  18. Computing Binomial Coefficients • Previously, the triangle has been investigated by Omar Khayyam (1048-1131) Awake! for Morning in the Bowl of Night Has flung the Stone that puts the Stars to Flight; And Lo! the Hunter of the East has caught The Sultan’s Turret in a Noose of Light! [from Fitzgerald’s translation of the Rubaiyat of Omar Khayyam] Prof. Amr Goneid, AUC

  19. Computing Binomial Coefficients • The coefficients are related to the Fibonacci numbers. The sums of the “hidden“ diagonals give the Fibonacci sequence. Prof. Amr Goneid, AUC

  20. Computing Binomial Coefficients • The coefficients also represent the number of combinations of n items taken m at a time. • Given m <= n, to choose mthings out of n, either: • Choose the first item. Then we must choose the remaining m−1 items from the other n−1 items. Or • Don’t choose the first item. Then we must choose the mitems from the other n − 1 items. Prof. Amr Goneid, AUC

  21. Computing Binomial Coefficients • This leads to the following recurrence relation: • Again, this suggests a Divide & Conquer algorithm Prof. Amr Goneid, AUC

  22. Divide & Conquer Algorithm Divide & Conquer Algorithm int comb (int n, int m) { if ((m == 0) || (m == n)) return1; else return comb (n − 1, m − 1) + comb (n − 1, m); } Analysis Let T(n) = worst case running time over all values of m. Then, T(n) = 2T(n − 1) + 1 for n > 1 with T(1) = 1 Hence T(n) = 2n - 1 = O(2n) = exponential Time Prof. Amr Goneid, AUC

  23. Counting Combinations • Again the cost is very high because we keep computing the same instance over and over again, i.e., an overlapping subproblem. 5,3 4,2 4,3 3,1 3,2 3,2 3,3 2,1 2,2 2,1 2,2 1,0 1,1 1,0 1,1 Prof. Amr Goneid, AUC

  24. 3. The Sum of Subset Problem • Given a set of positive integers W = {w1,w2...wn} • The problem:is there a subset of W that sums exactly to m? i.e, isSumSub (w,n,m)true? • Example: W = { 11 , 13 , 27 , 7} , m = 31 A possible subset that sums exactly to 31 is {11 , 13 , 7} Hence, SumSub (w,4,31) is true Prof. Amr Goneid, AUC

  25. Divide & Conquer Approach • Consider the partial problemSumSub (w,i,j) to return true if there is a subset of the first i items that sums up exactly to j: • SumSub (w, i, j)is true if: • wi is not needed, and {w1,..,wi-1} has a subset that sums to (j), i.e., SumSub (w, i-1, j)is true, OR • wi is needed to fill the rest of (j), i.e., {w1,..,wi-1} has a subset that sums to (j-wi) so that adding wi sums up exactly to j- wi + wi = j • If there are no elements, i.e. (i = 0) then SumSub (w, 0, j)is true if (j = 0) and false otherwise Prof. Amr Goneid, AUC

  26. Divide & Conquer Approach Algorithm: bool SumSub (w, i, j) { if (i == 0) return (j == 0); else if (SumSub (w, i-1, j)) return true; else if ((j - wi) >= 0) return SumSub (w, i-1, j - wi); else return false; } Prof. Amr Goneid, AUC

  27. Divide & Conquer Approach Analysis: T(n) = no. of calls to SumSub (w, n, m): • For n = 0, one main call, T(0) = 1 • For n > 0, one main call plus two calls each with n-1 • The recurrence relation is: T(n) = 2T(n-1) + 1 for n > 0 with T(0) = 1 • Hence T(n) = 2n+1 -1 = O(2n) = exponential time Can We Do Better? Prof. Amr Goneid, AUC

  28. 4. Summary • The recursive Fibonacci algorithm, recursive combination counting and sum of subsets are examples of problems where: • Problem is solved by divide & conquer methods • Same sub-problems are solved repeatedly • This produces an exponential time • Conclusion: Sometimes, the divide and conquer approach seems appropriate but fails to produce an efficient algorithm. Prof. Amr Goneid, AUC

More Related