Question

ALgorithm problem

Recall the problem of ¯nding the number of inversions. As in the text, we are given a
sequence of numbers a1; : : : ; an, which we assume are all distinct, and we de¯ne an inversion
to be a pair i < j such that ai > aj .
We motivated the problem of counting inversions as a good measure of how di®erent two
orderings are. However, one might feel that this measure is two sensitive. Let's call a pair a
signi¯cant inversion if i < j and ai > 2aj . Give an O(n log n) time algorithm to count the
number of signi¯cant inversions.
1 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1

The algorithm is very similar to the algorithm of counting inversions. The only change is that here we separate the counting of significant inversions from the merge-sort process.

Algorithm:

Let A = (a1, a2, . . . , an).

Function CountSigInv(A[1...n])

if n = 1 return 0; //base case

Let L := A[1...floor(n/2)]; // Get the first half of A

Let R := A[floor(n/2)+1...n]; // Get the second half of A

//Recurse on L. Return B, the sorted L,

//and x, the number of significant inversions in $L$

Let B, x := CountSigInv(L);

Let C, y := CountSigInv(R); //Do the counting of significant split inversions

Let i := 1;

Let j := 1;

Let z := 0;

// to count the number of significant split inversions while(i <= length(B) and j <= length(C)) if(B[i] > 2*C[j]) z += length(B)-i+1; j += 1; else i += 1;

//the normal merge-sort process i := 1; j := 1;

//the sorted A to be output Let D[1...n] be an array of length n, and every entry is initialized with 0; for k = 1 to n if B[i] < C[j] D[k] = B[i]; i += 1; else D[k] = C[j]; j += 1; return D, (x + y + z);

Runtime Analysis: At each level, both the counting of significant split inversions and the normal merge-sort process take O(n) time, because we take a linear scan in both cases. Also, at each level, we break the problem into two subproblems and the size of each subproblem is n/2. Hence, the recurrence relation is T(n) = 2T(n/2) + O(n). So in total, the time complexity is O(n log n).

Correctness Proof: We will prove a stronger statement that on any input A our algorithm produces correctly the sorted version of A and the number of significant inversions in A. And, we will prove it by induction. Base Case: When n = 1, the algorithm returns 0, which is obviously correct. Inductive Case: Suppose that the algorithm works correctly for 1, 2, . . . , n − 1. We have to show that the algorithm also works for n. By the induction hypothesis, B, x, C, y are correct. We need to show that the counting of significant split inversions and the normal merge-sort process are both correct. In other words, we are to show that z is the correct number of significant split inversions, and that D is correctly sorted. First, we prove that z is correct. Due to the fact that B and C are both sorted by the induction hypothesis, for every C[j], where j = 1, . . . , length(C), we can always find the smallest i such that B[i] > 2C[j]. Therefore, B[1], B[2], . . . , B[i − 1] are all smaller than or equal to 2C[j], and B[i], B[i + 1], . . . , B[length(B)] are all greater than 2C[j]. Thus, length(B) − i + 1 is the correct significant split inversion count for pairs involving C[j]. After counting the number of correct significant split inversions for all C[j], where j = 1, . . . , length(C), z is the correct number of significant split inversions. Second, we show that D is the sorted version of A. This is actually the proof of merge-sort. Each time, we append one element from B or C to D. At each iteration k, since B and C are both sorted by the induction hypothesis, min(B[i], C[j]), the one to be appended to D, is also the smallest number among all the numbers that have not been added to D yet. Consequently, every time we append the smallest remaining element (of B and C) to D, which makes D a sorted array

