SlideShare a Scribd company logo
Tournament Trees 
Winner trees. 
Loser Trees.
Winner Trees 
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.
Winner Tree For 16 Players 
player match node
Winner Tree For 16 Players 
1 
1 2 
3 1 2 2 
3 6 1 3 2 4 2 5 
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 
Smaller element wins => min winner tree.
Winner Tree For 16 Players 
1 
1 2 
3 1 2 2 
3 6 1 3 2 4 2 5 
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 
height is log2 n (excludes player level)
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.
Applications 
Sorting. 
Put elements to be sorted into a winner 
tree. 
Repeatedly extract the winner and 
replace by a large value.
Sort 16 Numbers 
1 
1 2 
3 1 2 2 
3 6 1 3 2 4 2 5 
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
Sort 16 Numbers 
1 
1 2 
3 1 2 2 
3 6 1 3 2 4 2 5 
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
Sort 16 Numbers 
1 
1 2 
3 1 2 2 
3 6 1 3 2 4 2 5 
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 
1 
Sorted array.
Sort 16 Numbers 
1 
1 2 
3 1 2 2 
3 6 5 3 2 4 2 5 
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 
1 
Sorted array.
Sort 16 Numbers 
1 
1 2 
3 2 2 
3 6 
3 
5 3 2 4 2 5 
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 
1 
Sorted array.
Sort 16 Numbers 
1 
3 2 
3 2 2 
3 6 
3 
5 3 2 4 2 5 
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 
1 
Sorted array.
Sort 16 Numbers 
2 
3 2 
3 2 2 
3 6 
3 
5 3 2 4 2 5 
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 
1 
Sorted array.
Sort 16 Numbers 
3 2 
3 2 2 
3 6 
3 
5 3 2 4 2 5 
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 
2 
2 
1 Sorted array.
Sort 16 Numbers 
3 2 
3 2 2 
3 6 
3 
5 3 6 4 2 5 
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 
2 
2 
1 Sorted array.
Sort 16 Numbers 
3 2 
3 4 2 
3 6 
3 
5 3 6 4 2 5 
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 
2 
2 
1 Sorted array.
Sort 16 Numbers 
3 2 
3 4 2 
3 6 
3 
5 3 6 4 2 5 
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 
2 
2 
1 Sorted array.
Sort 16 Numbers 
3 2 
3 4 2 
3 6 
3 
5 3 6 4 2 5 
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 
2 
2 
1 Sorted array.
Sort 16 Numbers 
2 
3 2 
3 4 2 
3 6 
3 
5 3 6 4 2 5 
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 
1 2 2 
Sorted array.
Sort 16 Numbers 
2 
3 2 
3 4 2 
3 6 
3 
5 3 6 4 5 5 
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 
1 2 2 
Sorted array.
Sort 16 Numbers 
2 
3 2 
3 4 5 
3 6 
3 
5 3 6 4 5 5 
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 
1 2 2 
Sorted array.
Sort 16 Numbers 
2 
3 4 
3 4 5 
3 6 
3 
5 3 6 4 5 5 
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 
1 2 2 
Sorted array.
Sort 16 Numbers 
3 
3 4 
3 4 5 
3 6 
3 
5 3 6 4 5 5 
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 
1 2 2 
Sorted array.
Sort 16 Numbers 
3 
3 4 
3 4 5 
3 6 
3 
5 3 6 4 5 5 
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 
1 2 2 3 
Sorted array.
Time To Sort 
• Initialize winner tree. 
 O(n) time 
• Remove winner and replay. 
 O(log n) time 
• Remove winner and replay n times. 
 O(n log n) time 
• Total sort time is O(n log n). 
• Actually Theta(n log n).
Winner Tree Operations 
• Initialize 
 O(n) time 
• Get winner 
 O(1) time 
• Remove/replace winner and replay 
 O(log n) time 
 more precisely Theta(log n)
