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

Similar documents
Today s Outline. CS 362, Lecture 4. Annihilator Method. Lookup Table. Annihilators for recurrences with non-homogeneous terms Transformations

CS 361, Lecture 14. Outline

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

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

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

Analysis of Algorithms - Using Asymptotic Bounds -

CS173 Running Time and Big-O. Tandy Warnow

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

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Asymptotic Analysis, recurrences Date: 9/7/17

Data structures Exercise 1 solution. Question 1. Let s start by writing all the functions in big O notation:

Data Structures and Algorithms CSE 465

Methods for solving recurrences

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures

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

Data Structures and Algorithms Chapter 3

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

Solving recurrences. Frequently showing up when analysing divide&conquer algorithms or, more generally, recursive 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

Lecture 3. Big-O notation, more recurrences!!

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

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

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

CS Non-recursive and Recursive Algorithm Analysis

Growth of Functions (CLRS 2.3,3)

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

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

Chapter 2. Recurrence Relations. Divide and Conquer. Divide and Conquer Strategy. Another Example: Merge Sort. Merge Sort Example. Merge Sort Example

Chapter 4 Divide-and-Conquer

Asymptotic Analysis and Recurrences

Recurrence Relations

Homework 1 Submission

1 Substitution method

Computational Complexity

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

data structures and algorithms lecture 2

Divide and Conquer. Arash Rafiey. 27 October, 2016

Algorithm efficiency analysis

CS 5321: Advanced Algorithms Analysis Using Recurrence. Acknowledgement. Outline

4.4 Recurrences and Big-O

CSCI 3110 Assignment 6 Solutions

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

CS 61B Asymptotic Analysis Fall 2017

Algorithms Chapter 4 Recurrences

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

CSCE 222 Discrete Structures for Computing. Review for Exam 2. Dr. Hyunyoung Lee !!!

Today s Outline. CS 362, Lecture 13. Matrix Chain Multiplication. Paranthesizing Matrices. Matrix Multiplication. Jared Saia University of New Mexico

Chapter 4. Recurrences

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

Mathematical Induction. Rosen Chapter 4.1,4.2 (6 th edition) Rosen Ch. 5.1, 5.2 (7 th edition)

CS F-01 Algorithm Analysis 1

Computational Complexity. This lecture. Notes. Lecture 02 - Basic Complexity Analysis. Tom Kelsey & Susmit Sarkar. Notes

COMP 9024, Class notes, 11s2, Class 1

Design Patterns for Data Structures. Chapter 3. Recursive Algorithms

CS Analysis of Recursive Algorithms and Brute Force

Algorithms Design & Analysis. Analysis of Algorithm

COL106: Data Structures and Algorithms (IIT Delhi, Semester-II )

Divide and Conquer CPE 349. Theresa Migler-VonDollen

University of New Mexico Department of Computer Science. Midterm Examination. CS 361 Data Structures and Algorithms Spring, 2003

CIS 121. Analysis of Algorithms & Computational Complexity. Slides based on materials provided by Mary Wootters (Stanford University)

Analysis of Multithreaded Algorithms

CS161: Algorithm Design and Analysis Recitation Section 3 Stanford University Week of 29 January, Problem 3-1.

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

Review Of Topics. Review: Induction

CS 4104 Data and Algorithm Analysis. Recurrence Relations. Modeling Recursive Function Cost. Solving Recurrences. Clifford A. Shaffer.

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

Divide-and-Conquer Algorithms Part Two

CS 2110: INDUCTION DISCUSSION TOPICS

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

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 3. Θ Notation. Comparing Algorithms

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

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

Lecture 2: Asymptotic Notation CSCI Algorithms I

CS Data Structures and Algorithm Analysis

Omega notation. Transitivity etc.

CS473 - Algorithms I

EECS 477: Introduction to algorithms. Lecture 5

Lecture 10: Big-Oh. Doina Precup With many thanks to Prakash Panagaden and Mathieu Blanchette. January 27, 2014

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Outline. 1 Introduction. Merging and MergeSort. 3 Analysis. 4 Reference

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

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

COE428 Notes Week 4 (Week of Jan 30, 2017)

CSC236 Week 4. Larry Zhang

INF2220: algorithms and data structures Series 1

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

CS383, Algorithms Spring 2009 HW1 Solutions

