CS Non-recursive and Recursive Algorithm Analysis

Similar documents
Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Introduction to Algorithms

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

Analysis of Algorithms

CS 4407 Algorithms Lecture 2: Growth Functions

CS Analysis of Recursive Algorithms and Brute Force

CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms

CS473 - Algorithms I

Data Structures and Algorithms Running time and growth functions January 18, 2018

Module 1: Analyzing the Efficiency of Algorithms

Cpt S 223. School of EECS, WSU

Grade 11/12 Math Circles Fall Nov. 5 Recurrences, Part 2

EECS 477: Introduction to algorithms. Lecture 5

Introduction to Algorithms: Asymptotic Notation

Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc.

CS483 Design and Analysis of Algorithms

Analysis of Algorithms

Module 1: Analyzing the Efficiency of Algorithms

COMP Analysis of Algorithms & Data Structures

CSC Design and Analysis of Algorithms. Lecture 1

Asymptotic Analysis 1

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation:

COMP Analysis of Algorithms & Data Structures

COMP 555 Bioalgorithms. Fall Lecture 3: Algorithms and Complexity

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation:

with the size of the input in the limit, as the size of the misused.

i=1 i B[i] B[i] + A[i, j]; c n for j n downto i + 1 do c n i=1 (n i) C[i] C[i] + A[i, j]; c n

Data Structures and Algorithms. Asymptotic notation

Big O 2/14/13. Administrative. Does it terminate? David Kauchak cs302 Spring 2013

COMPUTER ALGORITHMS. Athasit Surarerks.

CS Asymptotic Notations for Algorithm Analysis

CS483 Design and Analysis of Algorithms

Ch01. Analysis of Algorithms

CSE 531 Homework 1 Solution. Jiayi Xian

COMP 9024, Class notes, 11s2, Class 1

CS Data Structures and Algorithm Analysis

Computer Science 431 Algorithms The College of Saint Rose Spring Topic Notes: Analysis Fundamentals

Computational Complexity

Lecture 2: Asymptotic Notation CSCI Algorithms I

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 2

Lecture 2. More Algorithm Analysis, Math and MCSS By: Sarah Buchanan

The Time Complexity of an Algorithm

3.1 Asymptotic notation

The Time Complexity of an Algorithm

Introduction to Algorithms and Asymptotic analysis

Growth of Functions (CLRS 2.3,3)

The Growth of Functions and Big-O Notation

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 1: Introduction

More Asymptotic Analysis Spring 2018 Discussion 8: March 6, 2018

CS 4407 Algorithms Lecture 3: Iterative and Divide and Conquer Algorithms

Problem Set 1 Solutions

In-Class Soln 1. CS 361, Lecture 4. Today s Outline. In-Class Soln 2

CS483 Design and Analysis of Algorithms

Algorithms, CSE, OSU. Introduction, complexity of algorithms, asymptotic growth of functions. Instructor: Anastasios Sidiropoulos

2. a. Consider the definition-based algorithm for finding the difference betweentwonxnmatrices.

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. January 16, 2019

Running Time Evaluation

Analysis of Multithreaded Algorithms

What we have learned What is algorithm Why study algorithm The time and space efficiency of algorithm The analysis framework of time efficiency Asympt

Analysis of Algorithms I: Asymptotic Notation, Induction, and MergeSort

Data Structures and Algorithms Chapter 2

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

CS 310 Advanced Data Structures and Algorithms

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

CS 350 Midterm Algorithms and Complexity

Homework 1 Solutions

Algorithm efficiency analysis

Asymptotic Notation. such that t(n) cf(n) for all n n 0. for some positive real constant c and integer threshold n 0

Lecture 1: Asymptotic Complexity. 1 These slides include material originally prepared by Dr.Ron Cytron, Dr. Jeremy Buhler, and Dr. Steve Cole.

Algorithm Analysis Recurrence Relation. Chung-Ang University, Jaesung Lee

Md Momin Al Aziz. Analysis of Algorithms. Asymptotic Notations 3 COMP Computer Science University of Manitoba

Written Homework #1: Analysis of Algorithms

Ch 01. Analysis of Algorithms

Practical Session #3 - Recursions

