Counting Inversion: Algorithm Tracing

Consider the counting inversion algorithm. Sort-and-Count studied in class and shown below.

Sort-and-Count inversion algorithm

Run this algorithm against the input file shown below, and:

  1. Show the recursion tree during the divide phase and the conquer & combine phase, respectively, and
  2. Show each returned result as the pair of the inversion count and the sorted result (e.g., “(1, {4, 6})”) exactly in the order of execution. If the input array has an odd number of elements, the left half contains one fewer element than the right half. See the example recursion trees below.

Input file:

Example Recursion tree for an input file:

1. Divide and Conquer Phase Diagrams

See Figure 2 and Figure 3(may be on a separate page due to LaTeX compilation bs

Divide Phase Diagram

Conquer Phase Diagram

2. Returned Results in Execution Order


Divide and Conquer: Majority Equivalence Class

Do Textbook Exercise 3 in Chapter 5. Note that the problem of finding a set of more than half the bank cards that correspond to the same account can be reduced to finding a majority equivalence class, that is, an equivalence class that contains more than half the set elements (i.e., bank cards). Assume that the equivalence tester requires two cards to give a test result. Give your answers as specified below.

  1. [Property] Prove the property that if there exists a majority equivalence class in the set of size n then at least one of the two halves (each of size n/2) has a majority equivalence class. Suggested proof technique: proof by contradiction.
  2. [Algorithm] Based on the proven property, write an executable pseudocode of the recursive algorithm designed using the divide-and-conquer approach. Name the algorithm “MajEC”. Hint: divide the set of bank cards into two halves and call the algorithm recursively on each half.
  3. [Run-time] Write a recurrence relation expressing the run-time T(n) of the recursive algorithm, and solve it to derive the run-time in big-O; it suffices to derive the closed functional form of the run-time and do not prove the big-O formally. The recurrence relation should have both recursive cases and base cases. Either the recursion tree technique or the telescoping technique can be used to derive the closed functional form. If the recurrence relation is one we have already solved in class, it is sufficient to refer to it without solving it again.

1. Property

Definining our theorem:

If there exists a majority equilvelance class in the set of size , then at least one of the two halves

We seek to prove this by contradiction:

Let be a set of bank cars, such that there exists a majority equivalence class , thus .

Divide into two halves, and respectively. Each halve has a size of (assuming is even for simplicity, this can be extended to odd later)

For sake of contradiction, assume that neither contain majority equivalence class.

Meaning that every equivalence class in has size since majority requires .

Consider the cards of now, we let be the number of cards from in respectively.

Then, . And since is an equivalence class, all its cards are equivalent to each other. Therefore forms an equivalence class in , so by our assumption ( where ).

We are left with:

This contradics our initial premise that , and thus our assumption is false and meaning that at least one of the two halves must contain a majority equivalence class

2. Algorithm

see figure 4(might be on another page)

MajEC algorithm pseudocode

3. Run-time

We will perfomr a recurrence relation:

Let:

Our base cases are:

And the recursive cases for are:

where:

  • represents the verification phase
  • at most n tests for candidate1
  • at most n tests for candidate2 (plus 1 test to check if candidates are equivalent)
  • at most tests in the combine step total

We can simplify this further:

for a constant c

Now we can solve:

Num levels:

work per level:

total work:

Thus: