Data Structures and Algorithms Week 3

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

CS473 - Algorithms I

CS473 - Algorithms I

Section IV.1: Recursive Algorithms and Recursion Trees

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

1/1 7/4 2/2 12/7 10/30 12/25

Data Structures. Algorithm Performance and Big O Analysis

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

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

Full and Complete Binary Trees

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

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

the recursion-tree method

Algorithms. Margaret M. Fleck. 18 October 2010

Closest Pair Problem

HOMEWORK 5 SOLUTIONS. n!f n (1) lim. ln x n! + xn x. 1 = G n 1 (x). (2) k + 1 n. (n 1)!

Why? A central concept in Computer Science. Algorithms are ubiquitous.

External Sorting. Chapter 13. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

Analysis of Algorithms I: Binary Search Trees

A Note on Maximum Independent Sets in Rectangle Intersection Graphs

Indiana State Core Curriculum Standards updated 2009 Algebra I

Converting a Number from Decimal to Binary

Near Optimal Solutions

8 Primes and Modular Arithmetic

The Running Time of Programs

External Sorting. Why Sort? 2-Way Sort: Requires 3 Buffers. Chapter 13

Randomized algorithms

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

Solutions of Equations in One Variable. Fixed-Point Iteration II

Mathematical Induction. Lecture 10-11

Approximation Algorithms

3. Mathematical Induction

Lecture 1: Course overview, circuits, and formulas

Analysis of Algorithms I: Optimal Binary Search Trees

Faster deterministic integer factorisation

ML for the Working Programmer

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

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

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Data Structures and Algorithms Written Examination

Algorithms and Methods for Distributed Storage Networks 9 Analysis of DHT Christian Schindelhauer

Binary Search Trees CMPSC 122

PRIME FACTORS OF CONSECUTIVE INTEGERS

Catalan Numbers. Thomas A. Dowling, Department of Mathematics, Ohio State Uni- versity.

5.2 The Master Theorem

CS711008Z Algorithm Design and Analysis

Math 55: Discrete Mathematics

Sequential Data Structures

Large induced subgraphs with all degrees odd

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

Some Polynomial Theorems. John Kennedy Mathematics Department Santa Monica College 1900 Pico Blvd. Santa Monica, CA

Introduction. Appendix D Mathematical Induction D1

Dynamic Programming. Lecture Overview Introduction

PUTNAM TRAINING POLYNOMIALS. Exercises 1. Find a polynomial with integral coefficients whose zeros include

CSE 421 Algorithms: Divide and Conquer

Lecture Notes on Linear Search

Factoring & Primality

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

FACTORING SPARSE POLYNOMIALS

recursion, O(n), linked lists 6/14

CS/COE

Classification - Examples

Scheduling Single Machine Scheduling. Tim Nieberg

Algorithms and Data Structures

Dynamic Programming Problem Set Partial Solution CMPSC 465

GRAPH THEORY LECTURE 4: TREES

Discuss the size of the instance for the minimum spanning tree problem.

8 Divisibility and prime numbers

CS 103X: Discrete Structures Homework Assignment 3 Solutions

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

Analysis of Algorithms, I

1 FCS Project Submission System

Regular Expressions with Nested Levels of Back Referencing Form a Hierarchy

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

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

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

Analysis of Binary Search algorithm and Selection Sort algorithm

Single machine parallel batch scheduling with unbounded capacity

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 313]

General Framework for an Iterative Solution of Ax b. Jacobi s Method

From Last Time: Remove (Delete) Operation

6. Standard Algorithms

6 March Array Implementation of Binary Trees

Sample Induction Proofs

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Notes on Determinant

CMSC 451 Design and Analysis of Computer Algorithms 1

Lecture Notes on Polynomials

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

Treemaps with bounded aspect ratio

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

To My Parents -Laxmi and Modaiah. To My Family Members. To My Friends. To IIT Bombay. To All Hard Workers

Cost Model: Work, Span and Parallelism. 1 The RAM model for sequential computation:

