Lectures on Recursive Algorithms1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.

Slides:



Advertisements
Similar presentations
Lecture 3: Parallel Algorithm Design
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
A simple example finding the maximum of a set S of n numbers.
Analysis of Algorithms
Theory of Computing Lecture 3 MAS 714 Hartmut Klauck.
Data Structures Lecture 9 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
Divide-and-Conquer The most-well known algorithm design strategy:
The Divide-and-Conquer Strategy
CS4413 Divide-and-Conquer
Divide-and-Conquer Recursive in structure –Divide the problem into several smaller sub-problems that are similar to the original but smaller in size –Conquer.
Deterministic Selection and Sorting Prepared by John Reif, Ph.D. Analysis of Algorithms.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Dr. Sumanta Guha Slide Sources: CLRS “Intro.
Divide and Conquer. Recall Complexity Analysis – Comparison of algorithm – Big O Simplification From source code – Recursive.
Spring 2015 Lecture 5: QuickSort & Selection
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
Updated QuickSort Problem From a given set of n integers, find the missing integer from 0 to n using O(n) queries of type: “what is bit[j]
CS38 Introduction to Algorithms Lecture 7 April 22, 2014.
Algorithm Design Strategy Divide and Conquer. More examples of Divide and Conquer  Review of Divide & Conquer Concept  More examples  Finding closest.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 4 Comparison-based sorting Why sorting? Formal analysis of Quick-Sort Comparison.
Algorithm Design Techniques: Induction Chapter 5 (Except Sections 5.6 and 5.7)
CSC 2300 Data Structures & Algorithms March 27, 2007 Chapter 7. Sorting.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Algorithm Design Techniques: Induction Chapter 5 (Except Section 5.6)
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
5 - 1 § 5 The Divide-and-Conquer Strategy e.g. find the maximum of a set S of n numbers.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Data Structures Review Session 1
CSC 2300 Data Structures & Algorithms January 30, 2007 Chapter 2. Algorithm Analysis.
Analysis of Algorithms CS 477/677
CSC 2300 Data Structures & Algorithms March 20, 2007 Chapter 7. Sorting.
CSE 421 Algorithms Richard Anderson Lecture 13 Divide and Conquer.
DAST 2005 Week 4 – Some Helpful Material Randomized Quick Sort & Lower bound & General remarks…
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
Induction and recursion
Analysis of Recursive Algorithms October 29, 2014
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
Divide-and-Conquer 7 2  9 4   2   4   7
CS 3343: Analysis of Algorithms
1 Lecture 16: Lists and vectors Binary search, Sorting.
Order Statistics. Order statistics Given an input of n values and an integer i, we wish to find the i’th largest value. There are i-1 elements smaller.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
Analysis of Algorithms CS 477/677
Divide & Conquer  Themes  Reasoning about code (correctness and cost)  recursion, induction, and recurrence relations  Divide and Conquer  Examples.
1 Prune-and-Search Method 2012/10/30. A simple example: Binary search sorted sequence : (search 9) step 1  step 2  step 3  Binary search.
Divide and Conquer Applications Sanghyun Park Fall 2002 CSE, POSTECH.
Lectures on Greedy Algorithms and Dynamic Programming
COSC 3101A - Design and Analysis of Algorithms 6 Lower Bounds for Sorting Counting / Radix / Bucket Sort Many of these slides are taken from Monica Nicolescu,
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
1 Chapter 7 Quicksort. 2 About this lecture Introduce Quicksort Running time of Quicksort – Worst-Case – Average-Case.
Advanced Algorithms Analysis and Design
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Recursive Algorithms Section 5.4.
Fundamental Data Structures and Algorithms
Lecture 3: Parallel Algorithm Design
Chapter 4 Divide-and-Conquer
Data Structures Review Session
Lectures on Graph Algorithms: searching, testing and sorting
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Divide and Conquer Algorithms Part I
Divide-and-Conquer 7 2  9 4   2   4   7
Lecture 15, Winter 2019 Closest Pair, Multiplication
The Selection Problem.
Presentation transcript:

Lectures on Recursive Algorithms1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski

Announcements Web page: Slides constantly updated based on your feedback – check for a new version a few hours after the lecture(s) Lectures on Recursive Algorithms2