Replace Winner And Replay 
1 
1 2 
3 1 2 2 
3 6 1 3 2 4 2 5 
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 
Replace winner with 6.
Replace Winner And Replay 
1 
1 2 
3 1 2 2 
3 6 1 3 2 4 2 5 
4 3 6 8 6 5 7 3 2 6 9 4 5 2 5 8 
Replay matches on path to root.
Replace Winner And Replay 
1 
1 2 
3 1 2 2 
3 6 1 3 2 4 2 5 
4 3 6 8 6 5 7 3 2 6 9 4 5 2 5 8 
Replay matches on path to root.
Replace Winner And Replay 
1 
1 2 
3 1 2 2 
3 6 1 3 2 4 2 5 
4 3 6 8 6 5 7 3 2 6 9 4 5 2 5 8 
Opponent is player who lost last match played at this node.
Loser Tree 
Each match node stores the match 
loser rather than the match winner.
Min Loser Tree For 16 Players 
4 
3 
8 
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
Min Loser Tree For 16 Players 
4 
6 
8 
3 
5 
1 
7 
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
Min Loser Tree For 16 Players 
4 
6 
8 
3 
5 
3 
7 
1 
6 
2 
9 
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
Min Loser Tree For 16 Players 
4 
6 
8 
3 
5 
3 
7 
2 
5 
2 
8 
1 
6 
4 
9 
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
Min Loser Tree For 16 Players 
4 
6 
8 
3 
5 
3 
7 
2 
5 
5 
8 
1 
6 
4 
9 
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
Min Loser Tree For 16 Players 
4 
6 
8 
3 
5 
3 
7 
2 
5 
5 
8 
1 
6 
4 
9 
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
Min Loser Tree For 16 Players 
4 
6 
8 
3 
5 
3 
7 
2 
5 
5 
8 
2 
6 
4 
9 
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
4 
6 
8 
3 
5 
3 
7 
1 Winner 
2 
5 
5 
8 
2 
6 
4 
9 
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
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).
Winner 
4 
6 
3 
8 
5 
3 
7 
2 
5 
5 
8 
1 
2 
2 
6 
4 
9 
9 
5 
9 
3 
4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 
Replace winner with 9 and replay matches.
Complexity Of Replay 
• One match at each level that has a match 
node. 
• O(log n) 
• More precisely Theta(log n).
More Tournament Tree 
Applications 
• k-way merging of runs during an external 
merge sort 
• Truck loading
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
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.
Truck Loading 
n = 5 packages 
weights [2, 5, 6, 3, 4] 
truck capacity c = 10 
truck1 = [2, 5] 
truck2 = [6, 3] 
truck3 = [4] 
uses 3 trucks when 2 trucks suffice
Truck Loading 
n = 5 packages 
weights [2, 5, 6, 3, 4] 
truck capacity c = 10 
truck1 = [2, 5, 3] 
truck2 = [6, 4]
Bin Packing 
• n items to be packed into bins 
• each item has a size 
• each bin has a capacity of c 
• minimize number of bins
Bin Packing 
Truck loading is same as bin packing. 
Truck is a bin that is to be packed (loaded). 
Package is an item/element. 
Bin packing to minimize number of bins is NP-hard. 
Several fast heuristics have been proposed.
Bin Packing Heuristics 
• First Fit. 
 Bins are arranged in left to right order. 
 Items are packed one at a time in given order. 
 Current item is packed into leftmost bin into 
which it fits. 
 If there is no bin into which current item fits, 
start a new bin.
First Fit 
n = 4 
weights = [4, 7, 3, 6] 
capacity = 10 
Pack red item into first bin.
First Fit 
n = 4 
weights = [4, 7, 3, 6] 
capacity = 10 
Pack blue item next. 
Doesn’t fit, so start a new bin.
First Fit 
n = 4 
weights = [4, 7, 3, 6] 
capacity = 10
First Fit 
n = 4 
weights = [4, 7, 3, 6] 
capacity = 10 
Pack yellow item into first 
bin.
First Fit 
n = 4 
weights = [4, 7, 3, 6] 
capacity = 10 
Pack green item. 
Need a new bin.
First Fit 
n = 4 
weights = [4, 7, 3, 6] 
capacity = 10 
Not optimal. 
2 bins suffice.
Bin Packing Heuristics 
• First Fit Decreasing. 
 Items are sorted into decreasing order. 
 Then first fit is applied.