CS Asymptotic Notations for Algorithm Analysis Outline

Homework 1 Submission

1 Substitution method

CS F-01 Algorithm Analysis 1

COMP 382: Reasoning about algorithms

Asymptotic Analysis of Algorithms. Chapter 4

Data Structures and Algorithms

Algorithms. Adnan YAZICI Dept. of Computer Engineering Middle East Technical Univ. Ankara - TURKEY. Algorihms, A.Yazici, Fall 2007 CEng 315

CSCI 3110 Assignment 6 Solutions

INTRODUCTION 1.1 ALGORITHM

Algorithms Design & Analysis. Analysis of Algorithm

CS 2210 Discrete Structures Advanced Counting. Fall 2017 Sukumar Ghosh

Agenda. We ve discussed

Computational Complexity - Pseudocode and Recursions

Input Decidable Language -- Program Halts on all Input Encoding of Input -- Natural Numbers Encoded in Binary or Decimal, Not Unary

Data Structures and Algorithms CSE 465

Design Patterns for Data Structures. Chapter 3. Recursive Algorithms

Asymptotic Analysis and Recurrences

Comparison of functions. Comparison of functions. Proof for reflexivity. Proof for transitivity. Reflexivity:

Algorithms, Design and Analysis. Order of growth. Table 2.1. Big-oh. Asymptotic growth rate. Types of formulas for basic operation count

Advanced Analysis of Algorithms - Homework I (Solutions)

Analysis of Algorithms

Big O (Asymptotic Upper Bound)

Asymptotic Analysis. Slides by Carl Kingsford. Jan. 27, AD Chapter 2

Compare the growth rate of functions

CSC2100B Data Structures Analysis

Transcription:

CS483-04 Non-recursive and Recursive Algorithm Analysis Instructor: Fei Li Room 443 ST II Office hours: Tue. & Thur. 4:30pm - 5:30pm or by appointments lifei@cs.gmu.edu with subject: CS483 http://www.cs.gmu.edu/ lifei/teaching/cs483_fall07/ CS483 Design and Analysis of Algorithms 1 Lecture 04, September 6, 2007 Outline Review and More Analysis of Non-recursive Algorithms Analysis of Recursive Algorithms Examples CS483 Design and Analysis of Algorithms 2 Lecture 04, September 6, 2007

Review O(g(n)) := {f(n) there exist positive constants c and n 0 such that 0 f(n) c g(n) for all n n 0 }. f(n) O(g(n)) f(n) grow no faster than g(n). Ω(g(n)) := {f(n) there exist positive constants c and n 0 such that 0 c g(n) f(n) for all n n 0 }. f(n) grows at least as fast as g(n). f(n) Ω(g(n)) Θ(g(n)) := {f(n) there exist positive constants c 1, c 2, and n 0 such that c 1 g(n) f(n) c 2 g(n) for all n n 0 }. f(n) grows at the same rate as g(n). f(n) Θ(g(n)) CS483 Design and Analysis of Algorithms 3 Lecture 04, September 6, 2007 Asymptotic Notations c 2 g(n) f(n) c 1 g(n) n 0 n f(n) Θ(g(n)) cg(n) f(n) f(n) cg(n) n 0 n f(n) O(g(n)) n 0 n f(n) Ω(g(n)) CS483 Design and Analysis of Algorithms 4 Lecture 04, September 6, 2007

Review Tools and techniques to get asymptotic notation L Hopital s rule If lim n f(n) = lim n g(n) = and the derivatives f and g exist, then Stirling s formula f(n) lim n g(n) = lim f (n) n g (n) n! 2πn( n e )n where e is the base of natural logarithm, e 2.718. π 3.1415. CS483 Design and Analysis of Algorithms 5 Lecture 04, September 6, 2007 Exercises All logarithmic functions log a n belong to the same class Θ(log n) no matter what the logarithmic base a>1 is. All polynomials of the same degree k belong to the same class a k n k + a k 1 n k 1 + + a 1 n + a 0 Θ(n k ). Exponential functions a n have different orders of growth for different a s, i.e., 2 n / Θ(3 n ). ln n<(ln n) 2 < n<n<n ln n<n 2 <n 3 < 2 n <n! <n n CS483 Design and Analysis of Algorithms 6 Lecture 04, September 6, 2007

