Ece1762 HW2
Ece1762 HW2
Homework Assignment 2
ECE 1762 Algorithms and Data Structures
Winter Semester, 2024
Unless otherwise stated, all page numbers are from the 3rd edition of Cormen, Leiserson, Rivest
and Stein. Unless otherwise stated, for each algorithm you design you should give a
detailed description of the idea, proof of correctness, termination, analysis and proof
of time and space complexity. If not, your answer will be incomplete and you will miss
credit. You are allowed to refer to pages in the textbook.
1. [Randomization, 20 points]
The following program determines the maximum value in an unordered array A[1 . . . n] of
distinct elements:
1: function FindMax(A)
2: max = −∞
3: for each i = 1 : n do
4: if A[i] > max then
5: max = A[i]
6: Return R[n]
(a) If a number x is randomly chosen from a set of n distinct numbers, what is the probability
that x is the largest in that set?
(b) When line 5 of the program is executed, what is the relationship between A[i] and A[j]
for 1 ≤ j ≤ i ?
(c) For each i in the range 1 ≤ i ≤ n, what is the probability that line 5 is executed?
(d) Let s1 , s2 , . . . , sn be n random variables, where si represents the number of times (0 or 1)
that line 5 is executed during the i-th iteration of the for-loop. What is E[si ]?
(e) Let s = s1 + s2 + . . . + sn be the total number of times that line 5 is executed during some
run of the program. Prove that E[s] = Θ(log n).