CSC2100B Data Structures Analysis

Similar documents
Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

CS 2210 Discrete Structures Advanced Counting. Fall 2017 Sukumar Ghosh

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Advanced Counting Techniques. 7.1 Recurrence Relations

Recurrence Relations

CS 310 Advanced Data Structures and Algorithms

Principles of Algorithm Analysis

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

1 Recursive Algorithms

Advanced Counting Techniques

Recursion: Introduction and Correctness

Jong C. Park Computer Science Division, KAIST

What is Performance Analysis?

Applications. More Counting Problems. Complexity of Algorithms

Advanced Counting Techniques. Chapter 8

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

Data Structures and Algorithms

Recurrences COMP 215

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

Recurrence Relations

CSI2101-W08- Recurrence Relations

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

Divide and Conquer. Recurrence Relations

O Notation (Big Oh) We want to give an upper bound on the amount of time it takes to solve a problem.

Analysis of Algorithms

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Catie Baker Spring 2015

Topic 17. Analysis of Algorithms

Announcements. CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis. Today. Mathematical induction. Dan Grossman Spring 2010

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

COMP 555 Bioalgorithms. Fall Lecture 3: Algorithms and Complexity

CS Non-recursive and Recursive Algorithm Analysis

Binomial Coefficient Identities/Complements

V. Adamchik 1. Recurrences. Victor Adamchik Fall of 2005

Algorithms Test 1. Question 1. (10 points) for (i = 1; i <= n; i++) { j = 1; while (j < n) {

Fall 2016 Test 1 with Solutions

COMPUTER ALGORITHMS. Athasit Surarerks.

CISC 235: Topic 1. Complexity of Iterative Algorithms

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2019

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

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

csci 210: Data Structures Program Analysis

3.1 Asymptotic notation

Data Structures and Algorithms. Asymptotic notation

LECTURE NOTES ON DESIGN AND ANALYSIS OF ALGORITHMS

6.2 SOLVING EASY RECURRENCES

Asymptotic Algorithm Analysis & Sorting

Solving Recurrences. 1. Express the running time (or use of some other resource) as a recurrence.

Divide-and-Conquer Algorithms and Recurrence Relations. Niloufar Shafiei

CHAPTER 8 Advanced Counting Techniques

csci 210: Data Structures Program Analysis

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING QUESTION BANK UNIT-I SUB NAME: DESIGN AND ANALYSIS OF ALGORITHMS. PART A (2 Marks)

Mathematical Background. Unsigned binary numbers. Powers of 2. Logs and exponents. Mathematical Background. Today, we will review:

Discrete Mathematics -- Chapter 10: Recurrence Relations

Cpt S 223. School of EECS, WSU

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2018

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2018

Answer the following questions: Q1: ( 15 points) : A) Choose the correct answer of the following questions: نموذج اإلجابة

Divide and Conquer. CSE21 Winter 2017, Day 9 (B00), Day 6 (A00) January 30,

Analysis of Algorithms

Analysis of Algorithms

Algorithms Design & Analysis. Analysis of Algorithm

Algorithm Analysis, Asymptotic notations CISC4080 CIS, Fordham Univ. Instructor: X. Zhang

Every subset of {1, 2,...,n 1} can be extended to a subset of {1, 2, 3,...,n} by either adding or not adding the element n.

Solving Recurrences. Lecture 23 CS2110 Fall 2011

Algorithm. Executing the Max algorithm. Algorithm and Growth of Functions Benchaporn Jantarakongkul. (algorithm) ก ก. : ก {a i }=a 1,,a n a i N,

Learning Objectives

Running Time Evaluation

Asymptotic Analysis. Thomas A. Anastasio. January 7, 2004

Algorithm efficiency analysis

Module 1: Analyzing the Efficiency of Algorithms

Measuring Goodness of an Algorithm. Asymptotic Analysis of Algorithms. Measuring Efficiency of an Algorithm. Algorithm and Data Structure

Solving Recurrences. 1. Express the running time (or use of some other resource) as a recurrence.

Discrete Mathematics. Kishore Kothapalli

UCSD CSE 21, Spring 2014 [Section B00] Mathematics for Algorithm and System Analysis

Analysis of Algorithms Review

COMP Analysis of Algorithms & Data Structures

Linear Recurrence Relations

UCSD CSE 21, Spring 2014 [Section B00] Mathematics for Algorithm and System Analysis

CS Analysis of Recursive Algorithms and Brute Force

Part III, Sequences and Series CS131 Mathematics for Computer Scientists II Note 16 RECURRENCES

ASYMPTOTIC COMPLEXITY SEARCHING/SORTING

COMP Analysis of Algorithms & Data Structures

2.2 Asymptotic Order of Growth. definitions and notation (2.2) examples (2.4) properties (2.2)

5. Sequences & Recursion

Practical Session #3 - Recursions

Discrete Mathematics: Logic. Discrete Mathematics: Lecture 17. Recurrence

The maximum-subarray problem. Given an array of integers, find a contiguous subarray with the maximum sum. Very naïve algorithm:

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

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

Growth of Functions (CLRS 2.3,3)

Notes for Recitation 14

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

Math 2602 Finite and Linear Math Fall 14. Homework 8: Core solutions

12 Sequences and Recurrences

Section 5.2 Solving Recurrence Relations

CS 344 Design and Analysis of Algorithms. Tarek El-Gaaly Course website:

Asymptotic Analysis 1

CSED233: Data Structures (2017F) Lecture4: Analysis of Algorithms

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

A SUMMARY OF RECURSION SOLVING TECHNIQUES

Transcription:

CSC2100B Data Structures Analysis Irwin King king@cse.cuhk.edu.hk http://www.cse.cuhk.edu.hk/~king Department of Computer Science & Engineering The Chinese University of Hong Kong

Algorithm An algorithm is a clearly specified set of simple instructions to be followed to solve a problem. How to estimate the time required for a program. How to reduce the running time of a program from days or years to fractions of a second. What is the storage complexity of the program. How to deal with trade-offs.

Running Time There are two contradictory goals: We would like an algorithm that is easy to understand, code, and debug. We would like an algorithm that makes efficient use of the computer's resources, especially, one that runs as fast as possible.

Function Comparison Given two functions, f(n) and g(n), what does it mean when we say that f(n) < g(n)? Should this hold for all N? We need to compare their relative rates of growth.

Example http://science.slc.edu/~jmarshall/courses/2002/spring/cs50/bigo/index.html

Why Use Bounds The idea is to establish a relative order among functions. We are more concerned about the relative rates of growth of functions. For example, which function is greater, 1,000N or N 2? The turning point is N = 1,000 where N 2 will be greater for larger N.

First Definition It says that there is some point n0 past which c f(n) is always at least as large as T(N). In our case, T(N) = 1000N, f(n) = N 2, n0 = 1,000, and c = 1. We could also use n0 = 10, and c = 100. So we can say that 1000N = O(N 2 ). It is an upper bound on T(N).

Other Definitions The second definition says that the growth rate of T(N) is greater than or equal to that of g(n). The third definition says that the growth rate of T(N) equals the growth rate of h(n). The fourth definition says that the growth rate of T(N) is less than the growth rate of p(n).

Big-O Notation If f(n) and g(n) are functions defined for positive integers, then to write f(n) is O(g(n)). f(n) is big-o of g(n) means that there exists a constant c such that f(x) c g(n) for all sufficiently large positive integers n. Under these conditions we also say that f(n) has order at most g(n) or f(n) grows no more rapidly than g(n).

Examples f(n) = 100n then f(n) = O(n). f(n) = 4n + 200 then f(n) = O(n). f(n) = n 2 then f(n) = O(n 2 ). f(n) = 3 n 2-100 then f(n) = O(n 2 ).

Rules If T1(N) = O(f(N)) and T2(N) = O(g(N)), then T1(N) + T2(N) = max(o(f(n)), O(g(N))), T1(N) * T2(N) = O(f(N)) * g(n)), If T(N) is a polynomial of degree k, then T(N) = (N k ). log k N = O(N) for any constant k. This tells us that logarithms grow very slowly.

