 Have to copy one array into another (N)

Slides:



Advertisements
Similar presentations
David Luebke 1 6/1/2014 CS 332: Algorithms Medians and Order Statistics Structures for Dynamic Sets.
Advertisements

CS16: Introduction to Data Structures & Algorithms
Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue.
Binomial Heaps. Heap Under most circumstances you would use a “normal” binary heap Except some algorithms that may use heaps might require a “Union” operation.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 11: Priority Queues and Heaps Java Software Structures: Designing.
10 -1 Chapter 10 Amortized Analysis A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)
Foundations of Data Structures Practical Session #7 AVL Trees 2.
§2 Binary Trees Note: In a tree, the order of children does not matter. But in a binary tree, left child and right child are different. A B A B andare.
§6 Leftist Heaps CHAPTER 5 Graph Algorithms  Heap: Structure Property + Order Property Target : Speed up merging in O(N). Leftist Heap: Order Property.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 9 Prepared by İnanç TAHRALI.
COL 106 Shweta Agrawal and Amit Kumar
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)
CMSC 341 Binary Heaps Priority Queues. 8/3/2007 UMBC CSMC 341 PQueue 2 Priority Queues Priority: some property of an object that allows it to be prioritized.
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
More sorting algorithms: Heap sort & Radix sort. Heap Data Structure and Heap Sort (Chapter 7.6)
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
CSE 373 Data Structures Lecture 12
Dijkstra/Prim 1 make-heap |V| insert |V| delete-min |E| decrease-key Priority Queues make-heap Operation insert find-min delete-min union decrease-key.
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.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Binomial Heaps Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Binary and Binomial Heaps These lecture slides are adapted from CLRS, Chapters.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
§3 Binary Heap 1. Structure Property: 【 Definition 】 A binary tree with n nodes and height h is complete iff its nodes correspond to the nodes numbered.
PRIORITY QUEUES (HEAPS). Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important.
§8 Binomial Queues Haven’t we had enough about queues? What is a binomial queue for? Well, what is the average time for insertions with leftist or skew.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
CMSC 341 Binary Heaps Priority Queues. 2 Priority: some property of an object that allows it to be prioritized WRT other objects (of the same type) Priority.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin.
Cpt S 223 – Advanced Data Structures Priority Queues
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 8 Prepared by İnanç TAHRALI.
Priority Queues (Heaps)
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
1 Binomial Tree Binomial tree. n Recursive definition: B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4.
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
Heaps Priority Queues. 1/6/2016Data Structure: Heaps2 Outline Binary heaps Binomial queues Leftist heaps.
CMSC 341 Binary Heaps Priority Queues. 2 Priority: some property of an object that allows it to be prioritized WRT other objects (of the same type) Priority.
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
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.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Heaps and Priority Queues What is a heap? A heap is a binary tree storing keys at its internal nodes and satisfying the following properties:
CS 201 Data Structures and Algorithms
BST Trees
Hashing Exercises.
Source: Muangsin / Weiss
CMSC 341 Lecture 13 Leftist Heaps
Binary Trees, Binary Search Trees
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Binary Heaps Priority Queues
CMSC 341 Lecture 14 Priority Queues & Heaps
CE 221 Data Structures and Algorithms
Heaps Priority Queues.
Binary Trees, Binary Search Trees
CS 6310 Advanced Data Structure Wei-Shian Wang
Fundamental Structures of Computer Science II
CSE 373 Data Structures Lecture 12
Binary Trees, Binary Search Trees
Presentation transcript:

 Have to copy one array into another (N) CHAPTER 5 Graph Algorithms §6 Leftist Heaps Target : Speed up merging in O(N).  Heap: Structure Property + Order Property  Have to copy one array into another (N)  Use pointers  Slow down all the operations Leftist Heap: Order Property – the same Structure Property – binary tree, but unbalanced 1/19

Npl(X) = min { Npl(C) + 1 for all C as children of X } §6 Leftist Heaps 【Definition】The null path length, Npl(X), of any node X is the length of the shortest path from X to a node without two children. Define Npl(NULL) = –1. Note: Npl(X) = min { Npl(C) + 1 for all C as children of X } 【Definition】The leftist heap property is that for every node X in the heap, the null path length of the left child is at least as large as that of the right child. 1 1 The tree is biased to get deep toward the left. 1 1   1 2/19

§6 Leftist Heaps 【Theorem】A leftist tree with r nodes on the right path must have at least 2r – 1 nodes. Proof: By induction on p. 162. Note: The leftist tree of N nodes has a right path containing at most log(N+1) nodes.  We can perform all the work on the right path, which is guaranteed to be short. Trouble makers: Insert and Merge Note: Insertion is merely a special case of merging. 3/19

 Merge (recursive version): §6 Leftist Heaps  Declaration: Step 1: Merge( H1->Right, H2 ) 8 17 26 6 12 18 24 33 7 37 struct TreeNode { ElementType Element; PriorityQueue Left; PriorityQueue Right; int Npl; } ; Step 2: Attach( H2, H1->Right ) 3 10 21 14 23 8 17 26 6 12 18 24 33 7 37  Merge (recursive version): 3 10 21 14 23 8 17 26 6 12 18 24 33 7 37 H1 H2 Step 3: Swap(H1->Right, H1->Left ) if necessary 4/19

What if Npl is NOT updated? §6 Leftist Heaps PriorityQueue Merge ( PriorityQueue H1, PriorityQueue H2 ) { if ( H1 == NULL ) return H2; if ( H2 == NULL ) return H1; if ( H1-Element < H2->Element ) return Merge1( H1, H2 ); else return Merge1( H2, H1 ); } static PriorityQueue Merge1( PriorityQueue H1, PriorityQueue H2 ) { if ( H1->Left == NULL ) /* single node */ H1->Left = H2; /* H1->Right is already NULL and H1->Npl is already 0 */ else { H1->Right = Merge( H1->Right, H2 ); /* Step 1 & 2 */ if ( H1->Left->Npl < H1->Right->Npl ) SwapChildren( H1 ); /* Step 3 */ H1->Npl = H1->Right->Npl + 1; } /* end else */ return H1; } What if Npl is NOT updated? Tp = O(log N) 5/19

 Merge (iterative version): Step 2: Swap children if necessary §6 Leftist Heaps  Merge (iterative version): Step 2: Swap children if necessary 3 10 21 14 23 8 17 26 6 12 18 24 33 7 37 H1 H2 3 10 21 14 23 6 12 18 24 33 7 37 18 8 17 26 Step 1: Sort the right paths without changing their left children 3 10 21 14 23  DeleteMin: 6 12 18 24 33 Step 1: Delete the root 7 37 Step 2: Merge the two subtrees 8 17 26 18 Tp = O(log N) 6/19

Target : Any M consecutive operations take at most O(M log N) time. §7 Skew Heaps -- a simple version of the leftist heaps Target : Any M consecutive operations take at most O(M log N) time.  Merge: Always swap the left and right children except that the largest of all the nodes on the right paths does not have its children swapped. No Npl. 3 10 21 14 23 3 10 21 14 23 8 17 26 6 12 18 24 33 7 37 H1 H2 Not really a special case, but a natural stop in the recursions. 6 12 18 24 33 7 37 26 8 17 18 This is NOT always the case. 7/19

【Example】Insert 15  Merge (iterative version): H1 H2 §7 Skew Heaps 3 10 21 14 23 6 12 18 24 33 7 37 26 8 17 15 3 6 12 18 24 33 7 37 26 8 17 10 21 14 23 15  Merge (iterative version): 3 10 21 14 23 3 10 21 14 23 8 17 26 6 12 18 24 33 7 37 H1 H2 6 12 18 24 33 7 37 26 8 17 18 8/19

