Find the Kth largest number Special topics in Advanced Algorithms -Slides prepared by Raghu Srinivasan.

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Order Statistics. order - 2 Lin / Devi Comp 122 Order Statistic i th order statistic: i th smallest element of a set of n elements.
Advertisements

Discrete Structures CISC 2315
1 Sorting in Linear Time How can we do better?  CountingSort  RadixSort  BucketSort.
Sorted list matching & Experimental run-Time COP 3502.
Medians and Order Statistics
1 Selection --Medians and Order Statistics (Chap. 9) The ith order statistic of n elements S={a 1, a 2,…, a n } : ith smallest elements Also called selection.
1 Today’s Material Medians & Order Statistics – Ch. 9.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Dr. Sumanta Guha Slide Sources: CLRS “Intro.
SELECTION CS16: Introduction to Data Structures & Algorithms Tuesday, March 3,
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
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.
Divide and Conquer. Recall Complexity Analysis – Comparison of algorithm – Big O Simplification From source code – Recursive.
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
The Substitution method T(n) = 2T(n/2) + cn Guess:T(n) = O(n log n) Proof by Mathematical Induction: Prove that T(n)  d n log n for d>0 T(n)  2(d  n/2.
Median/Order Statistics Algorithms
CS38 Introduction to Algorithms Lecture 7 April 22, 2014.
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
1 Introduction to Randomized Algorithms Md. Aashikur Rahman Azim.
Quicksort Divide-and-Conquer. Quicksort Algorithm Given an array S of n elements (e.g., integers): If array only contains one element, return it. Else.
The Selection Problem. The selection problem Input: A set S of n elements Output: The k-th smallest element of S The median problem: to find the -th smallest.
Lecture 25 Selection sort, reviewed Insertion sort, reviewed Merge sort Running time of merge sort, 2 ways to look at it Quicksort Course evaluations.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Recursive Definitions Rosen, 3.4. Recursive (or inductive) Definitions Sometimes easier to define an object in terms of itself. This process is called.
Analysis of Recursive Algorithms
1 Recursion, Recurrences and Induction Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.
1 CSC401 – Analysis of Algorithms Lecture Notes 9 Radix Sort and Selection Objectives  Introduce no-comparison-based sorting algorithms: Bucket-sort and.
Selection: Find the ith number
Selection1. 2 The Selection Problem Given an integer k and n elements x 1, x 2, …, x n, taken from a total order, find the k-th smallest element in this.
Induction and recursion
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
Analyzing Recursive Algorithms A recursive algorithm can often be described by a recurrence equation that describes the overall runtime on a problem of.
Chapter 1 Introduction. Goals Why the choice of algorithms is so critical when dealing with large inputs Basic mathematical background Review of Recursion.
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
Order Statistics The ith order statistic in a set of n elements is the ith smallest element The minimum is thus the 1st order statistic The maximum is.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
Order Statistics. Order statistics Given an input of n values and an integer i, we wish to find the i’th largest value. There are i-1 elements smaller.
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.
Sorting Fun1 Chapter 4: Sorting     29  9.
Divide & Conquer  Themes  Reasoning about code (correctness and cost)  recursion, induction, and recurrence relations  Divide and Conquer  Examples.
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
Recurrences David Kauchak cs161 Summer Administrative Algorithms graded on efficiency! Be specific about the run times (e.g. log bases) Reminder:
Time Complexity of Algorithms (Asymptotic Notations)
Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2.Solve smaller instances.
Foundations II: Data Structures and Algorithms
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
1 Chapter 7 Quicksort. 2 About this lecture Introduce Quicksort Running time of Quicksort – Worst-Case – Average-Case.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Quick-Sort 2/18/2018 3:56 AM Selection Selection.
Lecture 3: Parallel Algorithm Design
Order Statistics Comp 122, Spring 2004.
Randomized Algorithms
Selection Selection 1 Quick-Sort Quick-Sort 10/30/16 13:52
CS 3343: Analysis of Algorithms
Randomized Algorithms
Divide-and-Conquer 7 2  9 4   2   4   7
CS 3343: Analysis of Algorithms
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Quick-Sort 4/25/2019 8:10 AM Quick-Sort     2
Order Statistics Comp 122, Spring 2004.
At the end of this session, learner will be able to:
Quick-Sort 5/7/2019 6:43 PM Selection Selection.
The Selection Problem.
Quick-Sort 5/25/2019 6:16 PM Selection Selection.
CS200: Algorithm Analysis
Given a list of n  8 integers, what is the runtime bound on the optimal algorithm that sorts the first eight? O(1) O(log n) O(n) O(n log n) O(n2)
Divide-and-Conquer 7 2  9 4   2   4   7
Presentation transcript:

Find the Kth largest number Special topics in Advanced Algorithms -Slides prepared by Raghu Srinivasan

Problem Input: Unsorted set of numbers and an integer k Output: kth largest number from the given set of numbers Deterministic Solution – Using median of medians – Runtime: Linear i.e., O(n)

Randomization Let S be the set of numbers that are provided, of which we need to find the kth largest RandSelect(S, k) Step 1: Select a partitioning element s belonging to S at random Step 2: Partition S into {L} {s} {R}, where L <= s <= R

Randomization (Contd…) Step 3: If |L| = k-1, then return s else if [L] >= k then return RandSelect(L,k) else return RandSelect(R, k-|L|-1) What is the expected number of recursive calls?

Algorithms behavior simplified Consider this Similar to the algorithm, here we don’t know how many steps it takes for the marker to reach ‘1’; could be anywhere between 1 and n Marker n Marker is initially set to n Pick a number at random such that its less than n. E.g., x and move marker to that number x Pick a number at random such that its less than x. E.g., y and move marker to that number y Continue this way till marker reaches 1

Intuition The randomly selected element, for most of the runs will divide the given set of numbers equally. From the above intuition, the estimated number of steps or recursive calls for most of the runs would be, O(log n)

Proof by induction We will prove that the number of steps in the recursive process, T(N) <= f(N) Using summation we have: - f(N) = sum_{i=1}^{n} (1/(i/2)), pleas read f(N) equals sigma, i ranging from 1 to n of, 1 over i by 2 Note: Here, the above solution is already known and we are just verifying its correctness.

Proof by induction With the diagram on LHS as reference, please consider, that the marker was moved from N to N-X in the first move, therefore T(N) = 1 + E [ f(N-X)] T(N) = 1 + E [ f(N) - sum_{i=N-X}^{N} (1/(i/2))] T(N) = 1 + E[f(N)] – E [sum_{i=N-X}^{N} (1/(i/2))] T(N) = 1 + f(N) - E [sum_{i=N-X}^{N} (1/(i/2))] N N-X

Proof by induction Since N is always >= I, T(N) <= 1 + f(N) – E [sum_{i=N-X}^{N} (1/(N/2))] T(N) <= 1 + f(N) – (E[X])/(N/2) T(N) <= 1 + f(N) = (N/2)/(N/2) Therefore T(N) <= f(N) Hence the number of recursive calls is atmost O(log n) Exercise: Prove that the total word done is linear in time N N-X

References Class notes on 01/30/2008 Randomized Algorithms, by Rajeev Motwani and Prabhakar Raghavan