Bin Packing Heuristics 
• Best Fit. 
 Items are packed one at a time in given order. 
 To determine the bin for an item, first 
determine set S of bins into which the item fits. 
 If S is empty, then start a new bin and put item 
into this new bin. 
 Otherwise, pack into bin of S that has least 
available capacity.
Bin Packing Heuristics 
• Best Fit Decreasing. 
 Items are sorted into decreasing order. 
 Then best fit is applied.
Performance 
• For first fit and best fit: 
Heuristic Bins <= (17/10)(Minimum Bins) + 2 
• For first fit decreasing and best fit 
decreasing: 
Heuristic Bins <= (11/9)(Minimum Bins) + 4
Complexity Of First Fit 
Use a max tournament tree in which 
the players are n bins and the value 
of a player is the available capacity 
in the bin. 
O(n log n), where n is the number of 
items.

More Related Content

What's hot

04 - 15 Jan - Heap Sort
04 - 15 Jan - Heap Sort04 - 15 Jan - Heap Sort
04 - 15 Jan - Heap Sort
Neeldhara Misra
 
04 - 15 Jan - Heap Sort
04 - 15 Jan - Heap Sort04 - 15 Jan - Heap Sort
04 - 15 Jan - Heap Sort
Neeldhara Misra
 
maXbox Regular Expression in PI
maXbox Regular Expression in PImaXbox Regular Expression in PI
maXbox Regular Expression in PI
Max Kleiner
 
Work-stealing Tree Data Structure
Work-stealing Tree Data StructureWork-stealing Tree Data Structure
Work-stealing Tree Data Structure
Aleksandar Prokopec
 
PyCon Ukraine 2017: Operational Transformation
PyCon Ukraine 2017: Operational Transformation PyCon Ukraine 2017: Operational Transformation
PyCon Ukraine 2017: Operational Transformation
Max Klymyshyn
 
Introduction to r
Introduction to rIntroduction to r
Introduction to r
Ghassan Al-Yafie
 
Heap sort
Heap sort Heap sort
Ctrie Data Structure
Ctrie Data StructureCtrie Data Structure
Ctrie Data Structure
Aleksandar Prokopec
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
Amit Kumar Rathi
 
heapsort_bydinesh
heapsort_bydineshheapsort_bydinesh
heapsort_bydinesh
Dinesh Kumar
 
Return Oriented Programming (ROP chaining)
Return Oriented Programming (ROP chaining)Return Oriented Programming (ROP chaining)
Return Oriented Programming (ROP chaining)
Abhinav Chourasia, GMOB
 
The Ring programming language version 1.10 book - Part 45 of 212
The Ring programming language version 1.10 book - Part 45 of 212The Ring programming language version 1.10 book - Part 45 of 212
The Ring programming language version 1.10 book - Part 45 of 212
Mahmoud Samir Fayed
 
Ds0601 stack
Ds0601 stackDs0601 stack
Ds0601 stack
virajrana
 
lecture 4
lecture 4lecture 4
lecture 4sajinsc
 
Exploring Parallel Merging In GPU Based Systems Using CUDA C.
Exploring Parallel Merging In GPU Based Systems Using CUDA C.Exploring Parallel Merging In GPU Based Systems Using CUDA C.
Exploring Parallel Merging In GPU Based Systems Using CUDA C.
Rakib Hossain
 

What's hot (20)

04 - 15 Jan - Heap Sort
04 - 15 Jan - Heap Sort04 - 15 Jan - Heap Sort
04 - 15 Jan - Heap Sort
 
04 - 15 Jan - Heap Sort
04 - 15 Jan - Heap Sort04 - 15 Jan - Heap Sort
04 - 15 Jan - Heap Sort
 
