Divide-and-conquer: Order Statistics. Curs: Fall 2017

Similar documents
Divide-and-conquer. Curs 2015

Algorithm Design and Analysis

Data Structures and Algorithms CSE 465

CPS 616 DIVIDE-AND-CONQUER 6-1

Data Structures and Algorithms

Chapter 5. Divide and Conquer CLRS 4.3. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Chapter 2. Recurrence Relations. Divide and Conquer. Divide and Conquer Strategy. Another Example: Merge Sort. Merge Sort Example. Merge Sort Example

Algorithms, Design and Analysis. Order of growth. Table 2.1. Big-oh. Asymptotic growth rate. Types of formulas for basic operation count

A design paradigm. Divide and conquer: (When) does decomposing a problem into smaller parts help? 09/09/ EECS 3101

The maximum-subarray problem. Given an array of integers, find a contiguous subarray with the maximum sum. Very naïve algorithm:

Analysis of Algorithms I: Asymptotic Notation, Induction, and MergeSort

Algorithms. Quicksort. Slide credit: David Luebke (Virginia)

Divide-and-Conquer. Consequence. Brute force: n 2. Divide-and-conquer: n log n. Divide et impera. Veni, vidi, vici.

Divide & Conquer. Jordi Cortadella and Jordi Petit Department of Computer Science

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

ITEC2620 Introduction to Data Structures

Chapter 5. Divide and Conquer CLRS 4.3. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

CS483 Design and Analysis of Algorithms

Divide & Conquer. Jordi Cortadella and Jordi Petit Department of Computer Science

COMP 9024, Class notes, 11s2, Class 1

5. DIVIDE AND CONQUER I

Lecture 4. Quicksort

Chapter 5. Divide and Conquer. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Review Of Topics. Review: Induction

CS 470/570 Divide-and-Conquer. Format of Divide-and-Conquer algorithms: Master Recurrence Theorem (simpler version)

CS 577 Introduction to Algorithms: Strassen s Algorithm and the Master Theorem

data structures and algorithms lecture 2

Algorithms And Programming I. Lecture 5 Quicksort

5. DIVIDE AND CONQUER I

Divide and Conquer Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 14

Copyright 2000, Kevin Wayne 1

Divide and Conquer. Recurrence Relations

Divide and Conquer Algorithms

Divide-and-Conquer Algorithms and Recurrence Relations. Niloufar Shafiei

Data Structures and Algorithms CSE 465

V. Adamchik 1. Recurrences. Victor Adamchik Fall of 2005

Inf 2B: Sorting, MergeSort and Divide-and-Conquer

Partition and Select

CS361 Homework #3 Solutions

Chapter 4 Divide-and-Conquer

Sorting. Chapter 11. CSE 2011 Prof. J. Elder Last Updated: :11 AM

Notes for Recitation 14

CSE 613: Parallel Programming. Lectures ( Analyzing Divide-and-Conquer Algorithms )

Divide and Conquer Algorithms

CS161: Algorithm Design and Analysis Recitation Section 3 Stanford University Week of 29 January, Problem 3-1.

CSE 613: Parallel Programming. Lecture 8 ( Analyzing Divide-and-Conquer Algorithms )

! Break up problem into several parts. ! Solve each part recursively. ! Combine solutions to sub-problems into overall solution.

Week 5: Quicksort, Lower bound, Greedy

Data Structures and Algorithms Chapter 3

Divide and Conquer. Arash Rafiey. 27 October, 2016

Mergesort and Recurrences (CLRS 2.3, 4.4)

Algorithms Chapter 4 Recurrences

The Divide-and-Conquer Design Paradigm

Divide and Conquer. Andreas Klappenecker. [based on slides by Prof. Welch]

Searching. Sorting. Lambdas

Divide and Conquer. Andreas Klappenecker

Divide-Conquer-Glue Algorithms

Introduction to Algorithms 6.046J/18.401J/SMA5503

Divide-and-conquer algorithm

CS 4104 Data and Algorithm Analysis. Recurrence Relations. Modeling Recursive Function Cost. Solving Recurrences. Clifford A. Shaffer.

Analysis of Algorithms - Using Asymptotic Bounds -

CSCI 3110 Assignment 6 Solutions

Divide and Conquer. CSE21 Winter 2017, Day 9 (B00), Day 6 (A00) January 30,

CS325: Analysis of Algorithms, Fall Midterm

Asymptotic Analysis and Recurrences