§7 Skew Heaps Note:  Skew heaps have the advantage that no extra space is required to maintain path lengths and no tests are required to determine when to swap children.  It is an open problem to determine precisely the expected right path length of both leftist and skew heaps. 9/19

 Bk consists of a root with children, which are §8 Binomial Queues  Structure: A binomial queue is not a heap-ordered tree, but rather a collection of heap-ordered trees, known as a forest. Each heap-ordered tree is a binomial tree. According to Theorem 5.1 on p.156, the total time should be O(N)… Constant! So O(log N) for an insertion is NOT good enough! Haven’t we had enough about queues? What is a binomial queue for? Well, what is the average time for insertions with leftist or skew heaps? What is the total time for inserting N keys into an empty binary heap? O(log N) – What’s wrong? Then what is the average time? A binomial tree of height 0 is a one-node tree. A binomial tree, Bk, of height k is formed by attaching a binomial tree, Bk – 1, to the root of another binomial tree, Bk – 1. B0 B1 B2 B3 Binomial coefficient  Bk consists of a root with children, which are k B0, B1, …, Bk – 1 . k d Bk has exactly nodes. The number of nodes at depth d is . 2k 10/19

Bk structure + heap order + one binomial tree for each height §8 Binomial Queues Bk structure + heap order + one binomial tree for each height A priority queue of any size can be uniquely represented by a collection of binomial trees. 【Example】Represent a priority queue of size 13 by a collection of binomial trees. Solution: 13 = 20 + 021 + 22 + 23 = 11012 B0 B1 B2 B3 13 23 51 24 65 12 21 14 26 16 18 11/19

Must keep the trees in the binomial queue sorted by height.  Operations:  FindMin: The minimum key is in one of the roots. There are at most roots, hence Tp = O( ). log N log N Note: We can remember the minimum and update whenever it is changed. Then this operation will take O(1).  Merge: H1 H2 13 12 21 24 65 16 18 14 26 23 51 1 1 0 2 = 6 + 1 1 1 2 = 7 Must keep the trees in the binomial queue sorted by height. c c 1 1 1 H1 12 21 24 65 23 51 14 26 16 18 13 Tp = O( ) log N 12/19

 Insert: a special case for merging. §8 Binomial Queues  Insert: a special case for merging. 【Example】Insert 1, 2, 3, 4, 5, 6, 7 into an initially empty queue. 1 2 3 4 5 6 3 7 5 1 2 4 Note: If the smallest nonexistent binomial tree is Bi , then Tp = Const · (i + 1). Performing N Inserts on an initially empty binomial queue will take O(N) worst-case time. Hence the average time is constant. 13/19

 DeleteMin ( H ): H H’ H” H §8 Binomial Queues Step 1: FindMin in Bk 13 14 26 16 18 12 21 24 65 23 51 Step 1: FindMin in Bk /* O(log N) */ H’ 13 14 26 16 18 Step 2: Remove Bk from H /* O(1) */ 21 24 65 23 51 H” Step 3: Remove root from Bk /* O(log N) */ Step 4: Merge (H’, H”) 23 51 24 65 H 13 21 14 26 16 18 /* O(log N) */ 14/19

Operation Property Solution DeleteMin Merge H §8 Binomial Queues  Implementation: Binomial queue = array of binomial trees Operation Property Solution DeleteMin Find all the subtrees quickly Left-child-next-sibling with linked lists The children are ordered by their sizes The new tree will be the largest. Hence maintain the trees in decreasing sizes Merge H …… [0] [1] [2] [3] [4] 12 24 21 65 23 51 14 16 26 18 13 13 14 26 16 18 12 21 24 65 23 51 15/19

typedef struct BinNode *Position; typedef struct Collection *BinQueue; §8 Binomial Queues typedef struct BinNode *Position; typedef struct Collection *BinQueue; typedef struct BinNode *BinTree; /* missing from p.176 */ struct BinNode { ElementType Element; Position LeftChild; Position NextSibling; } ; struct Collection int CurrentSize; /* total number of nodes */ BinTree TheTrees[ MaxTrees ]; 16/19

Tp = O( 1 ) §8 Binomial Queues BinTree CombineTrees( BinTree T1, BinTree T2 ) { /* merge equal-sized T1 and T2 */ if ( T1->Element > T2->Element ) /* attach the larger one to the smaller one */ return CombineTrees( T2, T1 ); /* insert T2 to the front of the children list of T1 */ T2->NextSibling = T1->LeftChild; T1->LeftChild = T2; return T1; } Tp = O( 1 ) 12 24 21 65 14 16 26 18 T1 T2 17/19

§8 Binomial Queues BinQueue Merge( BinQueue H1, BinQueue H2 ) { BinTree T1, T2, Carry = NULL; int i, j; if ( H1->CurrentSize + H2-> CurrentSize > Capacity ) ErrorMessage(); H1->CurrentSize += H2-> CurrentSize; for ( i=0, j=1; j<= H1->CurrentSize; i++, j*=2 ) { T1 = H1->TheTrees[i]; T2 = H2->TheTrees[i]; /*current trees */ switch( 4*!!Carry + 2*!!T2 + !!T1 ) { /* assign each digit to a tree */ case 0: /* 000 */ case 1: /* 001 */ break; case 2: /* 010 */ H1->TheTrees[i] = T2; H2->TheTrees[i] = NULL; break; case 4: /* 100 */ H1->TheTrees[i] = Carry; Carry = NULL; break; case 3: /* 011 */ Carry = CombineTrees( T1, T2 ); H1->TheTrees[i] = H2->TheTrees[i] = NULL; break; case 5: /* 101 */ Carry = CombineTrees( T1, Carry ); H1->TheTrees[i] = NULL; break; case 6: /* 110 */ Carry = CombineTrees( T2, Carry ); H2->TheTrees[i] = NULL; break; case 7: /* 111 */ H1->TheTrees[i] = Carry; Carry = CombineTrees( T1, T2 ); } /* end switch */ } /* end for-loop */ return H1; } Carry T2 T1 18/19

§8 Binomial Queues 19/19 ElementType DeleteMin( BinQueue H ) { BinQueue DeletedQueue; Position DeletedTree, OldRoot; ElementType MinItem = Infinity; /* the minimum item to be returned */ int i, j, MinTree; /* MinTree is the index of the tree with the minimum item */ if ( IsEmpty( H ) ) { PrintErrorMessage(); return –Infinity; } for ( i = 0; i < MaxTrees; i++) { /* Step 1: find the minimum item */ if( H->TheTrees[i] && H->TheTrees[i]->Element < MinItem ) { MinItem = H->TheTrees[i]->Element; MinTree = i; } /* end if */ } /* end for-i-loop */ DeletedTree = H->TheTrees[ MinTree ]; H->TheTrees[ MinTree ] = NULL; /* Step 2: remove the MinTree from H => H’ */ OldRoot = DeletedTree; /* Step 3.1: remove the root */ DeletedTree = DeletedTree->LeftChild; free(OldRoot); DeletedQueue = Initialize(); /* Step 3.2: create H” */ DeletedQueue->CurrentSize = ( 1<<MinTree ) – 1; /* 2MinTree – 1 */ for ( j = MinTree – 1; j >= 0; j – – ) { DeletedQueue->TheTrees[j] = DeletedTree; DeletedTree = DeletedTree->NextSibling; DeletedQueue->TheTrees[j]->NextSibling = NULL; } /* end for-j-loop */ H->CurrentSize – = DeletedQueue->CurrentSize + 1; Merge( H, DeletedQueue ); /* Step 4: merge H’ and H” */ return MinItem; } 19/19