maXbox Regular Expression in PI
maXbox Regular Expression in PImaXbox Regular Expression in PI
maXbox Regular Expression in PI
 
Work-stealing Tree Data Structure
Work-stealing Tree Data StructureWork-stealing Tree Data Structure
Work-stealing Tree Data Structure
 
PyCon Ukraine 2017: Operational Transformation
PyCon Ukraine 2017: Operational Transformation PyCon Ukraine 2017: Operational Transformation
PyCon Ukraine 2017: Operational Transformation
 
Introduction to r
Introduction to rIntroduction to r
Introduction to r
 
Heap sort
Heap sort Heap sort
Heap sort
 
Ctrie Data Structure
Ctrie Data StructureCtrie Data Structure
Ctrie Data Structure
 
Lec10
Lec10Lec10
Lec10
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
 
heapsort_bydinesh
heapsort_bydineshheapsort_bydinesh
heapsort_bydinesh
 
Return Oriented Programming (ROP chaining)
Return Oriented Programming (ROP chaining)Return Oriented Programming (ROP chaining)
Return Oriented Programming (ROP chaining)
 
The Ring programming language version 1.10 book - Part 45 of 212
The Ring programming language version 1.10 book - Part 45 of 212The Ring programming language version 1.10 book - Part 45 of 212
The Ring programming language version 1.10 book - Part 45 of 212
 
Ds0601 stack
Ds0601 stackDs0601 stack
Ds0601 stack
 
lecture 4
lecture 4lecture 4
lecture 4
 
Wepwhacker !
Wepwhacker !Wepwhacker !
Wepwhacker !
 
Binsort
BinsortBinsort
Binsort
 
Lec9
Lec9Lec9
Lec9
 
Exploring Parallel Merging In GPU Based Systems Using CUDA C.
Exploring Parallel Merging In GPU Based Systems Using CUDA C.Exploring Parallel Merging In GPU Based Systems Using CUDA C.
Exploring Parallel Merging In GPU Based Systems Using CUDA C.
 
Algorithms
AlgorithmsAlgorithms
Algorithms
 

Similar to Lec25

Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
Edward Blurock
 
Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...
BhumikaBiyani1
 
Sorting
SortingSorting
Sorting
Sameer Memon
 
Ordering fractions
Ordering fractionsOrdering fractions
Pc12 sol c08_ptest
Pc12 sol c08_ptestPc12 sol c08_ptest
Pc12 sol c08_ptestGarden City
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
Zaid Hameed
 
How To Make Multiplication Chart Without Knowing Your Times Tables
How To Make Multiplication Chart Without Knowing Your Times TablesHow To Make Multiplication Chart Without Knowing Your Times Tables
How To Make Multiplication Chart Without Knowing Your Times Tables
William Garrett
 
merge sort help in language C with algorithms
merge sort help in language C with algorithmsmerge sort help in language C with algorithms
merge sort help in language C with algorithms
tahamou4
 
23T1W3 Mathematics ppt on Number Sequence.ppt
23T1W3 Mathematics ppt on Number Sequence.ppt23T1W3 Mathematics ppt on Number Sequence.ppt
23T1W3 Mathematics ppt on Number Sequence.ppt
RuthAdelekun1
 
Counting
CountingCounting
Aizaz comb sort
Aizaz comb sortAizaz comb sort
Aizaz comb sortNauman Ali
 
Pw fractions ordering
Pw fractions orderingPw fractions ordering
Pw fractions ordering
Joanne Blannin
 
Ordering fractions
Ordering fractionsOrdering fractions
Ordering fractions
mkwoods77
 
G10-MathQ3-Week-2- Definition of Permutation
G10-MathQ3-Week-2- Definition of PermutationG10-MathQ3-Week-2- Definition of Permutation
G10-MathQ3-Week-2- Definition of Permutation
grace joy canseco
 
The Games Up!
The Games Up!The Games Up!
The Games Up!
Games Above Board
 
Pre-Algebra 4 1, 4-2
Pre-Algebra 4 1, 4-2Pre-Algebra 4 1, 4-2
Pre-Algebra 4 1, 4-2Kathy Favazza
 

