CS483 Design and Analysis of Algorithms

Similar documents
CS483 Design and Analysis of Algorithms

Fast Convolution; Strassen s Method

CS Analysis of Recursive Algorithms and Brute Force

Divide and Conquer algorithms

Asymptotic Analysis and Recurrences

Divide and Conquer Strategy

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

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

1 Substitution method

Divide and conquer. Philip II of Macedon

FFT: Fast Polynomial Multiplications

Divide and Conquer. Arash Rafiey. 27 October, 2016

Divide and Conquer. Recurrence Relations

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

Divide and Conquer CPE 349. Theresa Migler-VonDollen

Recurrence Relations

CS Non-recursive and Recursive Algorithm Analysis

Divide and Conquer Algorithms

The divide-and-conquer strategy solves a problem by: 1. Breaking it into subproblems that are themselves smaller instances of the same type of problem

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

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

Kartsuba s Algorithm and Linear Time Selection

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

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

Divide and Conquer Algorithms

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

COMP 382: Reasoning about algorithms

Analysis of Algorithms - Using Asymptotic Bounds -

Recurrence Relations

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

CPS 616 DIVIDE-AND-CONQUER 6-1

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

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

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

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

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

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

data structures and algorithms lecture 2

CS 2210 Discrete Structures Advanced Counting. Fall 2017 Sukumar Ghosh

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

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

Mergesort and Recurrences (CLRS 2.3, 4.4)

Algorithm efficiency analysis

Divide and Conquer. Andreas Klappenecker

COMP Analysis of Algorithms & Data Structures

Chapter 5 Divide and Conquer

Methods for solving recurrences

Divide-and-Conquer and Recurrence Relations: Notes on Chapter 2, Dasgupta et.al. Howard A. Blair

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

Lecture 2: Divide and conquer and Dynamic programming

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

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

CSCI 3110 Assignment 6 Solutions

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

Practical Session #3 - Recursions

Chapter 1 Divide and Conquer Algorithm Theory WS 2016/17 Fabian Kuhn

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

The divide-and-conquer strategy solves a problem by: Chapter 2. Divide-and-conquer algorithms. Multiplication

CS325: Analysis of Algorithms, Fall Midterm

Divide and Conquer. Maximum/minimum. Median finding. CS125 Lecture 4 Fall 2016

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

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

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

Copyright 2000, Kevin Wayne 1

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

Introduction to Algorithms 6.046J/18.401J/SMA5503

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

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

Class Note #14. In this class, we studied an algorithm for integer multiplication, which. 2 ) to θ(n

Review Of Topics. Review: Induction

DIVIDE AND CONQUER II

Reductions, Recursion and Divide and Conquer

Chapter 4 Divide-and-Conquer

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

Data Structures and Algorithms Chapter 3

COMP 9024, Class notes, 11s2, Class 1

CMPSCI611: Three Divide-and-Conquer Examples Lecture 2

COMP Analysis of Algorithms & Data Structures

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

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

CS 2110: INDUCTION DISCUSSION TOPICS

Data Structures and Algorithms Chapter 3

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

COMP Analysis of Algorithms & Data Structures

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

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

CS 231: Algorithmic Problem Solving

CSI2101-W08- Recurrence Relations

Design and Analysis of Algorithms

CS483 Design and Analysis of Algorithms

CS483 Design and Analysis of Algorithms

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

CS 161 Summer 2009 Homework #2 Sample Solutions

1 Divide and Conquer (September 3)

4.4 Recurrences and Big-O

CS483 Design and Analysis of Algorithms

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

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

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

Transcription:

CS483 Design and Analysis of Algorithms Lecture 6-8 Divide and Conquer Algorithms Instructor: Fei Li lifei@cs.gmu.edu with subject: CS483 Office hours: STII, Room 443, Friday 4:00pm - 6:00pm or by appointments Course web-site: http://www.cs.gmu.edu/ lifei/teaching/cs483 fall08/ Figures unclaimed are from books Algorithms and Introduction to Algorithms 1 / 49

Divide and Conquer Algorithms 1 Breaking the problem into subproblems of the same type 2 Recursively solving these subproblems 3 Appropriately combining their answers 2 / 49

