0% found this document useful (0 votes)
19 views2 pages

Tutorial 5 Q 3

This document outlines a tutorial for COMP 3711, focusing on the Randomized Selection algorithm and its analysis using the Indicator Random Variable method. It includes questions on the expected running time of randomized quicksort with equal elements, modifications to the PARTITION procedure, and constructing a min-heap in O(n) time. The tutorial emphasizes understanding algorithm performance and adjustments needed when element values are not distinct.

Uploaded by

yanchi.3dv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views2 pages

Tutorial 5 Q 3

This document outlines a tutorial for COMP 3711, focusing on the Randomized Selection algorithm and its analysis using the Indicator Random Variable method. It includes questions on the expected running time of randomized quicksort with equal elements, modifications to the PARTITION procedure, and constructing a min-heap in O(n) time. The tutorial emphasizes understanding algorithm performance and adjustments needed when element values are not distinct.

Uploaded by

yanchi.3dv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

COMP 3711

2025 Fall Semester - Tutorial 5

Question 1: In class we learned the Randomized Selection algorithm and used a geometric series anal-
ysis approach to show that it runs in O(n) expected time.
In this tutorial you will re-derive the O(n) time in a different way, using the Indicator
Random Variable method used to analyze Quicksort.
Recall the Randomized Selection algorithm to find the k-th smallest element
Pick a random pivot, divide the array into 3 parts:

left subarray, pivot item, right subarray.

We then either stop immediately or recursively solve the problem in the left OR the right
part of the array.

– As in quicksort, denote the elements in sorted order by z1 , · · · , zn (so we are search-


ing for zk )
– We use the same random model for choosing the pivot as for quicksort.

I) Define: 󰀫
1 if zi and zj are compared by the algorithm
Xij =
0 otherwise
Prove the following three facts
2
(a) i ≤ k ≤ j : Pr[Xij = 1] = j−i+1
2
(b) i < j < k : Pr[Xij = 1] = k−i+1
2
(c) k < i < j : Pr[Xij = 1] = j−k+1
II) By the indicator random variable technique, the expected total number of compar-
isons is 󰁛 󰁛
E[Xij ] = Pr[Xij = 1]
j<j i<j
󰁓
Use this to thow that i<j E[Xij ] = O(n).

Question 2: The analysis of the expected running time of randomized quicksort in the lecture notes
assumed that all element values are distinct. In this problem, we examine what happens
when they are not.

(a) Suppose that all element values are equal. What would be randomized quicksort’s
running time?
(b) The PARTITION procedure taught returns an index q such that each element of
A[p, · · · , q − 1] is less than or equal to A[q] and each element of A[q + 1, · · · , r] is
greater than A[q].
Modify PARTITION to produce a new procedure PARTITION′ (A, p, r), which per-
mutes the elements of A[p, · · · , r] and returns two indices q, t, where p ≤ q ≤ t ≤ r,
such that:
∗ all elements of A[q, · · · , t] are equal,
∗ each element of A[p, · · · , q − 1] is less than A[q], and
∗ each element of A[t + 1, · · · , r] is greater than A[q]

1
Like PARTITION, your PARTITION′ procedure should take Θ(r − p) time.
(c) Modify the QUICKSORT procedure to produce QUICKSORT′ (A, p, r) that calls
PARTITION′ but then only recurses on A[p, q − 1] and A[t + 1, r]
(d) [Advanced Topic: Not needed for class] Our analysis of QUICKSORT in class as-
sumed that all elements were distinct. Using QUICKSORT′ , how would you adjust
the analysis (binary tree plus indicator random variables) in the lecture notes to
avoid the assumption that all elements are distinct?

Question 3: In class, we learned how to maintain a min-heap implicitly in an array.


Given that A[i, · · · , (j − 1)] represents an implicit min-heap, we saw how to add A[j] to
the min-heap, in O(log j) time.
This led to an O(n log n) algorithm for constructing a min-heap from array A[1, · · · , n].
For this problem show how to construct a min-heap from an array A[1, · · · , n]
in O(n) time.
It might help to visualize the min-heap as a binary tree and not an array.
For simplification, you may assume that n = 2k+1 − 1 for some k, i.e., the tree is complete.
Hint: Consider “heapifying” the nodes processing them from bottom to top.

You might also like