Data Structures and Algorithms CMPSC 465

Similar documents
Data Structures and Algorithms CSE 465

CS473 - Algorithms I

Algorithm Design and Analysis

Algorithm Design and Analysis

We can use a max-heap to sort data.

CSC Design and Analysis of Algorithms

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

Algorithms with numbers (1) CISC4080, Computer Algorithms CIS, Fordham Univ.! Instructor: X. Zhang Spring 2017

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

CS 171: Introduction to Computer Science II. Quicksort

CS583 Lecture 01. Jana Kosecka. some materials here are based on Profs. E. Demaine, D. Luebke A.Shehu, J-M. Lien and Prof. Wang s past lecture notes

Divide-and-Conquer. Dr. Yingwu Zhu

Divide and Conquer. Algorithm Fall Semester

Parallel Algorithms for (PRAM) Computers & Some Parallel Algorithms. Reference : Horowitz, Sahni and Rajasekaran, Computer Algorithms

Advanced Algorithms and Data Structures

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

Lecture 2: Getting Started

Chapter 4. Divide-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

COE428 Lecture Notes Week 1 (Week of January 9, 2017)

Recurrences and Divide-and-Conquer

Divide-and-Conquer. The most-well known algorithm design strategy: smaller instances. combining these solutions

Advanced Algorithms and Data Structures

Chapter 1 Programming: A General Overview

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides

CSE 421 Algorithms: Divide and Conquer

6/12/2013. Introduction to Algorithms (2 nd edition) Overview. The Sorting Problem. Chapter 2: Getting Started. by Cormen, Leiserson, Rivest & Stein

PRAM Divide and Conquer Algorithms

Lecture 13: Divide and Conquer (1997) Steven Skiena. skiena

Divide and Conquer Algorithms

To begin this textbook, we need to start with a refresher of the topics of numbers and numbering systems.

Algorithms (IX) Guoqiang Li. School of Software, Shanghai Jiao Tong University

Lecture 2 Algorithms with numbers

The divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems.

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1

Divide and Conquer 4-0

CS/COE 1501 cs.pitt.edu/~bill/1501/ More Math

CSE 332: Data Structures & Parallelism Lecture 12: Comparison Sorting. Ruth Anderson Winter 2019

Theory and Algorithms Introduction: insertion sort, merge sort

Lecture 3: Sorting 1

Selection (deterministic & randomized): finding the median in linear time

Divide-and-Conquer Algorithms

II (Sorting and) Order Statistics

Chapter 1 Divide and Conquer Algorithm Theory WS 2013/14 Fabian Kuhn

Algorithms and Data Structures (INF1) Lecture 7/15 Hua Lu

Divide and Conquer. Design and Analysis of Algorithms Andrei Bulatov

08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58

Analyzing Complexity of Lists

Divide and Conquer Algorithms. Sathish Vadhiyar

n = 1 What problems are interesting when n is just 1?

Algorithm Design Techniques part I

CS302 Topic: Algorithm Analysis #2. Thursday, Sept. 21, 2006

Dynamic Programming Group Exercises

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Merge Sort & Quick Sort

Lecture 1. Introduction / Insertion Sort / Merge Sort

How invariants help writing loops Author: Sander Kooijmans Document version: 1.0

Lecture 3. Recurrences / Heapsort

Midterm CSE 21 Spring 2012

Divide and Conquer. Algorithm D-and-C(n: input size)

Randomized Algorithms, Quicksort and Randomized Selection

COMP 250 Fall recurrences 2 Oct. 13, 2017

Choice of C++ as Language

Dynamic Programming. Introduction, Weighted Interval Scheduling, Knapsack. Tyler Moore. Lecture 15/16

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 5. The Towers of Hanoi. Divide and Conquer

CSC236 Week 5. Larry Zhang

CSE 202: Design and Analysis of Algorithms Lecture 5

Midterm 1. CS Intermediate Data Structures and Algorithms. October 23, 2013

Pseudo code of algorithms are to be read by.

Test 1 Review Questions with Solutions

Design and Analysis of Algorithms

So far... Finished looking at lower bounds and linear sorts.

Introduction to Algorithms 6.046J/18.401J

Lecture 15 : Review DRAFT

Data Structures and Algorithms. Part 2

QB LECTURE #1: Algorithms and Dynamic Programming

The divide and conquer strategy has three basic parts. For a given problem of size n,

Unit-2 Divide and conquer 2016

Dynamic Programming Intro

Reading 8 : Recursion

Design and Analysis of Algorithms

Multiple-choice (35 pt.)

Merge Sort. Algorithm Analysis. November 15, 2017 Hassan Khosravi / Geoffrey Tien 1

(Refer Slide Time: 01.26)

Algorithm Design and Recursion. Search and Sort Algorithms

Chapter 3:- Divide and Conquer. Compiled By:- Sanjay Patel Assistant Professor, SVBIT.

UNIT 1 BASICS OF AN ALGORITHM

Algorithms and Data Structures, or

Chapter 1 Divide and Conquer Algorithm Theory WS 2014/15 Fabian Kuhn

SORTING AND SELECTION

Towards a Memory-Efficient Knapsack DP Algorithm

Mergesort again. 1. Split the list into two equal parts

We will give examples for each of the following commonly used algorithm design techniques:

Lecture #2. 1 Overview. 2 Worst-Case Analysis vs. Average Case Analysis. 3 Divide-and-Conquer Design Paradigm. 4 Quicksort. 4.

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014

The Limits of Sorting Divide-and-Conquer Comparison Sorts II

CSC Design and Analysis of Algorithms. Lecture 5. Decrease and Conquer Algorithm Design Technique. Decrease-and-Conquer

ESc101 : Fundamental of Computing

CS Divide and Conquer

Is the statement sufficient? If both x and y are odd, is xy odd? 1) xy 2 < 0. Odds & Evens. Positives & Negatives. Answer: Yes, xy is odd

EECS 2011M: Fundamentals of Data Structures

Transcription:

Data Structures and Algorithms CMPSC 465 LECTURES 7-8 More Divide and Conquer Multiplication Adam Smith S. Raskhodnikova and A. Smith; based on slides by E. Demaine and C. Leiserson

John McCarthy (1927 2011) Pioneer in artificial intelligence In fact, he coined the term Lots of work on how reasoning can be automated Meta-lesson: you learn a lot about something when you teach a computer to do it.

