Sorting and recurrence analysis techniques

Slides:



Advertisements
Similar presentations
Sorting in Linear Time Introduction to Algorithms Sorting in Linear Time CSE 680 Prof. Roger Crawfis.
Advertisements

Theory of Computing Lecture 3 MAS 714 Hartmut Klauck.
ADA: 5. Quicksort1 Objective o describe the quicksort algorithm, it's partition function, and analyse its running time under different data conditions.
September 12, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
CS 171: Introduction to Computer Science II Quicksort.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
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.
Survey of Sorting Ananda Gunawardena. Naïve sorting algorithms Bubble sort: scan for flips, until all are fixed Etc...
Searching and Sorting Recursion, Merge-sort, Divide & Conquer, Bucket sort, Radix sort Lecture 5.
Sorting: Implementation Fundamental Data Structures and Algorithms Klaus Sutner February 24, 2004.
Midterm Review 1. Midterm Exam Thursday, October 15 in classroom 75 minutes Exam structure: –TRUE/FALSE questions –short questions on the topics discussed.
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
Quicksort This is probably the most popular sorting algorithm. It was invented by the English Scientist C.A.R. Hoare It is popular because it works well.
Sorting Lower Bounds n Beating Them. Recap Divide and Conquer –Know how to break a problem into smaller problems, such that –Given a solution to the smaller.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting So Far Insertion sort: –Easy to code –Fast on small inputs (less than ~50 elements) –Fast on nearly-sorted.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
CSE373: Data Structure & Algorithms Lecture 19: Comparison Sorting Lauren Milne Summer 2015.
Advanced Sorting.
Divide and Conquer Sorting
CMPT 438 Algorithms.
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Introduction to Algorithms
Lecture 2 Sorting.
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
May 17th – Comparison Sorts
Lecture 4 Divide-and-Conquer
Linear-Time Sorting Continued Medians and Order Statistics
Introduction to Algorithms
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
Divide-And-Conquer-And-Combine
Divide and Conquer.
Algorithm Design and Analysis (ADA)
Instructor: Lilian de Greef Quarter: Summer 2017
CS 3343: Analysis of Algorithms
Adapted from slides by Marty Stepp and Stuart Reges
Chapter 4: Divide and Conquer
Lecture 5: Master Theorem, Maps, and Iterators
Algorithms and Data Structures Lecture III
Data Structures and Algorithms
8/04/2009 Many thanks to David Sun for some of the included slides!
Data Structures and Algorithms
Divide-And-Conquer-And-Combine
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
Divide-and-Conquer The most-well known algorithm design strategy:
CSE 373: Data Structures and Algorithms
CSE373: Data Structure & Algorithms Lecture 21: Comparison Sorting
CSE 373: Data Structures and Algorithms
Divide and Conquer Algorithms Part I
Linear-Time Sorting Algorithms
CSE 326: Data Structures Sorting
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
CSE 332: Data Abstractions Sorting I
Topic 5: Heap data structure heap sort Priority queue
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
The Selection Problem.
Design and Analysis of Algorithms
CSCE 3110 Data Structures & Algorithm Analysis
Algorithms and Data Structures Lecture II
Presentation transcript:

Sorting and recurrence analysis techniques CSE 373: Data Structures and Algorithms Sorting and recurrence analysis techniques Autumn 2018 Shrirang (Shri) Mare shri@cs.washington.edu Thanks to Kasey Champion, Ben Jones, Adam Blank, Michael Lee, Evan McCarty, Robbie Weber, Whitaker Brand, Zora Fung, Stuart Reges, Justin Hsia, Ruth Anderson, and many others for sample slides and materials ...

Sorting problem statement Given n comparable elements, rearrange them in an increasing order. Input An array 𝐴 that contains n elements Each element has a key 𝑘 and an associated data Keys support a comparison function (e.g., keys implement a Comparable interface) Expected output An output array 𝐴 such that for any 𝑖 and 𝑗, 𝐴[𝑖] ≤ 𝐴[𝑗] if 𝑖 < 𝑗 (increasing order) Array 𝐴 can also have elements in reverse order (decreasing order) CSE 373 AU 18

