Format of Divide-and-Conquer algorithms:

Similar documents
The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Many algorithms, particularly divide and conquer algorithms, have time complexities which are naturally

5.4 Closest Pair of Points

0.4 FACTORING POLYNOMIALS

Algorithm Analysis [2]: if-else statements, recursive algorithms. COSC 2011, Winter 2004, Section N Instructor: N. Vlajic

Analysis of Algorithms I: Optimal Binary Search Trees

Data Structures and Algorithms

Biostatistics 615/815

Analysis of Binary Search algorithm and Selection Sort algorithm

Algorithm Design and Analysis Homework #1 Due: 5pm, Friday, October 4, 2013 TA === Homework submission instructions ===

CS473 - Algorithms I

Algorithms. Margaret M. Fleck. 18 October 2010

Closest Pair of Points. Kleinberg and Tardos Section 5.4

Closest Pair Problem

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

Linear Equations in One Variable

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Appendix: Solving Recurrences [Fa 10] Wil Wheaton: Embrace the dark side! Sheldon: That s not even from your franchise!

Near Optimal Solutions

Math 115 Spring 2011 Written Homework 5 Solutions

HW4: Merge Sort. 1 Assignment Goal. 2 Description of the Merging Problem. Course: ENEE759K/CMSC751 Title:

Algorithm Design and Recursion

Converting a Number from Decimal to Binary

Dynamic Programming. Lecture Overview Introduction

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

1 = (a 0 + b 0 α) (a m 1 + b m 1 α) 2. for certain elements a 0,..., a m 1, b 0,..., b m 1 of F. Multiplying out, we obtain

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

CSE 421 Algorithms: Divide and Conquer

Zabin Visram Room CS115 CS126 Searching. Binary Search

DIVISIBILITY AND GREATEST COMMON DIVISORS

Co-ordinate Geometry THE EQUATION OF STRAIGHT LINES

Factoring Polynomials

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

Data Structures and Algorithms Written Examination

CM2202: Scientific Computing and Multimedia Applications General Maths: 2. Algebra - Factorisation

What Is Recursion? Recursion. Binary search example postponed to end of lecture

Factoring Polynomials

Binary Heap Algorithms

Using the ac Method to Factor

28 Closest-Point Problems

Analysis of a Search Algorithm

Computer Science 210: Data Structures. Searching

4. How many integers between 2004 and 4002 are perfect squares?

6. Standard Algorithms

Online EFFECTIVE AS OF JANUARY 2013

The Prime Numbers. Definition. A prime number is a positive integer with exactly two positive divisors.

8.1. Cramer s Rule for Solving Simultaneous Linear Equations. Introduction. Prerequisites. Learning Outcomes. Learning Style

1.3 Polynomials and Factoring

Binary Multiplication


CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Divide-and-conquer algorithms

Cubic Functions: Global Analysis

FOPR-I1O23 - Fundamentals of Programming

Complex Numbers. w = f(z) z. Examples

Efficiency of algorithms. Algorithms. Efficiency of algorithms. Binary search and linear search. Best, worst and average case.

CHAPTER 5. Number Theory. 1. Integers and Division. Discussion

BEGINNING ALGEBRA ACKNOWLEDMENTS

Figure 1: Graphical example of a mergesort 1.

CIRCLE COORDINATE GEOMETRY

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

csci 210: Data Structures Recursion

