tut12sol - A- { x } = { 1 , 3 , 4 } , y = 4, Pr ( y ≥ x )...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
COMP 271H Design and Analysis of Algorithms 2006 Fall Semester Tutorial 12 1. (From Computational Geometry by de Berg, van Kreveld, Overmars, and Schwarzkopf) Let A be a set of numbers. Analyze the expected running time of the following procedure RandMax ( A ). The set A in the first call to RandMax contains n distinct numbers. RandMax ( A ) (a) If | A | = 1, return the number in A . (b) Otherwise, choose a random number x A . i. y := RandMax ( A - { x } ). ii. If y x , return y . iii. Otherwise, compare x with all other elements in A to confirm that x is larger than them. Return x . Possible solution: Worst case analysis: T (1) = 1 T ( n ) = T ( n - 1) + O ( n ) Result: T ( n ) = O ( n 2 ) Average case analysis: T (1) = 1 T ( n ) = T ( n - 1) + 1 n x [ Pr ( y x )(1) + Pr ( y < x )( n )] x [ Pr ( y x )(1) + Pr ( y < x )( n )] A small example: We have a set of 4 numbers, { 1 , 2 , 3 , 4 } Case 1: x = 1, A - { x } = { 2 , 3 , 4 } , y = 4, Pr ( y x ) = 1 Case 2: x = 2,
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: A- { x } = { 1 , 3 , 4 } , y = 4, Pr ( y ≥ x ) = 1 Case 3: x = 3, A- { x } = { 1 , 2 , 4 } , y = 4, Pr ( y ≥ x ) = 1 Case 4: x = 4, A- { x } = { 1 , 2 , 3 } , y = 3, Pr ( y ≥ x ) = 0 So, we know that Pr ( y ≥ x ) = 0 if x is maximal among all n numbers, otherwise Pr ( y ≥ x ) = 1. Also, Pr ( y < x ) = 1-Pr ( y ≥ x ), Thus, we have ∑ ∀ x [ Pr ( y ≥ x )(1) + Pr ( y < x )( n )] = 1 + 1 + 1 .... + 1 + n = ( n-1)(1) + n = 2 n-1 T ( n ) = T ( n-1) + 1 n ∑ ∀ x [ Pr ( y ≥ x )(1) + Pr ( y < x )( n )] T ( n ) = T ( n-1) + 1 n (2 n-1) T ( n ) < T ( n-1) + 2 T ( n ) = O ( n ) c ± 2006 Chung Kai Lun Peter. Comments are welcomed. Email: [email protected] 1...
View Full Document

This note was uploaded on 12/09/2010 for the course ENGLISH 1303 taught by Professor May during the Spring '10 term at HKU.

Ask a homework question - tutors are online