Outline. Part 5. Computa0onal Complexity (3) Compound Interest 2/15/12. Recurrence Rela0ons: An Overview. Recurrence Rela0ons: Formal Defini0on

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

CPS 616 DIVIDE-AND-CONQUER 6-1

Recurrence Relations

Divide and Conquer. Recurrence Relations

ITEC2620 Introduction to Data Structures

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

CS 2210 Discrete Structures Advanced Counting. Fall 2017 Sukumar Ghosh

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

Divide-and-conquer: Order Statistics. Curs: Fall 2017

Design and Analysis of Algorithms

Advanced Counting Techniques. Chapter 8

Topic 17. Analysis of Algorithms

COMP 355 Advanced Algorithms

COMP 355 Advanced Algorithms Algorithm Design Review: Mathematical Background

CS481: Bioinformatics Algorithms

Advanced Counting Techniques

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

Divide and Conquer Algorithms

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

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

Divide and Conquer. Andreas Klappenecker. [based on slides by Prof. Welch]

Divide and Conquer Algorithms

Limitations of Algorithm Power

Discrete Structures Lecture Sequences and Summations

COMP 182 Algorithmic Thinking. Algorithm Efficiency. Luay Nakhleh Computer Science Rice University

CS483 Design and Analysis of Algorithms

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

CSE 421 Algorithms: Divide and Conquer

1 Substitution method

Data Structures and Algorithms

Copyright 2000, Kevin Wayne 1

CSI2101-W08- Recurrence Relations

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

Algorithms and Data Structures

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

Divide and Conquer. Arash Rafiey. 27 October, 2016

Mergesort and Recurrences (CLRS 2.3, 4.4)

CS173 Running Time and Big-O. Tandy Warnow

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

Algorithm Design and Analysis

Divide and Conquer. Andreas Klappenecker

Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018

Data Structures and Algorithms Chapter 3

3. Algorithms. What matters? How fast do we solve the problem? How much computer resource do we need?

CSCI 3110 Assignment 6 Solutions

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

Divide and Conquer: Polynomial Multiplication Version of October 1 / 7, 24201

COMP 9024, Class notes, 11s2, Class 1

CS 577 Introduction to Algorithms: Strassen s Algorithm and the Master Theorem

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

Sec$on Summary. Sequences. Recurrence Relations. Summations. Ex: Geometric Progression, Arithmetic Progression. Ex: Fibonacci Sequence

Discrete Mathematics CS October 17, 2006

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

Divide and Conquer Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 14

Notes for Recitation 14

CMPT 307 : Divide-and-Conqer (Study Guide) Should be read in conjunction with the text June 2, 2015

Algorithms 2/6/2018. Algorithms. Enough Mathematical Appetizers! Algorithm Examples. Algorithms. Algorithm Examples. Algorithm Examples

data structures and algorithms lecture 2

Solving recurrences. Frequently showing up when analysing divide&conquer algorithms or, more generally, recursive algorithms.

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

Solving Recurrences. Lecture 23 CS2110 Fall 2011

Divide & Conquer. Jordi Cortadella and Jordi Petit Department of Computer Science

Data Structures and Algorithms

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

CSCE 222 Discrete Structures for Computing

CS Data Structures and Algorithm Analysis

Recurrences COMP 215

Asymptotic Running Time of Algorithms

Data Structures and Algorithms Chapter 3

Divide and Conquer CPE 349. Theresa Migler-VonDollen

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

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

COMP 382: Reasoning about algorithms

A design paradigm. Divide and conquer: (When) does decomposing a problem into smaller parts help? 09/09/ EECS 3101

Analysis of Algorithms

b + O(n d ) where a 1, b > 1, then O(n d log n) if a = b d d ) if a < b d O(n log b a ) if a > b d

CS2223 Algorithms D Term 2009 Exam 3 Solutions

Recurrence Relations

Analysis of Algorithms

Divide and conquer. Philip II of Macedon

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

Asymptotic Analysis and Recurrences

LECTURE NOTES ON DESIGN AND ANALYSIS OF ALGORITHMS

CSE 373: Data Structures and Algorithms Pep Talk; Algorithm Analysis. Riley Porter Winter 2017

Objec&ves. Review. Divide and conquer algorithms

Recurrence Relations

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

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

CS 310 Advanced Data Structures and Algorithms

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

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