Lecture 3: Finding integer solutions to systems of linear equations

The Epsilon-Delta Limit Definition:

Termination Checking: Comparing Structural Recursion and Sized Types by Examples

Elementary Number Theory and Methods of Proof. CSE 215, Foundations of Computer Science Stony Brook University

SECTION 10-2 Mathematical Induction

Natural cubic splines

9.2 Summation Notation

Transcription:

Data Structures and Algorithms Week 3 1. Divide and conquer 2. Merge sort, repeated substitutions 3. Tiling 4. Recurrences

Recurrences Running times of algorithms with recursive calls can be described using recurrences. A recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs. For divide and conquer algorithms: T n = { solving trivial problem NumPieces T n/subprobfactor divide combine Example: Merge Sort if n=1 } if n 1 T n = { 1 2T n/2 n if n=1 } if n 1 2

Solving Recurrences Repeated (backward) substitution method Expanding the recurrence by substitution and noticing a pattern (this is not a strictly formal proof). Substitution method guessing the solutions verifying the solution by the mathematical induction Recursion trees Master method templates for different classes of recurrences 3

Repeated Substitution Let s find the running time of merge sort (assume n=2 b ). 1 if n 1 T( n) 2 T( / n2 ) n if n 1 T(n) = 2T(n/2) + n substitute = 2(2T(n/4) + n/2) + n expand = 2 2 T(n/4) + 2n substitute = 2 2 (2T(n/8) + n/4) + 2n expand = 2 3 T(n/8) + 3n observe pattern 4

Repeated Substitution/2 From we get T(n) = 2 3 T(n/8) + 3n T(n) = 2 i T(n/2 i ) + i n An upper bound for i is log n: T(n) = 2 log n T(n/n) + n log n T(n) = n + n log n 5

Repeated Substitution Method 2 The procedure is straightforward: Substitute, Expand, Substitute, Expand, Observe a pattern and determine the expression after the i-th substitution. Find out what the highest value of i (number of iterations, e.g., log n) should be to get to the base case of the recurrence (e.g., T(1)). Insert the value of T(1) and the expression of i into your expression. 6

Analysis of Sort Merge Let s find a more exact running time of merge sort (assume n=2 b ). T( n) 2 if n1 2 T( n/ 2 ) n2 3 n if 1 T(n) = 2T(n/2) + 2n + 3 substitute = 2(2T(n/4) + n + 3) + 2n +3 expand = 2 2 T(n/4) + 4n + 2*3 + 3 substitute = 2 2 (2T(n/8) + n/2 + 3) + 4n + 2*3 + 3 expand = 2 3 T(n/2 3 ) + 2*3n + (2 2+ 2 1+ 2 0 )*3 observe pattern 7

Analysis of Sort Merge/2 6 3 T(n) = 2 i T(n/2 i ) + 2in + 3 i 1 2 j j=0 An upper bound for i is log n = 2 log n T(n/2 log n ) + 2nlog n + 3*(2 log n - 1) = 5n + 2nlog n 3 = Θ(n log n) 8

Substitution Method The substitution method to solve recurrences entails two steps: Guess the solution. Use induction to prove the solution. Example: T(n) = 4T(n/2) + n 9

Substitution Method/2 7 1) Guess T(n) = O(n 3 ), i.e., T(n) is of the form cn 3 2) Prove T(n) cn 3 by induction T(n) = 4T(n/2) + n recurrence 4c(n/2) 3 + n induction hypothesis = 0.5cn 3 + n simplify = cn 3 (0.5cn 3 n) rearrange cn 3 if c>=2 and n>=1 Thus T(n) = O(n 3 ) 10

Substitution Method/3 Tighter bound for T(n) = 4T(n/2) + n: Try to show T(n) = O(n 2 ) Prove T(n) cn 2 by induction T(n) = 4T(n/2) + n 4c(n/2) 2 + n = cn 2 + n NOT cn 2 => contradiction 11

