280 likes | 417 Views
Problem: Selection Design and Analysis: Adversary Arguments. The selection problem << Ranking elements of a set in nondecreasing order, find an element rank K >> Finding max and min Adversary Arguments( 反论)
E N D
Problem: SelectionDesign and Analysis: Adversary Arguments The selection problem << Ranking elements of a set in nondecreasing order, find an element rank K >> Finding max and min Adversary Arguments(反论) Suppose you are playing a guessing game with a friend. You are to pick a date(a month and day), and the friend will try to guess the date by asking yes or no questions, such as ‘Is it in the winter’. Designing against an adversary<< An algorithm playing Information game against an adversary >>
Design and Analysis using Adversary arguments • An algorithm is playing an Information game against an adversary. • The algorithm wants to get as much Information as possible in order to get as much work done as effective as possible. • The adversary wants to give as least Information as possible to give the algorithm the worst case. • The rule of the game is Consistency. The adversary can trick but cannot cause inconsistency in the given information. • e.g. your algorithm needs to guess a date (a month and day) and your adversary gives yes/no answers. • Let’s play!
Strategy for Designing against an adversary • Assume a strong adversary! • the adversary will give as least information as possible • Choose questions (or operations) as balance as possible • e.g. for comparison of two keys, x > y • Yes: x > y and • No: x not > y • should provide about the same amount information
The Selection Problem • Find an element with rank k • in an array E using indexes 1 through n • the elements in E are assumed to be unsorted • where 1 <= k <= n • Finding an element with rank k is equivalent to answering the question: • If the array were sorted in nondecreasing order • which element would be in E[k]? • The largest key (called max) should be k = n • The smallest key (called min) should be k = 1 • The median key should be k = Ceiling[n/2]
Finding min, finding max, finding min and max • Finding min in an unsorted array of n elements • require at least n-1 comparisons • Finding max in an unsorted array of n elements • require at least n-1 comparisons • Now we want to find both min and max • can we do better than 2(n-1) ? • What is the lower bound? • Theorem 5.1: Any algorithm to find both min and max of n keys by comparison of keys must do at least 3n/2 – 2 key comparisons in the worst case.
Proof by adversary arguments and units of information • To know that a key v is max, an algorithm must know that every key other than v has lost some comparison • To know that a key u is min , an algorithm must know that every key other than u has won some comparison. • If we count each win as one unit of information • and each loss as one unit of information • Then an algorithm must have at least 2(n-1) units of information for finding both min and max • We need to determine how many comparison are required (in the worst case) to get total 2(n-1) units of information. • The adversary to give us the worst case will provide as few information as possible.
Key status meaning • W has won at least one comparison and never lost. • L has lost at least one comparison and never won. • WL has won and lost at least one comparison. • N has not yet participated in a comparison.
Our strategy to gain as must information • Our algorithm can do at most n/2 comparisons of previously unseen keys • suppose for the moment that n is even • each of these comparison give us 2 units of information • now we have n units of information • Our algorithm need total 2(n-1) = 2n – 2, so now we need n – 2 additional units of information • for each other comparison we gain at most one unit of information • so we need at least n – 2 additional comparisons • In total our algorithm requires at least n/2 + n – 2 comparisons. For n is odd, (n-1)/2+2(n-1)-(n-1)= 3n/2- 3/2 comparisons are needed. QED
Table 5.2 An example of the adversary strategy for max and min
Design an algorithm to find min and max • Now we know the lower bound (in the worst case) • Can we design an algorithm to reach the lower bound? • Exercise • design an algorithm to find both min and max • the algorithm should do at most (about) 3n/2 comparison (in the worst case) for a problem size of n elements
An algorithm to find min and max • Suppose that n is even, first do n/2 comparisons of previously unseen keys, each of these comparison give us 2 units of information. • Do n/2-1 comparisons among winners to get the max. • Do n/2-1 comparisons among losers to get the min. • In total our algorithm requires n/2 +2(n/2 – 1)= 3n/2 – 2 comparisons. • Suppose that n is odd, first do (n-1)/2 comparisons of previously unseen keys. • Do (n-1)/2 comparisons among winners and the last key to get the max. • Do (n-1)/2 comparisons among losers and the last key to get the min. • In total our algorithm requires (n-1)/2 +n – 1= 3n/2 –3/2 comparisons.
“是或非”判断元素 • 设用k个“是或非”的问题在m个元素的集合M中判断一个元素. • 第一个是或非问题把M分成两部分,设较大部分为A1,则A1|>=m/2 • 第二个是或非问题把M分成两部分,设较大部分为A2,则A2|>=m/2^2 • …… |Ak|>= m/2^k • 设Ak只用一个元素,即|Ak|=1,所以 • m<= 2^k,即k>=lgm • 所以,在m个元素的集合M中判断一个元素,最坏情况下,提问题个数至少为lgm.
Theorem 5.2 • Theorem 5.2 Any algorithm that works by comparing keys to find the second largest in a set of n keys must do at least n+ -2 comparisons in the worst case. • Proof: Initially w(x)=1 for all x. • 1. A key has lost a comparison if and only if its weight is zero. • 2. In the first three cases, the key chosen as the winner has nonzero weight, so it has not yet lost.The adversary can give it an arbitrarily high value to make sure it wins without contradicting any of its earlier replies. • 3. The sum of the weights is always n.
The proof of Theorem 5.2 • 4. When the algorithm stops, only one key can have nonzero weight. • Let x be the key that has nonzero weight when the algorithm stops. Using fact 3, w(x)=n when the algorithm stops. • To complete the proof of the theorem, we need to show that x has directly won against at least
The proof of Theorem 5.2 • Let k be the number of comparisons when the algorithm stops. Then • QED • Now we know the lower bound (in the worst case) • Can we design an algorithm to reach the lower bound?
Exercise • 5.2a, 5.4, 5.5a, 5.6
Ex 5.4 • 5.4 In this exercise you will write an algorithm based on the heap (Section 4.8.1) for the tournament method to find max and secondLargest. a. Show that the following procedure places the max in E[1]. Array E is allocated for indexes 1,...,2n-1. (Recall that "last-=2" subtracts 2 from last.) • heapFindMax(E,n) • int last; • load n elements into E[n],...,E[2*n-1]. • for (last=2*n-2; last>=2; last-=2) • E[last/2]=max(E[last],E[last+1]); b. Explain how to determine which elements lost to the winner. c. Complete the code to find secondLargest after heapFindMax finishes.
Ex 5.4a • The tournament method based on heap structure • Suppose n=2^k, in the loop • from 2*n-2 to n, 两元素中的较大值存入 n-1 to n/2,共n/2个元素 • from n-2 to n/2, 两元素中的较大值存入 n/2-1 to n/2^2,共n/2^2个元素 • …… • from n/2^(k-2)-2 to n/2^(k-1),即 2 to 2, 两元素中的较大值存入 E[1].
Ex 5.4c • max = E[1]; • secondLargest = -∞ • i = 1; • while (i < n) • if (E[2*i] == max) • i = 2*i; • if (E[i+1] > secondLargest) • secondLargest = E[i+1]; • else i = 2*i+1; • if (E[i-1] > secondLargest) • secondLargest = E[i-1]; • return secondLargest;
Ex5.5a 锦标赛方法:A(n)=W(n)=lgn • 5.5 How many comparisons are done by the tournament method to find secondLargest on the average, • a. if n is a power of 2? • 锦标赛方法:A(n)=W(n)=lgn