Alex. Gerbessiotis 03/07/2025 (c) All rights reserved. MARCH 2025. ---------------------------------------- Homework 2 solution key ------------------------- 1. (a) K is larger. See also a Problem in Subject 2 1. (b) 10 = 5 (5-1)/2. Done in class (at least thrice). 1. (c) 3n-6 ; For n we do 0 comp; For n-1, 1 comp; For 1 we do 2 comp; For 2 , 3 comp For 3 we do 3 comp; ... For n-2 we do 3 comp; Thus for the first three keys of the input we do 3 comp For the remaining (n-3) keys we do (n-3)3 comp Total is (n-3)3 +3 = 3n-9+3=3n-6. 1. (d) T(m)=Theta(mlgm) 1. (e) TRUE 1. (f) FALSE 1. (g) T(n)=Theta(n/lgn * lg(n/lgn)) = Theta (n) 1. (h) FALSE ; InsSort is Theta(n) 1. (i) T(n)=O(n) though technically running time is T(n)=Theta(n), this is not one of the three options given. 2.(a) T(n)=O((lgn)**2) W= X intersect Y intersect Z is not more than |Z|=lgn. Do a binary search of each Z[i] in X and also in Y. Total would be |Z| x O(lg|X|) + |Z|xO(lg|Y|)= O(lgn*lgn). 2.(b) T(n,m)=O(n*lgn) The Union-Find data structure can be built in O(nlgn+m)=O(nlgn) since m=32nlgn. The t queries whether bi and ci are in the same connected component require 2 FIND operations each for a total of 2 x t x Theta(1)= Theta(lgn). (t=lgn.) Running time is dominated by the building of U-F. 3.(a) K=0 L=4 Build a MIN heap X in <=2n comparisons. Build a MAX heap Y in <=2n comparisons. Perform on X, 5 RemoveMin ops to find 1st, 3rd, 5th, 7th (discard 2nd, 4th, 6th keys) smallest keys. This takes 7*O(lgn) time i.e. O(lgn). Perform on Y, 5 RemoveMax ops to find 1st, 3rd, 5th, 7th (discard 2nd, 4th, 6th keys) largest keys. This takes 7*O(lgn) time i.e. O(lgn). Total is 0 * n *lgn + 4 *n + o(n) 4.(a) T(n,m)=Theta(n*n) 4.(b) T(n,m)=Theta(n) 4.(c) T(n,m)=Theta(n) 4.(d) T(n,m)=Theta(n+m) 4.(e) T(n,m)=Theta(n+m) -------------------------------------------------------------------- Date created 03/07/2025 @ 17:50 Date last edited 03/07/2025 @ 18:26