Recursive Algorithms. Recursion. Motivating Example Factorial Recall the factorial function. { 1 if n = 1 n! = n (n 1)! if n > 1

The Greatest Common Factor; Factoring by Grouping

THE NUMBER OF REPRESENTATIONS OF n OF THE FORM n = x 2 2 y, x > 0, y 0

Section 9.5: Equations of Lines and Planes

Solutions Manual for How to Read and Do Proofs

calculating the result modulo 3, as follows: p(0) = = 1 0,

1.4 Compound Inequalities

Vector Notation: AB represents the vector from point A to point B on a graph. The vector can be computed by B A.

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Zeros of a Polynomial Function

3.5 RECURSIVE ALGORITHMS

Advanced GMAT Math Questions

CSC 180 H1F Algorithm Runtime Analysis Lecture Notes Fall 2015

The Method of Partial Fractions Math 121 Calculus II Spring 2015

11 Multivariate Polynomials

COMPUTER SCIENCE. Paper 1 (THEORY)

CS 103X: Discrete Structures Homework Assignment 3 Solutions

( ) FACTORING. x In this polynomial the only variable in common to all is x.

C H A P T E R Regular Expressions regular expression

Factoring Patterns in the Gaussian Plane

Shortest Path Algorithms

Linear algebra and the geometry of quadratic equations. Similarity transformations and orthogonal matrices

Creating a More Efficient Course Schedule at WPI Using Linear Optimization

Chapter 17. Orthogonal Matrices and Symmetries of Space

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Recursion vs. Iteration Eliminating Recursion

Mathematical Induction. Lecture 10-11

Chapter 13: Query Processing. Basic Steps in Query Processing

Section IV.1: Recursive Algorithms and Recursion Trees

MATH Fundamental Mathematics IV

SUM OF TWO SQUARES JAHNAVI BHASKAR

Introduction. The Quine-McCluskey Method Handout 5 January 21, CSEE E6861y Prof. Steven Nowick

Algebra Cheat Sheets

Applications of Fermat s Little Theorem and Congruences

Transcription:

CS 360: Data Structures and Algorithms Divide-and-Conquer (part 1) Format of Divide-and-Conquer algorithms: Divide: Split the array or list into smaller pieces Conquer: Solve the same problem recursively on smaller pieces Combine: Build the full solution from the recursive results

Merge sort: 0 1 2 3 4 5 6 7 26 48 17 31 50 9 21 16 Split 26 48 17 31 50 9 21 16 Split Split 26 48 17 31 50 9 21 16 Split Split Split Split 26 48 17 31 50 9 21 16 Merge Merge Merge Merge 26 48 17 31 9 50 16 21 Merge Merge 17 26 31 48 9 16 21 50 Merge 9 16 17 21 26 31 48 50

MergeSort (A[ ], low, high) { // low=0, high=7, n=high low+1 if (low == high) return; mid = (low+high)/2; MergeSort (A, low, mid); MergeSort (A, mid+1, high); Merge (A, low, mid, high); } Merge (A[ ], low, mid, high) { // some details omitted allocate array B[ ] of size n; repeat n times { compare the next values from each half of A; copy the smaller of these two values from A to B; } repeat n times copy the next value from B to A; } Analysis of merge sort: Merge function takes θ(n) time, where n=high low+1 Merge sort has lg n levels of recursive calls All the merges at any given level take θ(n) total time So θ(n lg n) total time for all merges at all levels

Merge sort: Divide: Split array of size n into two subarrays of size n/2 Conquer: Two recursive calls on subarrays of size n/2 Combine: Merge two sorted subarrays to create sorted array of size n Let T(n) = running time of merge sort on array of size n T(n) = T divide (n) + T conquer (n) + T combine (n) T(n) = θ(1) + [T(n/2) + T(n/2)] + θ(n) T(n) = 2 T(n/2) + θ(n) Number of recursive subproblems = 2 Size of each subproblem = n/2 Time for all the non-recursive steps = θ(n) Later we ll see how to solve these kinds of recurrence equations to determine T(n) The solution for T(n) = 2 T(n/2) + θ(n) is T(n) = θ(n lg n)

Binary search: Find(27) 0 1 2 3 4 5 6 7 8 9 10 13 18 24 27 34 41 49 55 60 66 72 0 1 2 3 4 13 18 24 27 34 27 > 24 3 4 27 34 found 27 < 41 Let T(n) = running time of binary search on array of size n T(n) = T divide (n) + T conquer (n) + T combine (n) T(n) = θ(1) + T(n/2) + 0 T(n) = T(n/2) + θ(1) Number of recursive subproblems = 1 Size of each subproblem = n/2 Time for all the non-recursive steps = θ(1) Later we ll see how to solve these kinds of recurrence equations to determine T(n) The solution for T(n) = T(n/2) + θ(1) is T(n) = θ(lg n)

Polynomial multiplication and Large integer multiplication: Given two polynomials: P = 5x 3 + 7x 2 + 6x + 2 [5, 7, 6, 2] = [p 3, p 2, p 1, p 0 ] Q = x 3 8x 2 + 9x 1 [1, 8, 9, 1] = [q 3, q 2, q 1, q 0 ] P*Q = (5x 3 + 7x 2 + 6x + 2)(x 3 8x 2 + 9x 1) Alternatively, given two large integers: P = 9863 = 9x 3 + 8x 2 + 6x + 3 where x = 10 Q = 7245 = 7x 3 + 2x 2 + 4x + 5 where x = 10 P*Q = (9863)(7245) = (9x 3 + 8x 2 + 6x + 3)(7x 3 + 2x 2 + 4x + 5) where x = 10 So large integer multiplication is a special case of polynomial multiplication, where x = the base in which the numbers are represented

Divide-and-conquer Algorithm 1 for Polynomial multiplication: P = 5x 3 + 7x 2 + 6x + 2 = (5x + 7)x 2 + (6x + 2) = Ax 2 + B Q = x 3 8x 2 + 9x 1 = (x 8)x 2 + (9x 1) = Cx 2 + D A = 5x + 7 [5, 7] B = 6x + 2 [6, 2] C = x 8 [1, 8] D = 9x 1 [9, 1] Let n = number of terms in P and Q P = Ax n/2 + B Q = Cx n/2 + D A, B, C, D are polynomials with n/2 terms P*Q = (Ax n/2 + B)(Cx n/2 + D) = (AC)x n + (BC + AD)x n/2 + (BD) Four recursive subproblems: A*C B*C A*D B*D Stop the recursion when n = 1

Let T(n) = running time for the preceding Algorithm 1 T(n) = 4 T(n/2) + θ(n) Number of recursive subproblems = 4 Size of each subproblem = n/2 Time for all the non-recursive steps = θ(n): o adding n-term polynomials o x n and x n/2 which are just shifts (not products) o also note the final product P*Q has < 2n terms Again, later we ll see how to solve these kinds of recurrence equations to determine T(n) The solution for T(n) = 4 T(n/2) + θ(n) is T(n) = θ(n 2 )

Divide-and-conquer Algorithm 2 for Polynomial multiplication (Karatsuba s algorithm): As before, P*Q = (Ax n/2 + B)(Cx n/2 + D) = (AC)x n + (BC + AD)x n/2 + (BD) This time we write (BC + AD) = (A+B)(C+D) (AC) (BD) So only three recursive calls: A*C B*D (A+B)*(C+D) Again stop the recursion when n = 1 Let T(n) = running time for the preceding Algorithm 2 T(n) = 3 T(n/2) + θ(n) Number of recursive subproblems = 3 Size of each subproblem = n/2 Time for all the non-recursive steps = θ(n) Again, later we will see how to solve these kinds of recurrence equations to determine T(n) The solution for T(n) = 3 T(n/2) + θ(n) is T(n) = θ(n lg 3 ) θ(n 1.58 )

Majority element problem: Given array A of size n, does there exist any value M that appears more than n/2 times in array A? Majority element algorithm: Phase 1: Use divide-and-conquer to find candidate value M Phase 2: Check if M really is a majority element, θ(n) time, simple loop Phase 1 details: Divide: o Group the elements of A into n/2 pairs o If n is odd, there is one unpaired element, x Check if this x is majority element of A If so, then return x, but otherwise discard x o Compare each pair (y, z) If (y==z) keep y and discard z If (y!=z) discard both y and z o So we keep n/2 elements Conquer: One recursive call on subarray of size n/2 Combine: Nothing remains to be done, so omit this step

Example: A = [7, 7, 5, 2, 5, 5, 4, 5, 5, 5, 7] (7, 7) (5, 2) (5, 5) (4, 5) (5, 5) (7) A = [7, 5, 5] (7, 5) (5) return 5 (candidate, also majority) Example: A = [1, 2, 3, 1, 2, 3, 1, 2, 9, 9] (1, 2) (3, 1) (2, 3) (1, 2) (9, 9) A = [9] return 9 (candidate, but not majority) Let T(n) = running time of Phase 1 on array of size n T(n) = T(n/2) + θ(n) Number of recursive subproblems = 1 Size of each subproblem = n/2 [worst-case] Time for all the non-recursive steps = θ(n) Later we ll see how to solve these kinds of recurrence equations to determine T(n) The solution for T(n) = T(n/2) + θ(n) is T(n) = θ(n)