3 Overview Previous lectures: Algorithms: correctness, termination, performance Asymptotic notation Popular asymptotic complexities: log n, n, n log n, n 2,... Graphs - basic definitions and examples These lectures: Recursive algorithms - basic recursion Searching algorithm in time O(log n) Finding majority in time O(n) Sorting in time O(n log n) Other examples

Lectures on Recursive Algorithms4 Algorithms based on recurrence Algorithmic scheme: Reduce the input to smaller sub-input(s) Solve the problem for (some) sub-inputs Merge the obtained solution(s) into one global solution

Lectures on Recursive Algorithms5 Complexity analysis of recursive process Let T(m) denote time (or other complexity measure) of solving a given problem by a given algorithm working on input length m Simple recursive formula: T(n)  qT(n/2) + f(n) Solve the problem for half of the input q times, and spend at most f(n) time for partitioning and/or merging

Lectures on Recursive Algorithms6 Example 1: Searching in time O(log n)

Lectures on Recursive Algorithms7 Searching: problem and algorithm Input: sorted array A of n numbers and number x Problem: find if x is in array A Solution: Algorithm SEARCH(x,1,n) Procedure SEARCH(x,i,j): –If i = j then if x = A[i] then answer YES if x  A[i] then answer NO –Else compare x with element A[(i+j)/2]: If x = A[(i+j)/2] then answer YES If x < A[(i+j)/2] then SEARCH(x, i, (i+j)/2-1) If x > A[(i+j)/2] then SEARCH(x, (i+j)/2+1, j)

Lectures on Recursive Algorithms8 Tree structure of searching height = 3 root 8 leaves find if value 5 is there