Lecture 3: Big-O and Big-Θ

Fall 2016 Test 1 with Solutions

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 2

Divide & Conquer. CS 320, Fall Dr. Geri Georg, Instructor CS320 Div&Conq 1

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Dynamic Programming II Date: 10/12/17

Introduction to Divide and Conquer

Analysis of Algorithms

Data Structures and Algorithms CMPSC 465

Analysis of Algorithms - Midterm (Solutions)

Mathematical Induction

Divide and Conquer. Andreas Klappenecker

Divide and Conquer. Recurrence Relations

CS 4349 Lecture August 30th, 2017

Transcription:

In-Class Soln 1 Let f(n) be an always positive function and let g(n) = f(n) log n. Show that f(n) = o(g(n)) CS 361, Lecture 4 Jared Saia University of New Mexico For any positive constant c, we want to show there is a n 0 > 0 such that 0 f(n) < cg(n) for all n n 0. In other words, we want to show that there is n 0 > 0 such that 0 f(n) < cf(n) log n Dividing by f(n) log n, we get: 0 f(n) f(n) log n < c f(n) We know that lim n f(n) log n = lim n log 1 n = 0. Thus, for any constant c, there must be a n 0 such that the above inequality is satisfied for all n n 0. 2 Today s Outline In-Class Soln 2 Let f(n) be an always positive function and let g(n) = f(n) log n. Show that f(n) = o(g(n)) Recurrence Relations 1 For any positive constant c, we want to show there is a n 0 > 0 such that 0 f(n) < cg(n) for all n n 0. In other words, we want to show that there is n 0 > 0 such that 0 f(n) < cf(n) log n Dividing by f(n) log n, we get: 1 log n < c (1/c) < log n 2 1/c < n Thus, for any c, if we choose n 0 > 2 1/c, for all n n 0, f(n) < cg(n) 3

Asymptotic Analysis - Take Away Alg: Binary Search In studying behavior of algorithms, we ll be more concerned with rate of growth than with constants O, Θ, Ω, o, ω give us a way to talk about rates of growth Asymptotic analysis is an extremely useful way to compare run times of algorithms However, empirical analysis is also important (you ll be studying this in your project) bool BinarySearch (int arr[], int s, int e, int key){ if (e-s<=0) return false; int mid = (e-s)/2; if (arr[key]==arr[mid]){ return true; }else if (key < arr[mid]){ return BinarySearch (arr,s,mid,key);} else{ return BinarySearch (arr,mid,e,key)} } 4 6 Recurrence Relations What? Getting the run times of recursive algorithms can be challenging Recall our algorithm for binary search Let T (n) be the run time of this algorithm on an array of size n Then we can write T (1) = 1, T (n) = T (n/2) + 1 T (n) is a function giving the run time of Binary Search on an array of size n T (n) = T (n/2) + 1 is a an example of a recurrence relation A Recurrence Relation is any equation for a function T (n), where T (n) appears on both the left and right sides of the equation. We always want to solve these recurrence relation by getting an equation for T (n), where T appears on just the left side of the equation 5 7

Use of Recurrences Alg1 We can use recurrence relations to analyze many properties of recursive algorithms e.g. run time, value returned, etc. To do this we need to: 1) write down the correct recurrence relation 2) solve the recurrence relation Step 1 is usually easier than step 2 Alg1 (int n){ if (n<=1) return 1; else return Alg1(n/2) + Alg1(n/2) + n; } 8 10 A Side Note Example1 The running time of an algorithm on a constant size input is always Θ(1) Thus for convenience, we frequently omit statements of the boundary conditions and just assume T (n) is constant when n is a constant. Example: Instead of saying If n = 1, T (n) = θ(1), and if T (n) = 2 T (n/2) + Θ(n), we just say T (n) = 2 T (n/2) + Θ(n) Let T (n) be the run time of Alg1 on input n Then we can write T (n) = 2T (n/2) + 1 Let f(n) be the value returned by Alg1 on input n Then we can write f(n) = 2f(n/2) + n and f(1) = 1 9 11