Desired properties in a sorting algorithm Stable In the output, equal elements (i.e., elements with equal keys) appear in their original order In-place Algorithm uses a constant additional space, 𝑂(1) extra space Adaptive Performs better when input is almost sorted or nearly sorted (Likely different big-O for best-case and worst-case) Fast. 𝑂 (𝑛 log 𝑛) No algorithm has all of these properties. So choice of algorithm depends on the situation. CSE 373 AU 18

Sorting algorithms – High-level view - 𝑂 𝑛 2 Insertion sort Selection sort Quick sort (worst) - 𝑂(𝑛 log 𝑛) Merge sort Heap sort Quick sort (avg) - Ω(𝑛 log 𝑛) -- lower bound on comparison sorts - 𝑂(𝑛) – non-comparison sorts Bucket sort (avg) CSE 373 AU 18

A framework to think about sorting algos Some questions to consider when analyzing a sorting algorithm. What is the state of the data during each step while sorting Yes/No Yes/No/Can-be Yes/No/Can-be > or < or approx. equal Which data structure is better suited for this algo CSE 373 AU 18

Visualization: https://visualgo.net/en/sorting Insertion sort Idea: At step 𝑖, insert the 𝑖 𝑡ℎ element in the correct position among the first 𝑖 elements. CSE 373 AU 18

Visualization: https://visualgo.net/en/sorting Selection sort Idea: At step 𝑖, find the smallest element among the not-yet-sorted elements (𝑖 … 𝑛) and swap it with the element at 𝑖. CSE 373 AU 18

Heap sort Idea: CSE 373 AU 18

In-place heap sort Idea: 1. Treat initial array as a heap 2. When you call removeMin(), that frees up a slot towards the end in the array. Put the extract min element there. 3. More specifically, when you remove the 𝑖 𝑡ℎ element, put it at 𝐴 𝑛−𝑖 4. This gives you a reverse sorted array. But easy to fix in-place. CSE 373 AU 18

Design technique: Divide-and-conquer Very important technique in algorithm to attack problems Three steps: 1. Divide: Split the original problem into smaller parts 2. Conquer: Solve individual parts independently (think recursion) 3. Combine: Put together individual solved parts to produce an overall solution Merge sort and Quick sort are classic examples of sorting algorithms that use this technique CSE 373 AU 18

Merge sort To sort a given array, Visualization: https://visualgo.net/en/sorting Merge sort To sort a given array, Divide: Split the input array into two halves Conquer: Sort each half independently Combine: Merge the two sorted halves into one sorted whole (HW3 Problem 6!) CSE 373 AU 18

Merge sort Split array in the middle Sort the two halves 1 2 3 4 8 57 91 22 Merge sort 1 8 2 1 2 57 91 22 Split array in the middle Sort the two halves Merge them together 8 2 57 1 91 22 91 22 1 22 91 1 2 8 1 2 22 57 91 𝑇 𝑛 = 𝑐 1 if 𝑛≤1 2𝑇 𝑛 2 + 𝑐 2 𝑛 otherwise 1 2 3 4 8 22 57 91

Review: Unfolding (technique 1) 𝑇 𝑛 = 𝑐 1 if 𝑛≤1 2𝑇 𝑛 2 + 𝑐 2 𝑛 otherwise CSE 373 AU 18

Technique 2: Tree method Level Number of Nodes at level Work per Node Work per Level 1 2 𝑖 base Last recursive level: CSE 373 AU 18

Technique 2: Tree method Level Number of Nodes at level Work per Node Work per Level 1 2 𝑖 base Last recursive level: CSE 373 AU 18

