Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 9. Intermezzo.

Slides:



Advertisements
Similar presentations
Dynamic Programming Introduction Prof. Muhammad Saeed.
Advertisements

Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R4. Disjoint Sets.
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
Divide-and-Conquer Recursive in structure –Divide the problem into several smaller sub-problems that are similar to the original but smaller in size –Conquer.
11 Computer Algorithms Lecture 6 Recurrence Ch. 4 (till Master Theorem) Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Lecture 6: Dynamic Programming 0/1 Knapsack Making Change (any coin set)
CSC 427: Data Structures and Algorithm Analysis
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Analysis & Design of Algorithms (CSCE 321)
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
CSC401 – Analysis of Algorithms Lecture Notes 12 Dynamic Programming
Analysis of Recursive Algorithms
Recursive Definitions Rosen, 3.4 Recursive (or inductive) Definitions Sometimes easier to define an object in terms of itself. This process is called.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 5. Recursive Algorithms.
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
Rev.S08 MAC 1140 Module 12 Introduction to Sequences, Counting, The Binomial Theorem, and Mathematical Induction.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Chapter 8. Section 8. 1 Section Summary Introduction Modeling with Recurrence Relations Fibonacci Numbers The Tower of Hanoi Counting Problems Algorithms.
Advanced Counting Techniques CSC-2259 Discrete Structures Konstantin Busch - LSU1.
The power of logarithmic computations Jordi Cortadella Department of Computer Science.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
Dynamic Programming. Well known algorithm design techniques:. –Divide-and-conquer algorithms Another strategy for designing algorithms is dynamic programming.
Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang.
Analysis of Algorithms
Recursion and Dynamic Programming. Recursive thinking… Recursion is a method where the solution to a problem depends on solutions to smaller instances.
1 Recurrences Algorithms Jay Urbain, PhD Credits: Discrete Mathematics and Its Applications, by Kenneth Rosen The Design and Analysis of.
1 Computer Algorithms Lecture 7 Master Theorem Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
Project 2 due … Project 2 due … Project 2 Project 2.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2008 Dynamic programming  top-down vs. bottom-up  divide & conquer vs. dynamic programming  examples:
Dynamic Programming. What is dynamic programming? Break problem into subproblems Work backwards Can use ‘recursion’ ‘Programming’ - a mathematical term.
Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized.
DR. NAVEED AHMAD DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF PESHAWAR LECTURE-5 Advance Algorithm Analysis.
1 Binomial Coefficients CS 202 Epp, section ??? Aaron Bloomfield.
CSC401: Analysis of Algorithms CSC401 – Analysis of Algorithms Chapter Dynamic Programming Objectives: Present the Dynamic Programming paradigm.
Chapter 8 With Question/Answer Animations. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
INTRODUCTION. What is an algorithm? What is a Problem?
Dynamic Programming continued David Kauchak cs302 Spring 2012.
Recursion Review: A recursive function calls itself, but with a smaller problem – at least one of the parameters must decrease. The function uses the results.
Main Index Contents 11 Main Index Contents Building a Ruler: drawRuler() Building a Ruler: drawRuler() Merge Algorithm Example (4 slides) Merge Algorithm.
Advanced Counting Techniques CSC-2259 Discrete Structures Konstantin Busch - LSU1.
Dynamic Programming. Many problem can be solved by D&C – (in fact, D&C is a very powerful approach if you generalize it since MOST problems can be solved.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 1. Complexity Bounds.
Asymptotics and Recurrence Equations Prepared by John Reif, Ph.D. Analysis of Algorithms.
1 Dynamic Programming Topic 07 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology.
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
Dynamic Programming David Kauchak cs161 Summer 2009.
Dynamic Programming (DP) By Denon. Outline Introduction Fibonacci Numbers (Review) Longest Common Subsequence (LCS) More formal view on DP Subset Sum.
Analysis & Design of Algorithms (CSCE 321)
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
Section 6.4. Powers of Binomial Expressions Definition: A binomial expression is the sum of two terms, such as x + y. (More generally, these terms can.
Binomial Coefficients and Identities
Polynomials Count! CS Lecture 9 X1 X1 X2 X2 + + X3 X3.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 3. Time Complexity Calculations.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 3. Introduction to the Analysis of Algorithms.
6/12/2016 Prepared by Dr.Saad Alabbad1 CS100 : Discrete Structures Proof Techniques(2) Mathematical Induction & Recursion Dr.Saad Alabbad Department of.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar.
Introduction to Algorithms: Divide-n-Conquer Algorithms
Fundamentals of Algorithms MCS - 2 Lecture # 11
Lecture 12.
Introduction to the Design and Analysis of Algorithms
0/1 Knapsack Making Change (any coin set)
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Applied Algorithms (Lecture 17) Recursion Fall-23
Algorithm Analysis (for Divide-and-Conquer problems)
CS201: Data Structures and Discrete Mathematics I
Divide and Conquer Algorithms Part I
Lecture 4 Dynamic Programming
Presentation transcript:

Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 9. Intermezzo

Prof. Amr Goneid, AUC2

3 1. Fibonacci Numbers 2. Computing Binomial Coefficients 3. The Sum of Subset Problem 4. Summary

Prof. Amr Goneid, AUC4 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, AUC5 Fibonacci Numbers Fibonacci numbers are closely related to the Golden Ratio φ since:

Prof. Amr Goneid, AUC6 The Golden Ratio

Prof. Amr Goneid, AUC7 In Nature

Prof. Amr Goneid, AUC8 In Nature

Prof. Amr Goneid, AUC9 In Architecture & Design

Prof. Amr Goneid, AUC10 In ART

Prof. Amr Goneid, AUC11 In Art

Prof. Amr Goneid, AUC12 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, AUC13 Divide & Conquer Algorithm Analysis Tracing for n = n12345 Calls135915

Prof. Amr Goneid, AUC14 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, AUC15 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, AUC16 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, AUC17 Computing Binomial Coefficients The coefficients are represented by the Pascal Triangle, named after the French mathematician Blaise Pascal (1665).

Prof. Amr Goneid, AUC18 Computing Binomial Coefficients Previously, the triangle has been investigated by Omar Khayyam ( ) 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, AUC19 Computing Binomial Coefficients The coefficients are related to the Fibonacci numbers. The sums of the “hidden“ diagonals give the Fibonacci sequence.

Prof. Amr Goneid, AUC20 Computing Binomial Coefficients The coefficients also represent the number of combinations of n items taken m at a time. Given m <= n, to choose m things 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 m items from the other n − 1 items.

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

Prof. Amr Goneid, AUC22 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) = 2 n - 1 = O(2 n ) = exponential Time

Prof. Amr Goneid, AUC23 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,24,3 3,13,23,3 2,12,2 1,01,1 3,2 2,12,2 1,01,1

Prof. Amr Goneid, AUC24 3. The Sum of Subset Problem Given a set of positive integers W = {w 1,w 2...w n } The problem: is there a subset of W that sums exactly to m? i.e, is SumSub (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 w1w1 w2w2....wnwn m

Prof. Amr Goneid, AUC25 Divide & Conquer Approach Consider the partial problem SumSub (w,i,j) SumSub (w, i, j) is true if: w i is not needed, {w 1,..,w i-1 } has a subset that sums to (j), i.e., SumSub (w, i-1, j) is true, OR w i is needed to fill the rest of (j), i.e., {w 1,..,w i-1 } has a subset that sums to (j-w i ) If there are no elements, i.e. (i = 0) then SumSub (w, 0, j) is true if (j = 0) and false otherwise w1w1 w2w2....wiwi j

Prof. Amr Goneid, AUC26 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 - w i ) >= 0) return SumSub (w, i-1, j - w i ); else return false; }

Prof. Amr Goneid, AUC27 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) = 2 n+1 -1 = O(2 n ) = exponential time Can We Do Better?

Prof. Amr Goneid, AUC28 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.