Chapter 22 Developing Efficient Algorithms

Similar documents
Algorithm Analysis. Algorithms that are equally correct can vary in their utilization of computational resources

Advanced Course of Algorithm Design and Analysis

CS583 Lecture 02. Jana Kosecka. some materials here are based on E. Demaine, D. Luebke slides

2.4 - Sequences and Series

Algorithm Analysis. Chapter 3

Ch3. Asymptotic Notation

Model of Computation and Runtime Analysis

Classification of problem & problem solving strategies. classification of time complexities (linear, logarithmic etc)

Analysis of Algorithms. Introduction. Contents

Model of Computation and Runtime Analysis

Data Structures and Algorithm. Xiaoqing Zheng

Design and Analysis of ALGORITHM (Topic 2)

A recurrence equation is just a recursive function definition. It defines a function at one input in terms of its value on smaller inputs.

Sums, products and sequences

COMP285 Midterm Exam Department of Mathematics

CS 270 Algorithms. Oliver Kullmann. Growth of Functions. Divide-and- Conquer Min-Max- Problem. Tutorial. Reading from CLRS for week 2

Recursive Algorithms. Recurrences. Recursive Algorithms Analysis

Chapter 2. Asymptotic Notation

4.3 Growth Rates of Solutions to Recurrences

CSI 5163 (95.573) ALGORITHM ANALYSIS AND DESIGN

Sorting Algorithms. Algorithms Kyuseok Shim SoEECS, SNU.

CS / MCS 401 Homework 3 grader solutions

is also known as the general term of the sequence

Test One (Answer Key)

Examples: data compression, path-finding, game-playing, scheduling, bin packing

2. ALGORITHM ANALYSIS

CS 332: Algorithms. Linear-Time Sorting. Order statistics. Slide credit: David Luebke (Virginia)

Mathematical Foundation. CSE 6331 Algorithms Steve Lai

Divide & Conquer. Divide-and-conquer algorithms. Conventional product of polynomials. Conventional product of polynomials.

Lecture 3: Asymptotic Analysis + Recurrences

CS284A: Representations and Algorithms in Molecular Biology

Data Structures Lecture 9

Sequences, Mathematical Induction, and Recursion. CSE 2353 Discrete Computational Structures Spring 2018

COMP26120: Introducing Complexity Analysis (2018/19) Lucas Cordeiro

Chapter 6. Advanced Counting Techniques

CIS 121 Data Structures and Algorithms with Java Spring Code Snippets and Recurrences Monday, February 4/Tuesday, February 5

Unit 6: Sequences and Series

Analysis of Algorithms -Quicksort-

Topic 1 2: Sequences and Series. A sequence is an ordered list of numbers, e.g. 1, 2, 4, 8, 16, or

Recurrence Relations

CS161 Design and Analysis of Algorithms. Administrative

CSE 4095/5095 Topics in Big Data Analytics Spring 2017; Homework 1 Solutions

) n. ALG 1.3 Deterministic Selection and Sorting: Problem P size n. Examples: 1st lecture's mult M(n) = 3 M ( È

Shannon s noiseless coding theorem

SECTION 1.5 : SUMMATION NOTATION + WORK WITH SEQUENCES

This Lecture. Divide and Conquer. Merge Sort: Algorithm. Merge Sort Algorithm. MergeSort (Example) - 1. MergeSort (Example) - 2

CS:3330 (Prof. Pemmaraju ): Assignment #1 Solutions. (b) For n = 3, we will have 3 men and 3 women with preferences as follows: m 1 : w 3 > w 1 > w 2

OPTIMAL ALGORITHMS -- SUPPLEMENTAL NOTES

CS161: Algorithm Design and Analysis Handout #10 Stanford University Wednesday, 10 February 2016

MAT1026 Calculus II Basic Convergence Tests for Series

Section 6.4: Series. Section 6.4 Series 413

62. Power series Definition 16. (Power series) Given a sequence {c n }, the series. c n x n = c 0 + c 1 x + c 2 x 2 + c 3 x 3 +

Matriculation number: You have 90 minutes to complete the exam of InformatikIIb. The following rules apply:

Merge and Quick Sort

CHAPTER 10 INFINITE SEQUENCES AND SERIES

Average-Case Analysis of QuickSort

The Quest for Efficiency

An Introduction to Randomized Algorithms

Department of Informatics Prof. Dr. Michael Böhlen Binzmühlestrasse Zurich Phone:

INFINITE SEQUENCES AND SERIES

Parallel Vector Algorithms David A. Padua

ITEC 360 Data Structures and Analysis of Algorithms Spring for n 1

10.1 Sequences. n term. We will deal a. a n or a n n. ( 1) n ( 1) n 1 2 ( 1) a =, 0 0,,,,, ln n. n an 2. n term.

Math 475, Problem Set #12: Answers