Divide-and-Conquer Recurrence 3 / 49

Divide-and-Conquer Recurrence Divide the problems into b smaller instances; a of them need to be solved. f (n) is the time spent on dividing and merging 4 / 49

Divide-and-Conquer Recurrence Divide the problems into b smaller instances; a of them need to be solved. f (n) is the time spent on dividing and merging T (n) = a T (n/b) + f (n) 5 / 49

Divide-and-Conquer Recurrence Divide the problems into b smaller instances; a of them need to be solved. f (n) is the time spent on dividing and merging 1 The iteration method T (n) = a T (n/b) + f (n) Expand (iterate) the recurrence and express it as a summation of terms depending only on n and the initial conditions 2 The substitution method Guess the form of the solution Use mathematical induction to find the constants 3 Master Theorem (T (n) = a T (n/b) + f (n)) 6 / 49

Iteration Method Examples n! Tower of Hanoi T (n) = T (n 1) + 1 http://en.wikipedia.org/wiki/tower of Hanoi T (n) = 2 T (n 1) + 1 7 / 49

Iteration Example n! (T (n) = T (n 1) + 1) T (n) = T (n 1) + 1 = (T (n 2) + 1) + 1 = T (n 2) + 2 = T (n i) + i = T (0) + n = n Tower of Hanoi (T (n) = 2 T (n 1) + 1)? 8 / 49

Iteration Example Tower of Hanoi (T (n) = 2 T (n 1) + 1) T (n) = 2 T (n 1) + 1 = 2 (2 T (n 2) + 1) + 1 = 2 2 T (n 2) + 2 + 1 = 2 i T (n i) + 2 i 1 + + 1 = 2 n 1 T (1) + 2 n 1 + 2 n 1 + + 1 n 2 = 2 n 1 T (1) + 2 i i=0 = 2 n 1 + 2 n 1 1 = 2 n 1 9 / 49

Substitution Method Count Number of Bits Count number of bits (T (n) = T ( n/2 ) + 1) 10 / 49

Substitution Method Count Number of Bits Count number of bits (T (n) = T ( n/2 ) + 1) Guess T (n) log n. T (n) = T ( n/2 ) + 1 log( n/2 ) + 1 log(n/2) + 1 (log n log 2) + 1 log n 1 + 1 = log n 11 / 49

Substitution Method Tower of Hanoi Tower of Hanoi (T (n) = 2 T (n 1) + 1) 12 / 49

Substitution Method Tower of Hanoi Tower of Hanoi (T (n) = 2 T (n 1) + 1) Guess T (n) 2 n T (n) = 2 T (n 1) + 1 2 2 n 1 + 1 2 n + 1, wrong! 13 / 49

Substitution Method Tower of Hanoi Tower of Hanoi (T (n) = 2 T (n 1) + 1) Guess T (n) 2 n T (n) = 2 T (n 1) + 1 2 2 n 1 + 1 2 n + 1, wrong! Guess T (n) 2 n 1 T (n) = 2 T (n 1) + 1 2 (2 n 1 1) + 1 = 2 n 2 + 1 = 2 n 1, correct! 14 / 49

Substitution Method Extension F n Fibonacci Numbers (F 0 = 0, F 1 = 1, F n = F n 1 + F n 2 ) 15 / 49

Substitution Method Extension F n Fibonacci Numbers (F 0 = 0, F 1 = 1, F n = F n 1 + F n 2 ) F n 2 < F n 1 < F n, n 1 16 / 49

Substitution Method Extension F n Fibonacci Numbers (F 0 = 0, F 1 = 1, F n = F n 1 + F n 2 ) F n 2 < F n 1 < F n, n 1 Assume 2 n 1 < F n < 2 n Guess F n = c φ n, 1 < φ < 2 17 / 49

Substitution Method Extension F n Fibonacci Numbers (F 0 = 0, F 1 = 1, F n = F n 1 + F n 2 ) F n 2 < F n 1 < F n, n 1 Assume 2 n 1 < F n < 2 n Guess F n = c φ n, 1 < φ < 2 c φ n = c φ n 1 + c φ n 2 φ 2 = φ + 1 φ = 1 ± 5 2 18 / 49

