Presentation is loading. Please wait.

Presentation is loading. Please wait.

Problem: Selection Design and Analysis: Adversary Arguments The selection problem >  Finding max and min Adversary Arguments( 反论 )  Suppose you are playing.

Similar presentations


Presentation on theme: "Problem: Selection Design and Analysis: Adversary Arguments The selection problem >  Finding max and min Adversary Arguments( 反论 )  Suppose you are playing."— Presentation transcript:

1 Problem: Selection Design and Analysis: Adversary Arguments The selection problem >  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 >

2 CH05.2 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!

3 CH05.3 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

4 CH05.4 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]

5 CH05.5 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.

6 CH05.6 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.

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

8 CH05.8 The adversary strategy to give us the worst case

9 CH05.9 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

10 CH05.10 Table 5.2 An example of the adversary strategy for max and min

11 CH05.11 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

12 CH05.12 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.

13 CH05.13 “ 是或非 ” 判断元素 设用 k 个 “ 是或非 ” 的问题在 m 个元素的集合 M 中判 断一个元素. 第一个是或非问题把 M 分成两部分, 设较大部分为 A1, 则 A1|>=m/2 第二个是或非问题把 M 分成两部分, 设较大部分为 A2, 则 A2|>=m/2^2 …… |Ak|>= m/2^k 设 Ak 只用一个元素, 即 |Ak|=1, 所以 m =lgm 所以, 在 m 个元素的集合 M 中判断一个元素, 最坏情 况下, 提问题个数至少为 lgm.

14 CH05.14 八个元素的锦标赛

15 CH05.15 八个元素的锦标赛比较次数为七 (find max)

16 CH05.16 八个元素的堆形结构

17 CH05.17 Adversary rules

18 CH05.18 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.

19 CH05.19 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

20 CH05.20 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?

21 CH05.21 Example 5.2 The adversary strategy in action

22 CH05.22 Exercise 5.2a, 5.4, 5.5a, 5.6

23 CH05.23 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.

24 CH05.24 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].

25 CH05.25 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;

26 CH05.26 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

27 CH05.27 Ex 5.6

28 CH05.28 Ex 5.6


Download ppt "Problem: Selection Design and Analysis: Adversary Arguments The selection problem >  Finding max and min Adversary Arguments( 反论 )  Suppose you are playing."

Similar presentations


Ads by Google