Watch Out! It is bad to include constants or low-order terms inside a Big-Oh notation. Do not say T(N) = O(2N2 ) or T(N) = O(N 2 + N). In both cases, T(N) = O(N2 ).

Observations If f(n) is a polynomial in n with degree r, then f(n) is O(n r ), but f(n) is not O(n s ) for any power s less than r. Any logarithm of n grows more slowly (as n increases) than any positive power of n. Hence log n is O(n k ) for any k > 0, but n k is never O(log n) for any power k > 0.

Common Orders O(1) means computing time that is bounded by a constant (not dependent on n) O(n) means that the time is directly proportional to n, and is called linear time. O(n2 ) means quadratic time. O(n3 ) means cubic time. O(2n ) means exponential time. O(log n) means logarithmic time. O(log2 n) means log-squared time.

Algorithm Analyses On a list of length n, sequential search has running time O(n). On a ordered list of length n, binary search has running time O(log n). The sum of the sum of integer index of a loop from 1 to n is O(n 2 ), i.e., 1 + 2 + 3 + + n. For i = 1 to n For j = i to n

Recurrence Relations Recurrence relations are useful in certain counting problems. A recurrence relation relates the n-th element of a sequence to its predecessors. Recurrence relations arise naturally in the analysis of recursive algorithms.

Sequences and Recurrence Relations A (numerical) sequence is an ordered list of number. 2, 4, 6, 8, (positive even numbers) 0, 1, 1, 2, 3, 5, 8, (the Fibonacci numbers) 0, 1, 3, 6, 10, 15, (numbers of key comparisons in selection sort)

Definitions A recurrence relation for the sequence a 0, a1,... is an equation that relates an to certain of its predecessors a0, a1,..., an-1. Initial conditions for the sequence a 0, a1,... are explicitly given values for a finite number of the terms of the sequence.

