Lecture 2: Divide and Conquer I: Merge-Sort and Master Theorem Shang-Hua Teng.

Slides:



Advertisements
Similar presentations
Divide and Conquer (Merge Sort)
Advertisements

CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 6.
A simple example finding the maximum of a set S of n numbers.
5/5/20151 Analysis of Algorithms Lecture 6&7: Master theorem and substitution method.
Lecture 4 Divide and Conquer for Nearest Neighbor Problem
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
September 12, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
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.
Chapter 2. Getting Started. Outline Familiarize you with the to think about the design and analysis of algorithms Familiarize you with the framework to.
A Basic Study on the Algorithm Analysis Chapter 2. Getting Started 한양대학교 정보보호 및 알고리즘 연구실 이재준 담당교수님 : 박희진 교수님 1.
11 Computer Algorithms Lecture 6 Recurrence Ch. 4 (till Master Theorem) Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Analysis of Algorithms CS 477/677 Sorting – Part B Instructor: George Bebis (Chapter 7)
Spring 2015 Lecture 5: QuickSort & Selection
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
Sorting. Input: A sequence of n numbers a 1, …, a n Output: A reordering a 1 ’, …, a n ’, such that a 1 ’ < … < a n ’
Lecture 3 Nearest Neighbor Algorithms Shang-Hua Teng.
CS421 - Course Information Website Syllabus Schedule The Book:
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences - 1 Recurrences.
Lecture 5: Master Theorem and Linear Time Sorting
Lecture 6 Divide and Conquer for Nearest Neighbor Problem Shang-Hua Teng.
CS Main Questions Given that the computer is the Great Symbol Manipulator, there are three main questions in the field of computer science: What kinds.
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions Analysis of.
Unit 1. Sorting and Divide and Conquer. Lecture 1 Introduction to Algorithm and Sorting.
Introduction to Algorithm design and analysis
October 1, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input.
Algorithm Correctness A correct algorithm is one in which every valid input instance produces the correct output. The correctness must be proved mathematically.
1 Computer Algorithms Lecture 7 Master Theorem Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
Project 2 due … Project 2 due … Project 2 Project 2.
10/14/ Algorithms1 Algorithms - Ch2 - Sorting.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
1 Designing algorithms There are many ways to design an algorithm. Insertion sort uses an incremental approach: having sorted the sub-array A[1…j - 1],
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
Algorithms Merge Sort Solving Recurrences The Master Theorem.
September 17, 2001 Algorithms and Data Structures Lecture II Simonas Šaltenis Nykredit Center for Database Research Aalborg University
COSC 3101A - Design and Analysis of Algorithms 2 Asymptotic Notations Continued Proof of Correctness: Loop Invariant Designing Algorithms: Divide and Conquer.
Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.
Midterm Review 1. Midterm Exam Thursday, October 15 in classroom 75 minutes Exam structure: –TRUE/FALSE questions –short questions on the topics discussed.
Foundations II: Data Structures and Algorithms
Introduction to Algorithms (2 nd edition) by Cormen, Leiserson, Rivest & Stein Chapter 2: Getting Started.
Algorithms A well-defined computational procedure that takes some value as input and produces some value as output. (Also, a sequence of computational.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Recurrences (in color) It continues…. Recurrences When an algorithm calls itself recursively, its running time is described by a recurrence. When an algorithm.
CSC317 1 So far so good, but can we do better? Yes, cheaper by halves... orkbook/cheaperbyhalf.html.
 Design and Analysis of Algorithms تصميم وتحليل الخوارزميات (311 عال) Chapter 2 Sorting (insertion Sort, Merge Sort)
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture3.
Lecture 2 Algorithm Analysis
Analysis of Algorithms CS 477/677
Unit 1. Sorting and Divide and Conquer
Algorithm Design & Analysis
CS 3343: Analysis of Algorithms
Divide and Conquer (Merge Sort)
Algorithms and Data Structures Lecture III
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
CS200: Algorithms Analysis
Divide and Conquer (Merge Sort)
Divide & Conquer Algorithms
Introduction To Algorithms
nalysis of lgorithms A A Universidad Nacional de Colombia
Quicksort Quick sort Correctness of partition - loop invariant
Algorithms and Data Structures Lecture II
Presentation transcript:

Lecture 2: Divide and Conquer I: Merge-Sort and Master Theorem Shang-Hua Teng

Algorithm Design Paradigm I Solve smaller problems, and use solutions to the smaller problems to solve larger ones –Incremental (e.g., insertion and selection sort) –Divide and Conquer (Today’s topic) –Dynamic Programming (Feb 11 and Feb 13) Correctness: mathematical induction Running Time Analysis: recurrences Incremental is often a special case of Divide and Conquer

Divide and Conquer Divide the problem into a number of sub-problems (similar to the original problem but smaller); Conquer the sub-problems by solving them recursively (if a sub-problem is small enough, just solve it in a straightforward manner. Combine the solutions to the sub-problems into the solution for the original problem

Sorting Input: Array A[1...n], of elements in arbitrary order; array size n Output: Array A[1...n] of the same elements, but in the non-decreasing order

Merge Sort Divide the n-element sequence to be sorted into two subsequences of n/2 element each Conquer: Sort the two subsequences recursively using merge sort Combine: merge the two sorted subsequences to produce the sorted answer Note: during the recursion, if the subsequence has only one element, then do nothing.

Merge-Sort(A,p,r) A procedure sorts the elements in the sub-array A[p..r] using divide and conquer Merge-Sort(A,p,r) –if p >= r, do nothing –if p< r then Merge-Sort(A,p,q) Merge-Sort(A,q+1,r) Merge(A,p,q,r) Starting by calling Merge-Sort(A,1,n)

A = MergeArray(L,R) Assume L[1:s] and R[1:t] are two sorted arrays of elements: Merge-Array(L,R) forms a single sorted array A[1:s+t] of all elements in L and R. A = MergeArray(L,R) – –for k 1 to s + t do if –then –else

Correctness of MergeArray Loop-invariant –At the start of each iteration of the for loop, the subarray A[1:k-1] contains the k-1 smallest elements of L[1:s+1] and R[1:t+1] in sorted order. Moreover, L[i] and R[j] are the smallest elements of their arrays that have not been copied back to A

Inductive Proof of Correctness Initialization: (the invariant is true at beginning) prior to the first iteration of the loop, we have k = 1, so that A[1,k-1] is empty. This empty subarray contains k-1 = 0 smallest elements of L and R and since i = j = 1, L[i] and R[j] are the smallest element of their arrays that have not been copied back to A.

Inductive Proof of Correctness Maintenance: (the invariant is true after each iteration) WLOG: assume L[i] <= R[j], the L[i] is the smallest element not yet copied back to A. Hence after copy L[i] to A[k], the subarray A[1..k] contains the k smallest elements. Increasing k and i by 1 reestablishes the loop invariant for the next iteration.

Inductive Proof of Correctness Termination: (loop invariant implies correctness) At termination we have k = s+t + 1, by the loop invariant, we have A contains the k-1 (s+t) smallest elements of L and R in sorted order.

Complexity of MergeArray At each iteration, we perform 1 comparison, 1 assignment (copy one element to A) and 2 increments (to k and i or j ) So number of operations per iteration is 4. Thus, Merge-Array takes at most 4(s+t) time. Linear in input size.

Merge (A,p,q,r) Assume A[p..q] and A[q+1..r] are two sorted Merge(A,p,q,r) forms a single sorted array A[p..r]. Merge (A,p,q,r) –

Merge-Sort(A,p,r) A procedure sorts the elements in the sub-array A[p..r] using divide and conquer Merge-Sort(A,p,r) –if p >= r, do nothing –if p< r then Merge-Sort(A,p,q) Merge-Sort(A,q+1,r) Merge(A,p,q,r)

Running Time of Merge-Sort Running time as a function of the input size, that is the number of elements in the array A. The Divide-and-Conquer scheme yields a clean recurrences. Assume T(n) be the running time of merge- sort for sorting an array of n elements. For simplicity assume n is a power of 2, that is, there exists k such that n = 2 k.

Recurrence of T(n) T(1) = 1 for n > 1, we have if n = 1 if n > 1

Solution of Recurrence of T(n) T(n) = 4 nlog n + n Picture Proof by Recursion Tree

Asymptotic Notation As input size grow, how fast the running time grow. –T 1 (n) = 100 n –T 2 (n) = n 2 Which algorithms is better? When n < 100 is small then T 1 is smaller As n becomes larger, T 2 grows much faster To solve ambitious, large-scale problem, algorithm1 is preferred.

Asymptotic Notation (Removing the constant factor) The  Notation For example  (g(n)) = { f(n): there exist positive c 1 and c 2 and n 0 such that for all n > n 0 } For example T(n) = 4nlog n + n =  (nlog n)

Asymptotic Notation (Removing the constant factor) The  Big  Notation For example O(g(n)) = { f(n): there exist positive c and n 0 such that for all n > n 0 } For example T(n) = 4nlog n + n =  (nlog n) But also T(n) = 4nlog n + n =  (n 2 )

General Recurrence for Divide-and-Conquer If a divide and conquer scheme divides a problem of size n into a sub-problems of size at most n/b. Suppose the time for Divide is D(n) and time for Combination is C(n), then How do we bound T(n)? if n < c if n > 1

The Master Theorem Consider where a >= 1 and b>= 1 we will ignore ceilings and floors (all absorbed in the O or  notation) if n < c if n > 1

The Master Theorem If for some constant  > 0 then If then If for some constant  > 0 and if a f(n/b) <= c f(n) for some constant c < 1 and all sufficiently large n, then T(n) =  (f (n))

If T(n) = T(2n/3) + 1 then T(n) =  (log n) If T(n) = 9T(n/3) + n, then T(n) =  (n 2 ) If T(n) = 3T(n/4) + n log n then T(n) =  (n log n) If T(n) = 2T(n/2) + n log n then T(n) = ??? (not polynomially large!!!) but we can show that T(n) =  (n log 2 n) Example:The Master Theorem