מבוא מורחב למדעי המחשב בשפת Scheme תרגול 2. Reminder: Recursive algorithm Reduce problem to one or more sub-problems of smaller sizes (linear or tree.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms
Advertisements

Intro to Analysis of Algorithms. Algorithm “A sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? – Log.
1 ICS 353 Design and Analysis of Algorithms Spring Semester (062) King Fahd University of Petroleum & Minerals Information & Computer Science.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 2. 2 Outline Scoping and block structure Recursive and iterative processes Orders of growth.
Complexity Analysis (Part I)
מבוא מורחב - שיעור 4 1 Lecture 4 Order of Growth Fun with recursion  Fast exponentiation  Hanoi towers.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline High order procedures –Finding Roots –Compose Functions Accelerating Computations –Fibonacci 2.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 3. 2 Outline Let High order procedures.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Elementary Data Structures and Algorithms
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
מבוא מורחב 1 Lecture 3 Material in the textbook on Pages of 2nd Edition Sections to
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline High order procedures –Finding Roots –Compose Functions Accelerating Computations –Fibonacci 2.
Algorithm Analysis (Big O)
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.
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Algorithm Analysis An algorithm is a clearly specified set of simple instructions to be followed to solve a problem. Three questions for algorithm analysis.
Analysis of Algorithms
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
Analyzing algorithms & Asymptotic Notation BIO/CS 471 – Algorithms for Bioinformatics.
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Divide & Conquer  Themes  Reasoning about code (correctness and cost)  recursion, induction, and recurrence relations  Divide and Conquer  Examples.
Tonga Institute of Higher Education Design and Analysis of Algorithms IT 254 Lecture 2: Mathematical Foundations.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Today’s topics Orders of growth of processes Relating types of procedures to different orders of growth.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
Asymptotic Growth Rates  Themes  Analyzing the cost of programs  Ignoring constants and Big-Oh  Recurrence Relations & Sums  Divide and Conquer 
Algorithm Analysis Part of slides are borrowed from UST.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 1. Complexity Bounds.
CS220 Programming Principles 프로그래밍의 이해 2003 가을학기 Class 2 한 태숙.
ADVANCED ALGORITHMS REVIEW OF ANALYSIS TECHNIQUES (UNIT-1)
Algorithm Analysis (Big O)
Complexity Analysis. 2 Complexity The complexity of an algorithm quantifies the resources needed as a function of the amount of input data size. The resource.
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
Searching Topics Sequential Search Binary Search.
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 3. Outline High order procedures –Finding Roots –Compose Functions Accelerating Computations –Fibonacci 2.
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
Analyzing Programs: Order of Growth CMSC Introduction to Computer Programming October 11, 2002.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Complexity of Algorithms Fundamental Data Structures and Algorithms Ananda Guna January 13, 2005.
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
Algorithm Analysis 1.
CMPT 438 Algorithms.
Chapter 2 Algorithm Analysis
Design and Analysis of Algorithms
Introduction to the Design and Analysis of Algorithms
Analysis of Algorithms
CS 3343: Analysis of Algorithms
Time Complexity Analysis Neil Tang 01/19/2010
Algorithm Analysis Neil Tang 01/22/2008
Applied Algorithms (Lecture 17) Recursion Fall-23
CS 3343: Analysis of Algorithms
Algorithm Analysis (not included in any exams!)
Algorithm design and Analysis
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
Programming and Data Structure
Chapter 2.
Programming and Data Structure
CSE 373 Data Structures Lecture 5
Fundamentals of the Analysis of Algorithm Efficiency
Mathematical Background 2
At the end of this session, learner will be able to:
Fundamentals of the Analysis of Algorithm Efficiency
CS210- Lecture 2 Jun 2, 2005 Announcements Questions
Presentation transcript:

מבוא מורחב למדעי המחשב בשפת Scheme תרגול 2

Reminder: Recursive algorithm Reduce problem to one or more sub-problems of smaller sizes (linear or tree recursion) and solve them recursively Solve the very small sized problems directly Usually some computations are required in order to split problem into sub-problems or /and to combine the results 2

Example 1 Compute f(n)=n! Notice that f(n)=n*f(n-1) if n>1 and f(1)=1; Algorithm: 1.If n=1 return 1 2.Computing factorial for n-1 if n>1 3.Multiply result by n and return 3

Example 2a Compute the average of set of n=2^k numbers –Avg({x 1.. x n })=(x x n )/n Notice that: –Avg({x 1.. x n })=[Avg({x 1.. X n/2 })+Avg({x n/2+1.. x n })]/2 Algorithm 1.If input set size is 1, return the input as its average 2.Divide set into 2 subsets of n/2 if n>1 3.Find an average of each subset 4.Return average of two results 4

Example 2b Compute the average of set of numbers –Avg({x 1.. x n })=(x x n )/n Notice that: –Avg({x 1.. x n })=[x n + (n-1)*Avg({x 1.. X n-1 })]/n Algorithm 1.If input set size is 1, return the input as its average 2.Find average of last n-1 numbers 3.Multiply result by (n-1) add x n and the divide by n 5

Example 3 Decide if the number x is a power of 2 F(x)= #t,if x=2^n for some n; or #f,otherwise Notice –F(x)=F(x/2) if x is even, F(x)=#t if x=1 and #f otherwise Algorithms 1.if x=1 return #t; If x is odd return #f; else 2.Compute and return for x/2 6

Recursive calls Function/procedure that calls to itself called recursive procedure Can’t call to itself every time as have to stop somewhere Recursive algorithms are usually implemented using recursive calls 7

8 The conditional form (cond ( ) ( )... ( ) (else )) (define (abs x) (cond ((> x 0) x) ((= x 0) 0) (else (- x))))

Scheme Examples n! (define (f n) (if (= n 1) 1 (* n f (- n 1))))) Decide whether x is a power of 2 (define (is_pow2 x) (cond ( ((= x 1) #t) ((odd? x) #f) (else (is_pow2 (/ x 2)))) )) 9

Special case of recursive processes No need to do computations after return of recursive call 10 call return val calc call return valreturn same val calcno calc call return same val

11 converting a tail recursive call to an iterative process call calc call return val don’t wait In Scheme, a tail recursive procedure results in an iterative process!

Different Meanings of Recursions 12 Recursive AlgorithmsIterative Algorithms Recursive Scheme Function Recursive ProcessIterative Process Tail Recursion

13 Fibonacci Series Recursive algorithm fib(0) = 0 fib(1) = 1 fib(n) = fib(n-1) + fib(n-2) :n > 1 Tree recursion (define (fib n) (cond ((= n 0) 0) ((= n 1) 1) (else (+ (fib (- n 1)) (fib (- n 2)))))) Iterative algorithm Initialize: a = 1, b = 0 count = n Step: a new = a old + b old b new = a old count = count - 1 Tail Recursion (define (fib n) (define (fib-iter a b count) (if (= count 0) b (fib-iter (+ a b) a (- count 1)))) (fib-iter 1 0 n)) Every element is the sum of its predecessors: 1, 1, 2, 3, 5, 8, 13, 21…

14 Tree Recursion

Implementing loops using tail recursion Usually using helper local function (iterator) Pass loop local variables as an argument (usually includes some sort of counter) Check stopping condition Make initial call with initial state arguments 15

Example(Recursion) Input: Amount of money : $1.50 Output: Number of different ways to construct the amount using coins of 1,2,5,10,15,20,50 cents Assume there is unlimited number of coins of each value 16

17 Counting Change =2x=3x+ 4x=++ 93x

18 Counting Change CC(, { } ) = CC(, { } ) + CC( -, { } )

19 Counting Change (define (count-change amount) (cc amount 6)) (define (cc amount kinds-of-coins) (cond ((= amount 0) ) ((or (< amount 0) (= kinds-of-coins 0)) ) (else (+ (cc ) (cc ))))) (define (first-denomination kinds-of-coins) (cond ((= kinds-of-coins 1) 1) ((= kinds-of-coins 2) 2) ((= kinds-of-coins 3) 5) ((= kinds-of-coins 4) 10) ((= kinds-of-coins 5) 20) ((= kinds-of-coins 6) 50)))) 1 0 amount (- kinds-of-coins 1) (- amount (first-denomination kinds-of-coins)) kinds-of-coins

20 Orders of Growth We say that function R(n)   (f(n)) if there are constants c 1,c 2 > 0 such that for all n≥1 c 1 f(n) ≤ R(n) ≤ c 2 f(n) We say that function R(n)   (f(n)) if there is a constant c>0 such that for all n ≥1 c f(n) ≤ R(n) We say that function R(n)  O(f(n)) if there is a constant c>0 such that for all n ≥1 R(n) ≤ c f(n)

Asymptotic version Suppose f(n) < g(n) for all n≥N There is always a constant c such that f(n) < c*g(n) for all n ≥ 1 C=(max{f(n): n<N}+1)/min{g(n): n<N} 21

22 Practice 5n n + 300n 1/2 + 1  n  350  25 n 1/2 + 40log 5 n  12log 2 (n+1) + 6log(3n)  2 2n n   (n 3 )  (n 2 )  (1)  (n 1/2 )  (log 2 n)  (4 n )

Comparing algorithms Running time of a program implementing specific algorithm may vary depending on –Speed of specific computer(e.g. CPU speed) –Specific implementation(e.g. programming language) –Other factors (e.g. input characteristics) Compare number of operation required by different algorithms asymptotically, i.e. order of growth as size of input increases to infinity 23

Efficient Algorithm Algorithm for which number of operations and amount of required memory cells do not grow very fast as size of input increased –What operations to count? –What is input size (e.g. number of digits for representing input or input number itself)? –Different input of the same size can cause different running time. 24

Efficient algorithms cont’d Input size: usually the size of input representation (number of bits/digits) Usually interested in worst case running time,i.e. number of operations for worst case Usually we count primitive operations (arithmetic and/or comparisons) 25

26 Complexity Analysis Reminder: –Space complexity - number of deferred operations(Scheme) –Time complexity - number of primitive steps Method I – Qualitative Analysis –Example: Factorial –Each recursive call produce one deferred operation(multiplication) which takes constant time to perform –Total number of operations(time complexity)  (n) –Maximum number of deferred operations  (n) during the last call Method II – Recurrence Equations

27 Recurrence Equations Code (define (sum-squares n) (if (= n 1) 1 (+ (square n) (sum-squares (- n 1)) ))) Recurrence T(n) = T(n-1) +  (1) Solve T(n)  T(n-1) + c  T(n-2) + c + c  T(n-3) + c + c + c  … = T(1) + c + c + … + c = c + c + … + c = nc  O(n)

28 Recurrence Equations Code (define (fib n) (cond ((= n 0) 0) ((= n 1) 1) (else (+ (fib (- n 1)) (fib (- n 2)) )))) Recurrence T(n) = T(n-1) + T(n-2) +  (1) Solve For simplicity, we will solve T(n) = 2T(n-1) +  (1), Which will give us an upper bound

29 Recursion Trees T(n) T(n-1)  (1) T(n-2) 2  (1) 4  (1) T(1) 2 n-1  (1) Total:  (2 n )

30 Fibonacci - Complexity Time complexity –T(n) < 2T(n-1)+  (1) –Upper Bound: O(2 n ) –Tight Bound:  (  n ) where  =(  5+1)/2 –Space Complexity:  (n)

31 Common Recurrences T(n) = T(n-1) +  (1)   (n) T(n) = T(n-1) +  (n)   (n 2 ) T(n) = T(n/2) +  (1)   (logn) T(n) = T(n/2) +  (n)   (n) T(n) = 2T(n-1) +  (1)   (2 n ) T(n) = 2T(n/2) +  (1)   (n) T(n) = 2T(n/2) +  (n)   (nlogn)

32 Back to GCD (define (gcd a b) (if (= b 0) a (gcd b (remainder a b)))) Claim: The small number (b) is at least halved every second iteration For “double iterations” we have T(b)  T(b/2) +  (1) T(a)  O(log(b))