Sie sind auf Seite 1von 10

CSCI 5870 Data Structures and Algorithms

1 Title

2 Finding #2
Finding the Second-Largest Element

3 Observations
Robert W. Kramer
(1)
4 Observations
(2)
5 Reducing
Counts
6 FindSecond
Algorithm
7 How Many
Comparisons?
8 Another
Adversary
9 A Lower
Bound
10 What Next?
Finding the Second-Largest Element

1 Title
Suppose now we want the second-largest element
2 Finding #2

3 Observations Naı̈ve algorithm


(1)
4 Observations
1. Find largest element
(2)
2. Discard it
5 Reducing
Counts 3. Find largest element
6 FindSecond
Algorithm
7 How Many
Comparisons?
8 Another Step 1 performs n − 1 comparisons
Adversary
Step 3 performs n − 2 comparisons
9 A Lower
Bound
10 What Next? Can we do better?

What is the best we can do?


Key Observations (1)

1 Title
What tags does the second-largest element have?
2 Finding #2
⊲ W , unless there are only two elements
3 Observations ⊲L
(1)
4 Observations
(2) Where does it get the L tag from?
5 Reducing ⊲ Must have lost to largest element
Counts
6 FindSecond
Algorithm Does second-largest have to be compared against largest?
7 How Many ⊲ Yes!
Comparisons?
8 Another
⊲ Two elements without L tags – bad!
Adversary
9 A Lower
Bound
10 What Next?
Key Observations (2)

1 Title
How many comparisons must be made against largest element?
2 Finding #2 ⊲ Naı̈ve algorithm does n − 1 (why?)
3 Observations
(1) What about a tournament?
4 Observations
(2)
5 Reducing 8
Counts
6 FindSecond
Algorithm 5 8
7 How Many
Comparisons?
8 Another
3 5 8 7
Adversary
9 A Lower
Bound
3 1 4 5 8 2 7 6
10 What Next?

In a tournament, winner competes against ⌈lg n⌉ opponents


Reducing the number of comparisons

1 Title
Winner competes against ⌈lg n⌉ opponents. So what?
2 Finding #2

3 Observations
One of those opponents must be the second-largest
(1) ⊲ Needs an L tag
4 Observations
(2)
5 Reducing
Suppose each element keeps a list of defeated opponents
Counts
6 FindSecond
Algorithm
Largest element of winner’s oppponent list is second-largest element
7 How Many
Comparisons?
8 Another
Adversary
9 A Lower
Bound
10 What Next?
The FindSecond Algorithm

1 Title
Algorithm 1 The FindSecond Algorithm
2 Finding #2 1: procedure FindSecond(Comparable[] L,int n)
Competitor c
CompetitorQueue q
3 Observations
2: for i ← 0 to n − 1 do ⊲ Add elements to queue
(1)
3: c.value ← L[i]
4 Observations 4: c.list = ()
(2) 5: q.enqueue(c)
5 Reducing 6: end for
Counts 7: while q.size > 1 do ⊲ Run the tournament
8: a ← q.dequeue()
6 FindSecond 9: b ← q.dequeue()
Algorithm 10: if a.value < b.value then
7 How Many 11: b.list.append(a.value) ⊲ Loser appended to winner’s list
Comparisons? 12: q.enqueue(b) ⊲ Winner is requeued
13: else
8 Another 14: a.list.append(b.value)
Adversary
15: q.enqueue(a)
9 A Lower 16: end if
Bound 17: end while
10 What Next? 18: a ← q.dequeue() ⊲ Last element is largest
19: return FindMax(a.list, |a.list|) ⊲ Search its list for second-largest
20: end procedure
How Many Comparisons Are Done?

1 Title
Clearly, the tournament performs n − 1 comparisons
2 Finding #2 ⊲ Each comparison removes an element
3 Observations
(1) FindMax on line 20 does ⌈lg n⌉ − 1 comparisons
4 Observations
(2)
⊲ Winner’s list has ⌈lg n⌉ elements
5 Reducing
Counts Total comparison count: n + ⌈lg n⌉ − 2
6 FindSecond
Algorithm
7 How Many Is this optimal?
Comparisons?
8 Another
Adversary
9 A Lower
Bound
10 What Next?
Yet Another Adversary Argument

1 Title
Adversary uses coins to track previous outcomes
2 Finding #2 ⊲ Each element starts with one coin
3 Observations
⊲ Losers give their coins to winners
(1) ⊲ Moving coins gives information
4 Observations
(2)
⊲ Adversary makes decision in favor of least coin movement
5 Reducing ⊲ Tournament decides largest element
Counts ⊲ After tournament, winner has n coins, all others have 0
6 FindSecond
Algorithm
7 How Many Suppose tournament requires R rounds, and let nJ be the number
Comparisons?
of coins winner holds after round J.
8 Another
Adversary
9 A Lower Since winner decided by number of coins, nR ≤ 2nR−1 (why?)
Bound
10 What Next?
Extending this, n = nR ≤ 2nR−1 ≤ 22nR−2 ≤ · · · ≤ 2k nR−k ≤
· · · ≤ 2 R n 0 = 2R

Thus, n ≤ 2R and R ≥ lg n
A Lower Bound

1 Title
What does this tell us?
2 Finding #2

3 Observations
Winner must be compared against at least ⌈lg n⌉ elements
(1)
4 Observations
(2)
Since second-largest must be in that set of lg n elements. . .
5 Reducing
Counts Any algorithm to find the second-largest element must perform
6 FindSecond
Algorithm n − 1 + ⌈lg n⌉ − 1 = n + ⌈lg n⌉ − 2 comparisons
7 How Many
Comparisons?
Since our algorithm does at most that many comparisons, it is op-
8 Another
Adversary timal.
9 A Lower
Bound
10 What Next?
What Next?

1 Title
How hard is it to find the third-largest element?
2 Finding #2
Fourth-largest?
3 Observations k-th largest?
(1)
4 Observations
(2) The hardest should be the median element
5 Reducing
Counts
Next up. . . finding the median in Θ(n) time.
6 FindSecond
Algorithm
7 How Many
Comparisons?
8 Another
Adversary
9 A Lower
Bound
10 What Next?

Das könnte Ihnen auch gefallen