Substitution Method/4 What is the problem? Rewriting T(n) = O(n 2 ) = cn 2 + (something positive) as T(n) cn 2 does not work with the inductive proof. Solution: Strengthen the hypothesis for the inductive proof: T(n) (answer you want) - (something > 0) 12

Substitution Method/5 4 Fixed proof: strengthen the inductive hypothesis by subtracting lower-order terms: Prove T(n) cn 2 - dn by induction T(n) = 4T(n/2) + n 4(c(n/2) 2 d(n/2)) + n = cn 2 2dn + n = cn 2 dn (dn n) cn 2 dn if d 1 13

Recursion Tree A recursion tree is a convenient way to visualize what happens when a recurrence is iterated. Good for guessing asymptotic solutions to recurrences n 2 n 2 1 4 n 2 1 2 n 2 T 1 4 n T 1 2 n T 1 1 6 n T 1 8 n T 1 8 n T 1 4 n 14

Recursion Tree/2 n 2 n 2 1 4 n 2 1 2 n 2 5 1 6 n2 1 1 6 n 2 1 8 n 2 1 2 1 2 8 n 4 n 2 5 2 5 6 n2 5 1 6 3 n 2 n 2 15

Master Method The idea is to solve a class of recurrences that have the form T(n) = at(n/b) + f(n) Assumptions: a 1 and b > 1, and f(n) is asymptotically positive. Abstractly speaking, T(n) is the runtime for an algorithm and we know that a subproblems of size n/b are solved recursively, each in time T(n/b). f(n) is the cost of dividing the problem and combining the results. In merge-sort T(n) = 2T(n/2) + (n). 16

Master Method/2 Iterating the recurrence (expanding the tree) yields T(n) = f(n) + at(n/b) = f(n) + af(n/b) + a 2 T(n/b 2 ) = f(n) + af(n/b) + a 2 f(n/b 2 ) + T(n) = + a b log n-1 f(n/a b log n-1 ) + a b log n T(1) b log n 1 j=0 a j f n /b j n b log a The first term is a division/recombination cost (totaled across all levels of the tree). The second term is the cost of doing all subproblems of size 1 (total of all work pushed to leaves). 17

Master Method/3 f(n) a f(n) f(n/b) a f(n/b) a f(n/b) a a f(n/b) b log n a 2 f(n/b 2 ) f(n/b 2 ) f(n/b 2 ) f(n/b 2 ) f(n/b 2 ) f(n/b 2 ) f(n/b 2 ) f(n/b 2 ) f(n/b 2 ) f(n/b 2 ) a a a a a a a a a Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) n b log a Θ(1) Θ(1) Θ(1) log b n 1 Total: (n b log a ) + j=0 Note: split into a parts, b log n levels, a b log n = n b log a leaves. (n b log a ) a j f n/ b j 18

Master Method, Intuition Three common cases: 1. Running time dominated by cost at leaves. 2. Running time evenly distributed throughout the tree. 3. Running time dominated by cost at the root. To solve the recurrence, we need to identify the dominant term. In each case compare f(n) with O(n b log a ). 19

Master Method, Case 1 f(n) = O(n b log a-ε ) for some constant ε>0 f(n) grows polynomially slower than n b log a (by factor n ε ). The work at the leaf level dominates T(n) = (n b log a ) Cost of all the leaves 20

Master Method, Case 2 f(n) = (n b log a ) f(n) and n b log a are asymptotically the same The work is distributed equally throughout the tree T(n) = (n b log a log n) (level cost) (number of levels) 21

Master Method, Case 3 f(n) = (n b log a+ε ) for some constant ε>0 Inverse of Case 1 f(n) grows polynomially faster than n b log a Also need a regularity condition c 1 and n 0 such a fthat n b( / c ) f ( n ) n n 0 0 The work at the root dominates T(n) = (f(n)) division/recombination cost 22

