Title: Definition: A complete binary tree that is stored using the formulabased representation'
1Chapter 10Tournament Trees
- Definition A complete binary tree that is
stored using the formula-based representation. - Basic Operations
- - Find/Replace the min or max element
- 2 Categories
- - Winner Trees
- - Loser Trees
-
?
?
?
e
d
c
?
a
b
5 players
2Chapter 10Tournament Trees
- Internal nodes represent a match between the
children of the node - 2 Categories
- - Winner Trees - Each internal node records the
winner of the match played there - Min Winner Tree Player with the smaller
value wins - Max Winner Tree Player with the larger value
wins - - Loser Trees Not the same as Min Winner Trees
-
?
?
?
e
d
c
?
a
b
5 players
3Chapter 10Tournament Trees
- Winner of each match competes at the next higher
level
c
c
e
e6
d2
c7
a
a4
b3
5 players
4Chapter 10Tournament Trees
?
?
?
?
?
?
?
f3
e10
g1
h5
i9
j4
?
?
c7
d2
a4
b3
10 players
5Chapter 10Tournament Trees
e
i
e
h
c
i
e
f3
e10
g1
h5
i9
j4
a
c
c7
d2
a4
b3
10 players
6Chapter 10Tournament Trees
- Algorithm for initialize
- 1. Start at lowest level, visit each n-1 nodes
once O(n) - or 2. PostOrder traversal O(n)
-
e
i
e
h
c
i
e
f3
e10
g1
h5
i9
j4
a
c
c7
d2
a4
b3
10 players
7Chapter 10Tournament Trees
- Replay
- 1. Change es value from 10 to 1
- 2. Complexity of replay is O(log n)
-
e
i
e
h
c
i
e
f3
e10
g1
h5
i9
j4
a
c
c7
d2
a4
b3
10 players
8Chapter 10Tournament Trees
- Winner at each level for the Min Winner Tree?
g
g
d
g
d
j
f
f3
e10
g1
h5
i9
j4
b
d
c7
d2
a4
b3
10 players
9Chapter 10Tournament Trees
- Sort Algorithm
- 1. Initialize Min Winner Tree
- 2. For i 0 to n
- ai min element
- min elements value ? ?
- replay
g
g
d
g
d
j
f
f3
e10
g1
h5
i9
j4
b
d
c7
d2
a4
b3
10 players
10Chapter 10Tournament Trees
- n 5
- ti gives the winner of the match played at node
i - Must be able to determine the parent of node i...
t1
t2
t3
t4
e3
e4
e5
e1
e2
5 players
11Chapter 10Tournament Trees
- n 5
- (i offset)/2 i ? LowExt
- p
- (i - LowExt n - 1)/2 i gt LowExt
t1
t2
t3
t4
e3
e4
e5
e1
e2
5 players
12Chapter 10Tournament Trees
- (i offset)/2 i ? LowExt
- p
- (i - LowExt n - 1)/2 i gt LowExt
- s ?log2(n - 1)?
- n 5 External Nodes
- s 2
t1
t2
t3
t4
e3
e4
e5
e1
e2
5 players
13Chapter 10Tournament Trees
- (i offset)/2 i ? LowExt
- p
- (i - LowExt n - 1)/2 i gt LowExt
- s ?log2(n - 1)?
- s 2
- The number of internal nodes at the lowest
- level is n - 2s
- The number of external nodes at the lowest
- level, LowExt, is 2(n - 2s)
- LowExt 2
- offset 2s1 -1
t1
t2
t3
t4
e3
e4
e5
e1
e2
5 players
14Chapter 10Tournament Trees
- (i offset)/2 i ? LowExt
- p
- (i - LowExt n - 1)/2 i gt LowExt
- s ?log2(n - 1)?
- The number of internal nodes at the lowest
- level is n - 2s
- LowExt 2(n - 2s)
- offset 2s1 -1
g
g
d
g
d
j
f
f3
e10
g1
h5
i9
j4
b
d
c7
d2
a4
b3
10 players
15Chapter 10Tournament Trees
- (i offset)/2 i ? LowExt
- p
- (i - LowExt n - 1)/2 i gt LowExt
- s ?log2(n - 1)?
- The number of internal nodes at the lowest
- level is n - 2s
- LowExt 2(n - 2s)
- offset 2s1 -1
- Compare with p. 469
- Initialize()
- Play()
- RePlay()
g
g
d
g
d
j
f
f3
e10
g1
h5
i9
j4
b
d
c7
d2
a4
b3
10 players
16Tournament TreesApplications
- Parallel Sorting Example Requirement is to sort
16000 records -
- - 16 Processors Sort Simultaneously
- - Merge the Results
-
- 1000 1000 1000
1000 1000
P1
P2
P3
P16
P15
17Tournament TreesApplications
- Parallel Sorting Example Requirement is to sort
16000 records -
- - 16 processors sort 1000 records simultaneously
- - Merge the Results
- - Complexity?
-
-
. . .
P1
P2
P16
P15
18Tournament TreesApplications
- Assume a complexity of O(n log n) for the sort
algorithm on each processor - Since they sort in parallel across k processors
its - O(n/k) log (n/k) O(whatevertimeittakestomerge
) -
-
. . .
P1
P2
P16
P15
19Tournament TreesApplications
- Merge Algorithm
- 1. Initialize a min winner tree O(k), where k
is the number of processors - 2. Move the winner to the output file O(1)
- 3. Replay O(log k)
- 4. Do this n times O(n log k)
-
-
. . .
P1
P2
P16
P15
20Tournament TreesApplications
- Complexity
- O(n/k) log (n/k) O(n log k)
-
-
. . .
P1
P2
P16
P15
21Tournament TreesLoser Trees
- Reduce the work needed to determine the outcome
in a RePlay - Record the LOSER of each match rather than the
WINNER - Overall winner recorded at the top
-
-
c
c
e
c
e
a
d
e6
e6
d2
d2
c7
c7
a
b
a4
b3
a4
b3
5 players - Max Winner Tree
5 players - Loser Tree
22Tournament TreesLoser Trees
- Now, when the value of c changes to 2, theres a
rematch at the parent of c, and c loses. Notice
the pattern in the replay -
-
e
c
a
c
e
c
e
a
d
c
e6
e6
d2
d2
c7
c7
a
b
c2
a4
b3
a4
b3
5 players - Max Winner Tree
5 players - Loser Tree
23Tournament TreesLoser Trees
- Now, when the value of c changes to 2, theres a
rematch at the parent of c, and c loses. -
-
c
c
e
c
e
a
d
c
e6
e6
d2
d2
c7
c7
a
b
c2
a4
b3
a4
b3
5 players - Max Winner Tree
5 players - Loser Tree
24Tournament TreesApplications
- Bin Packing Problem
- Bins have a capacity c
- n objects need to be packed into the bins
- Object i requires si units of capacity,
- 0 lt si ? c
- A feasible packing is an assignment of objects
s.t. no bins capacity is exceeded - A feasible packing that uses the fewest bins is
optimal
10
10
10
10
10
10
25Example
- Example The following items have to go in the
bins. - 1. Find a feasible solution.
- 2. Find the optimal solution.
- f 5
- e 9
- c 2
- b 3
- d 6
- a 5
-
10
10
10
10
10
10
26Algorithms(Approximation Algorithms)
- First Fit Objects are considered for packing in
the order in which they arrive. - f 5
- e 9
- c 2
- b 3
- d 6
- a 5
-
10
10
10
10
10
10
27Algorithms(Approximation Algorithms)
- Best Fit Objects are packed into the bin with
the least capacity available into which they will
fit (cAvailj ? si) - f 5
- e 9
- c 2
- b 3
- d 6
- a 5
-
10
10
10
10
10
10
28Algorithms(Approximation Algorithms)
- First Fit Decreasing Same as First Fit, but
objects are first reordered s.t. si ? si1 - f 5
- e 9
- c 2
- b 3
- d 6
- a 5
-
10
10
10
10
10
10
29Algorithms(Approximation Algorithms)
- Best Fit Decreasing Same as Best Fit, but
objects are first reordered s.t. si ? si1 - f 5
- e 9
- c 2
- b 3
- d 6
- a 5
-
10
10
10
10
10
10