Lectures on Recursive Algorithms9 Time and memory consumption Each recursive call –needs additional constant memory, and –the size of input decreases by factor 2 Formula for time (# comparisons): T(n)  T(n/2) + 4 Additional memory size: log n = O(log n) In other words: recursive algorithm must store a path from the root to the searched leaf, and it has logarithmic length

Lectures on Recursive Algorithms10 Case q = 1, f(n) = c T(n)  T(n/2) + c, T(2)  c T(n)  T(n/2) + c  ( T ( (n/2)/2 ) + c ) + c = T(n/4) + 2c  ( T ( (n/4)/2 ) + c ) + 2c = T(n/8) + 3c …  ( T(n/2 i ) + c ) + (i-1). c = T(n/2 i ) + i. c …  (T(n/2 log n – 1 ) + c) + (log n - 2). c = T ( n/(n/2) ) + (log n - 2). c = T(2) + (log n – 1). c  c + (log n – 1). c = c log n

Lectures on Recursive Algorithms11 Case q = 1, f(n) = c formal analysis T(n)  T(n/2) + c T(2)  c Solution: T(n)  c log n Proof: by induction. –For n = 2 straightforward: T(2)  c  c log 2 –Suppose T(n/2)  c log (n/2); then T(n)  T(n/2) + c  c log (n/2) + c  c log n - c + c  c log n

Lectures on Recursive Algorithms12 Example 2: Finding a majority in time O(n)

Lectures on Recursive Algorithms13 Finding a majority Input: array A of n elements Problem: find if there is an element x that occurs in array A more than n/2 times (a majority value) Solution: Algorithm Majority(A) returning a majority element in A, if it exists, or null otherwise

Lectures on Recursive Algorithms14 Finding a majority: algorithm Algorithm Majority(A[1..n]): If |A| = 0 then output null, else if |A| = 1 then output A[1] ; else: If n = |A| is odd then –check whether A[n] is a majority in A by counting the number of occurrences of value A[n]; if yes then output A[n], otherwise decrease n by one Initialize additional array B of size |A|/2 Set j to 0 For i = 1,2,…,n/2 do: –if A[2i-1] = A[2i] then increase j by one set B[j] to A[2i] Find if there is a majority in B[1..j] by executing Majority(B[1..j]) If a majority value x in B[1..j] is returned then check whether x is a majority in A, by going through array A and counting the number of occurrences of value x in A; if successful output x; otherwise null

Lectures on Recursive Algorithms15 Tree structure of finding majority height = 2 root 8 leaves odd, no majority

Lectures on Recursive Algorithms16 Correctness of Majority Algorithm Lemma: (to be proved later) If x is a majority in A then x is a majority in B. Correctness: (proof by induction, based on the Lemma) Inductive assumption: –Majority(B) works correctly for all B’s of size smaller than |A| Case 1: There is a majority in A. –Then it is a majority in B, by Lemma, therefore it will be found in the recursive stage of the algorithm Majority(B) (by inductive argument), and will be checked (successfully) in the last point of the algorithm Majority(A). Case 2: There is no majority in A. –Then even if there is a candidate value found in the recursive stage Majority(B) of the algorithm, it will be rejected anyway in the last point of the algorithm Majority(A).

Lectures on Recursive Algorithms17 Proof of the Lemma Suppose, to the contrary, that x is a majority in A but is not a majority in B. Let m be the number of occurrences of x in A, and k be the number of occurrences of x in B. It follows that the other values appear at least k times in B. Consequently, the other values appear in A: at least 2k times: those pairs that are represented in B by a value different than x; plus m-2k times: each occurrence of x in A that is not paired by another x (there are k pairs xx in A, thus m-2k of other occurrences of x in A) is paired by some other value (different than x) which gives at least 2k+(m-2k) = m occurrences in total, and contradicts the fact that x is a majority in A. Contradiction! Therefore x must be also a majority in B.

Lectures on Recursive Algorithms18 Memory consumption Each call to the recursive procedure needs additional memory of half of the current input size reduces the size of the input by factor at least two Formula for time (# comparisons): T(n)  n n/2 + T(n/2) + n = T(n/2) + 7n/2 Additional memory used (for smaller arrays B): M(n)  n/2 + M(n/2)  n/2 + n/4 + M(n/4)  n/2 + n/4 + … + 1 = n - 1 = O(n) Intuition about memory size: the algorithm needs a binary tree of linear size, in the worst case, to store “winners”

Lectures on Recursive Algorithms19 Case q = 1, f(n) = cn T(n)  T(n/2) + cn, T(1)  c T(n)  T(n/2) + cn  (T(n/4) + cn/2) + cn = T(n/4) + (3/2)cn  (T(n/8) + cn/4) + (3/2)cn = T(n/8) + (7/4)cn …  (T(n/2 i ) + cn/2 i-1 ) + ((2 i-1 -1)/2 i-2 ). cn = T(n/2 i ) + ((2 i -1)/2 i-1 ). cn …  T(n/2 log n ) + ((2 log n -1)/2 log n-1 ). cn = T(1) + (n – 1)/(n/2). cn  c + 2cn – 2c  2cn

Lectures on Recursive Algorithms20 Case q = 1, f(n) = c n formal analysis T(n)  T(n/2) + c n T(1)  c Solution: T(n)  2c  n Proof: by induction. –For n = 1 straightforward: T(1)  c  2c  1 –Suppose T(n/2)  2c  (n/2); then T(n)  T(n/2) + c  n  2c  (n/2) + c n = 2c  n

Lectures on Recursive Algorithms21 Textbook and exercises READING: Chapter 5, up to Section 5.2 EXERCISES: Solve the following recursive formulas the best you can: –T(n)  T(n/2) + 4 –T(n)  T(n/2) + 5n –T(n)  T(n/2) + 3n 2 –T(n)  (3/2)  T(n/2) + 1 Compare and sort the obtained (asymptotic) formulas according to the asymptotic order. For each of them try to find an algorithmic example with performance giving by this formula. Sort the following formulas according to big/small Oh order: log (n 1/2 ), log (9n), log (n 3 ), 2 log n, 2 3log n, 2 log (9n), n 2, n log n Propose and analyse an algorithm for checking if there is a majority in a given sorted array A.

Lectures on Recursive Algorithms22 Example 3: Sorting in time O(n log n)

Lectures on Recursive Algorithms23 Divide and conquer Algorithm: Partition input into two halves in (at most) linear time Solve the problem for each half of the input separately Merge solutions into one in (at most) linear time Recursive formula on time complexity: T(n)  2 T(n/2) + c  n

Lectures on Recursive Algorithms24 Case q = 2, f(n) = cn T(n)  2T(n/2) + cn, T(2)  c T(n)  2T(n/2) + cn  2(2T(n/4) + cn/2) + cn = 4T(n/4) + 2cn  4(2T(n/8) + cn/4) + 2cn = 8T(n/8) + 3cn …  2 i-1 (2T(n/2 i ) + cn/2 i-1 ) + (i-1). cn = 2 i T(n/2 i ) + i. cn …  2 log n - 1 T(2) + (log n - 1). cn  cn log n

Lectures on Recursive Algorithms25 Solution for time formal analysis T(n)  2 T(n/2) + c  n T(2)  c Solution: T(n)  c  n  log n Proof: by induction. –For n = 2 straightforward: T(2)  c  c  2  log 2 –Suppose T(n/2)  c  (n/2)  log (n/2); then T(n)  2T(n/2) + c  n  2c  (n/2)  log (n/2) + c  n = 2c  (n/2)  log n – 2c  n/2 + c  n = c  n  log n

Lectures on Recursive Algorithms26 Example - sorting by merging Input: list L of n numbers Problem: Sort list L Algorithm MergeSort(L): If L has at most two elements then sort them by comparison and exit; Else: –Split L into two lists prefix and suffix, each of size n/2 –Sort, by merging, the prefix and the suffix separately: MergeSort(prefix) and MergeSort(suffix) –Merge sorted prefix with sorted suffix as follows: Initialize final list as empty Repeat until either prefix or suffix is empty: –Compare the first elements on the lists prefix and suffix and then move the smaller one to the end of the final list Concatenate final list with the remaining non-empty list (prefix or suffix)

Lectures on Recursive Algorithms27 MergeSort procedure

Lectures on Recursive Algorithms28 Tree structure of sorting height = 3 root 8 leaves merging operations

Lectures on Recursive Algorithms29 Time and memory consumption Each recursive call –needs additional constant memory, and –the number of inputs increases by factor 2, and –the size of each input decreases by factor 2 Formula for time (# comparisons and list operations): T(n)  n/2 + 2T(n/2) + 2n = 2T(n/2) + 5n/2 Additional memory size: M(n)  2 M(n/2) + 2 M(n)  2 M(n/2) + 2  4 M(n/4)  8 M(n/8)  …  n/2 +n/4 + … = n - 2 = O(n) In other words: divide and conquer algorithm must store the binary tree of pointers during the computation

Lectures on Recursive Algorithms30 Quick sort - algorithmic scheme Generic Quick Sort: Select one element x from the input Partition the input into part containing elements not greater than x and part containing elements bigger than x Sort each part separately Concatenate these two sorted parts Problem: how to choose element x to balance the sizes of these two parts? (to get the similar recursive equations as for MergeSort)

Lectures on Recursive Algorithms31 Why parts should be balanced? Suppose they are not balanced, but, say, the smallest element is chosen: T(n)  T(1) + T(n-1) + c n T(1)  c Solution: T(n)  (c/2). n 2 Proof: by induction. –For n = 1 straightforward: T(1)  c  (c/2). 1 2 –Suppose T(n-1)  (c/2). (n-1) 2 ; then T(n)  T(n-1) + c + cn  (c/2). (n-1) 2 + c (n+1)  (c/2). (n-1) 2 + (c/2). 2n  (c/2). n 2

Lectures on Recursive Algorithms32 Two approaches to be fast Deterministic: Additional tree structure of size O(n) needed for identifying the median value Time: O(n log n), additional memory O(n) Randomized: Select separation element x uniformly at random Time (expected): O(n log n), additional memory O(n)

Lectures on Recursive Algorithms33 Randomized approach - analysis Let T(n) denote the expected time: sum of all possible values of time weighted by the probabilities of these values T(n)  1/n ([ T(n-1)+T(1)] + [T(n-2)+T(2)] + … +[T(0)+T(n)] ) + cn T(0) = T(1) = 1, T(2)  c Solution: T(n)  d n log n, for some constant d  8c Proof: by induction. –For n = 2 straightforward: T(2)  c  d. 2. log 2 –Suppose T(m)  d m log m, for every m < n; then (1-1/n)  T(n)  (2/n)  ( T(0) + … + T(n-1) ) + c n  (2d/n)  ( log 2 + … + (n-1)log(n-1) ) + c n  (2d/n)  ((n 2 /2) log n – n 2 /8 ) + c n  d n log n - d (n/4) + c n  d n log n - d n/2 T(n)  (n/(n-1))  (d n log n - d n/2)  d n log n

Lectures on Recursive Algorithms34 Tree structure of random execution height = 5 root 8 leaves

Lectures on Recursive Algorithms35 Textbook and Exercises READING: Section 5.3 EXERCISES: Solved exercises 1, 2 from the textbook, chapter 5 “Divide and Conquer” Prove that log 2 + … + (n-1)log(n-1)  n(n/2) log(4n), and (n/(n-1))  (d n log n - d n/2)  d n log n, for n > 16

Lectures on Recursive Algorithms36 Example 4: Finding closest pair in time O(n log n)

Lectures on Recursive Algorithms37 Problem Closest pair of points: Input: n points on the plane Output: two points having the closest distance Remarks: Exhaustive search algorithm gives time O(n 2 ) Similar approach gives ALL pairs of closest points

Lectures on Recursive Algorithms38 Solution Preprocessing: Sort points according to the first coordinate (obtain horizontal list H) and according to the second coordinate (obtain vertical list V) Main Algorithm: Partition input list H into halves (H 1,H 2 ) in linear time (draw vertical line L containing medium point); split list V accordingly into V 1,V 2, where V i contains the same elements as H i and inherits the initial order from V (for i = 1,2) Solve the problem for each half of the input separately, obtaining two pairs of closest points; let  be the smallest distance from the obtained ones Find if there is a pair which has distance smaller than  - if yes then find the smallest distance pair

Lectures on Recursive Algorithms39 Main difficulty Find if there is a pair which has distance smaller than  : if yes then find the smallest distance pair How to do it in linear time?   L

Lectures on Recursive Algorithms40 Closest pair in the strip 1.Find a sub-list P of V containing all points with a distance at most  from the line L: –go through V and remove elements not  –close to L 2. For each element in P check its distance to the next 8 elements and remember the closest pair ever found   L P

Lectures on Recursive Algorithms41 Why to check only 8 points ahead? 1. Partition the strip into squares of length  /2 each, as shown in the picture 2. Each square contains at most 1 point - by definition of  3. If there is at least 2 full squares between points then they can not be the closest points 4. There are at most 8 squares to check  /2/2 L /2/2 /2/2 /2/2 /2/2 /2/2

Lectures on Recursive Algorithms42 Time complexity Preprocessing: sorting in time O(n log n) Main Algorithm: recursion in time O(n log n) T(n)  3n/2 + 2T(n/2) + 8n = 2T(n/2) + 9.5n T(2) = 1

Lectures on Recursive Algorithms43 Example 5: Integer multiplication in time O(n 1.59 )

Lectures on Recursive Algorithms44 Beyond  (n log n) : integer’s multiplication Input: two integers x and y, each consisting of at most n bits Output: multiply these integers Naïve Algorithm: –Multiply each bit i of x by y, add (i-1) zeroes at the end –Add the obtained at most n numbers Time:  (n 2 ) of bit operations

Lectures on Recursive Algorithms45 Divide and Conquer approach Let x = x n/2 x 2 and y = y n/2 y 2 Let p = x 1 + x 2 and q = y 1 + y 2. Then x y = (x n/2 x 2 ) (y n/2 y 2 ) = x 1 y n/2 (x 2 y 1 + x 1 y 2 ) + 2 n x 2 y 2 = x 1 y n/2 (pq - x 1 y 1 - x 2 y 2 ) + 2 n x 2 y 2 Algorithm: –Compute p and q (in linear time) –Compute recursively: x 1 y 1, x 2 y 2, pq –Perform x 1 y n/2 (pq - x 1 y 1 - x 2 y 2 ) + 2 n x 2 y 2 in linear time

Lectures on Recursive Algorithms46 Time complexity T(n)  3T(n/2) + c n T(1)  c Solution: T(n)  d n log 3, for some constant d  3c Proof: For n = 1 straightforward: T(1)  c  d n log 3 General case: T(n)  3T(n/2) + c n  9T(n/4) + 3c(n/2) + c n  …  c n (1 + 3/2 + (3/2) 2 + … + (3/2) log n )  c n 2(3/2) log n + 1  3c n  n log = 3c n log 3  d  n log 3 = O(n 1.59 )

Lectures on Recursive Algorithms47 Textbook and Exercises READING: Chapter 5 from Section 5.4 OBLIGATORY EXERCISES: How to add two n-bit integers in linear number of bit operations? ADDITIONAL EXERCISES: Solved Exercise 1 from the textbook, chapter 5 “Divide and Conquer” Exercises 1, 6, 7 from the textbook, chapter 5 “Divide and Conquer”

Lectures on Recursive Algorithms48 Conclusions Searching in time O(log n) Finding majority value in time O(n) Divide and conquer in time O(n log n) –sorting algorithms (MergeSort, RandQuickSort) –closest points algorithm Beyond  (n log n) : O(n log 3 ) time algorithm for integer multiplication