Master Theorem Summarized Given: recurrence of the form T(n) = at(n/b) + f(n) 1. f(n) = O(n b log a-ε ) => T(n) = (n b log a ) 2. f(n) = (n b log a ) => T(n) = (n b log a log n) 3. f(n) = (n b log a+ε ) and a f(n/b) α f(n) for some α < 1, n>n 0 => T(n) = (f(n)) 23

Strategy 1. Extract a, b, and f(n) from a given recurrence 2. Determine n b log a 3. Compare f(n) and n b log a asymptotically 4. Determine appropriate MT case and apply it Merge sort: T(n) = 2T(n/2) + Θ(n) a=2, b=2, f(n) = Θ(n) n 2 log2 = n Θ(n) = Θ(n) => Case 2: T(n) = Θ(n b log a log n) = Θ(n log n) 24

Examples of Master Method BinarySearch(A, l, r, q): m := (l+r)/2 if A[m]=q then return m else if A[m]>q then BinarySearch(A, l, m-1, q) else BinarySearch(A, m+1, r, q) T(n) = T(n/2) + 1 a=1, b=2, f(n) = 1 n 2 log1 = 1 1 = Θ(1) => Case 2: T(n) = Θ(log n) 25

Examples of Master Method/2 T(n) = 9T(n/3) + n a=9, b=3, f(n) = n n 3 log9 = n 2 n = Ο(n 3 log 9 - ε ) with ε = 1 => Case 1: T(n) = Θ(n 2 ) 26

Examples of Master Method/3 T(n) = 3T(n/4) + n log n a=3, b=4, f(n) = n log n n 4 log3 = n 0.792 n log n = Ω(n 4 log 3 + ε ) with ε = 0.208 => Case 3: regularity condition: af(n/b) <= cf(n) af(n/b) = 3(n/4)log(n/4) <= (3/4)n log n = cf(n) with c=3/4 T(n) = Θ(n log n) 15 27

BinarySearchRec1 Find a number in a sorted array: Trivial if the array contains one element. Else divide into two equal halves and solve each half. Combine the results. INPUT: A[1..n] sorted array of integers, q integer OUTPUT: index j s.t. A[j] = q, NIL if j(1 j n): A[j] q BinarySearchRec1(A, l, r, q): if l = r then if A[l] = q then return l else return NIL m := (l+r)/2 ret := BinarySearchRec1(A, l, m, q) if ret = NIL then return BinarySearchRec1(A, m+1, r, q) else return ret 28

T(n) of BinarySearchRec1 9 Example: Binary Search T n = { 1 2T n/2 1 if n=1 } if n 1 Solving the recurrence yields T(n) = Θ(n) 29

BinarySearchRec2 T(n) = Θ(n) not better than brute force! Better way to conquer: Solve only one half! INPUT: A[1..n] sorted array of integers, q integer OUTPUT: j s.t. A[j] = q, NIL if j(1 j n): A[j] q BinarySearchRec2(A, l, r, q): if l = r then if A[l] = q then return l else return NIL m := (l+r)/2 if A[m] q then return BinarySearchRec2(A, l, m, q) else return BinarySearchRec2(A, m+1, r, q) 30

T(n) of BinarySearchRec2 T n ={ 1 T n/2 1 if n=1 } if n 1 10 Solving the recurrence yields T(n) = Θ(log n) 31

Example: Finding Min and Max 16 Given an unsorted array, find a minimum and a maximum element in the array. INPUT: A[l..r] an unsorted array of integers, l r. OUTPUT: (min,max) s.t. j(l j r): A[j] min and A[j] max MinMax(A, l, r): if l = r then return (A[l], A[r]) // Trivial case m := (l+r)/2 // Divide (minl,maxl) := MinMax(A, l, m) // Conquer (minr,maxr) := MinMax(A, m+1, r) // Conquer if minl < minr then min = minl else min = minr // Combine if maxl > maxr then max = maxl else max = maxr // Combine return (min,max) 32

Summary Divide and conquer Merge sort Tiling Recurrences repeated substitutions substitution master method Example recurrences: Binary search 33

Next Week Sorting HeapSort QuickSort 34