Introduction to Algorithms 6.046J/18.401J LECTURE 3 Divide and conquer Binary search Powering a number Fibonacci numbers Matrix multiplication

Problems with Solutions in the Analysis of Algorithms. Minko Markov

PH 425 Quantum Measurement and Spin Winter SPINS Lab 1

Intensive Algorithms Lecture 11. DFT and DP. Lecturer: Daniel A. Spielman February 20, f(n) O(g(n) log c g(n)).

Design and Analysis of Algorithms

Once we have a sequence of numbers, the next thing to do is to sum them up. Given a sequence (a n ) n=1

Infinite Sequences and Series

Sequence A sequence is a function whose domain of definition is the set of natural numbers.

COMP26120: More on the Complexity of Recursive Programs (2018/19) Lucas Cordeiro

Algorithms and Data Structures Lecture IV

COMPUTING SUMS AND THE AVERAGE VALUE OF THE DIVISOR FUNCTION (x 1) + x = n = n.

Symbolic computation 2: Linear recurrences

Chapter 10: Power Series

Olli Simula T / Chapter 1 3. Olli Simula T / Chapter 1 5

Sequences. Notation. Convergence of a Sequence

SEQUENCE AND SERIES NCERT

Math 2784 (or 2794W) University of Connecticut

Sample Size Estimation in the Proportional Hazards Model for K-sample or Regression Settings Scott S. Emerson, M.D., Ph.D.

Sequences, Sums, and Products

The picture in figure 1.1 helps us to see that the area represents the distance traveled. Figure 1: Area represents distance travelled

Recursive Algorithm for Generating Partitions of an Integer. 1 Preliminary

CHAPTER 1 SEQUENCES AND INFINITE SERIES

x a x a Lecture 2 Series (See Chapter 1 in Boas)

Skip lists: A randomized dictionary

w (1) ˆx w (1) x (1) /ρ and w (2) ˆx w (2) x (2) /ρ.

Module 18 Discrete Time Signals and Z-Transforms Objective: Introduction : Description: Discrete Time Signal representation

6.3 Testing Series With Positive Terms

Divide and Conquer. 1 Overview. 2 Multiplying Bit Strings. COMPSCI 330: Design and Analysis of Algorithms 1/19/2016 and 1/21/2016

Roger Apéry's proof that zeta(3) is irrational

MISCELLANEOUS SEQUENCES & SERIES QUESTIONS

Random Variables, Sampling and Estimation

Design and Analysis of Algorithms

ENGI Series Page 6-01

XT - MATHS Grade 12. Date: 2010/06/29. Subject: Series and Sequences 1: Arithmetic Total Marks: 84 = 2 = 2 1. FALSE 10.

Series: Infinite Sums

Lecture 6 Chi Square Distribution (χ 2 ) and Least Squares Fitting

Transcription:

Chapter Developig Efficiet Algorithms 1

Executig Time Suppose two algorithms perform the same task such as search (liear search vs. biary search). Which oe is better? Oe possible approach to aswer this questio is to implemet these algorithms i Java ad ru the programs to get executio time. But there are two problems for this approach: First, there are may tasks ruig cocurretly o a computer. The executio time of a particular program is depedet o the system load. Secod, the executio time is depedet o specific iput. Cosider liear search ad biary search for example. If a elemet to be searched happes to be the first i the list, liear search will fid the elemet quicker tha biary search.

Growth Rate It is very difficult to compare algorithms by measurig their executio time. To overcome these problems, a theoretical approach was developed to aalyze algorithms idepedet of computers ad specific iput. This approach approximates the effect of a chage o the size of the iput. I this way, you ca see how fast a algorithm s executio time icreases as the iput size icreases, so you ca compare two algorithms by examiig their growth rates. 3

Big O Notatio Cosider liear search. The liear search algorithm compares the key with the elemets i the array sequetially util the key is foud or the array is exhausted. If the key is ot i the array, it requires comparisos for a array of size. If the key is i the array, it requires / comparisos o average. The algorithm s executio time is proportioal to the size of the array. If you double the size of the array, you will expect the umber of comparisos to double. The algorithm grows at a liear rate. The growth rate has a order of magitude of. Computer scietists use the Big O otatio to abbreviate for order of magitude. Usig this otatio, the complexity of the liear search algorithm is O(), proouced as order of. 4

Best, Worst, ad Average Cases For the same iput size, a algorithm s executio time may vary, depedig o the iput. A iput that results i the shortest executio time is called the best-case iput ad a iput that results i the logest executio time is called the worst-case iput. Best-case ad worst-case are ot represetative, but worst-case aalysis is very useful. You ca show that the algorithm will ever be slower tha the worst-case. A average-case aalysis attempts to determie the average amout of time amog all possible iput of the same size. Average-case aalysis is ideal, but difficult to perform, because it is hard to determie the relative probabilities ad distributios of various iput istaces for may problems. Worst-case aalysis is easier to obtai ad is thus commo. So, the aalysis is geerally coducted for the worst-case. 5

