Problem 1

Consider the following program

Algorithm 18 Sort(L[0N])(L[0 \ldots N])

Require: LL is an array.

1:while LL is not sorted do

2:LL \gets a random permutation of LL.

3:end while

Ensure: LL is sorted.

Assume we can test that L is sorted time, that we can compute a random permutation of L in time.

P1.1

Does the program sort correctly? If yes, then provide an invariant for the while-loop and provide a bound function that can be used to prove the correctness of the program. If no, then argue why the program is not correct.

Solution

The program does sort correctly, but inefficiently, given enough time. This is known as Bogosort, as there are finite permutation in the list, one of them are the sorted array.

The invariant for the while-loop is as follow:

This invariant holds through the while-loop because the permutation of the list is a permutation of the original list, and the number of permutations is .

The bound function for the while-loop is as follow:

  • The algorithm is probabilistic, and the expected number of iterations is .
  • The probability for get the sorted list is .

One can use Bernoulli’s trial to find success probability . The cumulative probability of having sorted the list after attempts is

P1.2

Assume the program is correct. Is the program stable? Explain why.

Solution

The program is not stable since SORT is non-deterministic.

  • Each iteration generates a random permutation of the list. If the list contains duplicate elements, their relative order is then not reserved between permutations.
  • The algorithm does not consider the original order of elements when determine the list is sorted.

P1.3

What is the worst case runtime complexity of this program? What is the best case runtime complexity of this program? Is this program optimal? Explain your arguments.

Solution

Worst case scenario occurs when the algorithm goes through all permutations before finding the sorted list. The worst case runtime complexity is .

Best case scenario occurs when the first generated permutation is the sorted list, which has the probability of , which has the runtime of as it only needs to generate one random permutation and check for sorted.

No, the program is not optimal, since it is based on random permutation, and the expected number of iterations is . There are no guarantee that the algorithm is reliable to sort the list. Definitely not as optimal as other sorting algorithm such as merge-sort or heap-sort.

P1.4

What is the expected case runtime complexity of this program? Explain your answer.

Solution

Similar to previous mentioned, one can use Bernoulli’s trial to find success probability .

This probability of success of each run is , where is the number of elements in the list.

The expected case runtime complexity is , since the expected number of iterations is . (since each permutation will take time to check if array is sorted.

Problem 2

The median of a list of distinct values is the middle value : an equal number of values in are smaller and larger than . For example, in the list , the median is 3. Consider two sorted lists and with distinct values. You may assume that the total number of values in and is odd ( is odd). Hence, there is a value such that an equal amount of other values smaller and larger than .

P2.1

Provide an algorithm Median(A, B) that computes the median of the combined list in time.

Solution

Algorithm 19 Median(A[0N),B[0M])(A[0 \ldots N), B[0 \ldots M])

Require: N < M AB|A| \leq |B|

1:NAN \gets |A|

2:MBM \gets |B|

3:LABL \gets A \cup B

4:low0low \coloneqq 0

5:highNhigh \coloneqq N

6:while lowhighlow \leq high do

7:ilow+high2index of Ai \coloneqq \lfloor \frac{low + high}{2} \rfloor \gets \text{index of A}

8:j:N+M+12iindex of Bj \coloneq \lfloor \frac{N+M+1}{2} \rfloor - i \gets \text{index of B}

9:Aleft=i>0 ? A[i1] : A_{\text{left}} = i > 0 \space ? \space A[i-1] \space : \space -\infty

10:Aright=i<N ? A[i] : A_{\text{right}} = i < N \space ? \space A[i] \space : \space \infty

11:Bleft=j>0 ? B[j1] : B_{\text{left}} = j > 0 \space ? \space B[j-1] \space : \space -\infty

12:Bright=j<M ? B[j] : B_{\text{right}} = j < M \space ? \space B[j] \space : \space \infty

13:if AleftBrightBleftArightA_{\text{left}} \leq B_{\text{right}} \land B_{\text{left}} \leq A_{\text{right}} then

14:if (N+M)mod2==1(N+M) \mod 2 == 1 then

15:return max(Aleft,Bleft)\max(A_{\text{left}}, B_{\text{left}})

16:else

17:return max(Aleft,Bleft)+min(Aright,Bright)2\frac{\max(A_{\text{left}}, B_{\text{left}}) + \min(A_{\text{right}}, B_{\text{right}})}{2}

18:end if

19:else if Aleft>BrightA_{\text{left}} > B_{\text{right}} then

20:highi1high \gets i - 1

21:else

22:lowi+1low \gets i + 1

23:end if

24:end while

P2.2

Explain why your algorithm is correct and why the complexity is .

Solution

The median of combined list is the value such that it either the maximum value of left elements or minimum value of right elements (since is odd). Additionally, it partitions the array such that left side will always contains elements.

Since it employs binary search on two smaller arrays and adjusting the partition , it halves the search space through each iteration to smaller array.

Thus, one can then achieve the complexity (of binary search) as .

P2.3

Let be an algorithm with complexity that computes the middle value . Argue how we can use to break up the Merge-step necessary to merge two sorted lists with values into two independent Merge-steps that each merge only values.

Solution

After using to find median of , given that , median will split the list into two halves, each with elements.

Partition and into two subsets and such that left subsets contains items median, right subsets contains median.

Proceed with two independent Merge-steps that each merge only values for both lower and higher sets. Finally concatenate these two arrays into one sorted lists.

Overall complexity for the merge ops is as each sub-problem involves merging elements.