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

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

CPS 616 DIVIDE-AND-CONQUER 6-1

Divide and Conquer. Recurrence Relations

Linear Time Selection

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

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

Asymptotic Analysis and Recurrences

Divide-Conquer-Glue Algorithms

Algorithm Design and Analysis

Introduction to Divide and Conquer

Divide and Conquer Algorithms

COMP 382: Reasoning about algorithms

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

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

Divide and Conquer. Andreas Klappenecker

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

CS 2110: INDUCTION DISCUSSION TOPICS

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

Algorithms And Programming I. Lecture 5 Quicksort

CS313H Logic, Sets, and Functions: Honors Fall 2012

Divide and Conquer Algorithms

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

Divide-and-conquer. Curs 2015

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) {

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

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

Mergesort and Recurrences (CLRS 2.3, 4.4)

Notes for Recitation 14

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

CS483 Design and Analysis of Algorithms

CSE 613: Parallel Programming. Lecture 8 ( Analyzing Divide-and-Conquer Algorithms )

MAKING A BINARY HEAP

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

COMP Analysis of Algorithms & Data Structures

Divide-and-Conquer. Consequence. Brute force: n 2. Divide-and-conquer: n log n. Divide et impera. Veni, vidi, vici.

Solving Recurrences. Lecture 23 CS2110 Fall 2011

COMP Analysis of Algorithms & Data Structures

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

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

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

Lecture 4. Quicksort

MAKING A BINARY HEAP

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

COMP 355 Advanced Algorithms

Chapter 4 Divide-and-Conquer

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

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

COMP 355 Advanced Algorithms Algorithm Design Review: Mathematical Background

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

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

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

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

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

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

Recurrences COMP 215

5. DIVIDE AND CONQUER I

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

Loop Invariants and Binary Search. Chapter 4.4, 5.1

Lecture 2: Divide and conquer and Dynamic programming

Data Structures and Algorithms Chapter 3

Week 5: Quicksort, Lower bound, Greedy

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

CS 231: Algorithmic Problem Solving

Design and Analysis of Algorithms

CSE 613: Parallel Programming. Lectures ( Analyzing Divide-and-Conquer Algorithms )

Analysis of Algorithms - Using Asymptotic Bounds -

Divide and Conquer CPE 349. Theresa Migler-VonDollen

CSC236 Week 4. Larry Zhang

5. DIVIDE AND CONQUER I

CS Analysis of Recursive Algorithms and Brute Force

Objec&ves. Review. Divide and conquer algorithms

Recurrence Relations

Divide-and-Conquer. Reading: CLRS Sections 2.3, 4.1, 4.2, 4.3, 28.2, CSE 6331 Algorithms Steve Lai

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

Divide and Conquer. Arash Rafiey. 27 October, 2016

CS483 Design and Analysis of Algorithms

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

Design and Analysis of Algorithms Recurrence. Prof. Chuhua Xian School of Computer Science and Engineering

Data Structures and Algorithms CMPSC 465

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

Chapter 1 Divide and Conquer Polynomial Multiplication Algorithm Theory WS 2015/16 Fabian Kuhn

Divide-and-Conquer Algorithms Part Two

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

Omega notation. Transitivity etc.

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

Central Algorithmic Techniques. Iterative Algorithms

ITEC2620 Introduction to Data Structures

Recursion. Algorithms and Data Structures. (c) Marcin Sydow. Introduction. Linear 2nd-order Equations. Important 3 Cases. Quicksort Average Complexity

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

Data Structures and Algorithms CSE 465

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

Divide and Conquer Strategy

data structures and algorithms lecture 2

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

CS173 Running Time and Big-O. Tandy Warnow

CSE 421 Algorithms: Divide and Conquer

CS Data Structures and Algorithm Analysis

Section 5.3 Divide and Conquer Relations. 0,a m. - Division into thirds gives m = 3. Apply the telescoping technique described in Section 5.2.

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

Transcription:

CSC236 Intro. to the Theory of Computation Lecture 7: Master Theorem; more D&C; correctness Amir H. Chinaei, Fall 2016 Office Hours: W 2-4 BA4222 ahchinaei@cs.toronto.edu http://www.cs.toronto.edu/~ahchinaei/ Course page: http://www.cdf.toronto.edu/~csc236h/fall/index.html Section page: http://www.cdf.toronto.edu/~csc236h/fall/amir_lectures.html Recurrences 7-1

review Last week application of recurrence relations to complexity of d&c algorithms in particular mergesort this week master theorem closestpair recursive correctness Recurrences 7-2

Example 63: mergesort calculating a lower bound T n c n log n Keep in mind: T( n ) T(n) T( n) 2 and T n = n log n + 2 n 1 T n T n 2 = n 2 log n 2 +2 n 2 1 = n 2 (log n log 2) + n 1 = n log n + n 1 2 2 n log n + n 1 2 2 n log n 2 c = 1 2 n 2 Recurrences and D&C 7-3

Example 63: mergesort calculating an upper bound T n c n log n Keep in mind: T( n ) T(n) T( n) 2 and T n = n log n + 2 n 1 T n T n = n log n +2 n 1 2n log 2n +2.2n 1 = 2n (log 2 + log n) +4n 1 = 2n log n +6n 1 2n log n +6n 2n log n +6n log n 8n log n c = 8 n 2 Recurrences and D&C 7-4

general d&c and master theorem D&C algorithms normally divide a problem of size n to a smaller problems of size n where a > 0, b > 1 N b Let g(x) denote the recombining cost (conquer), such that the corresponding recurrence relation is T n = 1 at n b n B N + g x n > B N When g x = cn d, c > 0 d 0 R θ n d if a < b d T n θ n d log n if a = b d θ n log b a if a > b d Recurrences and D&C 7-5

notes: Recurrences and D&C 7-6

Example 64: closestpair d(p 1, p 2 ) = x 1 x 2 2 + (y 1 y 2 ) 2 10 9 8 7 6 5 4 3 2 1 p 1 p 2 p 4 p 3 p 5 p 7 p 6 p 8 p 9 Brute Force p 10 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 min= Recurrences and D&C 7-7

Example 64: closestpair s = [set of points] d(p 1, p 2 ) = x 1 x 2 2 + (y 1 y 2 ) 2 Brute Force def closest_pair1(frm,to): min_d = d(s[0],s[1]) for i in range(to-frm): for j in range(i+1,to-frm+1): dist = d(s[i],s[j]) if dist < min_d: min_d = dist return min_d Recurrences and D&C 7-8

Example 64: closestpair d(p 1, p 2 ) = x 1 x 2 2 + (y 1 y 2 ) 2 10 Divide & Conquer 9 8 p 5 7 p 1 p 9 6 p 4 p 7 5 4 3 2 p 2 p 6 p 8 p 10 1 p 3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Recurrences and D&C 7-9

Example 64: closestpair s = [set of points] d(p 1, p 2 ) = x 1 x 2 2 + (y 1 y 2 ) 2 def closest_pairdc0(frm, to): if to-frm > 1: mid = (frm+to)//2 half1 = closest_pairdc0(frm, mid) half2 = closest_pairdc0(mid+1, to) c = min(half1,half2) border_min = border_bf(mid, c) return min(c, border_min) else: return d(s[frm],s[to]) Recurrences and D&C 7-10

analysis: Recurrences and D&C 7-11

Example 64: closestpair d(p 1, p 2 ) = x 1 x 2 2 + (y 1 y 2 ) 2 10 9 8 7 c p 1 c p 5 Divide & Conquer C: closest distance in both halves In this example: 2.8 6 p 4 p 7 5 4 3 2 p 2 p 6 p 8 1 p 3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Recurrences and D&C 7-12

Example 64: closestpair d(p 1, p 2 ) = x 1 x 2 2 + (y 1 y 2 ) 2 s = [set of points, sorted by x-coordinates] def closest_pairdc1(frm, to): if to-frm > 1: mid = (frm+to)//2 half1 = closest_pairdc1(frm, mid) half2 = closest_pairdc1(mid+1, to) c = min(half1,half2) mergesort(s, on y-coordinates) border_min = border_n(mid, c) return min(closest, border_min) else: return d(s[frm],s[to]) Recurrences and D&C 7-13

analysis: Recurrences and D&C 7-14

Example 64: closestpair d(p 1, p 2 ) = x 1 x 2 2 + (y 1 y 2 ) 2 s = [set of points, sorted by x-coordinates] def closest_pairdc(frm, to): if to-frm > 1: mid = (frm+to)//2 half1 = closest_pairdc(frm, mid) half2 = closest_pairdc(mid+1, to) c = min(half1,half2) merge(half1, half2, y-coordinates) border_min = border_n(mid, c) return min(closest, border_min) else: return d(s[frm],s[to]) Recurrences and D&C 7-15

analysis: Recurrences and D&C 7-16

programs correctness Recently, we saw the application of induction in asymptotic analysis in particular, in the worst case time complexity of recursive algorithms Let s move on to see application of induction in a more important topic: correctness of recursive algorithms followed by correctness of iterative algorithms, next week correctness 7-17

Example 73: correctness of binsearch (from Example 61) def binsearch(x, A, b, e): 1 if b == e: 2 if x == A[b]: 3 return b 4 else: 5 return -1 6 else: 7 m = (b + e) // 2 # midpoint 8 if x <= A[m]: 9 return binsearch(x, A, b, m) 10 else: 11 return binsearch(x, A, m+1, e) correctness 7-18

how to devise the correctness proof: define the pre- & post- conditions for the algorithm preconditions: conditions that the algorithm s input should satisfy postconditions: conditions that should be satisfied after the algorithm has run then, show: preconditions postconditions correctness 7-19

Example 73: correctness of binsearch: binsearch(x, A, b, e): preconditions: elements of A (from b to e) are sorted nondecreasingly elements of A and x are comparable 0 b e Len(A) = n = e b + 1 postconditions: binsearch(x, A, b, e) terminates and returns p such that b p e and x = A[p] if such a p exists; otherwise returns 1. correctness 7-20

Example 73: correctness of binsearch: We want to show: preconditions postconditions P(n): if 0 b e where A[b.. e] is non-decreasing and Len A = n = e b + 1, and x is comparable to elements of A, the call to binsearch(x,a,b,e) terminates and returns p such that b p e and x = A[p] if such a p exists; otherwise returns 1. Proof by complete induction. Basis step: Inductive step: correctness 7-21

proof by induction: correctness 7-22

proof by induction: correctness 7-23

notes: correctness 7-24