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

Ece1762 HW2

Uploaded by

Janki Patel
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)
6 views2 pages

Ece1762 HW2

Uploaded by

Janki Patel
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

UofToronto–ECE 1762–Winter, 2024 1 Homework Assignment 2

Homework Assignment 2
ECE 1762 Algorithms and Data Structures
Winter Semester, 2024

Due: Monday, Feb 12, 2024 at 11:59pm

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).

2. [Order Statistics, 15 points]


Let A1 [1 . . . n] and A2 [1 . . . n] be two arrays, each containing n numbers in sorted order. Devise
an O(lg n) algorithm that computes the kth largest number of the 2n numbers in the union of
the two arrays (Do not just give pseudocode—explain your algorithm and analyze its running
time.

3. [Search Trees, 5 + 20 points]


Suppose you are given two binary trees T1 and T2 where each node in T1 is strictly less than
each node in T2 . Let h1 and h2 denote their heights, respectively.
UofToronto–ECE 1762–Winter, 2024 2 Homework Assignment 2

(a) Suppose T1 and T2 satisfy the binary-search-tree properties. Devise an O(min(h1 , h2 ))


algorithm to merge T1 and T2 such that the resulting tree satisfies the binary-search-tree
properties.
(b) Suppose T1 and T2 satisfy the red-black properties. Devise an O(max(h1 , h2 )) algorithm
to merge T1 and T2 such that the red-black properties are satisfied.
Hint: For a Red-Black Tree of height h, we can always find a subtree of black-height
0 < b ≤ h. How can you remove a subtree of black-height b from one tree (T1 or T2 ),
combine it with the other tree, and re-insert it to maintain the black-height property?

You might also like