Transitivity Some Properties of Asymptotic Order of Growth f(n) O(g(n)) and g(n) O(h(n)) f(n) O(h(n)) f(n) Θ(g(n)) and g(n) Θ(h(n)) f(n) Θ(h(n)) f(n) Ω(g(n)) and g(n) Ω(h(n)) f(n) Ω(h(n)) Reflexivity f(n) O(f(n)) f(n) Θ(f(n)) f(n) Ω(f(n)) Symmetry and Transpose Symmetry f(n) Θ(g(n)) if and only if g(n) Θ(f(n)) f(n) O(g(n)) if and only if g(n) Ω(f(n)) CS483 Design and Analysis of Algorithms 7 Lecture 04, September 6, 2007 Outline Review Analysis of Non-recursive Algorithms Analysis of Recursive Algorithms Examples CS483 Design and Analysis of Algorithms 8 Lecture 04, September 6, 2007

Time Efficiency of Non-recursive Algorithms Decide on parameter n indicating input size. Identify algorithm s basic operation. Determine worst, average, and best cases for input of size n. Sum the number of basic operations executed. Simplify the sum using standard formula and rules (see Appendix A). CS483 Design and Analysis of Algorithms 9 Lecture 04, September 6, 2007 Time Efficiency of Non-recursive Algorithms u i=l 1=1+1+ +1=(u l)+1. n n(n+1) i=1 i =1+2+ + n = 2 n2 2 Ω(n2 ). n i=1 i2 =1 2 +2 2 + + n 2 = n(n+1)(2n+1) 6 n3 3 Ω(n3 ). n i=1 ai =1+a + a 2 + + a n = an+1 1, a 1. a 1 CS483 Design and Analysis of Algorithms 10 Lecture 04, September 6, 2007

Example1: Maximum Element Determine the value of the largest element in a given array. Input: An array A[0,,n 1] of real numbers. Output: The value of the largest element in A. Algorithm 0.1: MAXELEMENT(A[0, n 1]) max = A[0] for i =1to n 1 if A[i] >max do then max = A[i] return (max) CS483 Design and Analysis of Algorithms 11 Lecture 04, September 6, 2007 Example 2: Element Uniqueness Problem Determine whether all the elements in a given array are distinct. Input: An array A[0,...,n 1]. Output: Returns true if all the elements in A are distinct and false otherwise. Algorithm 0.2: UNIQUEELEMENTS(A[0, n 1]) for i =0to n 2 for j = i +1to n 1 do if A[i] =A[j] do return (true) then return (false) CS483 Design and Analysis of Algorithms 12 Lecture 04, September 6, 2007

Example 3: Matrix Multiplication Multiply 2 n-by-n matrices by the definition-based algorithm. Input: 2 n-by-n matrices A and B. Output: Matrix C = A B. Algorithm 0.3: MATRIXMULTI(A, B) for i =0to n 1 for j =0to n 1 do C[i, j] =0 do for k =0to n 1 do C[i, j] =C[i, j]+a[i, k] B[k, j] return (C) CS483 Design and Analysis of Algorithms 13 Lecture 04, September 6, 2007 Input: A positive decimal integer n. Example4: Counting Binary Bits Output: The number of binary digits in n s binary representation. Algorithm 0.4: COUNTBINARYBITS(n) count =1 while n>1 do count = count +1 n = n/2 return (count) CS483 Design and Analysis of Algorithms 14 Lecture 04, September 6, 2007

Outline Review Analysis of Non-recursive Algorithms Analysis of Recursive Algorithms Examples CS483 Design and Analysis of Algorithms 15 Lecture 04, September 6, 2007 Recurrences A recurrence is an equation or inequality that describes a function in terms of its value over a smaller value. Example: Findn! CS483 Design and Analysis of Algorithms 16 Lecture 04, September 6, 2007