Add a comment
Know the answer?
Add Answer to:
ALgorithm problem
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • ALGORITHM PROBLEM: A) Significant Inversions: We are given a sequence of n arbitrary but distinct real...

    ALGORITHM PROBLEM: A) Significant Inversions: We are given a sequence of n arbitrary but distinct real numbers <a1 , a2 ,..., an>. We define a significant inversion to be a pair i < j such that ai > 2 aj . Design and analyze an O(n log n) time algorithm to count the number of significant inversions in the given sequence. [Hint: Use divide-&-conquer. Do the “combine” step carefully] B) The Maximum-Sum Monotone Sub-Array Problem: Input: An array A[1..n] of...

  • Algorithm Problem - Counting Number of Inversions

    We are given a sequence ofndistinct numbers a1,...,an. We define an inversion to be a pair i<j such that ai>aj. Give an O(nlogn) algorithm to count the number of inversions. (Hint: assume that your algorithmreturns a sorted array of input sequences, as well as the number of inversions. Divide the input sequencesinto two halves, then count inversions and sort each half by doing recursive calls, and find a way to mergethese two sorted halves and to count the number of...

  • Can i please get help with problem 3? please, if possible, answer in a text format....

    Can i please get help with problem 3? please, if possible, answer in a text format. JN/2) f(n/2); f(n/2); f(n/2); Problem 3. Let A[O...n-1) be an array of n distinct integers. A pair (Ali), A[]) is said to be an inversion if these numbers are out of order, i.e., i<j but A[i] > AG). Design a O(n log n) time algorithm for counting the number of inversions.

  • Collaborative filtering is a technique for generating recommendations, such as suggestions for products, songs, movies, news...

    Collaborative filtering is a technique for generating recommendations, such as suggestions for products, songs, movies, news stories, and so on. The idea is to identify other users who have similar preferences, and recommend to you things that have been popular with them. It requires a formal notion of “similarity” between users, and some of the essence of this is captured by the problem of counting the number of inversions in an array. An inversion in an array A[1,...,n] is a...

  • maybe use induction to prove? Problem 2: Let p-p.Pn be a permutation considered in its one-line notation. An inversion in p is a pair 1 i<jS n such that j appears to the left of i in p (i.e.,...

    maybe use induction to prove? Problem 2: Let p-p.Pn be a permutation considered in its one-line notation. An inversion in p is a pair 1 i<jS n such that j appears to the left of i in p (i.e., an out-of-order pair). Let inv(p) be the total number of inversions in p. Prove that PES where z is a variable. Problem 2: Let p-p.Pn be a permutation considered in its one-line notation. An inversion in p is a pair 1...

  • Problem 2. Let n be a positive integer. We sample n numbers ai,...,an from the set...

    Problem 2. Let n be a positive integer. We sample n numbers ai,...,an from the set 1, 2,...,n} uniformly at random, with replacement. Say that the picks i and j with i < j are a match if a -aj. What is the expected total number of matches? Hint: Use indicators. Wİ

  • 4 Problem 4 -Extra Credit Given an array A, we say that elements A and Al...

    4 Problem 4 -Extra Credit Given an array A, we say that elements A and Al are swapped if J >「 but Alj]< A[i]. For example, if A - [8,5,9,7], then there are a total of3 swapped pairs, namely 8 and 5; 8 and 7; and 9 and 7 Describe a recursive algorithm that given an array A, determines the number of swapped pairs in the array in O(n log(n)) time. To analyze the algorithm, you must state the recurrence...

  • a. Use pseudocode to specify a brute-force algorithm that takes as input a list of n...

    a. Use pseudocode to specify a brute-force algorithm that takes as input a list of n positive integers and determines whether there are two distinct elements of the list that have as their sum a third element of the list. That is, whether there exists i, j.k such that iヂj, i关k,j关k and ai + aj = ak. The algorithm should loop through all triples of elements of the list checking whether the sum of the first two is the third...

  • 1) Design a greedy algorithm that solves the problem; describe your algorithm with clear pseudocode; and...

    1) Design a greedy algorithm that solves the problem; describe your algorithm with clear pseudocode; and prove the time efficiency class of your algorithm: If x, y are two adjacent elements in a sequence, with x before y, we say that the pair x, y is in order when x <= y and the pair is out of order when x > y. For example, in the string “BEGGAR” the pair G, A are out of order, but all the...

  • The input consists of n numbers a1, a2, . . . , an and a target...

    The input consists of n numbers a1, a2, . . . , an and a target value t. The goal is to determine in how many possible ways can we add up two of these numbers to get t. Formally, your program needs to find the number of pairs of indices i, j, i < j such that ai+aj = t. For example, for 2, 7, 3, 1, 5, 6 and t = 7, we can get t in two...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT