Definition: A complete binary tree that is stored using the formulabased representation' - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Definition: A complete binary tree that is stored using the formulabased representation'

Description:

... node records the winner of the match played there ... Play() RePlay() 10 players. f:3. d. c:7. d:2. e:10. g:1. g. g. d. g. f. b. a:4. b:3. d. h:5. i:9. j ... – PowerPoint PPT presentation

Number of Views:74
Avg rating:3.0/5.0
Slides: 30
Provided by: dson
Category:

less

Transcript and Presenter's Notes

Title: Definition: A complete binary tree that is stored using the formulabased representation'


1
Chapter 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
2
Chapter 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
3
Chapter 10Tournament Trees
  • Winner of each match competes at the next higher
    level

c
c
e
e6
d2
c7
a
a4
b3
5 players
4
Chapter 10Tournament Trees
  • Winner at each level?

?
?
?
?
?
?
?
f3
e10
g1
h5
i9
j4
?
?
c7
d2
a4
b3
10 players
5
Chapter 10Tournament Trees
  • The champion is
  • e

e
i
e
h
c
i
e
f3
e10
g1
h5
i9
j4
a
c
c7
d2
a4
b3
10 players
6
Chapter 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
7
Chapter 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
8
Chapter 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
9
Chapter 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
10
Chapter 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
11
Chapter 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
12
Chapter 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
13
Chapter 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
14
Chapter 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
15
Chapter 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
16
Tournament 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
17
Tournament TreesApplications
  • Parallel Sorting Example Requirement is to sort
    16000 records
  • - 16 processors sort 1000 records simultaneously
  • - Merge the Results
  • - Complexity?

. . .
P1
P2
P16
P15
18
Tournament 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
19
Tournament 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
20
Tournament TreesApplications
  • Complexity
  • O(n/k) log (n/k) O(n log k)

. . .
P1
P2
P16
P15
21
Tournament 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
22
Tournament 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
23
Tournament 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
24
Tournament 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
25
Example
  • 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
26
Algorithms(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
27
Algorithms(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
28
Algorithms(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
29
Algorithms(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
Write a Comment
User Comments (0)
About PowerShow.com