CS383, Algorithms Spring 2009 HW1 Solutions

COMP Analysis of Algorithms & Data Structures

Admin NP-COMPLETE PROBLEMS. Run-time analysis. Tractable vs. intractable problems 5/2/13. What is a tractable problem?

CS 5321: Advanced Algorithms - Recurrence. Acknowledgement. Outline. Ali Ebnenasir Department of Computer Science Michigan Technological University

Divide and Conquer Algorithms

Introduction to Divide and Conquer

CSE 421 Algorithms. T(n) = at(n/b) + n c. Closest Pair Problem. Divide and Conquer Algorithms. What you really need to know about recurrences

COMP Analysis of Algorithms & Data Structures

Introduction. An Introduction to Algorithms and Data Structures

Transcription:

Outline Part 5. Computa0onal Complexity (3) Recurrence Relations Divide and Conquer Understanding the Complexity of Algorithms CS 200 Algorithms and Data Structures 1 2 Recurrence Rela0ons: An Overview Recurrence Rela0ons: Formal Defini0on What is a recurrence relation? A recursively defined sequence Example Arithmetic progression: a, a+d, a+2d,, a+nd a 0 = a a n = a n-1 + d Sequence = Recurrence relation + Initial conditions ( base case ) Example: a n = 2a n-1 + 1, a 1 = 1 3 4 Compound Interest You deposit $10,000 in a savings account that yields 10% yearly interest. How much money will you have after 30 years? (b is balance, r is rate) Recursively defined func0ons and recurrence rela0ons A recursive function f(0) = a (base case) f(n) = f(n-1) + d for n > 0 (recursive part) The above recursively defined function generates the sequence a 0 = a a n = a n-1 + d 5 A recurrence relation produces a sequence, an application of a recursive function produces a value from the sequence 6 1

How to Approach Recursive Rela0ons Solving recurrence rela0ons (1) Recursive Func0ons Subs0tu0on Sequence of Values Solve a 0 = 2; a n = 3a n-1, n > 0 (1) What is the recursive function? (2) What is the sequence of values? f(0) = 0 (base case) f(n) = f(n-1) + 2 for n > 0 (recursive part) Closed Form (solu0on, explicit formula) f(0) = 0 f(1) = f(0) +2 = 2 f(2) = f(1)+2 = 4 f(3) = f(2) +2 = 6 7 Hint 1: Solve by substitution 8 Solving recurrence rela0ons (2) Solve a 0 = 2; a n = 3a n-1 + 2 for n > 0 (1) What is the recursive function? (2) What is the sequence of values? Hint 1: Solve by substitution Solving recurrence rela0ons (3) Solve a 0 = 1; a n = a n-1 + n for n > 0 Hint 1: Solve by substitution Hint 2: Use formula for summing geometric series 1 + r + r 2 + + r n = (r n+1-1)/(r 1) if r 1 9 10 Modeling with Recurrence Linear Recurrence Rela0ons Suppose that the number of bacteria in a colony triples every hour Set up a recurrence relation for the number of bacteria after n hours have elapsed. 100 bacteria are used to begin a new colony. A linear homogeneous recurrence relation of degree k with constant coefficients is a recurrence relation of a form a 0 = c 1 a n-1 + c 2 a n-2 + + c k a n-k where, c 1,c 2,c 3.,c k are real numbers and c k is not 0. 11 12 2

