Tournament Trees CSE, POSTECH.

Slides:



Advertisements
Similar presentations
SNU IDB Lab. Ch.14 Tournament Trees © copyright 2006 SNU IDB Lab.
Advertisements

Winner trees. Loser Trees.
Selection Trees. What are selection trees? Complete binary tree Each node represents a “match” Winner Trees Loser Trees.
Chapter 6: Transform and Conquer
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 24 Sorting.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Priority Queue (Heap) & Heapsort COMP171 Fall 2006 Lecture 11 & 12.
CMPT 225 Priority Queues and Heaps. Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest.
FALL 2004CENG 351 Data Management and File Structures1 External Sorting Reference: Chapter 8.
A Binary Tree root leaf. A Binary Tree root leaf descendent of root parent of leaf.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
FALL 2006CENG 351 Data Management and File Structures1 External Sorting.
Lec 6 Feb 17, 2011  Section 2.5 of text (review of heap)  Chapter 3.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L18 (Chapter 23) Algorithm.
CS Data Structures Chapter 5 Trees. Additional Binary Tree Operations (1/7)  Copying Binary Trees  we can modify the postorder traversal algorithm.
Heaps and heapsort COMP171 Fall 2005 Part 2. Sorting III / Slide 2 Heap: array implementation Is it a good idea to store arbitrary.
Priority Queues, Heaps & Leftist Trees
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 trees,
9/17/20151 Chapter 12 - Heaps. 9/17/20152 Introduction ► Heaps are largely about priority queues. ► They are an alternative data structure to implementing.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
CSE AU B-Trees1 B-Trees CSE 373 Data Structures.
Binary Heap.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
© 2010 AT&T Intellectual Property. All rights reserved. AT&T and the AT&T logo are trademarks of AT&T Intellectual Property. Case Studies: Bin Packing.
Chapter 21 Binary Heap.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
CSE233 Course Review CSE, POSTECH. What we studied in the 1 st half Week 1 : Overview of C++, Program performance Week 2 : Array-based and linked representations.
Data Structure & Algorithm II.  In a multiuser computer system, multiple users submit jobs to run on a single processor.  We assume that the time required.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Instructor Neelima Gupta Table of Contents Review of Lower Bounding Techniques Decision Trees Linear Sorting Selection Problems.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap.
Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue. Nov 4,
CPSC 252 Binary Heaps Page 1 Binary Heaps A complete binary tree is a binary tree that satisfies the following properties: - every level, except possibly.
CS 361 – Chapter 5 Priority Queue ADT Heap data structure –Properties –Internal representation –Insertion –Deletion.
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 1 Adam Smith L ECTURES Priority Queues and Binary Heaps Algorithms.
8 January Heap Sort CSE 2011 Winter Heap Sort Consider a priority queue with n items implemented by means of a heap  the space used is.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
FALL 2005CENG 351 Data Management and File Structures1 External Sorting Reference: Chapter 8.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
Winner trees. Loser Trees.
CENG 3511 External Sorting. CENG 3512 Outline Introduction Heapsort Multi-way Merging Multi-step merging Replacement Selection in heap-sort.
Heaps, Heap Sort, and Priority Queues. Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two children * A node that.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Heaps and Heap Sort. Sorting III / Slide 2 Background: Complete Binary Trees * A complete binary tree is the tree n Where a node can have 0 (for the leaves)
"Teachers open the door, but you must enter by yourself. "
Heaps (8.3) CSE 2011 Winter May 2018.
Priority Queues An abstract data type (ADT) Similar to a queue
Priority Queues and Heaps
Bohyung Han CSE, POSTECH
Interval Heaps Complete binary tree.
Tree Representation Heap.
"Teachers open the door, but you must enter by yourself. "
Heap Sort CSE 2011 Winter January 2019.
Chapter 6: Transform and Conquer
Priority Queues An abstract data type (ADT) Similar to a queue
Dr.Surasak Mungsing CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05-2: Analysis of time Complexity of Priority.
Algorithms: Design and Analysis
CENG 351 Data Management and File Structures
Priority Queues (Heaps)
Bin Packing Michael T. Goodrich Some slides adapted from slides from
The Heap ADT A heap is a complete binary tree where each node’s datum is greater than or equal to the data of all of the nodes in the left and right.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
CMPT 225 Lecture 16 – Heap Sort.
Presentation transcript:

Tournament Trees CSE, POSTECH

Tournament Trees Used when we need to break ties in a prescribed manner To select the element that was inserted first To select the element on the left Like the heap, a tournament tree is a complete binary tree that is most efficiently stored using array-based binary tree Used to obtain efficient implementations of two approximation algorithms for the bin packing problem (another NP-hard problem) Types of tournament trees: winner & loser trees

Tournament Trees The tournament is played in the sudden-death mode A player is eliminated upon losing a match Pairs of players play until only one remains The tournament tree is described by a binary tree Each external node represents a player Each internal node represents a match played Each level of internal nodes defines a round of matches Tournament trees are also called selection trees See Figure 13.1 for tournament trees

Winner Trees Definition A winner tree for n players is a complete binary tree with n external nodes and n-1 internal nodes. Each internal node records the winner of the match. To determine the winner of a match, we assume that each player has a value In a min (max) winner tree, the player with the smaller (larger) value wins See Figure 13.2 for winner trees

Winner Trees The height is log2(n+1) (excludes the player level) What kind of games would use (a) min winner tree? What kind of games would use (b) max winner tree?

Winner Tree Operations Select winner O(1) time to play match at each match node. Initialize n-1 match nodes O(n) time to initialize n-player winner tree Remove winner and replay O(log n) time

Winner Tree Sorting Method Read Example 13.1 Put elements to be sorted into a min winner tree. Remove the winner and replace its value with a large value (e.g., ∞). replay the matches. If not done, go to step 2.

Sort 16 Numbers 1. Initialize the min winner tree

Sort 16 Numbers 2. Remove the winner and replace its value

Sort 16 Numbers 3. Replay the matches

Sort 16 Numbers Remove the winner and replace its value

Sort 16 Numbers Replay the matches

Sort 16 Numbers Remove the winner and replace its value

Sort 16 Numbers Replay the matches

Sort 16 Numbers Remove the winner and replace its value Continue in this manner….

Time To Sort Initialize winner tree: O(n) time Remove winner and replay: O(logn) time Remove winner and replay n times : O(nlogn) time Thus, the total sort time is O(nlogn)

Exercise 1 – [3, 5, 6, 7, 20, 8, 2, 9] Max Winner Tree Min Winner Tree After the change, the max winner tree becomes: After the change, the min winner tree becomes: Is this correct?

The ADT WinnerTree Read ADT 13.1 for Winner Tree ADT specification Read Program 13.1 for the abstract class winnerTree

The Winner Tree Representation Using the array representation of a complete binary tree A winner tree of n players requires n-1 internal nodes tree[1:n-1] The players (external nodes) are represented as an array player[1:n] tree[i] is an index into the array player and gives the winner of the match played at node i See Figure 13.4 for tree-to-array correspondence

Determining the parent of external node To implement the interface methods, we need to determine the parent tree[p] of an external node player[i] When the number of external nodes is n, the number of internal nodes is n-1 The left-most internal node at the lowest level is numbered s, where s = 2log2(n-1) The number of internal nodes at the lowest level is n-s, and the number LowExt of external nodes at the lowest level is 2*(n-s)

Determining the parent of external node What is n and s for Figure 13.4? Let offset = 2*s - 1. Then for any external node player[i], its parent tree[p] is given by p = (i +offset)/2 i  LowExt p = (i – LowExt + n –1)/2 i  LowExt

Loser Trees Definition A loser tree for n players is also a complete binary tree with n external nodes and n-1 internal nodes. Each internal node records the loser of the match. The overall winner is recorded in tree[0] See Figure 13.5 for min loser trees Read Section 13.4

Example Min Loser Trees Figure 13.5 Eight-player min loser trees What is wrong with the min loser tree (b)?

Exercise 15 – [20, 10, 12, 18, 30, 16, 35, 33, 45, 7, 15, 19, 33, 11, 17, 25] Max Loser Tree Min Loser Tree After the change, the max loser tree becomes: After the change, the min loser tree becomes:

Bin Packing Problem We have bins that have a capacity binCapacity and n objects that need to be packed into these bins Object i requires objSize[i], where 0 < objSize[i]  binCapacity, units of capacity Feasible packing - an assignment of objects to bins so that no bin’s capacity is exceeded Optimal packing - a feasible packing that uses the fewest number of bins Goal: pack objects with the minimum number of bins The bin packing problem is an NP-hard problem  We use approximation algorithms to solve the problem

Truck Loading Problem Have parcels to pack into trucks Each parcel has a weight Each truck has a load limit Goal: Minimize the number of trucks needed Equivalent to the bin packing problem Read Examples 13.4 & 13.5

Bin Packing Approximation Algorithms First Fit (FF) First Fit Decreasing (FFD) Best Fit (BF) Best Fit Decreasing (BFD)

First Fit (FF) Bin Packing Bins are arranged in left to right order. Objects are packed one at a time in a given order. Current object is packed into the leftmost bin into which it fits. If there is no bin into which current object fits, start a new bin.

Best Fit (BF) Bin Packing Let bin[j].unusedCapacity denote the capacity available in bin j Initially, the available capacity is binCapacity for all bins Object i is packed into the bin with the least unusedCapacity that is at least objSize[i] If there is no bin into which current object fits, start a new bin.

First Fit Decreasing (FFD) Bin Packing This method is the same as FF except that the objects are ordered in a decreasing size so that objSize[i]  objSize[i+1], 1  i < n

Best Fit Decreasing (BFD) Bin Packing This method is the same as BF except that the objects are ordered as for FFD

Bin Packing Example Assume four objects with objSize[1:4] = [3, 5, 2, 4] Assuming each bin’s capacity is 7, what would the packing be if we use FF, BF, FFD, or BFD? FF Bin 1: objects 1 & 3, Bin 2: object 2, Bin 3: object 4 BF Bin 1: objects 1 & 4, Bin 2: objects 2 & 3 FFD Bin 1: objects 2 & 3, Bin 2: objects 1 & 4 BFD Read Example 13.6

First Fit Bin Packing with Max Winner Tree Use a max winner tree in which the players are n bins and the value of a player is the available capacity binCapacity in the bin. Read the section on First Fit and Winner Trees on pp. 521 & See Figure 13.6 for first-fit (FF) max winner trees See Program 13.2 for the first-fit bin packing program

First Fit Bin Packing with Max Winner Tree Example: n=8, binCapacity=10, objSize[] = {8,6,5,3,6,4,2,7} 1 1 5 1 3 5 7 10 10 10 10 10 10 10 10 1 2 3 4 5 6 7 8 Initial bin[tree[1]].unusedCapacity >= objSize[1]?

First Fit Bin Packing with Max Winner Tree Example: n=8, binCapacity=10, objSize[] = {8,6,5,3,6,4,2,7} 2 5 3 7 10 1 4 6 8 After objSize[1]=8 packed Where will objSize[2]=6 be packed into?

First Fit Bin Packing with Max Winner Tree Example: n=8, binCapacity=10, objSize[] = {8,6,5,3,6,4,2,7} 3 5 2 7 4 10 1 6 8 After objSize[2]=6 packed Where will objSize[3]=5 be packed into?

First Fit Bin Packing with Max Winner Tree Example: n=8, binCapacity=10, objSize[] = {8,6,5,3,6,4,2,7} 4 5 2 7 10 1 3 6 8 After objSize[3]=5 packed Where will objSize[4]=3 be packed into?

First Fit Bin Packing with Max Winner Tree Example: n=8, binCapacity=10, objSize[] = {8,6,5,3,6,4,2,7} 4 5 1 7 2 10 3 6 8 After objSize[4]=3 packed Where will objSize[5]=6 be packed into?

First Fit Bin Packing with Max Winner Tree Example: n=8, binCapacity=10, objSize[] = {8,6,5,3,6,4,2,7} 5 3 1 7 2 4 10 6 8 After objSize[5]=6 packed Where will objSize[6]=4, objSize[7]=2 and objSize[8]=7 be packed into?

More Bin Packing with Max Winner Tree Exercise – Do the same example using BF, FFD, BFD with Max Winner Tree Do Exercise 13.23 READ Chapter 13