b + O(n d ) where a 1, b > 1, then O(n d log n) if a = b d d ) if a < b d O(n log b a ) if a > b d

1 Divide and Conquer (September 3)

CMPS 2200 Fall Divide-and-Conquer. Carola Wenk. Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk

Analysis of Algorithms CMPSC 565

Kartsuba s Algorithm and Linear Time Selection

CMPT 307 : Divide-and-Conqer (Study Guide) Should be read in conjunction with the text June 2, 2015

Design and Analysis of Algorithms

1 Quick Sort LECTURE 7. OHSU/OGI (Winter 2009) ANALYSIS AND DESIGN OF ALGORITHMS

Analysis of Algorithms - Midterm (Solutions)

Divide & Conquer. CS 320, Fall Dr. Geri Georg, Instructor CS320 Div&Conq 1

Quicksort (CLRS 7) We previously saw how the divide-and-conquer technique can be used to design sorting algorithm Merge-sort

CS483 Design and Analysis of Algorithms

CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms

Randomized Sorting Algorithms Quick sort can be converted to a randomized algorithm by picking the pivot element randomly. In this case we can show th

Quick Sort Notes , Spring 2010

1 Terminology and setup

Design and Analysis of Algorithms

Recurrence Relations

Sorting Algorithms. We have already seen: Selection-sort Insertion-sort Heap-sort. We will see: Bubble-sort Merge-sort Quick-sort

Outline. 1 Merging. 2 Merge Sort. 3 Complexity of Sorting. 4 Merge Sort and Other Sorts 2 / 10

ECE250: Algorithms and Data Structures Analyzing and Designing Algorithms

Omega notation. Transitivity etc.

COMP Analysis of Algorithms & Data Structures

CSE 613: Parallel Programming. Lecture 9 ( Divide-and-Conquer: Partitioning for Selection and Sorting )

Algorithm Analysis Recurrence Relation. Chung-Ang University, Jaesung Lee

COMP Analysis of Algorithms & Data Structures

Introduction to Divide and Conquer

Outline. 1 Introduction. 3 Quicksort. 4 Analysis. 5 References. Idea. 1 Choose an element x and reorder the array as follows:

CSE 312, Winter 2011, W.L.Ruzzo. 8. Average-Case Analysis of Algorithms + Randomized Algorithms

Algorithms. Jordi Planes. Escola Politècnica Superior Universitat de Lleida

Outline. Part 5. Computa0onal Complexity (3) Compound Interest 2/15/12. Recurrence Rela0ons: An Overview. Recurrence Rela0ons: Formal Defini0on

Chapter 4. Recurrences

Asymptotic Algorithm Analysis & Sorting

CS 161 Summer 2009 Homework #2 Sample Solutions

2. (25%) Suppose you have an array of 1234 records in which only a few are out of order and they are not very far from their correct positions. Which

Transcription:

Divide-and-conquer: Order Statistics Curs: Fall 2017

The divide-and-conquer strategy. 1. Break the problem into smaller subproblems, 2. recursively solve each problem, 3. appropriately combine their answers. Known Examples: Binary search Merge-sort Quicksort Strassen matrix multiplication Julius Caesar (I-BC) Divide et impera J. von Neumann (1903-57) Merge sort

Recurrences Divide and Conquer T (n) = 3T (n/2) + O(n) The algorithm under analysis divides input of size n into 3 subproblems, each of size n/2, at a cost (of dividing and joining the solutions) of O(n) size n n/2 n/2 n/2 n/4 n/4 n/4 n/4 n/4 n/4 n/4 n/4 n/4 1 1 1 1 1 1

T (n) = 3T (n/2) + O(n). size n k=0 n n/2 n/2 n/2 lg n k=1 3/2n n/4 n/4 n/4 n/4 n/4 n/4 n/4 n/4 n/4 k=2 9/4n 27/8n 1 1 1 k=lg n 3 lg n T(n)=3T(n/2)+O(n)

At depth k of the tree there are 3 k subproblems, each of size n/2 k. For each of those problems we need O(n/2 k ) (splitting time + combination time). Therefore the cost at depth k is: ( n ) 3 k 2 k = ( ) 3 k O(n). 2 with max. depth k = lg n. (1 + 32 + (32 )2 + ( 32 )3 + + ( 32 )lg n ) Θ(n) Therefore T (n) = lg n k=0 O(n)( 3 2 )k.