Substitution Method Extension F n Fibonacci Numbers (F 0 = 0, F 1 = 1, F n = F n 1 + F n 2 ) F n 2 < F n 1 < F n, n 1 Assume 2 n 1 < F n < 2 n Guess F n = c φ n, 1 < φ < 2 c φ n = c φ n 1 + c φ n 2 φ 2 = φ + 1 φ = 1 ± 5 2 General solution: F n = c 1 φ n 1 + c 2 φ n 2 F 1 = 0, F 2 = 1 F n = 1 ( 1 + 5 ) n 1 ( 1 5 ) n 5 2 5 2 19 / 49

Master Theorem T (n) = a T (n/b) + f (n) 20 / 49

Master Theorem T (n) = a T (n/b) + f (n), a 1, b > 1 be constants. Theorem We interpret n/b to mean either n/b or n/b. If f (n) Θ(n d ), where d 0, then Θ(n d ) T (n) = Θ(n d log n) Θ(n log b a ) if a < b d if a = b d if a > b d T (n) = Θ(f (n)) if f (n) = Ω(n log b a+ɛ ) and if a f (n/b) c f (n) for some constant c < 1 and all sufficiently large n Θ(n log b a log n) if f (n) = Θ(n log b a ) Θ(n log b a ) if f (n) = O(n log b a ɛ ) for some constant ɛ > 0 1 T (n) = 4 T (n/2) + n =? 2 T (n) = 4 T (n/2) + n 2 =? 3 T (n) = 4 T (n/2) + n 3 =? 21 / 49

Chapter 2 of DPV Divide and Conquer Algorithms 1 Multiplication 2 Mergesort 3 Medians 4 Matrix Multiplication 5 The Fast Fourier Transform 22 / 49

An Unanswered Question 23 / 49

Multiplication (a + b) (c + d) = a c + a d + b c + b d (a + b i) (c + d i) = a c b d + (a d + b c) i = a c b d + [(a + b) (c + d) a c b d] i T (n) = 3 T (n/2) + O(n) = O(n log 2 3 ) 24 / 49

Mergesort 25 / 49

Mergesort Given an array of n numbers, sort the elements in non-decreasing order 26 / 49

Mergesort Given an array of n numbers, sort the elements in non-decreasing order function mergesort(a[n]) if (n = 1) return A; else B = A[1,..., n 2 ]; C = A[ n 2 + 1,..., n]; mergesort(b); mergesort(a); merge(b, C, A); 27 / 49

Mergesort Given an array of n numbers, sort the elements in non-decreasing order function mergesort(a[n]) if (n = 1) return A; else B = A[1,..., n 2 ]; C = A[ n 2 + 1,..., n]; mergesort(b); mergesort(a); merge(b, C, A); Is this algorithm complete? 28 / 49

Mergesort Merge two sorted arrays, B and C and put the result in A function merge(b[1,.. p], C[1,.. q], A[1,.. p + q]) i = 1; j = 1; for (k = 1, 2,.. p + 1) if (B[i] < C[j]) A[k] = B[i]; i = i + 1; else A[k] = C[j]; j = j + 1; 24, 11, 91, 10, 22, 32, 22, 3, 7, 99 T (n) = 2 T (n/2) + O(n) = O(n log n) 29 / 49

Medians The median is a single representative value of a list of numbers: half of them are larger and half of them are smaller; less sensitive to outliers Selection problem: S := {2, 36, 5, 21, 8, 13, 11, 20, 5, 4, 1} Input: A list of number S; an integer k. Output: The k-th smallest element of S. selection(s, 8) =? 30 / 49

Medians The median is a single representative value of a list of numbers: half of them are larger and half of them are smaller; less sensitive to outliers Let us split at v = 5 S := {2, 36, 5, 21, 8, 13, 11, 20, 5, 4, 1} S L = {2, 4, 1} S v = {5, 5} S R = {36, 21, 8, 13, 11, 20} selection(s, 8) = selection(s R, 3). selection(s, k) = selection(s L, k) if k S L v if S L < k S L + S v selection(s R, k S L S v ) if k > S L + S v. 31 / 49