Technique 2: Tree method Level Number of Nodes at level Work per Node Work per Level 1 2 𝑖 base Last recursive level: CSE 373 AU 18

Technique 2: Tree method Level Number of Nodes at level Work per Node Work per Level 1 2 𝑖 base Last recursive level: CSE 373 AU 18

Technique 2: Tree method Level Number of Nodes at level Work per Node Work per Level 1 2 𝑖 base Last recursive level: CSE 373 AU 18

Technique 2: Tree method Level Number of Nodes at level Work per Node Work per Level 1 𝑛 2 𝑛 2 4 𝑛 4 𝑖 2 𝑖 𝑛 2 𝑖 base 2 log2𝑛 Last recursive level: log 𝑛 −1 Combining it all together… 𝑇 𝑛 =𝑛+ 𝑖=0 log 2 𝑛 −1 𝑛 =𝑛+𝑛 log 𝑛 CSE 373 AU 18

Tree Method Practice 𝑇 𝑛 = 4 𝑤ℎ𝑒𝑛 𝑛≤1 3𝑇 𝑛 4 +𝑐 𝑛 2 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒 Level (i) Number of Nodes Work per Node Work per Level 1 2 𝑖 base 𝑇 𝑛 = 4 𝑤ℎ𝑒𝑛 𝑛≤1 3𝑇 𝑛 4 +𝑐 𝑛 2 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒 Tree Method Practice 𝑇 𝑛 4 +𝑇 𝑛 4 +𝑇 𝑛 4 +𝑐 𝑛 2 𝑐 n 2 𝑇 𝑛 4 𝑐 n 4 2 𝑐 n 4 2 𝑇 𝑛 4 𝑐 n 4 2 𝑇 𝑛 4 𝑐 n 16 2 𝑐 n 16 2 𝑐 n 16 2 𝑐 n 16 2 𝑐 n 16 2 𝑐 n 16 2 𝑐 n 16 2 𝑐 n 16 2 𝑐 n 16 2 … … … … … … … … … … … … … … … … … … … … … … … … … … … 4 4 4 4 4 4 4 4 4 Example provided by CS 161 – Jessica Su https://web.stanford.edu/class/archive/cs/cs161/cs161.1168/lecture3.pdf

Tree Method Practice 𝑇 𝑛 = 4 𝑤ℎ𝑒𝑛 𝑛≤1 3𝑇 𝑛 4 +𝑐 𝑛 2 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒 Level (i) Number of Nodes Work per Node Work per Level 1 𝑐𝑛2 3 𝑐 𝑛 4 2 3 16 𝑐 𝑛 2 2 9 𝑐 𝑛 16 2 9 256 𝑐 𝑛 2 𝑖 3 𝑖 𝑐 𝑛 4 𝑖 2 3 16 𝑖 𝑐 𝑛 2 base 3 log4𝑛 4 4∙3 log4𝑛 𝑇 𝑛 = 4 𝑤ℎ𝑒𝑛 𝑛≤1 3𝑇 𝑛 4 +𝑐 𝑛 2 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒 Tree Method Practice 𝑇 𝑛 4 +𝑇 𝑛 4 +𝑇 𝑛 4 +𝑐 𝑛 2 𝑐 n 2 𝑇 𝑛 4 𝑐 n 4 2 𝑐 n 4 2 𝑇 𝑛 4 𝑐 n 4 2 𝑇 𝑛 4 Last recursive level: log 4 𝑛 −1 Combining it all together… 𝑐 n 16 2 𝑐 n 16 2 𝑐 n 16 2 𝑐 n 16 2 𝑐 n 16 2 𝑐 n 16 2 𝑐 n 16 2 𝑐 n 16 2 𝑐 n 16 2 … … … … … … … … … … … … … … … … … … … … … … … 𝑇 𝑛 = 4 𝑛 log43 + 𝑖=0 log 4 𝑛 −1 3 16 𝑖 𝑐 𝑛 2 … … … … 4 4 4 4 4 4 4 4 4 Example provided by CS 161 – Jessica Su https://web.stanford.edu/class/archive/cs/cs161/cs161.1168/lecture3.pdf

