Data Structures and Algorithms

Similar documents
Algorithms and Data Structures 2016 Week 5 solutions (Tues 9th - Fri 12th February)

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

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

CSE 4502/5717: Big Data Analytics

Review Of Topics. Review: Induction

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

Data selection. Lower complexity bound for sorting

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

Week 5: Quicksort, Lower bound, Greedy

Fast Sorting and Selection. A Lower Bound for Worst Case

Selection and Adversary Arguments. COMP 215 Lecture 19

Algorithms And Programming I. Lecture 5 Quicksort

Partition and Select

CS361 Homework #3 Solutions

Design and Analysis of Algorithms

Divide and Conquer Algorithms

Sorting algorithms. Sorting algorithms

Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018

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

Divide and Conquer Algorithms

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

ITEC2620 Introduction to Data Structures

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

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

Data Structures and Algorithms CSE 465

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

Bin Sort. Sorting integers in Range [1,...,n] Add all elements to table and then

IS 709/809: Computational Methods in IS Research Fall Exam Review

Fundamental Algorithms

Fundamental Algorithms

Find an Element x in an Unsorted Array

CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: Oct 26. Homework #2. ( Due: Nov 8 )

Concrete models and tight upper/lower bounds

CPS 616 DIVIDE-AND-CONQUER 6-1

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

CS 161 Summer 2009 Homework #2 Sample Solutions

Bucket-Sort. Have seen lower bound of Ω(nlog n) for comparisonbased. Some cheating algorithms achieve O(n), given certain assumptions re input

COMP Analysis of Algorithms & Data Structures

Design and Analysis of Algorithms

Data Structures and Algorithm Analysis (CSC317) Randomized algorithms

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Limitations of Algorithms

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

Analysis of Algorithms CMPSC 565

Linear Time Selection

Quick Sort Notes , Spring 2010

Introduction to Randomized Algorithms: Quick Sort and Quick Selection

1 Terminology and setup

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

Quicksort. Where Average and Worst Case Differ. S.V. N. (vishy) Vishwanathan. University of California, Santa Cruz

Advanced Analysis of Algorithms - Midterm (Solutions)

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

Linear Selection and Linear Sorting

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

Limitations of Algorithm Power

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

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

Lecture 4. Quicksort

