Winner trees. Loser Trees.

Slides:



Advertisements
Similar presentations
Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue.
Advertisements

Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
Tournament Trees CSE, POSTECH.
SNU IDB Lab. Ch.14 Tournament Trees © copyright 2006 SNU IDB Lab.
Selection Trees. What are selection trees? Complete binary tree Each node represents a “match” Winner Trees Loser Trees.
Dictionaries Collection of items. Each item is a pair.  (key, element)  Pairs have different keys.
Heapsort By: Steven Huang. What is a Heapsort? Heapsort is a comparison-based sorting algorithm to create a sorted array (or list) Part of the selection.
Advanced Topics in Algorithms and Data Structures 1 Rooting a tree For doing any tree computation, we need to know the parent p ( v ) for each node v.
Optimal Merging Of Runs
FALL 2004CENG 351 Data Management and File Structures1 External Sorting Reference: Chapter 8.
FALL 2006CENG 351 Data Management and File Structures1 External Sorting.
Initializing A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
Balanced Trees. Binary Search tree with a balance condition Why? For every node in the tree, the height of its left and right subtrees must differ by.
Heaps and heapsort COMP171 Fall 2005 Part 2. Sorting III / Slide 2 Heap: array implementation Is it a good idea to store arbitrary.
Heapsort CIS 606 Spring Overview Heapsort – O(n lg n) worst case—like merge sort. – Sorts in place—like insertion sort. – Combines the best of both.
Priority Queues, Heaps & Leftist Trees
CSC 172 DATA STRUCTURES. Priority Queues Model Set with priorities associatedwith elements Priorities are comparable by a < operator Operations Insert.
Heapsort Based off slides by: David Matuszek
Leftist Trees Linked binary tree. Can do everything a heap can do and in the same asymptotic complexity.  insert  remove min (or max)  initialize Can.
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.
Data Structure & Algorithm II.  Delete-min  Building a heap in O(n) time  Heap Sort.
Chapter 21 Binary Heap.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
CSC 213 – Large Scale Programming Lecture 15: Heap-based Priority Queue.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue. Nov 4,
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
Binomial Tree B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4 Adapted from: Kevin Wayne B k : a binomial tree B k-1 with the addition of a left child with another.
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.
1 Heap Sort. A Heap is a Binary Tree Height of tree = longest path from root to leaf =  (lgn) A heap is a binary tree satisfying the heap condition:
FALL 2005CENG 351 Data Management and File Structures1 External Sorting Reference: Chapter 8.
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
Initializing A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
BINARY TREES A BINARY TREE t IS EITHER EMPTY OR CONSISTS OF AN ITEM, CALLED THE ROOT ITEM, AND TWO DISTINCT BINARY TREES, CALLED THE LEFT SUBTREE AND.
Dynamic Dictionaries Primary Operations:  get(key) => search  put(key, element) => insert  remove(key) => delete Additional operations:  ascend()
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
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.
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.
Dictionaries Collection of items. Each item is a pair. (key, element)
Leftist Trees Linked binary tree.
Priority Queues An abstract data type (ADT) Similar to a queue
Binary search tree. Removing a node
Heapsort CSE 373 Data Structures.
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
Heaps 8/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Binary Search Trees.
Best-fit bin packing in O(n log n) time
Binary Search Tree Chapter 10.
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Dynamic Dictionaries Primary Operations: Additional operations:
Binomial Tree Adapted from: Kevin Wayne Bk-1 B0 Bk
Interval Heaps Complete binary tree.
7/23/2009 Many thanks to David Sun for some of the included slides!
Pairing Heaps Actual Complexity.
Tree Representation Heap.
Improve Run Generation
Priority Queues An abstract data type (ADT) Similar to a queue
Heapsort CSE 373 Data Structures.
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Algorithms: Design and Analysis
CENG 351 Data Management and File Structures
Pairing Heaps Actual Complexity
Red Black Trees.
A Heap Is Efficiently Represented As An Array
Splay Trees Binary search trees.
Heaps 9/29/2019 5:43 PM Heaps Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

Winner trees. Loser Trees. Selection Trees Winner trees. Loser Trees. Tennis ladder.

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 match node See text for case when number of players is not a power of 2.

Winner Tree For 16 Players 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 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. Insert 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 Sorted array. 1 1 2 3 1 2 2 3 6 1 3 2 4 2 5 4 3 6 8 1 7 3 2 6 9 4 5 2 5 8 1 Sorted array.

Sort 16 Numbers Sorted array. 1 1 2 3 1 2 2 3 6 5 3 2 4 2 5 4 3 6 8 1 7 3 2 6 9 4 5 2 5 8 1 Sorted array.

