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

Similar documents
Recurrence Relations

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

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

CPS 616 DIVIDE-AND-CONQUER 6-1

Divide and Conquer. Andreas Klappenecker

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

Data Structures and Algorithms Chapter 3

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

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

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

CMPS 2200 Fall Divide-and-Conquer. Carola Wenk. Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk

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

Divide and Conquer. Recurrence Relations

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

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

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

Algorithm Design and Analysis

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

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

Chapter 4 Divide-and-Conquer

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

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

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

Notes for Recitation 14

CS2223 Algorithms D Term 2009 Exam 3 Solutions

The Divide-and-Conquer Design Paradigm

Asymptotic Analysis and Recurrences

Omega notation. Transitivity etc.

Algorithms Chapter 4 Recurrences

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

Analysis of Algorithms - Using Asymptotic Bounds -

CS 4349 Lecture August 30th, 2017

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

CS/COE 1501 cs.pitt.edu/~bill/1501/ Integer Multiplication

Advanced Counting Techniques. Chapter 8

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

Recurrence Relations

Introduction to Algorithms 6.046J/18.401J/SMA5503

A SUMMARY OF RECURSION SOLVING TECHNIQUES

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

Data Structures and Algorithms Chapter 3

Asymptotic Algorithm Analysis & Sorting

Data Structures and Algorithms CMPSC 465

CS 2210 Discrete Structures Advanced Counting. Fall 2017 Sukumar Ghosh

CSC2100B Data Structures Analysis

Design and Analysis of Algorithms

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

Data Structures and Algorithms CSE 465

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

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

CS313H Logic, Sets, and Functions: Honors Fall 2012

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

Analysis of Multithreaded Algorithms

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

CS 470/570 Divide-and-Conquer. Format of Divide-and-Conquer algorithms: Master Recurrence Theorem (simpler version)

Review Of Topics. Review: Induction

Inf 2B: Sorting, MergeSort and Divide-and-Conquer

CSI2101-W08- Recurrence Relations

data structures and algorithms lecture 2

Advanced Counting Techniques

Divide&Conquer: MergeSort. Algorithmic Thinking Luay Nakhleh Department of Computer Science Rice University Spring 2014

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

1 Substitution method

ITEC2620 Introduction to Data Structures

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

Chapter. Divide-and-Conquer. Contents

Analysis of Algorithms Generating Functions II. Holger Findling

Practical Session #3 - Recursions

Chapter 5. Divide and Conquer CLRS 4.3. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Lecture 6: Recurrent Algorithms:

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

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

Divide and Conquer. Arash Rafiey. 27 October, 2016

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

COMP 9024, Class notes, 11s2, Class 1

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures

COMP 355 Advanced Algorithms

Introduction to Divide and Conquer

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

Objective. - mathematical induction, recursive definitions - arithmetic manipulations, series, products

Divide and Conquer CPE 349. Theresa Migler-VonDollen

COMP 355 Advanced Algorithms Algorithm Design Review: Mathematical Background

CS483 Design and Analysis of Algorithms

6.046 Recitation 2: Recurrences Bill Thies, Fall 2004 Outline

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

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

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

COMP 382: Reasoning about algorithms

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

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

Recurrences COMP 215

Data Structures and Algorithms CSE 465

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

Solving Recurrences. Lecture 23 CS2110 Fall 2011

Divide and Conquer Strategy

Principles of Algorithm Analysis

CSCI 3110 Assignment 6 Solutions

CSC236 Intro. to the Theory of Computation Lecture 7: Master Theorem; more D&C; correctness

Design and Analysis of Algorithms

Transcription:

Divide-and-Conquer Algorithms and Recurrence Relations Niloufar Shafiei

Divide-and-conquer algorithms Divide-and-conquer algorithms: 1. Dividing the problem into smaller sub-problems 2. Solving those sub-problems 3. Combining the solutions for those smaller subproblems to solve the original problem Recurrences are used to analyze the computational complexity of divide-and-conquer algorithms. Example: Binary search is a divide-and-conquer algorithm. 1

Divide-and-conquer recurrence Assume a divide-and-conquer algorithm divides a problem of size n into a sub-problems. Assume each sub-problem is of size n/b. Assume f(n) extra operations are required to combine the solutions of sub-problems into a solution of the original problem. Let T(n) be the number of operations required to solve the problem of size n. T(n) = a T(n/b) + f(n) In order to make the recurrence well defined T(n/b) term will actually be either T( n/b ) or T( n/b ). The recurrence will also have to have initial conditions. (e.g. T(1) or T(0)) 2

Binary search: Search for 18 T(n) = T( n/2 ) + 2 T(1) = 2 1 3 5 12 13 18 20 12<18 1 3 5 12 13 18 20 18=18 1 3 5 12 13 18 20 Determine which half of the list to choose and determine whether any terms of the list remain 3

Finding the maximum and minimum of a sequence: Consider the sequence a 1, a 2,, a n. If n=1, then a 1 is the maximum and the minimum. If n>1, split the sequence into two sequences, either where both have the same number of elements or where one of the sequences has one more element than the other. The problem is reduced to finding the maximum and minimum of each of the two smaller sequences. The solution to the original problem results from the comparison of the separate maxima and minima of the two smaller sequences. Find a recurrence T(n) that be the number of operations required to solve the problem of size n. Solution: The problem of size n can be reduced into two problems of size n/2 and n/2. Two comparisons are required to find the original solution from those sub-problems. T(1) = 1 T(n) = T( n/2 ) + T( n/2 ) + 2 4

Merge sort: The merge sort algorithm splits a list with n elements into two list with n/2 and n/2 elements. (The list with 1 element is considered sorted.) It uses less than n comparison to merge two sorted lists of n/2 and n/2 elements. Find a recurrence T(n) that represents the number of operations required to solve the problem of size n. (The merge sort may have less operations than T(n)) Solution: The problem of size n can be reduced into two problems of size n/2 and n/2. n comparisons are required to find the original solution from those subproblems. T(1) = 0 T(n) = T( n/2 ) + T( n/2 ) +n 5

Master theorem There is a theorem that gives asymptotic behavior of any sequence defined by a divide-and-conquer recurrence with f(n)=c.n d for constants c>0 and d 0. This theorem is sometimes called the master theorem. 6

Master theorem Theorem: Assume a sequence is defined by a recurrence equation T(n) = at(n/b) + cn d for n>1, where a 1, b 2, c>0 and d 0 are constants and n/b is either n/b or n/b, then one of the following holds. T(n) = (n d ) T(n) = (n d log n) T(n) = (n log ba ) if a < b d if a = b d if a > b d 7

Assume an algorithm behavior is determined using the following recurrence.give big-theta estimate for its complexity. T(1) = 1 T(n) = T( n/2 ) + 4, for n 2 Solution: a=1, b=2, c=4 and d=0 So, a = b d = 1. By Master theorem, T(n) = (n d log n) = (log n). 8

Assume an algorithm behavior is determined using the following recurrence.give big-theta estimate for its complexity. T(1) = 0 T(n) = T( n/2 ) + T( n/2 ) + 1, for n 2 Solution: a=2, b=2, c=1 and d=0 So, a > b d = 1. By Master theorem, T(n) = (n log b a ) = (n log 2 2 ) = (n). 9

Assume an algorithm behavior is determined using the following recurrence.give big-theta estimate for its complexity. x 0 = 0 x n = 7x n/5 + 9n 2, for n 1 Solution: a=7, b=5, c=9 and d=2 So, a < b d = 25. By Master theorem, T(n) = (n d ) = (n 2 ). 10

Give big-theta estimate for the merge sort algorithm. Solution: By previous example, we know the following recurrence represent the merge sort algorithm. T(1) = 0 T(n) = T( n/2 ) + T( n/2 ) +n for n>1. a=2, b=2, c=1 and d=1 So, a = b d = 2. By Master theorem, T(n) = (n d log n) = (n log n). 11

Give big-theta estimate for following recurrence. f(n) = 2f( ) + 1 when a perfect square n >1, f(2)=1. (Hint: assume m = log 2 n.) Solution: m = log 2 n 2 m = n f(2 m ) = 2 f((2 m ) 1/2 ) + 1 f(2 m ) = 2 f(2 m/2 ) + 1 g(m) = 2 g(m/2) + 1 a=2, b=2, c=1 and d=0 So, a > b d = 1. By Master theorem, g(m) = (m log b a ) = (m log 2 2 ) = (m). So, f(n) = (log 2 n). 12

Recommended exercises 7, 17,21,22 Eric Ruppert s Notes about Solving Recurrences (http://www.cse.yorku.ca/course_archive/2007-08/f/1019/a/recurrence.pdf) 13