Algorithms Test 1. Question 1. (10 points) for (i = 1; i <= n; i++) { j = 1; while (j < n) {

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

5. DIVIDE AND CONQUER I

Asymptotic Analysis and Recurrences

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

Recommended readings: Description of Quicksort in my notes, Ch7 of your CLRS text.

CS173 Running Time and Big-O. Tandy Warnow

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

Quicksort algorithm Average case analysis

Divide-and-conquer. Curs 2015

Data Structures and Algorithm Analysis (CSC317) Randomized Algorithms (part 3)

Analysis of Algorithms - Midterm (Solutions)

Data Structures and Algorithms Chapter 3

Divide-and-Conquer Algorithms Part Two

CSE 4502/5717 Big Data Analytics Spring 2018; Homework 1 Solutions

CS 344 Design and Analysis of Algorithms. Tarek El-Gaaly Course website:

CSCI 3110 Assignment 6 Solutions

CS 5321: Advanced Algorithms Analysis Using Recurrence. Acknowledgement. Outline

About complexity. We define the class informally P in the following way:

5. DIVIDE AND CONQUER I

Data structures Exercise 1 solution. Question 1. Let s start by writing all the functions in big O notation:

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

CSCE 750, Spring 2001 Notes 2 Page 1 4 Chapter 4 Sorting ffl Reasons for studying sorting is a big deal pedagogically useful Λ the application itself

TTIC / CMSC Algorithms. Lectures 1 and 2 + background material

data structures and algorithms lecture 2

Sorting. CS 3343 Fall Carola Wenk Slides courtesy of Charles Leiserson with small changes by Carola Wenk

COMP 9024, Class notes, 11s2, Class 1

1. (a) Explain the asymptotic notations used in algorithm analysis. (b) Prove that f(n)=0(h(n)) where f(n)=0(g(n)) and g(n)=0(h(n)).

N/4 + N/2 + N = 2N 2.

1. Basic Algorithms: Bubble Sort

CSE 421, Spring 2017, W.L.Ruzzo. 8. Average-Case Analysis of Algorithms + Randomized Algorithms

Hardware/Software Co-Design

CS 361, Lecture 14. Outline

1. [10 marks] Consider the following two algorithms that find the two largest elements in an array A[1..n], where n >= 2.

Divide-and-Conquer. a technique for designing algorithms

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

Kartsuba s Algorithm and Linear Time Selection

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

CS 5321: Advanced Algorithms - Recurrence. Acknowledgement. Outline. Ali Ebnenasir Department of Computer Science Michigan Technological University

Analysis of Approximate Quickselect and Related Problems

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

CS 350 Algorithms and Complexity

CSC 8301 Design & Analysis of Algorithms: Lower Bounds

Transcription:

Data Structures and Algorithms Spring 2017-2018

Outline 1 Sorting Algorithms (contd.)

Outline Sorting Algorithms (contd.) 1 Sorting Algorithms (contd.)

Analysis of Quicksort Time to sort array of length 0, 1 is 1: T (0) = T (1) = 1 T (n) = T (i) + T (n i 1) + cn where i = S 1 and c is an appropriate constant Three cases to consider: worst case, best case and average case Best-case analysis Pivot will split S exactly in half T (n) = 2T (n/2) + cn = cn log n + n = O(n log n)

Analysis of Worst-case analysis Pivot is smallest every time: i = 0 T (n) = T (n 1) + cn T (n 1) = T (n 2) + c(n 1). T (2) = T (1) + 2c n T (n) = T (1) + c i = O(n 2 ) i=2

Analysis of Average-case analysis Let T (n) be average time to sort n values: Assume that every element has equal likelihood of being the pivot element This is only valid if every sub-array is random If the pivot element is jth largest (1 j n), which occurs with prob. 1/n, and cn is work done partitioning the set, then T (n) = cn + 1 n (T (j 1) + T (n j)) n j=1 = cn + 1 n 1 (T (j) + T (n j 1)) n j=0

Analysis of Therefore, and T (n) = 2 n 1 T (j) + cn n j=0 n 1 nt (n) = 2 T (j) + cn 2 j=0 n 2 (n 1)T (n 1) = 2 T (j) + c(n 1) 2 j=0 By subtracting the previous two equations, we get nt (n) (n 1)T (n 1) = 2T (n 1) + 2cn c nt (n) = (n + 1)T (n 1) + 2cn c

Analysis of By dropping c term to simplify the expression and by dividing by n(n + 1) we have a sum that can telescope T (n) n + 1 T (n 1) n T (2) 3 T (n 1) = n T (n 2) = n 1. = T (1) 2 + 2c 3 + 2c n + 1 + 2c n

Analysis of Thus, by adding all terms n+1 T (n) n + 1 = T (1) + 2c i=3 2c log e (n + 1) 2c log e log n 1 i And so, T (n) = O(n log n) Thus quicksort() runs in O(n log n) average time.

Outline Sorting Algorithms (contd.) 1 Sorting Algorithms (contd.)

: Finding the k th Largest Element Using priority queues we can find the kth largest element in time O(n + k log n) So finding the median element will take us (worst-case) O(n + n 2 log n) = O(n log n) In each call to quicksort() the set is partitioned about pivot element, whose final resting place we know If we re looking for the k th largest (smallest) element, since we know their sizes, and from the position of the pivot, we know which set, S 1 or S 2, we should be looking for it in So we only need to make one recursive call at each step From our analysis of General Solutions to Divide and Conquer when 1 = a < c = 2 the best-case running time for the algorithm must be O(n) We can show also that average-case running-time is O(n)

Outline Sorting Algorithms (contd.) 1 Sorting Algorithms (contd.)

Best Possible Performance Is O(n log n)-time as good as we can do from a sorting algorithm? Yes, provided we are limited to two-way comparisons of elements in unit time In these cases we show that in the worst case: any algorithm that uses only two-way comparisons requires log n! comparisons in the worst case and log n! comparisons in the average case

The Tree of All Orderings Idea: Given n items, {a, b, c,..., }, correct ordering is just one of n! possible orderings of items Through a 20 Questions -type of procedure the algorithm has to try and determine which one of the possible n! orderings has been given as the input We can think of all the possible orderings as being the leaves of a tree The branches of the internal nodes are decision points i.e., if a < b branch left, o/w branch right We have to ask enough questions to get us to a unique / leaf ordering: how deep will be the tree?

The Tree of All Orderings (contd.) a < c All possible inputs a < b < c a < c < b c < a < b a < b a > c a < b < c a < c < b b < a < c b < c < a c < a < b c < b < a a > b b < a < c b < c < a c < b < a b < c a < b < c a < b < c a < c < b c > b a < c < b c < a < b

The Tree of All Orderings (contd.) Easy to show by induction that a binary tree of depth d has at most 2 d leaves Therefore, a binary tree with L leaves will have depth at least log L Now, since any decision tree on n elements must have n! leaves a unique leaf for each possible ordering any sorting algorithm using only 2-way comparisons between elements needs at least log L comparisons in worst case log n! = log(n n 1 1) log n + log(n 1) + + log n 2 n 2 log n 2, and, since log a b = log a log b, n 2 log n n 2 = Ω(n log n)

Outline Sorting Algorithms (contd.) 1 Sorting Algorithms (contd.)

Bucket Sort Revisited We saw radix sort could sort data into buckets in time O(p(n + b)), p is number of passes and p = f (b) This is linear in n However, when we decide what bucket to put an element in to, we are doing a b-way comparison, hence this sort does not fit our previous model

More Sorting Algorithms Use these algorithms at your peril And bringing sorting algorithms to the masses: Quick sort Merge sort Insert sort