Sort 16 Numbers Sorted array. 1 1 2 3 3 2 2 3 6 5 3 2 4 2 5 4 3 6 8 1 7 3 2 6 9 4 5 2 5 8 1 Sorted array.

Sort 16 Numbers Sorted array. 1 3 2 3 3 2 2 3 6 5 3 2 4 2 5 4 3 6 8 1 7 3 2 6 9 4 5 2 5 8 1 Sorted array.

Sort 16 Numbers Sorted array. 2 3 2 3 3 2 2 3 6 5 3 2 4 2 5 4 3 6 8 1 7 3 2 6 9 4 5 2 5 8 1 Sorted array.

Sort 16 Numbers Sorted array. 2 3 2 3 3 2 2 3 6 5 3 2 4 2 5 4 3 6 8 1 7 3 2 6 9 4 5 2 5 8 1 2 2 2 Sorted array.

Sort 16 Numbers Sorted array. 2 3 2 3 3 2 2 3 6 5 3 6 4 2 5 4 3 6 8 1 7 3 2 6 9 4 5 2 5 8 1 2 2 2 Sorted array.

Sort 16 Numbers Sorted array. 2 3 2 3 3 4 2 3 6 5 3 6 4 2 5 4 3 6 8 1 7 3 2 6 9 4 5 2 5 8 1 2 2 2 Sorted array.

Sort 16 Numbers Sorted array. 2 3 2 3 3 4 2 3 6 5 3 6 4 2 5 4 3 6 8 1 7 3 2 6 9 4 5 2 5 8 1 2 2 2 Sorted array.

Sort 16 Numbers Sorted array. 2 3 2 3 3 4 2 3 6 5 3 6 4 2 5 4 3 6 8 1 7 3 2 6 9 4 5 2 5 8 1 2 2 2 Sorted array.

Sort 16 Numbers Sorted array. 2 3 2 3 3 4 2 3 6 5 3 6 4 2 5 4 3 6 8 1 7 3 2 6 9 4 5 2 5 8 1 2 2 Sorted array.

Sort 16 Numbers Sorted array. 2 3 2 3 3 4 2 3 6 5 3 6 4 5 5 4 3 6 8 1 7 3 2 6 9 4 5 2 5 8 1 2 2 Sorted array.

Sort 16 Numbers Sorted array. 2 3 2 3 3 4 5 3 6 5 3 6 4 5 5 4 3 6 8 1 7 3 2 6 9 4 5 2 5 8 1 2 2 Sorted array.

Sort 16 Numbers Sorted array. 2 3 4 3 3 4 5 3 6 5 3 6 4 5 5 4 3 6 8 1 7 3 2 6 9 4 5 2 5 8 1 2 2 Sorted array.

Sort 16 Numbers Sorted array. 3 3 4 3 3 4 5 3 6 5 3 6 4 5 5 4 3 6 8 1 7 3 2 6 9 4 5 2 5 8 1 2 2 Sorted array.

Sort 16 Numbers Sorted array. 3 3 4 3 3 4 5 3 6 5 3 6 4 5 5 4 3 6 8 1 7 3 2 6 9 4 5 2 5 8 1 2 2 3 Sorted array.

Time To Sort Initialize winner tree. Remove winner and replay. 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 4 3 6 8 1 5 7 2 9 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.

Each match node stores the match loser rather than the match winner. Loser Tree Each match node stores the match loser rather than the match winner.

Min Loser Tree For 16 Players 3 4 8 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8

Min Loser Tree For 16 Players 3 6 1 4 8 5 7 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8

Min Loser Tree For 16 Players 3 6 3 2 4 8 5 7 6 9 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8

Min Loser Tree For 16 Players 3 2 6 3 4 2 4 8 5 7 6 9 5 8 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8

Min Loser Tree For 16 Players 3 2 6 3 4 5 4 8 5 7 6 9 5 8 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8

Min Loser Tree For 16 Players 3 2 6 3 4 5 4 8 5 7 6 9 5 8 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8

Min Loser Tree For 16 Players 2 3 2 6 3 4 5 4 8 5 7 6 9 5 8 4 3 6 8 1 5 7 3 2 6 9 4 5 2 5 8

Winner 1 2 3 2 6 3 4 5 4 8 5 7 6 9 5 8 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).

Replace winner with 9 and replay matches. 4 3 6 8 1 5 7 2 9 2 3 3 5 9 9 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 Selection 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 Distribution center in Jacksonville sends packages to distribution center in Miami. Use the fewest 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.