Divide and Conquer Algorithms

Similar documents
Divide-and-Conquer Algorithms

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

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

CMSC 451: Divide and Conquer

Divide and Conquer. Design and Analysis of Algorithms Andrei Bulatov

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

Test 1 Review Questions with Solutions

Chapter 1 Divide and Conquer Algorithm Theory WS 2015/16 Fabian Kuhn

CS 171: Introduction to Computer Science II. Quicksort

Lecture 6: Divide-and-Conquer

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

Analysis of Algorithms - Quicksort -

CS 171: Introduction to Computer Science II. Quicksort

Union Find. Data Structures and Algorithms Andrei Bulatov

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

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

Data Structures and Algorithms Running time, Divide and Conquer January 25, 2018

Union Find 11/2/2009. Union Find. Union Find via Linked Lists. Union Find via Linked Lists (cntd) Weighted-Union Heuristic. Weighted-Union Heuristic

Divide-and-Conquer. Divide-and conquer is a general algorithm design paradigm:

Hiroki Yasuga, Elisabeth Kolp, Andreas Lang. 25th September 2014, Scientific Programming

II (Sorting and) Order Statistics

Quicksort. Repeat the process recursively for the left- and rightsub-blocks.

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

We can use a max-heap to sort data.

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

Quick-Sort fi fi fi 7 9. Quick-Sort Goodrich, Tamassia

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

(Refer Slide Time: 01.26)

Review implementation of Stable Matching Survey of common running times. Turn in completed problem sets. Jan 18, 2019 Sprenkle - CSCI211

Randomized Algorithms, Hash Functions

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

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

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

Recursion. Chapter 7. Copyright 2012 by Pearson Education, Inc. All rights reserved

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Sorting and Selection

15 150: Principles of Functional Programming Sorting Integer Lists

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

7. Sorting I. 7.1 Simple Sorting. Problem. Algorithm: IsSorted(A) 1 i j n. Simple Sorting

Merge Sort. Yufei Tao. Department of Computer Science and Engineering Chinese University of Hong Kong

Recursion defining an object (or function, algorithm, etc.) in terms of itself. Recursion can be used to define sequences

COMP Analysis of Algorithms & Data Structures

Algorithm Analysis and Design

Merge Sort fi fi fi 4 9. Merge Sort Goodrich, Tamassia

The complexity of Sorting and sorting in linear-time. Median and Order Statistics. Chapter 8 and Chapter 9

Programming II (CS300)

QuickSort

Sorting Goodrich, Tamassia Sorting 1

Randomized Algorithms: Selection

CSC236 Week 6. Larry Zhang

Lecture 15 : Review DRAFT

Programming II (CS300)

Introduction to Computers and Programming. Today

Priority Queues. T. M. Murali. January 26, T. M. Murali January 26, 2016 Priority Queues

Do you remember any iterative sorting methods? Can we produce a good sorting method by. We try to divide and conquer: break into subproblems

Section 7.3 Divide-and-Conquer Algorithms and Recurrence Relations. 0,a m. - Division into thirds gives m = 3

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

Lecture 19 Sorting Goodrich, Tamassia

Day 10. COMP1006/1406 Summer M. Jason Hinek Carleton University

Scribe: Sam Keller (2015), Seth Hildick-Smith (2016), G. Valiant (2017) Date: January 25, 2017

Randomized Algorithms, Quicksort and Randomized Selection

CSC Design and Analysis of Algorithms

Algorithms - Ch2 Sorting

Lecture 6: Sequential Sorting

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

Design and Analysis of Algorithms

Fundamental problem in computing science. putting a collection of items in order. Often used as part of another algorithm

Lecture 2: Getting Started

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

UNIT-2. Problem of size n. Sub-problem 1 size n/2. Sub-problem 2 size n/2. Solution to the original problem

Run Times. Efficiency Issues. Run Times cont d. More on O( ) notation

CIS 121 Data Structures and Algorithms with Java Spring Code Snippets and Recurrences Monday, January 29/Tuesday, January 30

