COMP20010: Algorithms and Imperative Programming Lecture 2 Data structures for binary trees Priority queues.

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Heaps © 2004 Goodrich, Tamassia Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of.
Advertisements

The Dictionary ADT Definition A dictionary is an ordered or unordered list of key-element pairs, where keys are used to locate elements in the list. Example:
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)
Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)
Priority Queues - Ed. 2. and 3.: Chapter 7 – - Ed. 4.: Chapter 8 -
Data Structures Lecture 7 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
The Priority Queue Abstract Data Type. Heaps. Adaptable Priority Queue. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich,
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.
TCSS 343, version 1.1 Algorithms, Design and Analysis Transform and Conquer Algorithms Presorting HeapSort.
© 2004 Goodrich, Tamassia Priority Queues1 Heaps: Tree-based Implementation of a Priority Queue.
© 2004 Goodrich, Tamassia Heaps © 2004 Goodrich, Tamassia Heaps2 Priority Queue Sorting (§ 8.1.4) We can use a priority queue to sort a set.
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Priority Queues. Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out –The “smallest” element.
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.
Priority Queues1 Part-D1 Priority Queues. Priority Queues2 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is.
Chapter 8: Priority Queues and Heaps Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided.
Applied Algorithmics - week2
1 Priority Queues CPS212 Gordon College VIP. 2 Introduction to STL Priority Queues Adaptor container - underlying container may be either: – a template.
© 2004 Goodrich, Tamassia Priority Queues1. © 2004 Goodrich, Tamassia Priority Queues2 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection.
Heaps and Priority Queues Priority Queue ADT (§ 2.4.1) A priority queue stores a collection of items An item is a pair (key, element) Main.
COMP20010: Algorithms and Imperative Programming Lecture 3 Heaps Dictionaries and Hash Tables.
1 Priority Queues Stock trading (motivation) The priority queue ADT Implementing a priority queue with a sequence Elementary sorting Issues in sorting.
COMP20010: Algorithms and Imperative Programming Lecture 4 Ordered Dictionaries and Binary Search Trees AVL Trees.
CSC 213 – Large Scale Programming Lecture 15: Heap-based Priority Queue.
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.
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
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.
Priority Queues and Heaps. Outline and Reading PriorityQueue ADT (§8.1) Total order relation (§8.1.1) Comparator ADT (§8.1.2) Sorting with a Priority.
Chapter 2.4: Priority Queues and Heaps PriorityQueue ADT (§2.4.1) Total order relation (§2.4.1) Comparator ADT (§2.4.1) Sorting with a priority queue (§2.4.2)
HEAPS • Heaps • Properties of Heaps • HeapSort
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
CH 8. HEAPS AND PRIORITY QUEUES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
The ADT Table The ADT table, or dictionary Uses a search key to identify its items Its items are records that contain several pieces of data 2 Figure.
12/23/2015 2:18 AMPriority Queues1 Sell100IBM$122 Sell300IBM$120 Buy500IBM$119 Buy400IBM$118.
Heaps & Priority Queues
CS 2468: Assignment 2 (Due Week 9, Tuesday. Drop a hard copy in Mail Box 75 or hand in during the lecture) Use array representation (double a[]) to implement.
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.
Heap Sorting and Building
Quotes “From each according to his ability, to each according to his needs” -- Karl Marx/Queue ADT “In America, first you get the sugar, then you get the.
Priority Queues and Heaps Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
Priority Queues CS 110: Data Structures and Algorithms First Semester,
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
Priority Queues CS /02/05 L7: PQs Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
Priority Queues Last Update: Oct 23, 2014 EECS2011: Priority Queues1.
Sorting With Priority Queue In-place Extra O(N) space
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
Priority Queues Chuan-Ming Liu
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Heaps and Priority Queues
Priority Queues and Heaps
Part-D1 Priority Queues
Heaps and Priority Queues
Heaps 11/27/ :05 PM Heaps Heaps.
Tree Representation Heap.
Heaps and Priority Queues
© 2013 Goodrich, Tamassia, Goldwasser
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
Ch. 8 Priority Queues And Heaps
Priority Queues Sell 100 IBM $ $120 Buy 500 $ $118
Heaps and Priority Queues
Copyright © Aiman Hanna All rights reserved
Priority Queues Sell 100 IBM $ $120 Buy 500 $ $118
© 2013 Goodrich, Tamassia, Goldwasser
Heaps and Priority Queues
1 Lecture 10 CS2013.
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.
Heaps 9/29/2019 5:43 PM Heaps Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

COMP20010: Algorithms and Imperative Programming Lecture 2 Data structures for binary trees Priority queues

Lecture outline Different data structures for representing binary trees (vector-based, linked), linked structure for general trees; Priority queues (PQs), sorting using PQs;

Data structures for representing trees A vector-based data structure A vector-based structure for binary trees is based on a simple way of numbering the nodes of T. For every node v of T define an integer p(v): If v is the root, then p(v)=1; If v is the left child of the node u, then p(v)=2p(u); If v is the right child of the node u, then p(v)=2p(u)+1; The numbering function p(.) is known as a level numbering of the nodes in a binary tree T.

Data structures for representing trees A vector-based data structure ((((3+1)*3)/((9-5)+2))-((3*(7-4))+6)) - / + * * Binary tree level numbering

Data structures for representing trees A vector-based data structure The level numbering suggests a representation of a binary tree T by a vector S, such that the node v from T is associated with an element S[p(v)]; S - / + * * /+*+*

Data structures for representing trees A vector-based data structure OperationTime positions(), elements()O(n)O(n) swapElements(), replaceElement()O(1) root(), parent(), children()O(1) leftChild(), rightChild(), sibling()O(1) isInternal(), isExternal(), isRoot()O(1) Running times of the methods when a binary tree T is implemented as a vector

Data structures for representing trees A linked data structure The vector implementation of a binary tree is fast and simple, but it may be space inefficient when the tree height is large (why?); A natural way of representing a binary tree is to use a linked structure. Each node of T is represented by an object that references to the element v and the positions associated with with its parent and children. parent element left childright child

Data structures for representing trees A linked data structure for binary trees A B C D E FG nullA B C D E F G

Data structures for representing trees A linked data structure for general trees In order to extend the previous data structure to the case of general trees; In order to register a potentially large number of children of a node, we need to use a container (a list or a vector) to store the children, instead of using instance variables; A BCDE nullA B C DE

Keys and the total order relation In various applications it is frequently required to compare and rank objects according to some parameters or properties, called keys that are assigned to each object in a collection. A key is an object assigned to an element as a specific attribute that can be used to identify, rank or weight that element. A rule for comparing keys needs to be robustly defined (not contradicting). We need to define a total order relation, denoted by with the following properties: Reflexive property: ; Antisymmetric property: if and, then ; Transitive property: if and, then ; The comparison rule that satisfies the above properties defines a linear ordering relationship among a set of keys. In a finite collection of elements with a defined total order relation we can define the smallest key as the key for which for any other key k in the collection.

Priority queues A priority queue P is a container of elements with keys associated to them at the time of insertion. Two fundamental methods of a priority queue P are: insertItem(k,e) – inserts an element e with a key k into P; removeMin() – returns and removes from P an element with a smallest key; The priority queue ADT is simpler than that of the sequence ADT. This simplicity originates from the fact that the elements in a PQ are inserted and removed based on their keys, while the elements are inserted and removed from a sequence based on their positions and ranks.

Priority queues A comparator is an object that compares two keys. It is associated with a priority queue at the time of construction. A comparator method provides the following objects, each taking two keys and comparing them: isLess( ) – true if ; isLessOrEqualTo( ) – true if ; isEqualTo( ) – true if ; isGreater( ) – true if ; isGreaterOrEqualTo( ) – true if ; isComparable(k) – true if k can be compared;

PQ-Sort Sorting problem is to transform a collection C of n elements that can be compared and ordered according to a total order relation. Algorithm outline: Given a collection C of n elements; In the first phase we put the elements of C into an initially empty priority queue P by applying n insertItem(c) operations; In the second phase we extract the elements from P in non- decreasing order by applying n removeMin operations, and putting them back into C;

PQ-Sort Algorithm PQ-Sort(C,P); Input: A sequence C[1:n] and a priority queue P that compares keys (elements of C) using a total order relation; Output: A sequence C[1:n] sorted by the total order relation; while C is not empty do e C.removeFirst(); {remove an element e from C} P.insertItem(e,e); {the key is the element itself} while P is not empty do e P.removeMin() {remove the smallest element from P} C.insertLast(e) {add the element at the end of C} This algorithm does not specify how the priority queue P is implemented. Depending on that, several popular schemes can be obtained, such as selection-sort, insertion-sort and heap-sort.

Priority queue implemented with an unordered sequence and Selection-Sort Assume that the elements of P and their keys are stored in a sequence S, which is implemented as either an array or a doubly-linked list. The elements of S are pairs (k,e), where e is an element of P and k is the key. New element is added to S by appending it at the end (executing insertLast(k,e)), which means that S will be unsorted. insertLast() will take O(1) time, but finding the element in S with a minimal key will take O(n).

Priority queue implemented with an unordered sequence and Selection-Sort The first phase of the algorithm takes O(n) time, assuming that each insertion takes O(1) time. Assuming that two keys can be compared in O(1) time, the execution time of each removeMin operation is proportional to the number of elements currently in P. The main bottleneck of this algorithm is the repeated selection of a minimal element from an unsorted sequence in Phase 2. This is why the algorithm is referred to as selection-sort. The bottleneck of the selection-sort algorithm is the second phase. Total time needed for the second phase is

Priority queue implemented with a sorted sequence and Insertion-Sort An alternative approach is to sort the elements in the sequence S by their key values. In this case the method removeMin actually removes the first element from S, which takes O(1) time. However, the method insertItem requires to scan through the sequence S for an appropriate position to insert the new element and its key. This takes O(n) time. The main bottleneck of this algorithm is the repeated insertion of elements into a sorted priority queue. This is why the algorithm is referred to as insertion-sort. The total execution time of insertion-sort is dominated by the first phase and is.