McCarthy s 91 function Why be so careful about correctness with recursive procedures? M(n) = { n 10, if n>100 M(M(n + 11)), if n 100 This function is tricky to reason about because it doesn t always make recursive calls on smaller inputs Programmer s goal: code that is easy to understand Do not write circular programs! S. Raskhodnikova and A. Smith; based on slides by E. Demaine and C. Leiserson 3

Reminder: geometric series 1 1 1 2 x x x = + + + for x < 1 1 1 1 1 2 x x x x x n n = + + + + + for x 1

Review For each tree: How many leaves? How many nodes in total? (big-theta) H

Multiplying large integers Given n-bit integers a, b (in binary), compute c=ab Naive (grade-school) algorithm: a n-1 a n-2 a 0 Write a,b in binary Compute n intermediate products Do n additions Running time? Total work: (n 2 ) n bits b n-1 b n-2 b 0 n bits n bits 2n bit output 0 0 0 0

Divide-and-conquer design 1. Divide the problem (instance) into subproblems. 2. Conquer the subproblems by solving them recursively. 3. Combine subproblem solutions.

Multiplying large integers Divide and Conquer (Attempt #1): Write a = A 1 2 n /2 + A 0 b = B 1 2 n /2 + B 0 We want ab = A 1 B 1 2 n + (A 1 B 0 + B 1 A 0 ) 2 n /2 + A 0 B 0 Multiply n/2 bit integers recursively T(n) = 4T(n/2) + (n) Alas! this is still (n 2 ). (Exercise: write out the recursion tree.)

Multiplying large integers Divide and Conquer (Attempt #1): Write a = A 1 2 n /2 + A 0 b = B 1 2 n /2 + B 0 We want ab = A 1 B 1 2 n + (A 1 B 0 + B 1 A 0 ) 2 n /2 + A 0 B 0 Multiply n/2 bit integers recursively T(n) Consider = 4T(n/2) the expression + (n) (A Alas! this is still (n 2 0 +A 1 ) (B 0 + B 1 ) = A 0 ) B. 0 + A 1 B 1 + (A 0 B 1 + B 1 A 0 ) (Exercise: We can get write away out with the 3 recursion multiplications! tree.) (in yellow) x = A 1 B 1 y = A 0 B 0 z = (A 0 +A 1 )(B 0 +B 1 ) Now we use ab = A 1 B 1 2 n + (A 1 B 0 + B 1 A 0 ) 2 n /2 + A 0 B 0 = x 2n + (z x y) 2 n /2 + y

Multiplying large integers MULTIPLY (n, a, b) a and b are n-bit integers Assume n is a power of 2 for simplicity 1. If n 2 then use grade-school algorithm else 2. A 1 a div 2 n /2 ; B 1 b div 2 n /2 ; 3. A 0 a mod 2 n /2 ; B 0 b mod 2 n /2. 4. x MULTIPLY(n/2, A 1, B 1 ) 5. y MULTIPLY(n/2, A 0, B 0 ) 6. z MULTIPLY(n/2, A 1 +A 0, B 1 +B 0 ) 7. Output x 2 n + (z x y)2 n /2 + y

Multiplying large integers The resulting recurrence T(n) = 3T(n/2) + (n) We will see that T(n) = (n log 23 ) = (n 1.59 ) Note: There is a (n log n) algorithm for multiplication (assumes constant-time word ops) Actually, (n log(n) 2 log*(n) ) bit-level operations (due to Martin Fürer, 2007)

Reminder: recursion trees Technique for guessing solution to recurrences Write out tree of recursive calls Each node gets assigned the work done during that call to the procedure (dividing and combining) Total work is sum of work at all nodes

Recursion tree for multiplication Solve T(n) = 3T(n/2) + cn, where c > 0 is constant. cn cn lg 2 n cn/2 cn/4 cn/4 (1) cn/4 cn/2 cn/2 At bottom level: (3/2)cn (9/4)cn At level k: (3/2) k cn (3/2) log n cn = c n log 2 3

Recursion tree for multiplication lg 2 n Solve T(n) = 3T(n/2) + cn, where c > 0 is constant. (1) cn/2 cn/4 cn/4 cn/4 Total work log 2 n = k= 0 cn cn/2 3 2 k cn cn/2 = (3 log 2 n ) = ( n log cn (3/2)cn (9/4)cn Geometric series: When base of exponent is constant (e.g. 3/2), the largest term is a constant fraction of total 2 3 )

Divide-and-conquer design 1. Divide the problem (instance) into subproblems. 2. Conquer the subproblems by solving them recursively. 3. Combine subproblem solutions.

Binary search Find an element in a sorted array: 1. Divide: Check middle element. 2. Conquer: Recursively search 1 subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15

Binary search Find an element in a sorted array: 1. Divide: Check middle element. 2. Conquer: Recursively search 1 subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15

Binary search Find an element in a sorted array: 1. Divide: Check middle element. 2. Conquer: Recursively search 1 subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15

Binary search Find an element in a sorted array: 1. Divide: Check middle element. 2. Conquer: Recursively search 1 subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15

Binary search Find an element in a sorted array: 1. Divide: Check middle element. 2. Conquer: Recursively search 1 subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15

Binary search Find an element in a sorted array: 1. Divide: Check middle element. 2. Conquer: Recursively search 1 subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15

Binary Search BINARYSEARCH(b, A[1.. n]) find b in sorted (increasing order) array A 1. If n=0 then return not found 2. If A[ n/2 ] = b then return n/2 3. If A[ n/2 ] > b then 4. return BINARYSEARCH(A[1.. n/2 ]) 5. Else 6. return n/2 + BINARYSEARCH(A[ n/2 +1.. n])

Proof of correctness Omitted here, similar to Merge-Sort

Recurrence for binary search T(n) = 1 T(n/2) + (1) # subproblems subproblem size work dividing and combining

Recurrence for binary search T(n) = 1 T(n/2) + (1) # subproblems subproblem size work dividing and combining T(n) = T(n/2) + c = T(n/4) + 2c = c log n = (lg n).

Exponentiation Problem: Compute a b, where b N is n bits long Question: How many multiplications? Naive algorithm: (b) = (2 n ) (exponential in the input length!) Divide-and-conquer algorithm: a b = a b/2 a b/2 if b is even; a (b 1)/2 a (b 1)/2 a if b is odd. T(b) = T(b/2) + (1) T(b) = (log b) = (n).

So far: 3 recurrences Merge Sort T(n) = 2 T(n/2) + (n) = (n log n) Binary Search / Exponentiation T(n) = 1 T(n/2) + (1) = (log n) Multiplication T(n) = 3 T(n/2) + (n) = (n log 2 3 ) Coming soon: systematic ways to solve these.