4. Sorting and Order-Statistics

Encoding/Decoding, Counting graphs

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

Back to Sorting More efficient sorting algorithms

Binary Search to find item in sorted array

Sorting is ordering a list of objects. Here are some sorting algorithms

Recurrences and Divide-and-Conquer

CPSC 311 Lecture Notes. Sorting and Order Statistics (Chapters 6-9)

Visualizing Data Structures. Dan Petrisko

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

Searching, Sorting. part 1

Quicksort (CLRS 7) We saw the divide-and-conquer technique at work resulting in Mergesort. Mergesort summary:

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 17: Divide and Conquer Design examples.

Sorting Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Sorting (I) Hwansoo Han

Scan and Quicksort. 1 Scan. 1.1 Contraction CSE341T 09/20/2017. Lecture 7

LESSON 16: ALGORITHM STRATEGIES

Component 02. Algorithms and programming. Sorting Algorithms and Searching Algorithms. Matthew Robinson

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

COMP Analysis of Algorithms & Data Structures

Analyze the obvious algorithm, 5 points Here is the most obvious algorithm for this problem: (LastLargerElement[A[1..n]:

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

Quick Sort. CSE Data Structures May 15, 2002

EECS 2011M: Fundamentals of Data Structures

CPSC 311: Analysis of Algorithms (Honors) Exam 1 October 11, 2002

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

Unit-2 Divide and conquer 2016

Transcription:

Divide and Conquer Algorithms T. M. Murali February 19, 2009

Divide and Conquer Break up a problem into several parts. Solve each part recursively. Solve base cases by brute force. Efficiently combine solutions for sub-problems into final solution.

Divide and Conquer Break up a problem into several parts. Solve each part recursively. Solve base cases by brute force. Efficiently combine solutions for sub-problems into final solution. Common use: Partition problem into two equal sub-problems of size n/2. Solve each part recursively. Combine the two solutions in O(n) time. Resulting running time is O(n log n).

Mergesort Sort INSTANCE: Nonempty list L = x 1, x 2,..., x n of integers. SOLUTION: A permutation y 1, y 2,..., y n of x 1, x 2,..., x n such that y i y i+1, for all 1 i < n. Mergesort is a divide-and-conquer algorithm for sorting. 1. Partition L into two lists A and B of size n/2 and n/2 respectively. 2. Recursively sort A. 3. Recursively sort B. 4. Merge the sorted lists A and B into a single sorted list.

Merging Two Sorted Lists Merge two sorted lists A = a 1, a 2,..., a k and B = b 1, b 2,... b l. Maintain a current pointer for each list. Initialise each pointer to the front of the list. While both lists are nonempty: Let a i and b j be the elements pointed to by the current pointers. Append the smaller of the two to the output list. Advance the current pointer in the list that the smaller element belonged to. EndWhile Append the rest of the non-empty list to the output.

Merging Two Sorted Lists Merge two sorted lists A = a 1, a 2,..., a k and B = b 1, b 2,... b l. Maintain a current pointer for each list. Initialise each pointer to the front of the list. While both lists are nonempty: Let a i and b j be the elements pointed to by the current pointers. Append the smaller of the two to the output list. Advance the current pointer in the list that the smaller element belonged to. EndWhile Append the rest of the non-empty list to the output. Running time of this algorithm is O(k + l).

Analysing Mergesort Worst-case running time for n elements (T (n)) is at most the sum of the worst-case running time for n/2 elements, for n/2 elements, for splitting the input into two lists, and for merging two sorted lists. Assume n is a power of 2.

Analysing Mergesort Worst-case running time for n elements (T (n)) is at most the sum of the worst-case running time for n/2 elements, for n/2 elements, for splitting the input into two lists, and for merging two sorted lists. Assume n is a power of 2. T (n) 2T (n/2) + cn, n > 2 T (2) c

Analysing Mergesort Worst-case running time for n elements (T (n)) is at most the sum of the worst-case running time for n/2 elements, for n/2 elements, for splitting the input into two lists, and for merging two sorted lists. Assume n is a power of 2. T (n) 2T (n/2) + cn, n > 2 T (2) c Three basic ways of solving this recurrence relation: 1. Unroll the recurrence (somewhat informal method). 2. Guess a solution and substitute into recurrence to check. 3. Guess solution in O() form and substitute into recurrence to determine the constants.

Unrolling the recurrence

Unrolling the recurrence Recursion tree has log n levels. Total work done at each level is cn. Running time of the algorithm is cn log n.

Substituting a Solution into the Recurrence Guess that the solution is T (n) cn log n (logarithm to the base 2). Use induction to check if the solution satisfies the recurrence relation.

Substituting a Solution into the Recurrence Guess that the solution is T (n) cn log n (logarithm to the base 2). Use induction to check if the solution satisfies the recurrence relation. Base case: n = 2. Is T (2) = c 2c log 2? Yes.

Substituting a Solution into the Recurrence Guess that the solution is T (n) cn log n (logarithm to the base 2). Use induction to check if the solution satisfies the recurrence relation. Base case: n = 2. Is T (2) = c 2c log 2? Yes. Inductive step: assume T (m) cm log 2 m for all m < n.

Substituting a Solution into the Recurrence Guess that the solution is T (n) cn log n (logarithm to the base 2). Use induction to check if the solution satisfies the recurrence relation. Base case: n = 2. Is T (2) = c 2c log 2? Yes. Inductive step: assume T (m) cm log 2 m for all m < n. Therefore, T (n/2) (cn/2) log(n/2).

Substituting a Solution into the Recurrence Guess that the solution is T (n) cn log n (logarithm to the base 2). Use induction to check if the solution satisfies the recurrence relation. Base case: n = 2. Is T (2) = c 2c log 2? Yes. Inductive step: assume T (m) cm log 2 m for all m < n. Therefore, T (n/2) (cn/2) log(n/2). ( n ) T (n) 2T + cn ( 2 cn ( n ) ) 2 2 log + cn 2 ( n ) = cn log + cn 2 = cn log n cn + cn = cn log n.

Partial Substitution Guess that the solution is kn log n (logarithm to the base 2). Substitute guess into the recurrence relation to check what value of k will satisfy the recurrence relation.

Partial Substitution Guess that the solution is kn log n (logarithm to the base 2). Substitute guess into the recurrence relation to check what value of k will satisfy the recurrence relation. k c will work.

Other Recurrence Relations Divide into q sub-problems of size n/2 and merge in O(n) time. Two distinct cases: q = 1 and q > 2. Divide into two sub-problems of size n/2 and merge in O(n 2 ) time.

T (n) = qt (n/2) + cn, q = 1

T (n) = qt (n/2) + cn, q = 1 Each invocation reduces the problem size by a factor of 2 there are log n levels in the recursion tree. At level i of the tree, the problem size is n/2 i and the work done is cn/2 i. Therefore, the total work done is i=log n i=0 cn 2 i = O(n).

T (n) = qt (n/2) + cn, q > 2

T (n) = qt (n/2) + cn, q > 2 There are log n levels in the recursion tree. At level i of the tree, there are q i sub-problems, each of size n/2 i. The total work done at level i is q i cn/2 i. Therefore, the total work done is T (n) i=log n i=0 q i cn 2 i

T (n) = qt (n/2) + cn, q > 2 There are log n levels in the recursion tree. At level i of the tree, there are q i sub-problems, each of size n/2 i. The total work done at level i is q i cn/2 i. Therefore, the total work done is T (n) i=log n i=0 q i cn 2 i O(n log 2 q ).

T (n) = 2T (n/2) + cn 2 Total work done is i=log n i=0 2 i ( cn 2 i ) 2

T (n) = 2T (n/2) + cn 2 Total work done is i=log n i=0 2 i ( cn 2 i ) 2 O(n 2 ).