+ All Categories
Home > Documents > Problem: Selection Design and Analysis: Adversary Arguments The selection problem > Finding max and...

Problem: Selection Design and Analysis: Adversary Arguments The selection problem > Finding max and...

Date post: 22-Dec-2015
Category:
View: 230 times
Download: 7 times
Share this document with a friend
28
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( 反反 ) 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 >>
Transcript
Page 1: Problem: Selection Design and Analysis: Adversary Arguments The selection problem >  Finding max and min Adversary Arguments( 反论 )  Suppose you are playing.

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

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

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!

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

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 > yYes: x > y andNo: x not > y should provide about the same amount information

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

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 unsortedwhere 1 <= k <= n

• Finding an element with rank k is equivalent to answering the question: If the array were sorted in nondecreasing orderwhich 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]

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

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.

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

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

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

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.

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

CH05.8

The adversary strategy to give us the worst case

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

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 informationnow 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

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

CH05.10

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

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

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?

• Exercisedesign 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

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

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.

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

CH05.13

“是或非”判断元素• 设用 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.

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

CH05.14

八个元素的锦标赛

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

CH05.15

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

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

CH05.16

八个元素的堆形结构

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

CH05.17

Adversary rules

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

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.

nlg

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

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

rules sadversary' by theThen key. undefeated

previously aagainst by x won comparisonkth the

afterjust w(x)= Let w keys.distinct lgn k

111 2 kkkk wwww

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

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?

kkk wwn 22 0

nkknk lg,integeran is since and,lg Thus

.determinedcorrectly be est tosecondLargfor again losemust

max lost to that keys lg theof onebut allThen

distinct course of are here counted keys The

n

k

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

CH05.21

Example 5.2 The adversary strategy in action

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

CH05.22

Exercise

• 5.2a, 5.4, 5.5a, 5.6

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

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.

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

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

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

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;

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

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

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

CH05.27

Ex 5.6

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

CH05.28

Ex 5.6

577.0lni

1

ln23)1(ln21i

121-n

)1i

2(1 )

i

21

i

2(21A(n)

keys. ifirst theamong keyslargest two theof one is E[i]

ifonly and if done are scomparison Two done. are

scomparison or two oneeither loop he through tEach time b)

order. increasing :case worst theof One

3-2nloop) 2)(in the-2(nloop) the1(before

scomparison ofnumber case Worst a)

n

1i

n

3i

n

3i

n

3i

nHere

nnnn


Recommended