CAIRO UNIVERSITY FACULTY OF ENGINEERING COMPUTER ENG. DEPT. CMP 302, CMPN 302 Algorithms Assignment 2 Due Date: Wednesday November 30th Q1: Design an algorithm for computing n for any positive integer n. Besides assignment and comparison, your algorithm may only use the four basic arithmetic operations. look at one of these algorithms: http://en.wikipedia.org/wiki/Methods_of_computing_square_roots Q2: Locker Doors: there are n lockers in a hallway, numbered sequentially from 1 to n. Initially all the doors are closed. You make n passes by the lockers, each time starting from locker #1. On the ith pass, i=1,2,…,n, you toggle the door of every ith locker: if the door is closed, you open it; if it is open, you close it. For example, after the first pass, every door is open; on the second pass, you toggle the even-numbered lockers (#2, #4,….) so that after the second pass, the even doors are closed and the odd ones are open; the third time through, you close the door of locker #3 (open from the first pass), open the door #6 (closed from the second pass), and so on. After the last pass, which locker doors are open and which are closed? How many of them open? Q3: Solve the following problems: 1. 1,000,000 n2 O(n2) why /why not? 2. (n - 1)n / 2 O(n2) why /why not? 3. n / 2 O(n2) why /why not? 4. lg (n2) O ( lg n ) why /why not? 5. n2 O(n) why /why not? 6. Does 1 2 n 3n (n 2 ) ? 2 7. Does 1 2 n 3n O(n 2 ) ? 2 8. Does 1 2 n 3n (n 2 ) ? 2 Q3: There are 22 gloves in a drawer: 5 pairs of red gloves, 4 pairs of yellow, and 2 pairs of green. You select the gloves in the dark and can check them only after a selection has been made. What is the smallest number of gloves you need to select to have at least one matching pair in the best case? in the worst case? Explain? Q4: Question 9 exercise 2.1 in the textbook Indicate whether the first function of each of the following pairs has smaller, the same, or larger order of growth than the second function: a. b. c. d. e. n(n-1) 100 n2 Log n (n-1)! 2n-1 and 2000n2 and 0.01 n3 and ln n and n! and 2n Q5: Give a careful proof that ∑ni=1 i log(i) = Θ(n2 log n)? Q6: What is the complexity of the Selection Problem algorithm? Q 7: Solve Q3 on page 187 of the textbook?
© Copyright 2026 Paperzz