Medians The median is a single representative value of a list of numbers: half of them are larger and half of them are smaller; less sensitive to outliers S := {2, 36, 5, 21, 8, 13, 11, 20, 5, 4, 1} selection(s, 8) = selection(s R, 3). selection(s, k) = selection(s L, k) if k S L v if S L < k S L + S v selection(s R, k S L S v ) if k > S L + S v. If S L S R (i.e., pick up v to be the median), Pick up v randomly from S T (n) = T (n/2) + O(n) 32 / 49

Medians v is good if it lies within 25% and 75% of the array it is chosen Lemma On average a fair coin needs to be tossed two times before a heads is seen Proof. E = 1 + 1 2 E Remark v has 50% chance of being in-between [25%, 75%]. We need to pick v twice to make it good Theorem T (n) T ((3/4) n) + O(n) = O(n) 33 / 49

Matrix Multiplication 34 / 49

Matrix Multiplication 1 Matrix Multiplication (by definition): [ ] [ ] [ ] C11 C 12 A11 A 12 B11 B 12 = C 21 C 22 A 21 A 22 B 21 B 22 [ ] A11 B 11 + A 12 B 21 A 11 B 12 + A 12 B 22 = A 21 B 11 + A 22 B 21 A 21 B 12 + A 12 B 22 35 / 49

Matrix Multiplication 1 Matrix Multiplication (by definition): [ ] [ ] [ ] C11 C 12 A11 A 12 B11 B 12 = C 21 C 22 A 21 A 22 B 21 B 22 [ ] A11 B 11 + A 12 B 21 A 11 B 12 + A 12 B 22 = A 21 B 11 + A 22 B 21 A 21 B 12 + A 12 B 22 2 T (n) = 8 T ( n 2 ) + O(n) = O(n3 ) Time complexity of the brute-force algorithm is O(n 3 ) 36 / 49

Matrix Multiplication 1 Strassen s Matrix Multiplication: [ ] [ ] [ ] C11 C 12 A11 A 12 B11 B 12 = C 21 C 22 A 21 A 22 B 21 B 22 [ ] m1 + m 4 m 5 + m 7 m 3 + m 5 = m 2 + m 4 m 1 + m 3 m 2 + m 6 m 1 = (A 11 + A 22 ) (B 11 + B 22 ) m 2 = (A 21 + A 22 ) B 11 m 3 = A 11 (B 12 B 22 ) m 4 = A 22 (B 21 B 11 ) m 5 = (A 11 + A 12 ) B 22 m 6 = (A 21 A 11 ) (B 11 + B 12 ) m 7 = (A 12 A 22 ) (B 21 + B 22 ) 37 / 49

Matrix Multiplication 1 Strassen s Matrix Multiplication: [ ] [ ] [ ] C11 C 12 A11 A 12 B11 B 12 = C 21 C 22 A 21 A 22 B 21 B 22 [ ] m1 + m 4 m 5 + m 7 m 3 + m 5 = m 2 + m 4 m 1 + m 3 m 2 + m 6 m 1 = (A 11 + A 22 ) (B 11 + B 22 ) m 2 = (A 21 + A 22 ) B 11 m 3 = A 11 (B 12 B 22 ) m 4 = A 22 (B 21 B 11 ) m 5 = (A 11 + A 12 ) B 22 m 6 = (A 21 A 11 ) (B 11 + B 12 ) m 7 = (A 12 A 22 ) (B 21 + B 22 ) 2 T (n) = 7 T ( n 2 ) + O(n) = O(log nlog 2 7 ) O(n 2.81 ) Time complexity of the brute-force algorithm is O(n 3 ) 38 / 49

Fourier Transform Fourier Series: Any periodic function can be expressed as the sum of a series of sines and cosines (of varying amplitudes) http://www.cs.ucl.ac.uk/teaching/gz05/03-fourier.pdf 39 / 49