lg n From T (n) = O(n) ( 3 2 )k, k=0 }{{} ( ) We have a geometric series of ratio 3/2, starting at 1 and ending at ( ( 3 2 )lg n) = nlg 3 ) = n1.58 n lg 2 n. As the series is increasing, T (n) is dominated by the last term: ( ) n lg 3 T (n) = O(n) = O(n 1.58 ). n

T (n) = T (n/4) + T (n/2) + n 2 Series of costs: (1 + (( 1 4 )2 + ( 1 2 )2 ) + (( 1 16 )2 + ( 1 8 )2 ) + )n 2 = (1 + 5 16 + 25 256 + )n2 Decreasing geometric series dominated by 1st. term, n 2. T (n) = O(n 2 )

General setting: Basic Theorem T (n) = at ( n b ) + f (n), where n: size of the problem, n/b: size of the subproblems f (n): cost of divide the problem and combine the solutions Theorem Let a 1, b > 1, d 0 be constants. The recurrence T (n) = at (n/b) + O(n d ) has asymptotic solution: 1. T (n) = O(n d ), if d > log b a, 2. T (n) = O(n d lg n) if d = log b a, 3. T (n) = O(n log b a ), if d < log b a.

Cost n f(n) lgb n a n/b n/b a a n/b2 n/b2 n/b2 n/b2 a a a a (a/b) f(n) (a/b) 2 f(n) O(1) O(1) O(1) O(1) O(1) O(1) O(1) O(1) n lg an O(1)

Selection and order statistics Problem: Given a list A of n of unordered distinct keys, and a i Z, 1 i n, select the i-smallest element x A that is larger than exactly i 1 other elements in A. Notice if: 1. i = 1 MINIMUM element 2. i = n MAXIMUM element 3. i = n+1 2 the MEDIAN 4. i = 0.9 n order statistics Sort A (O(n lg n)) and search for A[k]. Can we do it in linear time? Yes, Selection is easier than Sorting

Deterministic linear selection Generate deterministically a good split element x. Divide the n elements in n/5 groups, each with 5 elements (+ possible one group with < 5 elements).

Deterministic linear selection. Sort each set to find its median, say x i. (Each sorting needs 5 comparisons, i.e. Θ(1)) Total: n/5

Deterministic linear selection. Use recursively Select to find the median x of the medians {x i }, 1 i n/5. Use deterministic Partition (quick sort) to re-arrange the groups corresponding to medians {x i } around x, in linear time on the number of medians. x

Deterministic linear selection. Al least 3( 1 2 n/5 ) = 3n/10 of the elements are x. x

Deterministic linear selection. Al least 3( 1 2 n/5 ) = 3n/10 of the elements are x. x

The deterministic algorithm Select (A, i) 1.- Divide the n elements into n/5 groups of 5 O(n) 2.- Find the median by insertion sort, and take the middle element 3.- Use Select recursively to find the median x of the n/5 medians 4.- Use Partition to place x and its group. Let k=rank of x 5.- if i = k then return x else if i < k then use Select to find the i-th smallest in the left else use Select to find the i k-th smallest in the right end if

Example: Find the median Get the median ( n/2 ) on the following input: 3 13 9 4 5 1 15 12 10 2 6 14 8 11 17 3 4 5 9 13 1 2 10 12 15 6 8 11 14 17 PARTITION around 10: 3 4 5 9 1 2 6 8 10 13 12 15 11 14 17 To get the 7th element (mean) call SELECT on this instance

The deterministic algorithm Select (A, i) 1.- Divide the n elements into n/5 groups of 5 O(n) 2.- Find the median by insertion sort, and take the middle element O(n) 3.- Use Select recursively to find the median x of the n/5 medians T (n/5) 4.- Use Partition to place x and its group. O(n) Let k = rank of x 5.- if i = k then return x else if i < k then use Select to find the i-th smallest in the left else use Select to find the i k-th smallest in the right end if

Worst case Analysis. As at least 3n 10 of the elements are x. At least 3n 10 elements are < x. In the worst case, step 5 calls Select recursively n 3n 10 = 7n/10. So step 5 takes time T (7n/10). Therefore, we have { Θ(1) if n 50, T (n) = T (n/5) + T (7n/10) + Θ(n) if n > 50. Solving we get T (n) = Θ(n)

Notice: If we make groups of 7, the number of elements x is 2n 7, which yield T (n) T (n/7) + T (5n/7) + O(n) with solution T (n) = O(n). However, if we make groups of 3, then T (n) T (n/3) + T (2n/3) + O(n), which has a solution T (n) = O(n ln n).