Heapsort Running Time Build-Heap(A

Total Page:16

File Type:pdf, Size:1020Kb

Heapsort Running Time Build-Heap(A Heapsort Heap of n elements has height (show) - sorts in place – worst case running time All operations run in time proportional to the height - special data structure heap – complete binary tree Extract-Max, Insert, Extract-Min, Delete (completely filled except the lowest level) - Useful for priority queues Heap property : value of the node is at most - Discrete event simulators value of the parent A[Parent(i)] >= A[i] Algorithms for Heapify how to manipulate heaps Height of the node – number of edges on the longest path Buildheap how to build a heap from leaf to that node Heapsort how to sort using heap Idea: Implicit data structure – heap is represented by an Extract_max, array two attributes Insert maintain heap property is length(A) – length of the array heapsize(A) – number of elements of the heap Heap-Extract-Max(A) Remove A[1] How to get back and forth in the heap A[1] <- A[n] N <- n –1 Heapify(A,1,n) Different from the book Heapify(A,i,n) % assumes that L and R subtrees are heaps Build-Heap(A,n) % makes i’s subtree a heap if 2i · n and A[2i] > A[i] % build heap from unsorted array then largest <- 2i For i = n downto 1 else largest <- i do Heapify(A,i,n) if 2i+1 · n and A[2i+1] > A[largest] then largest <= 2i + 1 Simple bound on running time ≠ if largest i In fact more tight bound then exchange A[I] <-> A[largest] Heapify(A,largest,n) (idea of amortized analysis – more in chapter 18) Running time 1 Decision Tree Model Heapsort(A,n) Build-Heap(A,n) How to make sorting more efficient ? for i Å n downto 2 do exchange A[1] ÅÆ A[i] Can we do better then ? how to beat it ? Heapify(A,1,i-1) • establish the lower bound using the decision tree model Until now – sorting using comparisons only We for sure have to do at least comparisons to Variations – heap can be min (max) heap See all the input – can we provide tighter lower bound ? - heap can be k-ary tree instead of binary tree Decision Tree model – abstraction of comparison sorts Data Structures for dynamic sets – need to support -different algorithms follow different path in the decision tree efficiently basic operations (insert, delete, minimum, search) - there are at least n! leaves e.g. priority queues - decision tree for sorting n-elements has height - any sorting algorithm using comparisons takes at least on the order of n lg n operations 2.
Recommended publications
  • Advanced Data Structures
    Advanced Data Structures PETER BRASS City College of New York CAMBRIDGE UNIVERSITY PRESS Cambridge, New York, Melbourne, Madrid, Cape Town, Singapore, São Paulo Cambridge University Press The Edinburgh Building, Cambridge CB2 8RU, UK Published in the United States of America by Cambridge University Press, New York www.cambridge.org Information on this title: www.cambridge.org/9780521880374 © Peter Brass 2008 This publication is in copyright. Subject to statutory exception and to the provision of relevant collective licensing agreements, no reproduction of any part may take place without the written permission of Cambridge University Press. First published in print format 2008 ISBN-13 978-0-511-43685-7 eBook (EBL) ISBN-13 978-0-521-88037-4 hardback Cambridge University Press has no responsibility for the persistence or accuracy of urls for external or third-party internet websites referred to in this publication, and does not guarantee that any content on such websites is, or will remain, accurate or appropriate. Contents Preface page xi 1 Elementary Structures 1 1.1 Stack 1 1.2 Queue 8 1.3 Double-Ended Queue 16 1.4 Dynamical Allocation of Nodes 16 1.5 Shadow Copies of Array-Based Structures 18 2 Search Trees 23 2.1 Two Models of Search Trees 23 2.2 General Properties and Transformations 26 2.3 Height of a Search Tree 29 2.4 Basic Find, Insert, and Delete 31 2.5ReturningfromLeaftoRoot35 2.6 Dealing with Nonunique Keys 37 2.7 Queries for the Keys in an Interval 38 2.8 Building Optimal Search Trees 40 2.9 Converting Trees into Lists 47 2.10
    [Show full text]
  • A Pointer-Free Data Structure for Merging Heaps and Min-Max Heaps
    Theoretical Computer Science 84 (1991) 107-126 107 Elsevier A pointer-free data structure for merging heaps and min-max heaps Giorgio Gambosi and Enrico Nardelli Istituto di Analisi dei Sistemi ed Informutica, C.N.R., Roma, Italy Maurizio Talamo Dipartimento di Matematica Pura ed Applicata, University of L’Aquila, L’Aquila, Italy, and Istituto di Analisi dei Sistemi ed Informatica, C.N.R., Roma, Italy Abstract Gambosi, G., E. Nardelli and M. Talamo, A pointer-free data structure for merging heaps and min-max heaps, Theoretical Computer Science 84 (1991) 107-126. In this paper a data structure for the representation of mergeable heaps and min-max heaps without using pointers is introduced. The supported operations are: Insert, DeleteMax, DeleteMin, FindMax, FindMin, Merge, NewHeap, DeleteHeap. The structure is analyzed in terms of amortized time complexity, resulting in a O(1) amortized time for each operation except for Insert, for which a O(lg n) bound holds. 1. Introduction The use of pointers in data structures seems to contribute quite significantly to the design of efficient algorithms for data access and management. Implicit data structures [ 131 have been introduced in order to evaluate the impact of the absence of pointers on time efficiency. Traditionally, implicit data structures have been mostly studied for what concerns the dictionary problem, both in l-dimensional [4,5,9, 10, 141, and in multi- dimensional space [l]. In such papers, the maintenance of a single dictionary has been analyzed, not considering the case in which several instances of the same structure (i.e. several dictionaries) have to be represented and maintained at the same time and within the same array-structured memory.
    [Show full text]
  • Lecture III BALANCED SEARCH TREES
    §1. Keyed Search Structures Lecture III Page 1 Lecture III BALANCED SEARCH TREES Anthropologists inform that there is an unusually large number of Eskimo words for snow. The Computer Science equivalent of snow is the tree word: we have (a,b)-tree, AVL tree, B-tree, binary search tree, BSP tree, conjugation tree, dynamic weighted tree, finger tree, half-balanced tree, heaps, interval tree, kd-tree, quadtree, octtree, optimal binary search tree, priority search tree, R-trees, randomized search tree, range tree, red-black tree, segment tree, splay tree, suffix tree, treaps, tries, weight-balanced tree, etc. The above list is restricted to trees used as search data structures. If we include trees arising in specific applications (e.g., Huffman tree, DFS/BFS tree, Eskimo:snow::CS:tree alpha-beta tree), we obtain an even more diverse list. The list can be enlarged to include variants + of these trees: thus there are subspecies of B-trees called B - and B∗-trees, etc. The simplest search tree is the binary search tree. It is usually the first non-trivial data structure that students encounter, after linear structures such as arrays, lists, stacks and queues. Trees are useful for implementing a variety of abstract data types. We shall see that all the common operations for search structures are easily implemented using binary search trees. Algorithms on binary search trees have a worst-case behaviour that is proportional to the height of the tree. The height of a binary tree on n nodes is at least lg n . We say that a family of binary trees is balanced if every tree in the family on n nodes has⌊ height⌋ O(log n).
    [Show full text]
  • 23. Metric Data Structures
    This book is licensed under a Creative Commons Attribution 3.0 License 23. Metric data structures Learning objectives: • organizing the embedding space versus organizing its contents • quadtrees and octtrees. grid file. two-disk-access principle • simple geometric objects and their parameter spaces • region queries of arbitrary shape • approximation of complex objects by enclosing them in simple containers Organizing the embedding space versus organizing its contents Most of the data structures discussed so far organize the set of elements to be stored depending primarily, or even exclusively, on the relative values of these elements to each other and perhaps on their order of insertion into the data structure. Often, the only assumption made about these elements is that they are drawn from an ordered domain, and thus these structures support only comparative search techniques: the search argument is compared against stored elements. The shape of data structures based on comparative search varies dynamically with the set of elements currently stored; it does not depend on the static domain from which these elements are samples. These techniques organize the particular contents to be stored rather than the embedding space. The data structures discussed in this chapter mirror and organize the domain from which the elements are drawn—much of their structure is determined before the first element is ever inserted. This is typically done on the basis of fixed points of reference which are independent of the current contents, as inch marks on a measuring scale are independent of what is being measured. For this reason we call data structures that organize the embedding space metric data structures.
    [Show full text]
  • Fundamental Data Structures Contents
    Fundamental Data Structures Contents 1 Introduction 1 1.1 Abstract data type ........................................... 1 1.1.1 Examples ........................................... 1 1.1.2 Introduction .......................................... 2 1.1.3 Defining an abstract data type ................................. 2 1.1.4 Advantages of abstract data typing .............................. 4 1.1.5 Typical operations ...................................... 4 1.1.6 Examples ........................................... 5 1.1.7 Implementation ........................................ 5 1.1.8 See also ............................................ 6 1.1.9 Notes ............................................. 6 1.1.10 References .......................................... 6 1.1.11 Further ............................................ 7 1.1.12 External links ......................................... 7 1.2 Data structure ............................................. 7 1.2.1 Overview ........................................... 7 1.2.2 Examples ........................................... 7 1.2.3 Language support ....................................... 8 1.2.4 See also ............................................ 8 1.2.5 References .......................................... 8 1.2.6 Further reading ........................................ 8 1.2.7 External links ......................................... 9 1.3 Analysis of algorithms ......................................... 9 1.3.1 Cost models ......................................... 9 1.3.2 Run-time analysis
    [Show full text]
  • A Dynamic Data Structure for Approximate Range Searching∗
    A Dynamic Data Structure for Approximate Range Searching∗ David M. Mount Eunhui Park Department of Computer Science Department of Computer Science University of Maryland University of Maryland College Park, Maryland College Park, Maryland [email protected] [email protected] ABSTRACT 1. INTRODUCTION In this paper, we introduce a simple, randomized dynamic Recent decades have witnessed the development of many data structure for storing multidimensional point sets, called data structures for efficient geometric search and retrieval. a quadtreap. This data structure is a randomized, balanced A fundamental issue in the design of these data structures variant of a quadtree data structure. In particular, it de- is the balance between efficiency, generality, and simplicity. fines a hierarchical decomposition of space into cells, which One of the most successful domains in terms of simple, prac- are based on hyperrectangles of bounded aspect ratio, each tical approaches has been the development of linear-sized of constant combinatorial complexity. It can be viewed as a data structures for approximate geometric retrieval prob- multidimensional generalization of the treap data structure lems involving points sets in low dimensional spaces. In of Seidel and Aragon. When inserted, points are assigned this paper, our particular interest is in dynamic data struc- random priorities, and the tree is restructured through ro- tures, which allow the insertion and deletion of points for tations as if the points had been inserted in priority order. use in approximate retrieval problems, such as range search- In any fixed dimension d, we show it is possible to store a ing and nearest neighbor searching.
    [Show full text]
  • Worst Case Efficient Data Structures
    BRICS Basic Research in Computer Science BRICS DS-97-1 G. S. Brodal: Worst Case Efficient Data Structures Worst Case Efficient Data Structures Gerth Stølting Brodal BRICS Dissertation Series DS-97-1 ISSN 1396-7002 January 1997 Copyright c 1997, BRICS, Department of Computer Science University of Aarhus. All rights reserved. Reproduction of all or part of this work is permitted for educational or research use on condition that this copyright notice is included in any copy. See back inner page for a list of recent BRICS Dissertation Series publi- cations. Copies may be obtained by contacting: BRICS Department of Computer Science University of Aarhus Ny Munkegade, building 540 DK–8000 Aarhus C Denmark Telephone: +45 8942 3360 Telefax: +45 8942 3255 Internet: [email protected] BRICS publications are in general accessible through the World Wide Web and anonymous FTP through these URLs: http://www.brics.dk ftp://ftp.brics.dk This document in subdirectory DS/97/1/ Worst Case Efficient Data Structures Gerth Stølting Brodal Ph.D. Dissertation Department of Computer Science University of Aarhus Denmark Worst Case Efficient Data Structures A Dissertation Presented to the Faculty of Science of the University of Aarhus in Partial Fulfillment of the Requirements for the Ph.D. Degree by Gerth Stølting Brodal January 31, 1997 Abstract We study the design of efficient data structures. In particular we focus on the design of data structures where each operation has a worst case efficient implementations. The concrete prob- lems we consider are partial persistence, implementation of priority queues, and implementation of dictionaries. The first problem we consider is how to make bounded in-degree and out-degree data structures partially persistent, i.e., how to remember old versions of a data structure for later access.
    [Show full text]
  • MASTER of SCIENCE December, 1999 Oklaho.,..,,'" C '"I/O I It
    COMPARATIVE STUDY OF PRIORITY QUEUES IMPLEMENTAnON By DONGHONG WEI Bachelor ofForeign Languages Shanxi University Taiyuan, China 1990 Submitted to the Faculty ofthe Graduate College ofthe Oklahoma State University in partial fulfillment of the requirements for the degree of MASTER OF SCIENCE December, 1999 Oklaho.,..,,'" C '"I/o I It ... :• •~ ,._ .oJ., •, 'f """"':Iry COMPARATIVE STUDY OF PRIORITY QUEUES IMPLEMENTATION Thesis Approved: DeaOfthe Graduate College ii ACKNOWLEDGMENTS I wish to express my deepest appreciation to my advisor Dr. Jacques Lafrance for accepting to be my major advisor. His enthusiastic support, constructive guidance, encouragement, and friendship throughout time of acquaintance have been a constant source of inspiration and motivation that helped me gain confidence academically and professionally. My sincere appreciation also extends to Dr. John P. Chandler whose intelligent suggestion and guidance have made this thesis feasible. r would like to thank Dr. H. K. Dai for his constructive criticism, direction and wisdom. Grateful appreciation also goes to the faculty and students ofthe Department of Computer Science for their interest, guidance, and friendship. I wish to sincerely thank my husband for his unconditional love, spiritual support and endless encouragement. To my parents, Shuren Wei and Shuzhen Ma, I want to tell them how much I love them and that r could not have made it through graduate school without their total support and consistent encouragement. I dedicate this thesis to my dearest daughter, Connie Yang, who is the reason why I have worked so feverishly to complete this project. Finally, I would like to praise God for providing me with confidence, wisdom, persistence and strength that guided me through the entire process.
    [Show full text]
  • Strictly Implicit Priority Queues: on the Number of Moves and Worst-Case Time
    Strictly Implicit Priority Queues: On the Number of Moves and Worst-Case Time Gerth Stølting Brodal, Jesper Sindahl Nielsen, and Jakob Truelsen MADALGO?, Department of Computer Science, Aarhus University, Denmark fgerth,jasn,[email protected] Abstract. The binary heap of Williams (1964) is a simple priority queue characterized by only storing an array containing the elements and the number of elements n { here denoted a strictly implicit priority queue. We introduce two new strictly implicit priority queues. The first struc- ture supports amortized O(1) time Insert and O(log n) time Extract- Min operations, where both operations require amortized O(1) element moves. No previous implicit heap with O(1) time Insert supports both operations with O(1) moves. The second structure supports worst-case O(1) time Insert and O(log n) time (and moves) ExtractMin opera- tions. Previous results were either amortized or needed O(log n) bits of additional state information between operations. 1 Introduction In 1964 Williams presented \Algorithm 232" [12], commonly known as the binary heap. The binary heap is a priority queue data structure storing a dynamic set of n elements from a totally ordered universe, supporting the insertion of an element (Insert) and the deletion of the minimum element (ExtractMin) in worst-case O(log n) time. The binary heap structure is an implicit data structure, i.e., it consists of an array of length n storing the elements, and no information is stored between operations except for the array and the value n. Sometimes data structures storing O(1) additional words are also called implicit.
    [Show full text]
  • Binary Trees, While 3-Ary Trees Are Sometimes Called Ternary Trees
    Rooted Tree A rooted tree is a tree with a countable number of nodes, in which a particular node is distinguished from the others and called the root: In some contexts, in which only a rooted tree would make sense, the term tree is often used. Infinite Tree A rooted tree is infinite if it contains a countably infinite number of nodes. Finite Tree Similarly, a rooted tree is finite if it contains a finite number of nodes. Parent Consider a rooted tree T whose root is r T Let t be a node of T From Paths in Trees are Unique, there is only one path from t to r T Let π:T−{r T }→T be the mapping defined as: π(t)= the node adjacent to t on the path to r T Then π(t) is known as the parent (or parent node) of t , and π as the parent function or parent mapping. Root Node The root node, or just root, is the one node in a rooted tree which, by definition, has no parent. Ancestor An ancestor (or ancestor node) of a node t of a rooted tree T whose root is r T is a node in the path from t to r T . Thus, the root of a rooted tree T is the ancestor of every node of T (including itself). Proper Ancestor A proper ancestor of a node t is an ancestor of t which is not t itself. Children The children (or child nodes) of a node t in a rooted tree T are the elements of the set: {s∈T:π(s)=t} That is, the children of t are all the nodes of T of which t is the parent.
    [Show full text]
  • 38 POINT LOCATION Jack Snoeyink
    38 POINT LOCATION Jack Snoeyink INTRODUCTION “Where am I?” is a basic question for many computer applications that employ geometric structures (e.g., in computer graphics, geographic information systems, robotics, and databases). Given a set of disjoint geometric objects, the point- location problem asks for the object containing a query point that is specified by its coordinates. Instances of the problem vary in the dimension and type of objects and whether the set is static or dynamic. Classical solutions vary in preprocessing time, space used, and query time. Recent solutions also consider entropy of the query distribution, or exploit randomness, external memory, or capabilities of the word RAM machine model. Point location has inspired several techniques for structuring geometric data, which we survey in this chapter. We begin with point location in one dimension (Section 38.1) or in one polygon (Section 38.2). In two dimensions, we look at how techniques of persistence, fractional cascading, trapezoid graphs, or hierarchi- cal triangulations can lead to optimal comparison-based methods for point location in static subdivisions (Section 38.3), the current best methods for dynamic subdi- visions (Section 38.4), and at methods not restricted to comparison-based models (Section 38.5). There are fewer results on point location in higher dimensions; these we mention in (Section 38.6). The vision/robotics term localization refers to the opposite problem of de- termining (approximate) coordinates from the surrounding local geometry. This chapter deals exclusively with point location. 38.1 ONE-DIMENSIONAL POINT LOCATION The simplest nontrivial instance of point location is list searching.
    [Show full text]
  • A Survey on Priority Queues
    A Survey on Priority Queues Gerth Stølting Brodal MADALGO?, Department of Computer Science, Aarhus University, [email protected] Abstract. Back in 1964 Williams introduced the binary heap as a basic priority queue data structure supporting the operations Insert and Ex- tractMin in logarithmic time. Since then numerous papers have been published on priority queues. This paper tries to list some of the direc- tions research on priority queues has taken the last 50 years. 1 Introduction In 1964 Williams introduced \Algorithm 232" [125]|a data structure later known as binary heaps. This data structure essentially appears in all introduc- tory textbooks on Algorithms and Data Structures because of its simplicity and simultaneously being a powerful data structure. A tremendous amount of research has been done within the design and anal- ysis of priority queues over the last 50 years, building on ideas originating back to the initial work of Williams and Floyd in the early 1960s. In this paper I try to list some of this work, but it is evident by the amount of research done that the list is in no respect complete. Many papers address several aspects of priority queues. In the following only a few of these aspects are highlighted. 2 The beginning: Binary heaps Williams' binary heap is a data structure to store a dynamic set of elements from an ordered set supporting the insertion of an element (Insert) and the deletion of the minimum element (ExtractMin) in O(lg n) time, where n is the number of elements in the priority queue1. Williams' data structure was inspired by Floyd's 1962 paper on sorting using a tournament tree [65], but compared to Floyd's earlier work a binary heap is implicit, i.e.
    [Show full text]