CLRS 9.3 Selection in worst-case linear time | Rip's Infernal Majesty



CLRS 9.3 Selection in worst-case linear time | Rip's Infernal Majesty

9.3-1

In the algorithm SELECT, the input elements are divided into groups of 5. Will the algorithm work in linear time if they are divided into groups of 7? Argue that SELECT does not run in linear time if groups of 3 are used.

For groups of 7, the number of elements greater than x, or less than,  is at least 2n/7 – 8, we have

T (n) ≤ T (n/7) + T (5n/7 + 8 ) + O(n)

This works in linear time as can be proved similar to case of group 5.

For groups of 3, the number of elements greater than x, or less than,  is at least n/3 – 4, we have

T (n) ≤ T (n/4) + T (n/3 + 4) + O(n)

We can prove by induction this dose not work in linear time.

9.3-2

Analyze SELECT to show that if n ≥ 140, then at least ⌈n/4⌉ elements are greater than the median-of-medians x and at least ⌈n/4⌉ elements are less than x.

3n / 10 = 3*140/10 = 36, while 140/4 = 35.

9.3-3

Show how quicksort can be made to run in O(n lg n) time in the worst case.

Use the SELECT procedure in this chapter for Partition in Quicksort.

9.3-5

Suppose that you have a "black-box" worst-case linear-time median subroutine. Give a simple, linear-time algorithm that solves the selection problem for an arbitrary order statistic.

Just as Randomized-Select in textbook using Randomized-Partition, we use linear-time our median subroutine to do the partition every time. Because we get the median thus partition of an array always results in two subarrays at most half the number of elements of that array. So we have T (n) ≤ T (n/2) + O(n) = O(n).

9.3-7

Describe an O(n)-time algorithm that, given a set S of n distinct numbers and a positive integer k ≤ n, determines the k numbers in S that are closest to the median of S.

We first use linear time selection to find the (n − k)/2, n/2, and (n + k)/2 elements of the set S. Then we go over the set S once to find the numbers less than (n+k)/2 element, also greater than the (n−k)/2 and not equal to the n/2 one element. This takes O(n) time as we use linear time selection three times and traverse the n numbers in S once.

9.3-8

Let X[1 .. n] and Y [1 .. n] be two arrays, each containing n numbers already in sorted order. Give an O(lg n)-time algorithm to find the median of all 2n elements in arrays X and Y.

Because X and Y are already sorted, we first select in O(1) the medians of X and Y, say x and y. We compare X[x] with Y[y], say X[x] <= Y[y]. Then we repeat the procedure on two new arrays which are X[x..n], Y[y..n], till there is ≤ 2 elements left in each array. Return the median of these 4 numbers. There are O(lg) Selection of O(1) and O(lg n) comparisons, so this is O(lg n) time algorithms.

9.3-9

Professor Olay is consulting for an oil company, which is planning a large pipeline running east to west through an oil field of n wells. From each well, a spur pipeline is to be connected directly to the main pipeline along a shortest path (either north or south), as shown in Figure 9.2.

Given x- and y-coordinates of the wells, how should the professor pick the optimal location of the main pipeline (the one that minimizes the total length of the spurs)? Show that the optimal location can be determined in linear time.

When n is even, as long as the numbers of wells to the north and to the west of the main pipeline are equal is an optimal location.

If n is odd, let the main pipeline pass through the median of n wells would achieve optimal. Because otherwise make the pipeline move north or west by y, the new total length of all spur pipelines are L = L0 + (n+1)x – n*x > L0.

To solve we just need to select the median of n wells by their y-coordinates. It's linear time.


Read full article from CLRS 9.3 Selection in worst-case linear time | Rip's Infernal Majesty


No comments:

Post a Comment

Labels

Algorithm (219) Lucene (130) LeetCode (97) Database (36) Data Structure (33) text mining (28) Solr (27) java (27) Mathematical Algorithm (26) Difficult Algorithm (25) Logic Thinking (23) Puzzles (23) Bit Algorithms (22) Math (21) List (20) Dynamic Programming (19) Linux (19) Tree (18) Machine Learning (15) EPI (11) Queue (11) Smart Algorithm (11) Operating System (9) Java Basic (8) Recursive Algorithm (8) Stack (8) Eclipse (7) Scala (7) Tika (7) J2EE (6) Monitoring (6) Trie (6) Concurrency (5) Geometry Algorithm (5) Greedy Algorithm (5) Mahout (5) MySQL (5) xpost (5) C (4) Interview (4) Vi (4) regular expression (4) to-do (4) C++ (3) Chrome (3) Divide and Conquer (3) Graph Algorithm (3) Permutation (3) Powershell (3) Random (3) Segment Tree (3) UIMA (3) Union-Find (3) Video (3) Virtualization (3) Windows (3) XML (3) Advanced Data Structure (2) Android (2) Bash (2) Classic Algorithm (2) Debugging (2) Design Pattern (2) Google (2) Hadoop (2) Java Collections (2) Markov Chains (2) Probabilities (2) Shell (2) Site (2) Web Development (2) Workplace (2) angularjs (2) .Net (1) Amazon Interview (1) Android Studio (1) Array (1) Boilerpipe (1) Book Notes (1) ChromeOS (1) Chromebook (1) Codility (1) Desgin (1) Design (1) Divide and Conqure (1) GAE (1) Google Interview (1) Great Stuff (1) Hash (1) High Tech Companies (1) Improving (1) LifeTips (1) Maven (1) Network (1) Performance (1) Programming (1) Resources (1) Sampling (1) Sed (1) Smart Thinking (1) Sort (1) Spark (1) Stanford NLP (1) System Design (1) Trove (1) VIP (1) tools (1)

Popular Posts