Example A person invests $1,000 at 12% compounded annually. If An represents the amount at the end of n years, find a recurrence relation and initial conditions that define the sequence An. At the end of n-1 years, the amount is A n-1. After one more year, we will have the amount An-1 plus the interest. Thus An = An-1 + (0.12) An-1 =(1.12) An-1, n 1. To apply this recurrence relation for n = 1, we need to know the value of A0 which is 1,000.

Solving Recurrence Relations Iteration - we use the recurrence relation to write the n-th term an in terms of certain of its predecessors an-1,, a0. We then successively use the recurrence relation to replace each of an-1, by certain of their predecessors. We continue until an explicit formula is obtained.

Some Definitions of Linear Second-order recurrences with constant coefficients kth-order Elements x(n) and x(n-k) are k positions apart in the unknown sequence. Linear It is a linear combination of the unknown terms of the sequence. Constant coefficients Homogeneous If f(x) = 0 for every n. The assumption that a, b, and c are some fixed numbers.

Solving Recurrence Relations Linear homogeneous recurrence relations with constant coefficients - a linear homogeneous recurrence relation of order k with constant coefficients is a recurrence relation of the form a0 = c0, a1 = c1,..., ak-1= ck-1, Notice that a linear homogeneous recurrence relation of order K with constant coefficients, together with the k initial conditions an = c1 an-1 + c2 an-2 +... + ck an-k, c, 0 uniquely defines a sequence a 0, a1,...

Example Nonlinear a n = 3 an-1 an-2. Inhomogeneous a n - an-1 = 2n. Homogeneous recurrence relation with nonconstant coefficients a n = 3 n an-1.

Iteration Example We can solve the recurrence relation a n = an-1 + 3 subject to the initial condition a1 = 2, by iteration. a n-1 = an-2 + 3. a n = an-1 + 3 = an-2 + 3 + 3 = an-2 + 2 x 3. a n-2 = an-3 + 3. a n = an-2 + 2 x 3 = an-3 + 3 + 2 x 3 = an-3 + 3 x 3. a n = an-k + k x 3 = 2 + 3(n - 1).

Iteration Example In general, to solve a n = an-1 + k, a1 = c, one obtains an = c + k(n-1). We can solve the recurrence relation a n = k an-1, a0 = c. a n = k an-1 = k(k an-2) = = k n a0 = c k n.

Linear Homogeneous Recurrence Example an = 5 an-1-6 an-2, a0 = 7, a1 = 16 Since the solution was of the form a n = t n, thus for our first attempt at finding a solution of the second-order recurrence relation, we will search for a solution of the form an = t n. t n = 5 t n-1-6 t n-2 t2-5t + 6 = 0

Example Solving the above we obtain, t = 2, t = 3. S n = 2 n, Tn = 3 n. At this point, we have two solutions S and T given by We can verify that is S and T are solutions of the above, then bs + dt, where b and d are any numbers whatever, is also a solution of the above.

Example In our case, if we define the sequence U by the equation U n = b Sn + d Tn = b 2n + d 3 n To satisfy the initial conditions, we must have 7 = U 0 = b 2 0 + d 3 0 = b + d. 16 = U 1 = b 2 1 + d 3 1 = 2b + 3d.

Example Solving these equations for b and d, we obtain b = 5, d = 2. Therefore, the sequence U defined by Un = 5 x 2n + 2 x 3 n satisfies the recurrence relation and the initial conditions.

Fibonacci Sequence The Fibonacci sequence is defined by the recurrence relation fn = fn-1 + fn-2, n 3 and initial conditions f 1 = 1, f2 = 2. We begin by using the quadratic formula to solve t2 - t - 1 = 0. The solutions are t = 1± 5 2.

Example Thus the solution is of the form! f = b 1+ 5 $ # & n " 2 % n! + d 1 5 $ # & " 2 % n. To satisfy the initial conditions, we must have! b# 1 + 5 $! & + d# 1 5 $ & =1, " 2 % " 2 % b 1 + 5 2! # $ & " 2 % + d 1 5 2! # $ & " 2 % = 2.

Tower of Hanoi Find an explicit formula for an, the minimum number of moves in which the n-disk Tower of Hanoi puzzle can be solved. a n = 2 an-1 + 1, a1 = 1. Applying the iterative method, we obtain a n = 2a n 1 +1 = 2(2a n 2 +1) +1 = 2 2 a n 2 + 2 +1 = 2 2 (2a n 3 +1) + 2 +1 = 2 3 a n 3 + 2 2 + 2 +1 M = 2 n 1 a 1 + 2 n 2 + 2 n 3 +... + 2 +1 = 2 n 1 + 2 n 2 + 2 n 3 +... + 2 +1 = 2 n 1

Common Recurrence Types Decrease-by-one T(n) = T(n-1) + f(n) Decrease-by-a-constant-factor T(n) = T(n/b) + f(n) Divide-and-conquer T(n) = at(n/b) + f(n)