Igorig Multiplicative Costats The liear search algorithm requires comparisos i the worst-case ad / comparisos i the average-case. Usig the Big O otatio, both cases require O() time. The multiplicative costat (1/) ca be omitted. Algorithm aalysis is focused o growth rate. The multiplicative costats have o impact o growth rates. The growth rate for / or 100 is the same as, i.e., O() = O(/) = O(100). f() / 100 100 100 50 10000 00 00 100 0000 f(00) / f(100) 6

Igorig No-Domiatig Terms Cosider the algorithm for fidig the maximum umber i a array of elemets. If is, it takes oe compariso to fid the maximum umber. If is 3, it takes two comparisos to fid the maximum umber. I geeral, it takes -1 times of comparisos to fid maximum umber i a list of elemets. Algorithm aalysis is for large iput size. If the iput size is small, there is o sigificace to estimate a algorithm s efficiecy. As grows larger, the part i the expressio -1 domiates the complexity. The Big O otatio allows you to igore the o-domiatig part (e.g., -1 i the expressio -1) ad highlight the importat part (e.g., i the expressio -1). So, the complexity of this algorithm is O(). 7

8 Useful Mathematic Summatios 1 1... 1 1... 1) ( 1) (... 3 1 1 1) ( 3 1 0 1 1) ( 3 1 0 = = = a a a a a a a a

Examples: Determiig Big-O Repetitio Sequece Selectio Logarithm 9

Repetitio: Simple Loops executed times for (i = 1; i <= ; i) { k = k 5; costat time Time Complexity T() = (a costat c) * = c = O() Igore multiplicative costats (e.g., c ). 10

Repetitio: Nested Loops executed times for (i = 1; i <= ; i) { for (j = 1; j <= ; j) { k = k i j; costat time ier loop executed times Time Complexity T() = (a costat c) * * = c = O( ) Igore multiplicative costats (e.g., c ). 11

Repetitio: Nested Loops executed times Time Complexity for (i = 1; i <= ; i) { for (j = 1; j <= i; j) { k = k i j; costat time T() = c c 3c 4c c = c(1)/ = (c/) (c/) = O( ) Igore o-domiatig terms Igore multiplicative costats ier loop executed i times 1

Repetitio: Nested Loops executed times for (i = 1; i <= ; i) { for (j = 1; j <= 0; j) { k = k i j; costat time ier loop executed 0 times Time Complexity T() = 0 * c * = O() Igore multiplicative costats (e.g., 0*c) 13

Sequece executed 10 times executed times for (j = 1; j <= 10; j) { k = k 4; for (i = 1; i <= ; i) { for (j = 1; j <= 0; j) { k = k i j; ier loop executed 0 times Time Complexity T() = c *10 0 * c * = O() 14

O() Selectio if (list.cotais(e)) { System.out.pritl(e); else for (Object t: list) { System.out.pritl(t); Let be list.size(). Executed times. Time Complexity T() = test time worst-case (if, else) = O() O() = O() 15

Costat Time The Big O otatio estimates the executio time of a algorithm i relatio to the iput size. If the time is ot related to the iput size, the algorithm is said to take costat time with the otatio O(1). For example, a method that retrieves a elemet at a give idex i a array takes costat time, because it does ot grow as the size of the array icreases. 16

Commo Recurrece Relatios Recurrece Relatio Result Example = T( / ) O(1) = O(log ) Biary search, Euclid s GCD = T ( 1) O(1) = O( ) Liear search = T( / ) O(1) = O( ) = T( / ) O( ) = O( log) Merge sort (Chapter 4) = T( / ) O( log) = O( log ) = T ( 1) O( ) = O( ) Selectio sort, isertio sort = T( 1) O(1) = O( ) Towers of Haoi = T( 1) T( ) O(1) = O( ) Recursive Fiboacci algorithm 17

Comparig Commo Growth Fuctios 3 O (1) < O(log) < O( ) < O( log ) < O( ) < O( ) < O( ) O(1) O(log) O() O( log) O( ) O( 3 ) O( ) Costat time Logarithmic time Liear time Log-liear time Quadratic time Cubic time Expoetial time 18

Comparig Commo Growth Fuctios 3 O (1) < O(log) < O( ) < O( log ) < O( ) < O( ) < O( ) O( ) O( ) O(log) O() O(log) O(1) 19

Practical Cosideratios The big O otatio provides a good theoretical estimate of algorithm efficiecy. However, two algorithms of the same time complexity are ot ecessarily equally efficiet. Liag, Itroductio to Java Programmig, Teth Editio, (c) 013 Pearso Educatio, 0 Ic. All