Similar to Lec25 (20)

Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...
 
Merge sort
Merge sortMerge sort
Merge sort
 
Sorting
SortingSorting
Sorting
 
Ordering fractions
Ordering fractionsOrdering fractions
Ordering fractions
 
Pc12 sol c08_ptest
Pc12 sol c08_ptestPc12 sol c08_ptest
Pc12 sol c08_ptest
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
How To Make Multiplication Chart Without Knowing Your Times Tables
How To Make Multiplication Chart Without Knowing Your Times TablesHow To Make Multiplication Chart Without Knowing Your Times Tables
How To Make Multiplication Chart Without Knowing Your Times Tables
 
merge sort help in language C with algorithms
merge sort help in language C with algorithmsmerge sort help in language C with algorithms
merge sort help in language C with algorithms
 
23T1W3 Mathematics ppt on Number Sequence.ppt
23T1W3 Mathematics ppt on Number Sequence.ppt23T1W3 Mathematics ppt on Number Sequence.ppt
23T1W3 Mathematics ppt on Number Sequence.ppt
 
Counting
CountingCounting
Counting
 
Qa04 hcf & lcm
Qa04 hcf & lcmQa04 hcf & lcm
Qa04 hcf & lcm
 
Lec34
Lec34Lec34
Lec34
 
Aizaz comb sort
Aizaz comb sortAizaz comb sort
Aizaz comb sort
 
Sorting algos
Sorting algosSorting algos
Sorting algos
 
Pw fractions ordering
Pw fractions orderingPw fractions ordering
Pw fractions ordering
 
Ordering fractions
Ordering fractionsOrdering fractions
Ordering fractions
 
G10-MathQ3-Week-2- Definition of Permutation
G10-MathQ3-Week-2- Definition of PermutationG10-MathQ3-Week-2- Definition of Permutation
G10-MathQ3-Week-2- Definition of Permutation
 
The Games Up!
The Games Up!The Games Up!
The Games Up!
 
Pre-Algebra 4 1, 4-2
Pre-Algebra 4 1, 4-2Pre-Algebra 4 1, 4-2
Pre-Algebra 4 1, 4-2
 

More from Nikhil Chilwant

The joyless economy
The joyless economyThe joyless economy
The joyless economy
Nikhil Chilwant
 
I 7
I 7I 7
IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )
Nikhil Chilwant
 

More from Nikhil Chilwant (20)

The joyless economy
The joyless economyThe joyless economy
The joyless economy
 
I 7
I 7I 7
I 7
 
IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )
 
Lec39
Lec39Lec39
Lec39
 
Lec38
Lec38Lec38
Lec38
 
Lec37
Lec37Lec37
Lec37
 
Lec36
Lec36Lec36
Lec36
 
Lec35
Lec35Lec35
Lec35
 
Lec33
Lec33Lec33
Lec33
 
Lec32
Lec32Lec32
Lec32
 
Lec30
Lec30Lec30
Lec30
 
Lec29
Lec29Lec29
Lec29
 
Lec28
Lec28Lec28
Lec28
 
Lec27
Lec27Lec27
Lec27
 
Lec26
Lec26Lec26
Lec26
 
Lec21
Lec21Lec21
Lec21
 
Lec20
Lec20Lec20
Lec20
 
Lec19
Lec19Lec19
Lec19
 
Lec18
Lec18Lec18
Lec18
 
Lec17
Lec17Lec17
Lec17
 