Fourier Transform A function f (x) can be expressed as a series of sines and cosines. Fourier Series can be generalized to complex numbers, and further generalized to derive the Fourier Transform f (x) = 1 + a n cos(n x) + b n sin(n x) a 0 a 0 = 1 π a n = 1 π b n = 1 π n=1 π π π π π π f (x)dx f (x) cos(n x)dx f (x) sin(n x)dx e x i = cos(x) + i sin(x) F (k) = f (k) = f (x) e 2 π i k dk F (x) e 2 π i k dk n=1 40 / 49

Discrete Fourier Transform 1 Fourier Transform maps a time series (e.g., audio samples) into the series of frequencies (their amplitudes and phases) that composed the time series 2 Inverse Fourier Transform maps the series of frequencies (their amplitudes and phases) back into the corresponding time series 3 The two functions are inverses of each other F n = N 1 f k e 2 π i n k/n k=0 f k = 1 N 1 N F n e 2 π i k n/n n=0 41 / 49

Fast Fourier Transform 1 Polynomial n 1 A(x) = a i x i = a 0 + a 1 x + a 2 x 2 +... + a n 1 x n 1 i=0 Fox any distinct points x 0, x 1,..., x n 1, we can specify A(x) by (1.) a 0, a 1,..., a n 1 or (2.) A(x 0 ), A(x 1 ),..., A(x n 1 ) 42 / 49

Fast Fourier Transform 1 Polynomial n 1 A(x) = a i x i = a 0 + a 1 x + a 2 x 2 +... + a n 1 x n 1 i=0 Fox any distinct points x 0, x 1,..., x n 1, we can specify A(x) by (1.) a 0, a 1,..., a n 1 or (2.) A(x 0 ), A(x 1 ),..., A(x n 1 ) 2 Horner s Rule A(x) = a 0 + x (a 1 + x (a 2 +... + x (a n 2 + x a n 1 )...)) 43 / 49

Fast Fourier Transform 1 Polynomial n 1 A(x) = a i x i = a 0 + a 1 x + a 2 x 2 +... + a n 1 x n 1 i=0 Fox any distinct points x 0, x 1,..., x n 1, we can specify A(x) by (1.) a 0, a 1,..., a n 1 or (2.) A(x 0 ), A(x 1 ),..., A(x n 1 ) 2 Horner s Rule A(x) = a 0 + x (a 1 + x (a 2 +... + x (a n 2 + x a n 1 )...)) 3 Given coefficients (a 0, a 1,..., a n 1 ) and (b 0, b 1,..., b n 1 ), compute A(x) B(x) Horner s Rule does not work since n 1 C(x) := A(x) B(x) = c i x i, where c i = i=0 i a j b i j j=0 44 / 49

Fourier Transform Theorem For any set {(x 0, y 0 ), (x 1, y 1 ),..., (x n 1, y n 1 )} of n point-value pair such that all the x k values are distinct, there is a unique polynomial A(x) of degree-bound n such that y k = A(x k ) for k = 0, 1,..., n 1 Proof.? Remark C(x) = A(x) B(x) z, C(z) = A(z) B(z) C(x) has degree 2 n 2, it is determined by its values at any 2 n 1 points. The value at any given point z is A(z) B(z). Polynomial multiplication takes linear time in the value representation 45 / 49

46 / 49

Fast Fourier Transform 1 A number ω is a primitive n-th root of unity, for n > 1, if 1 ω n = 1 2 The numbers 1, ω, ω 2,..., ω n 1 are all distinct 2 The complex number e 2 π i/n is a primitive n-th root of unity, where i = 1 Inverse Property If ω is a primitive root of unity, ω 1 = ω n 1 Cancelation Property For non-zero n < k < n, n 1 j=0 ωk j = 0 Reduction Property If ω is a primitive 2n-th root of unity, then ω 2 is a primitive n-th root of unity Reflective Property If n is even, then ω n/2 = 1 47 / 49

Fast Fourier Transform Use Divide-and-Conquer Approach to Calculate C(x) = A(x) B(x) First step: calculate A(ω 0 ),..., A(ω n 1 ) 48 / 49

Fast Fourier Transform Implementation 49 / 49