What now? Example To get the real run time or value returned, we need to solve the recurrence relation This means that no function appear on the right hand side We will review several techniques for solving recurrences including: the substitution method, recursion trees, the Master method, and annihilators Let s guess that the solution to T (n) = 2 T (n/2) + n is T (n) = O(n log n) We want to show that T (n) cn log n for appropriate choice of constant c We can prove this by induction. 12 14 Substitution Method Proof Base Case: T (2) c 2 Inductive Hypothesis: For all j < n, T (j) cj log j Inductive Step: One way to solve recurrences is the substitution method aka guess and check What we do is make a good guess for the solution to T (n), and then try to prove this is the solution by induction T (n) = 2T (n/2) + n (1) 2(cn/2 log(n/2)) + n (2) cn log(n/2) + n (3) = cn(log n log 2) + n (4) = cn log n cn + n (5) cn log n (6) The last step holds if c 1 13 15

Recurrences and Induction Sum Problem Recurrences and Induction are closely related: To find some solution to f(n), solve a recurrence To prove that a solution for f(n) is correct, use induction f(n) is the sum of the integers 1,..., n For both recurrences and induction, we always solve a big problem by reducing it to smaller problems! 16 18 Some Examples Tree Problem f(n) is the maximum number of leaf nodes in a binary tree of height n The next several problems can be attacked by induction/recurrences For each problem, we ll need to reduce it to smaller problems Question: How can we reduce each problem to a smaller subproblem? Recall: In a binary tree, each node has at most two children A leaf node is a node with no children The height of a tree is the length of the longest path from the root to a leaf node. 17 19

Binary Search Problem Simpler Subproblems f(n) is the maximum number of queries that need to be made for binary search on a sorted array of size n. Sum Problem: What is the sum of all numbers between 1 and n 1 (i.e. f(n 1))? Tree Problem: What is the maximum number of leaf nodes in a binary tree of height n 1? (i.e. f(n 1)) Binary Search Problem: What is the maximum number of queries that need to be made for binary search on a sorted array of size n/2? (i.e. f(n/2)) Dominoes problem: What is the number of ways to tile a 2 by n 1 rectangle with dominoes? What is the number of ways to tile a 2 by n 2 rectangle with dominoes? (i.e. f(n 1), f(n 2)) 20 22 Dominoes Problem Recurrences f(n) is the number of ways to tile a 2 by n rectangle with dominoes (a domino is a 2 by 1 rectangle) Sum Problem: f(n) = f(n 1) + n, f(1) = 1 Tree Problem: f(n) = 2 f(n 1), f(0) = 1 Binary Search Problem: f(n) = f(n/2) + 1, f(2) = 1 Dominoes problem: f(n) = f(n 1) + f(n 2), f(1) = 1, f(2) = 1 21 23

Guesses Sum Problem Sum Problem: f(n) = (n + 1)n/2 Tree Problem: f(n) = 2 n Binary Search Problem: f(n) = log n Dominoes problem: f(n) = (1+ 1 5 5 2 ) n 1 5 (1 5 2 ) n Want to show that f(n) = (n + 1)n/2. Prove by induction on n Base case: f(1) = 2 1/2 = 1 Inductive hypothesis: for all j < n, f(j) = (j + 1)j/2 Inductive step: f(n) = f(n 1) + n (7) = n(n 1)/2 + n (8) = (n + 1)n/2 (9) 24 26 Inductive Proofs Tree Problem Trying is the first step to failure - Homer Simpson Now that we ve made these guesses, we can try using induction to prove they re correct (the substitution method) We ll give inductive proofs that these guesses are correct for the first three problems Want to show that f(n) = 2 n. Prove by induction on n Base case: f(0) = 2 0 = 1 Inductive hypothesis: for all j < n, f(j) = 2 j Inductive step: f(n) = 2 f(n 1) (10) = 2 (2 n 1 ) (11) = 2 n (12) 25 27

Binary Search Problem Todo Want to show that f(n) = log n. (assume n is a power of 2) Prove by induction on n Base case: f(2) = log 2 = 1 Inductive hypothesis: for all j < n, f(j) = log j Inductive step: f(n) = f(n/2) + 1 (13) = log n/2 + 1 (14) = log n log 2 + 1 (15) = log n (16) Read Chapter 4 (Recurrences) in text 28 30 In Class Exercise Consider the recurrence f(n) = 2f(n/2) + 1, f(1) = 1 Guess that f(n) cn 1: Q1: Show the base case - for what values of c does it hold? Q2: What is the inductive hypothesis? Q3: Show the inductive step. 29