Recurrences A recurrence is an equation or inequality that describes a function in terms of its value over a smaller value. Example: Findn! Non-recursive: 1 2 3 n Algorithm 0.5: FINDFACTORIAL-α(n) factorial =1 for i =1to n do factorial = factorial i return (factorial) Recurrence: f(n) =n f(n 1) CS483 Design and Analysis of Algorithms 17 Lecture 04, September 6, 2007 Algorithm 0.6: FINDFACTORIAL-β(n) if n =0 then return (1) else return (n FindFactorial β(n 1)) CS483 Design and Analysis of Algorithms 18 Lecture 04, September 6, 2007

Input: A positive decimal integer n. Example: Counting Number of Bits Output: The number of binary digits in n s binary representation. Algorithm 0.7: NON-RECURSIVECOUNT(n) count =1 while n>1 8 < count = count +1 do : n = n/2 return (count) Algorithm 0.8: RECURSIVECOUNT(n) if i =1 do return (1) else return (RecursiveCount( n/2 )+1) CS483 Design and Analysis of Algorithms 19 Lecture 04, September 6, 2007 Example: Fibonacci Numbers Output: A sequence of numbers F 0,F 1,F 2,...,F n,...such that 8 >< F n 1 + F n 2, if n>1 F n = 1, if n =1 >: 0, if n =0. CS483 Design and Analysis of Algorithms 21 Lecture 04, September 6, 2007

Example: Fibonacci Numbers Output: A sequence of numbers F 0,F 1,F 2,...,F n,...such that 8 >< F n 1 + F n 2, if n>1 F n = 1, if n =1 >: 0, if n =0. Algorithm 0.9: FIBNUMBER(n) if n =0 return (0) if n =1 return (1) else return (F ibnumber(n 1) + F ibnumber(n 2)) CS483 Design and Analysis of Algorithms 22 Lecture 04, September 6, 2007 Example: Hanoi Tower Problem Move all the disks from peg a to peg c. Large disk cannot be on top of a smaller one. Input: n disks in order of sizes on peg a. 3 pegs a, b, and c http://en.wikipedia.org/wiki/tower_of_hanoi Algorithm 0.10: HANOITOWER(n, a, c, b) if n =1 Move the disk from a to c 8 >< HanoiTower(n 1,a,b,c) else Move the largest disk from a to c >: HanoiTower(n 1,b,c,a) CS483 Design and Analysis of Algorithms 23 Lecture 04, September 6, 2007

Analysis of Recursive Algorithms The iteration method Expand (iterate) the recurrence and express it as a summation of terms depending only on n and the initial conditions. The substitution method Master Theorem (To be introduced in Chapter 4.) CS483 Design and Analysis of Algorithms 24 Lecture 04, September 6, 2007 n! Iteration Method: Examples Tower of Hanoi T (n) =T (n 1) + 1 T (n) =2T (n 1) + 1 CS483 Design and Analysis of Algorithms 25 Lecture 04, September 6, 2007

n! (T (n) =T (n 1) + 1) Iteration: Example T (n) = T (n 1) + 1 = (T (n 2) + 1) + 1 = T (n 2) + 2 = T (n i)+i = T (0) + n = n Tower of Hanoi (T (n) =2T (n 1) + 1)??? CS483 Design and Analysis of Algorithms 26 Lecture 04, September 6, 2007 n! (T (n) =T (n 1) + 1) Iteration: Example Tower of Hanoi (T (n) =2T (n 1) + 1) T (n) = 2T (n 1) + 1 = 2(2T (n 2) + 1) + 1 = 2 2 T (n 2)+2+1 = 2 i T (n i)+2 i 1 + +1 = 2 n 1 T (1) + 2 n 1 +2 n 1 + +1 = 2 n 1 T (1) + n 2 X i=0 = 2 n 1 +2 n 1 1 = 2 n 1 2 i CS483 Design and Analysis of Algorithms 27 Lecture 04, September 6, 2007

Assignment 1 Problems 1. Prove or find a counter-example: ( n 3 )n <n! < ( n 2 )n, if n 6. 2. p. 8, Exercises (1.1) 5, 6. 3. p. 60, Exercises (2.2) 5, 6 4. p. 67, Exercises (2.3) 2, 4 5. p. 76, Exercises (2.4) 1, 3, 5 Due date: September 20, 2007. In class CS483 Design and Analysis of Algorithms 37 Lecture 04, September 6, 2007