Is this linear homogeneous recurrence rela0on? f n = f n-1 + f n-2 a n = a n-1 + a 2 n-2 Theorem 1 Let c 1 and c 2 a. Suppose that r 2 c 1 r c 2 = 0 has two distinct roots r 1 and r 2. Then the sequence {a n } is a solution of the recurrence relation a n = c 1 a n-1 +c 2 a n-2 if and only if an = α 1 r 1n + α 2 r 2 n for n= 0,1,2, where α 1 and α 2 are constants. (Rosen 6 th edition, section7.2 theorem 1) Modeling of problems They can be systematically solved. 13 14 What is the solu0on of the recurrence rela0on a n = a n-1 + 2a n-2 with a 0 = 2 and a 1 = 7? Outline Recurrence Relations Divide and Conquer Understanding the Complexity of Algorithms 15 16 Divide- and- Conquer Example: Binary Search Basic idea: Take large problem and divide it into smaller problems until problem is trivial, then combine parts to make solution. Recurrence relation for the number of steps required: f(n) = a f(n / b) + g(n) n/b : the size of the sub-problems solved a : number of sub-problems public static int binsearch (int myarray[], int first,!!!!!! int last, int value) {!!// returns the index of value or -1 if not in the array!!int index;!!if (first > last) { index = -1; }!!else {!! int mno = (first + last)/2;!! if (value == myarray[mno]) { index = mno; }!! else if (value < myarray[mno]) {!!! index = binsearch(myarray, first, mno-1, value);! }!!! else {! index = binsearch(myarray, mno+1, last, value);! }!!}!!return index;! } What are a, b, and g(n)? g(n) : steps necessary to combine solutions to subproblems 17 18 3

Es0ma0ng big- O (Master Theorem) Example: Binary Search using the Master Theorem f(n) = a f(n / b) + n d for the binary search algorithm f(n) = a f(n / b) + n d = 1 f(n / 2) + 3 Therefore, d = 0 (to make n d a constant), b = 2, a = 1. b d = 2 0 = 1, It sa0sfies the second condi0on of the Master theorem. From sec)on 8.3 (for 6 th Edi)on 7.3) in Rosen 19 So, f(n) = O(n d log 2 n) = O(n 0 log 2 n) = O(log 2 n) 20 Step- by- step Master Theorem Modeling the divide-conquer with a recurrence relation. f(n) is a recurrence function representing the number of operations with size of input n. _A(array){ Step- by- step Master Theorem _A(array) Call _A(le`_half) A A } _A(le`_half); _A(right_half); Complete_the_(array); Call _A(right_half) A A A A A 21 22 _B(array){ Step- by- step Master Theorem _A(array) Call _B(le`_half) B B } _A(possible_half); Do_nothing_(array); Step- by- step Master Theorem _C(array) Call _C(first_one_thrid) C C Call _C(second_one_thrid) C _C(array){ _C(first_one_third); _C(second_one_third); _C(last_one_third); Complete_method(array); } Call _C(last_one_thrid) Call _A(right_half) C B C C C C C C C C C 23 24 4

LEVEL 1 LEVEL 2 LEVEL 1 Goal 1: What is the complexity per level? Goal 2: What is the total complexity? LEVEL 3 LEVEL 4 25 is called with input size n. calls recursively a 0mes calls recursively with input size n/b 26 LEVEL 1 LEVEL 1 includes so`ware fragment on top of calling methods recursively, and it takes g(n) opera0ons. 27 Therefore, in LEVEL 1, requires, f(n) = a f(n / b) + g(n) 28 LEVEL 2 This is also true for LEVEL 2 f(n) = a f(n / b) + g(n) 29 This is also true for LEVEL i f(n) = a f(n / b) + g(n) LEVEL i 30 5

More generally, Let f be an increasing function that satisfies f (n) = af (n /b) + g(n) = a f (n /b) + n c,if n > 1 f (n) = d,if n =1 whenever n = a b, where a is a positive integer, a 1, b is an integer > 1, and c and d are real numbers with c positive and d nonnegative. Base case We will set d = 1 The bottom level of the tree is equally well computed. Base case It is straightforward to extend the proof for the case when d 1. 31 32 Goal 1. complexity per level Let s think about the recursion tree. There will be levels. At each level, the number of subproblems will be multiplied by a. Therefore, the number of subproblems at level i will be a i. Each subproblem at level i is a problem of size (n/b i ). A subproblem of size (n/b i ) requires (n/b i ) c additional work. levels 33 The total number of units of work on level i a i (n/b i ) c = n c (a i /b ci ) = n c (a/b c ) i For the level i, the work per level levels is decreasing, constant, or increasng exactly when (a/b c ) i is decreasing, constant, or increasing. 34 Observa0on Goal 2: Bounding f(n) in the different cases. Now, observe that, (a/b c ) = 1 a = b c Therefore, the relations, (1) a < b c (2) a = b c, (3) a > b c are the conditions deciding the types of the growth function 35 In general, we have that the total work done is, n c (1) a < b c (2) a = b c (3) a > b c (a/b c ) i = n c ( a /b c ) i 36 6

n c Case 1. a < b c (a/b c ) i = n c ( a/b c ) i n c times a geometric series with a ratio of less than 1. First item is the biggest one. n c ( a /b c ) i = Ο(n c ) n c Case 2. a = b c (a/b c ) i = n c ( a /b c ) i (a/b c ) = 1 n c (1+1+1++1) = n c () n c ( a /b c ) i = Ο(n c log b n) 37 38 n c Case 3. a > b c (a/b c ) i = n c ( a/b c ) i (a/b c ) > 1 Therefore the largest term is the last one. n c (a /b c ) = n c (a /(b c ) ) = n c (n log b a /n log b b c ) = n c (n log b a /n c ) = n log b a n c ( a /b c ) i = Ο(n log b a ) 39 Complexity of MergeSort with Master Theorem (1/2) Mergesort splits a list to be sorted twice per level. Uses fewer than n comparisons to merge the two sorted lists of n/2 items each into one sorted list. Function M(n) satisfying the divide-andconquer recurrence relation M(n) = 2M(n/2) + n 40 Complexity of MergeSort with Master Theorem (2/2) M(n) = 2M(n/2) + n Quicksort? for the mergesort algorithm O n c f(n) = a f(n / b) + n c f (n) = O n c logn = 2 f(n / 2) + n 1 O n log b a Therefore, c = 1, b = 2, a = 2. b d = 2 1 = 2 ( ) if a < b c ( ) if a = b c ( ) if a > b c It sa0sfies the second condi0on of the Master theorem. So, f(n) = O(n c log 2 n) = O(n 1 log 2 n) = O(nlog 2 n) 41 42 7

The Closest- Pair Problem Solu0on (1/5) Consider the problem of determining the closest pair of points in a set of n points (x 1, y 1 )(x n, y n ) in the plain, where the distance between two points (x i, y i ) and (x j, y j ) is the usual Euclidean distance, (x i - x j ) 2 + (y i - y j ) 2 Calculate the distance between every pair of points and then find the smallest of these distances It will take O(n 2 ) 43 44 Solu0on (2/5) Assume that we have n points such that n = 2 k. Solu0on (3/5) l When n = 2, the distance between two points is the minimum distance. d L We sort the points in order of increasing x coordinates, and also sort the points in order of increasing y coordinates using mergesort. It will take O(n log n) Using sorted list by x coordinate, divide the group of points into two (Right and Left) 45 L d Closest pair d d R R 46 Solu0on (4/5) Find the closest pair within R and L. Merge the region R and L by examining the points on the boarder area. Find closest pair between pairs from R and L. (d = min(d r, d l )) Check the points within the strip of the maximum distance of d from the border. Sort the points within the strip with associated y coordinate. Solu0on (5/5) We need at most 7 comparisons for each of the points in the strip. l d/2 d/2 d/2 d/2 d/2 d/2 d/ 2 Beginning with a point in the strip with the smallest y coordinate. 47 d d 48 8

Analysis Outline We find the function f(n) satisfying the recurrence relations f(n) = 2 f(n / 2) + 7n where f(2) = 1 By the master theorem, f(n) =O(n logn) Recurrence Relations Divide and Conquer Understanding the Complexity of Algorithms 49 50 Understanding the Complexity of Algorithm Commonly used terminology for the complexity of algorithms O(1) : Constant complexity O(logn) : Logarithmic complexity O(n) : Linear complexity O(n log n) : Linearithmic complexity O(n b ) : Polynomial complexity O(b n ), where b > 1 : Exponential complexity O(n!) : Factorial complexity 51 Tractability A problem that is solvable using an algorithm with polynomial worst-case complexity is called tractable. If estimation has high degree or if the coefficients are extremely large, the algorithm may take an extremely long time to solve the problem. 52 Intractable problems and Unsolvable problems If the problem cannot be solved using an algorithm with worst-case polynomial time complexity, such problems are called intractable. If it can be shown that no algorithm exists for solving them, such problems are called unsolvable. Class NP and NP- Complete Problems for which a solution can be checked in polynomial time are said to belong to the class NP. Tractable problems belong to class P. NP-complete problems are an important class of problems If any of these problems can be solved by a polynomial worst-case time algorithm then All problems in the class NP can be solved by polynomial worstcase time algorithms. 53 54 9

NP- Hard Next Reading NP-Hard problems are at least as hard as the hardest problems in NP Section 11.1, 11.2 in Prichard 55 56 10