Technique 3: Master Theorem Given a recurrence of the following form: 𝑇 𝑛 = 𝑑 𝑤ℎ𝑒𝑛 𝑛=1 𝑎𝑇 𝑛 𝑏 + 𝑛 𝑐 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒 Where 𝑎, 𝑏, and 𝑐 are constants, then 𝑇(𝑛) has the following asymptotic bounds If log 𝑏 𝑎 <𝑐 then 𝑇 𝑛 ∈Θ 𝑛 𝑐 If log 𝑏 𝑎 =𝑐 then 𝑇 𝑛 ∈Θ 𝑛 𝑐 log 2 𝑛 If log 𝑏 𝑎 >𝑐 then 𝑇 𝑛 ∈Θ 𝑛 log 𝑏 𝑎

Apply Master Theorem log 𝑏 𝑎 =𝑐 ⇒ log 2 2 =1 𝑇 𝑛 = 𝑑 𝑤ℎ𝑒𝑛 𝑛=1 𝑎𝑇 𝑛 𝑏 + 𝑛 𝑐 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒 log 𝑏 𝑎 =𝑐 𝑇 𝑛 ∈Θ 𝑛 𝑐 log 2 𝑛 log 𝑏 𝑎 >𝑐 𝑇 𝑛 ∈Θ 𝑛 log 𝑏 𝑎 If 𝑇 𝑛 ∈Θ 𝑛 𝑐 log 𝑏 𝑎 <𝑐 then Given a recurrence of the form: a = 2 b = 2 c = 1 d = 1 𝑇 𝑛 = 1 𝑤ℎ𝑒𝑛 𝑛≤1 2𝑇 𝑛 2 +𝑛 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒 log 𝑏 𝑎 =𝑐 ⇒ log 2 2 =1 𝑇 𝑛 ∈Θ 𝑛 𝑐 log 2 𝑛 ⇒Θ 𝑛 1 log 2 𝑛

Reflecting on Master Theorem 𝑇 𝑛 = 𝑑 𝑤ℎ𝑒𝑛 𝑛=1 𝑎𝑇 𝑛 𝑏 + 𝑛 𝑐 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒 log 𝑏 𝑎 =𝑐 𝑇 𝑛 ∈Θ 𝑛 𝑐 log 2 𝑛 log 𝑏 𝑎 >𝑐 𝑇 𝑛 ∈Θ 𝑛 log 𝑏 𝑎 If 𝑇 𝑛 ∈Θ 𝑛 𝑐 log 𝑏 𝑎 <𝑐 then Given a recurrence of the form: The case Recursive case conquers work more quickly than it divides work Most work happens near “top” of tree Non recursive work in recursive case dominates growth, nc term Work is equally distributed across call stack (throughout the “tree”) Overall work is approximately work at top level x height Recursive case divides work faster than it conquers work Most work happens near “bottom” of tree Leaf work dominates branch work log 𝑏 𝑎 <𝑐 log 𝑏 𝑎 =𝑐 ℎ𝑒𝑖𝑔ℎ𝑡 ≈ log 𝑏 𝑎 log 𝑏 𝑎 >𝑐 𝑏𝑟𝑎𝑛𝑐ℎ𝑊𝑜𝑟𝑘 ≈ 𝑛 𝑐 log 𝑏 𝑎 𝑙𝑒𝑎𝑓𝑊𝑜𝑟𝑘 ≈𝑑 𝑛 log 𝑏 𝑎

Recurrence analysis techniques 1. Unfolding method more of a brute force method Tedious but works 2. Tree methods more scratch work but less error prone 3. Master theorem quick, but applicable only to certain type of recurrences does not give a closed form (gives big-Theta) CSE 373 AU 18 – Shri mare