Lec25

  • 1. Tournament Trees Winner trees. Loser Trees.
  • 2. Winner Trees 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.
  • 3. Winner Tree For 16 Players player match node
  • 4. Winner Tree For 16 Players 1 1 2 3 1 2 2 3 6 1 3 2 4 2 5 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 Smaller element wins => min winner tree.
  • 5. Winner Tree For 16 Players 1 1 2 3 1 2 2 3 6 1 3 2 4 2 5 4 3 6 8 1 5 7 3 2 6 9 4 5 2 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.
  • 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 1 2 2 3 6 1 3 2 4 2 5 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
  • 9. Sort 16 Numbers 1 1 2 3 1 2 2 3 6 1 3 2 4 2 5 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
  • 10. Sort 16 Numbers 1 1 2 3 1 2 2 3 6 1 3 2 4 2 5 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 1 Sorted array.
  • 11. Sort 16 Numbers 1 1 2 3 1 2 2 3 6 5 3 2 4 2 5 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 1 Sorted array.
  • 12. Sort 16 Numbers 1 1 2 3 2 2 3 6 3 5 3 2 4 2 5 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 1 Sorted array.
  • 13. Sort 16 Numbers 1 3 2 3 2 2 3 6 3 5 3 2 4 2 5 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 1 Sorted array.
  • 14. Sort 16 Numbers 2 3 2 3 2 2 3 6 3 5 3 2 4 2 5 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 1 Sorted array.
  • 15. Sort 16 Numbers 3 2 3 2 2 3 6 3 5 3 2 4 2 5 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 2 2 1 Sorted array.
  • 16. Sort 16 Numbers 3 2 3 2 2 3 6 3 5 3 6 4 2 5 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 2 2 1 Sorted array.
  • 17. Sort 16 Numbers 3 2 3 4 2 3 6 3 5 3 6 4 2 5 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 2 2 1 Sorted array.
  • 18. Sort 16 Numbers 3 2 3 4 2 3 6 3 5 3 6 4 2 5 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 2 2 1 Sorted array.
  • 19. Sort 16 Numbers 3 2 3 4 2 3 6 3 5 3 6 4 2 5 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 2 2 1 Sorted array.
  • 20. Sort 16 Numbers 2 3 2 3 4 2 3 6 3 5 3 6 4 2 5 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 1 2 2 Sorted array.
  • 21. Sort 16 Numbers 2 3 2 3 4 2 3 6 3 5 3 6 4 5 5 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 1 2 2 Sorted array.
  • 22. Sort 16 Numbers 2 3 2 3 4 5 3 6 3 5 3 6 4 5 5 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 1 2 2 Sorted array.
  • 23. Sort 16 Numbers 2 3 4 3 4 5 3 6 3 5 3 6 4 5 5 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 1 2 2 Sorted array.
  • 24. Sort 16 Numbers 3 3 4 3 4 5 3 6 3 5 3 6 4 5 5 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 1 2 2 Sorted array.
  • 25. Sort 16 Numbers 3 3 4 3 4 5 3 6 3 5 3 6 4 5 5 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 1 2 2 3 Sorted array.
  • 26. Time To Sort • Initialize winner tree.  O(n) time • Remove winner and replay.  O(log n) time • 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. Winner Tree Operations • Initialize  O(n) time • Get winner  O(1) time • Remove/replace winner and replay  O(log n) time  more precisely Theta(log n)
  • 28. Replace Winner And Replay 1 1 2 3 1 2 2 3 6 1 3 2 4 2 5 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 Replace winner with 6.
  • 29. Replace Winner And Replay 1 1 2 3 1 2 2 3 6 1 3 2 4 2 5 4 3 6 8 6 5 7 3 2 6 9 4 5 2 5 8 Replay matches on path to root.
  • 30. Replace Winner And Replay 1 1 2 3 1 2 2 3 6 1 3 2 4 2 5 4 3 6 8 6 5 7 3 2 6 9 4 5 2 5 8 Replay matches on path to root.
  • 31. Replace Winner And Replay 1 1 2 3 1 2 2 3 6 1 3 2 4 2 5 4 3 6 8 6 5 7 3 2 6 9 4 5 2 5 8 Opponent is player who lost last match played at this node.
  • 32. Loser Tree Each match node stores the match loser rather than the match winner.
  • 33. Min Loser Tree For 16 Players 4 3 8 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
  • 34. Min Loser Tree For 16 Players 4 6 8 3 5 1 7 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
  • 35. Min Loser Tree For 16 Players 4 6 8 3 5 3 7 1 6 2 9 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
  • 36. Min Loser Tree For 16 Players 4 6 8 3 5 3 7 2 5 2 8 1 6 4 9 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
  • 37. Min Loser Tree For 16 Players 4 6 8 3 5 3 7 2 5 5 8 1 6 4 9 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
  • 38. Min Loser Tree For 16 Players 4 6 8 3 5 3 7 2 5 5 8 1 6 4 9 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
  • 39. Min Loser Tree For 16 Players 4 6 8 3 5 3 7 2 5 5 8 2 6 4 9 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
  • 40. 4 6 8 3 5 3 7 1 Winner 2 5 5 8 2 6 4 9 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8
  • 41. 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).
  • 42. Winner 4 6 3 8 5 3 7 2 5 5 8 1 2 2 6 4 9 9 5 9 3 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8 Replace winner with 9 and replay matches.
  • 43. Complexity Of Replay • One match at each level that has a match node. • O(log n) • More precisely Theta(log n).
  • 44. More Tournament Tree Applications • k-way merging of runs during an external merge sort • Truck loading
  • 45. 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
  • 46. 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.
  • 47. Truck Loading n = 5 packages weights [2, 5, 6, 3, 4] truck capacity c = 10 truck1 = [2, 5] truck2 = [6, 3] truck3 = [4] uses 3 trucks when 2 trucks suffice
  • 48. Truck Loading n = 5 packages weights [2, 5, 6, 3, 4] truck capacity c = 10 truck1 = [2, 5, 3] truck2 = [6, 4]
  • 49. Bin Packing • n items to be packed into bins • each item has a size • each bin has a capacity of c • minimize number of bins
  • 50. Bin Packing Truck loading is same as bin packing. Truck is a bin that is to be packed (loaded). Package is an item/element. Bin packing to minimize number of bins is NP-hard. Several fast heuristics have been proposed.
  • 51. Bin Packing Heuristics • First Fit.  Bins are arranged in left to right order.  Items are packed one at a time in given order.  Current item is packed into leftmost bin into which it fits.  If there is no bin into which current item fits, start a new bin.
  • 52. First Fit n = 4 weights = [4, 7, 3, 6] capacity = 10 Pack red item into first bin.
  • 53. First Fit n = 4 weights = [4, 7, 3, 6] capacity = 10 Pack blue item next. Doesn’t fit, so start a new bin.
  • 54. First Fit n = 4 weights = [4, 7, 3, 6] capacity = 10
  • 55. First Fit n = 4 weights = [4, 7, 3, 6] capacity = 10 Pack yellow item into first bin.
  • 56. First Fit n = 4 weights = [4, 7, 3, 6] capacity = 10 Pack green item. Need a new bin.
  • 57. First Fit n = 4 weights = [4, 7, 3, 6] capacity = 10 Not optimal. 2 bins suffice.
  • 58. Bin Packing Heuristics • First Fit Decreasing.  Items are sorted into decreasing order.  Then first fit is applied.
  • 59. Bin Packing Heuristics • Best Fit.  Items are packed one at a time in given order.  To determine the bin for an item, first determine set S of bins into which the item fits.  If S is empty, then start a new bin and put item into this new bin.  Otherwise, pack into bin of S that has least available capacity.
  • 60. Bin Packing Heuristics • Best Fit Decreasing.  Items are sorted into decreasing order.  Then best fit is applied.
  • 61. Performance • For first fit and best fit: Heuristic Bins <= (17/10)(Minimum Bins) + 2 • For first fit decreasing and best fit decreasing: Heuristic Bins <= (11/9)(Minimum Bins) + 4
  • 62. Complexity Of First Fit Use a max tournament tree in which the players are n bins and the value of a player is the available capacity in the bin. O(n log n), where n is the number of items.

Editor's Notes

  1. Tennis ladder.
  2. See text for case when number of players is not a power of 2.
  3. Distribution center in Jacksonville sends packages to distribution center in Miami. Use the fewest number of trucks.