数据结构、算法与应用 第10章

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

• Get winner
O(1) time
• Remove/replace winner and replay
O(log n) time more precisely Theta(log n)
数据结构算法与应用
28
10.3 The CLASS WinnerTree
The left-most internal node at the lowest level is numbered 2s where s = ⎣log 2 ( n − 1) ⎦ . Therefore the number of internal nodes at the lowest level is n-2s, and the number LowExt of external nodes at the lowest level is twice this number.
35
Min Loser Tree For 16 Players
3
4 4 3 6
8 8 1 5 7 3 2 6 9 4 5 2 5 8
36
Min Loser Tree For 16 Players
3
6
1
4 4 3 6
8 8 1
5 5 7
7 3 2 6 9 4 5 2 5 8
37
Min Loser Tree For 16 Players
Sorted array.
20
Sort 16 Numbers
2 3
2
3
3
4
2
3 4 1 3 2 2 6
6 8 1
5 5 7
3 3 2
6 6 9
4 4 5
2 2 5
5 8
Sorted array.
21
Sort 16 Numbers
2 3
2
3
3
4
2
3 4 1 3 2 2 6
6 8 1
5 5 7
3 3 2
• Remove winner and replay n times.
O(n log n) time
• Total sort time is O(n log n). • Actually Theta(n log n).
27
10.2 The ADT WinnerTree • Initialize
O(n) time
6 8 6
1 5 7
3 3 2
2 6 9
4 4 5
2 2 5
5 8
Opponent is player who lost last match played at this node.
34
Loser Tree
Each match node stores the match loser rather than the match winner.
1 1
2
3
1
2
2
3 4 3 6
6 8 1
1 5 7
3 3 2
2 6 9
4 4 5
2 2 5
5 8
Replace winner with 6.
31
Replace Winner And Replay
1 1
2
3
1
2
2
3 4 3 6
6 8 6
1 5 7
3 3 2
2 6 9
4 4 5
2 2 5
5 8
Replay matches on path to root.
5
Winner Tree For 16 Players
1 1
2
3
1
2
2
3 4 3 6
6 8 1
1 5 7
3 3 2
2 6 9
4 4 5
2 2 5
5 8
height is log2 n (excludes player level)
6
Complexity Of Initialize
• O(1) time to play match at each match node. • n - 1 match nodes. • O(n) time to initialize n player winner tree.
4
3
3
4
5
3 4 1 3 2 2 6
6 8 3 1
5 5 7
3 3 2
6 6 9
4 4 5
5 2 5
5 8
Sorted array.
26
Time To Sort
• Initialize winner tree.
O(n) time
• Remove winner and replay.
O(log n) time
44
1 2 2 3
Winner
3
2
6
3 5
4
5
4 4 3 6
8 8 9 1
5 9 5 7
7 3 2
6 6 9
9 4 5
5 2 5
8 8
Replace winner with 9 and replay matches. 45
Complexity Of Replay
• One match at each level that has a match node. • O(log n) • More precisely Theta(log n).
• Complete binary tree with n external nodes and n - 1 internal nodes. • External nodes represent tournament players. • Each internal node represents a match played between its two children; the winner of the match is stored at the internal node. • Root has overall winner.
Sort 16 Numbers
2 3
2
3
3
4
2
3 4 1 3 2 6
6 8 1
5 5 7
3 3 2
6 6 9
4 4 5
2 2 5
5 8
Sorted array.
19
Sort 16 Numbers
2 3
2
3
3
4
2
3 4 1 3 2 6
6 8 1
5 5 7
3 3 2
6 6 9
4 4 5
2 2 5
5 8
2 3
2
6
3
4
5
4 4 3 6
8 8 1
5 5 7
7 3 2
6 6 9
9 4 5
5 2 5
8 8
42
1 2
Winner
3
2
6
3
4
5
4 4 3 6
8 8 1
5 5 7
7 3 2
6 6 9
9 4 5
5 2 5
8 8
43
Complexity Of Loser Tree Initialize
• • • • One match at each match node. One store of a left child winner. Total time is O(n). More precisely Theta(n).
6 6 9
4 4 5
5 2 5
5 8
Sorted array.
22
Sort 16 Numbers
2 3
2
3
3
4
5
3 4 1 3 2 2 6
6 8 1
5 5 7
3 3 2
6 6 9
4 4 5
5 2 5
5 8
Sorted array.
23
Sort 16 Numbers
2 3
4
3
3
4
5
3 4 1 3 2 2 6
46
More Tournament Tree Applications
• k-way merging of runs during an external merge sort • Truck loading
47
Truck Loading
n packages to be loaded into trucks each package has a weight each truck has a capacity of c tons minimize number of trucks
1 3
2
6
3
4
5
4 4 3 6
8 8 1
5 5 7
7 3 2
6 6 9
9 4 5
5 2 5
8 8
40
Min Loser Tree For 16 Players
1 3
2
6
3
4
5
4 4 3 6
8 8 1
5 5 7
7 3 2
6 6 9
9 4 5
5 2 5
8 8
41
Min Loser Tree For 16 Players
1 3
6
3
2
4 4 3 6
8 8 1
5 5 7
7 3 2
6 6 9
9 4 5 2 5 8
38
Min Loser Tree For 16 Players
1 3
2
6
3
4
2
4 4 3 6
8 8 1
5 5 7
7 3 2
6 6 9
9 4 5
5 2 5
8 8
39
Min Loser Tree For 16 Players
6 8 1
5 5 7
3 3 2
2 6 9
4 4 5
2 2 5
5 8
Sorted array.
13
Sort 16 Numbers
1 3
2
3
3
2
2
3 4 1 3 6
6 8 1
5 5 7
3 3 2
2 6 9
4 4 5
2 2 5
5 8
Sorted array.
14
Sort 16 Numbers
2 3
2
3
3
Winner Tree For 16 Players
player
match node
4
Winner Tree For 16 Players
1 1
2
3
1
2
2
3 4 3 6
6 8 1
1 5 7
3 3 2
2 6 9
4 4 5
2 2 5
5 8
Smaller element wins => min winner tree.
1
2
2
3 4 3 6
6 8 1
1 5 7
3 3 2
2 6 9
4 4 5
2 2 5
5 8
9
Sort 16 Numbers
1 1
2
3
1
2
2
3 4 3 6
6 8 1
1 5 7
3 3 2
2 6 9
4 4 5
2 2 5
5 8
10
Sort 16 Numbers
1 1
2
3
1
2
2
3 4 1 3 6
6 8 1
1 5 7
3 3 2
2 6 9
4 4 5
2 2 5
5 8
Sorted array.
11
Sort 16 Numbers
1 1
2
3
1
2
2
3 4 1 3 6
6 8 1
5 5 7
3 3 2
2 6 9
4 4 5
2 2 5
5 8
Sorted array.
12
Sort 16 Numbers
1 1
2
百度文库
3
3
2
2
3 4 1 3 6
32
Replace Winner And Replay
1 1
2
3
1
2
2
3 4 3 6
6 8 6
1 5 7
3 3 2
2 6 9
4 4 5
2 2 5
5 8
Replay matches on path to root.
33
Replace Winner And Replay
1 1
2
3
1
2
2
3 4 3 6
2 3
2
3
3
2
2
3 4 1 3 2 6
6 8 1
5 5 7
3 3 2
6 6 9
4 4 5
2 2 5
5 8
Sorted array.
17
Sort 16 Numbers
2 3
2
3
3
4
2
3 4 1 3 2 6
6 8 1
5 5 7
3 3 2
6 6 9
4 4 5
2 2 5
5 8
Sorted array.
18
7
Applications
Sorting. Put elements to be sorted into a winner tree. Repeatedly extract the winner and replace by a large value.
8
Sort 16 Numbers
1 1
2
3
Chapter 10 Tournament Trees
信息科学与技术学院 黄方军
huangfj@mail.sysu.edu.cn
东校区实验中心B502
数据结构算法与应用
1
10.1 Introduction
• • Winner trees. Loser Trees.
数据结构算法与应用
2
Winner Trees
数据结构算法与应用
29
10.3 The CLASS WinnerTree Let offset = 2s+1-1. Then for any external nodes e[i], its parent t[p] is given by
数据结构算法与应用
30
Replace Winner And Replay
3
2
2
3 4 1 3 6
6 8 1
5 5 7
3 3 2
2 6 9
4 4 5
2 2 5
5 8
Sorted array.
15
Sort 16 Numbers
2 3
2
3
3
2
2
3 4 1 3 2 6
6 8 1
5 5 7
3 3 2
2 6 9
4 4 5
2 2 5
5 8
Sorted array.
16
Sort 16 Numbers
6 8 1
5 5 7
3 3 2
6 6 9
4 4 5
5 2 5
5 8
Sorted array.
24
Sort 16 Numbers
3 3
4
3
3
4
5
3 4 1 3 2 2 6
6 8 1
5 5 7
3 3 2
6 6 9
4 4 5
5 2 5
5 8
Sorted array.
25
Sort 16 Numbers
3 3
48
Truck Loading
n = 5 packages weights [2, 5, 6, 3, 4] truck capacity c = 10 Load packages from left to right. If a package doesn’t fit into current truck, start loading a new truck.
相关文档
最新文档