Amortized Analysis and Heaps Intro David Kauchak cs302 Spring 2013.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

ArrayLists David Kauchak cs201 Spring Extendable array Arrays store data in sequential locations in memory Elements are accessed via their index.
Sorting Comparison-based algorithm review –You should know most of the algorithms –We will concentrate on their analyses –Special emphasis: Heapsort Lower.
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 24 Sorting.
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.
CSE332: Data Abstractions Lecture 9: B Trees Dan Grossman Spring 2010.
Sorting Heapsort Quick review of basic sorting methods Lower bounds for comparison-based methods Non-comparison based sorting.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
Tirgul 4 Sorting: – Quicksort – Average vs. Randomized – Bucket Sort Heaps – Overview – Heapify – Build-Heap.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
FALL 2006CENG 351 Data Management and File Structures1 External Sorting.
B + -Trees (Part 1). Motivation AVL tree with N nodes is an excellent data structure for searching, indexing, etc. –The Big-Oh analysis shows most operations.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
Tirgul 7 Heaps & Priority Queues Reminder Examples Hash Tables Reminder Examples.
Tirgul 4 Order Statistics Heaps minimum/maximum Selection Overview
Heaps and heapsort COMP171 Fall 2005 Part 2. Sorting III / Slide 2 Heap: array implementation Is it a good idea to store arbitrary.
Heapsort Based off slides by: David Matuszek
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
CS261 – Recitation 5 Fall Outline Assignment 3: Memory and Timing Tests Binary Search Algorithm Binary Search Tree Add/Remove examples 1.
TECH Computer Science Dynamic Sets and Searching Analysis Technique  Amortized Analysis // average cost of each operation in the worst case Dynamic Sets.
Chapter 21 Binary Heap.
Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.
1 Joe Meehean.  Problem arrange comparable items in list into sorted order  Most sorting algorithms involve comparing item values  We assume items.
CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 26 Sorting.
1 Heaps (Priority Queues) You are given a set of items A[1..N] We want to find only the smallest or largest (highest priority) item quickly. Examples:
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Kevin Quinn Fall 2015.
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.
Heapsort. What is a “heap”? Definitions of heap: 1.A large area of memory from which the programmer can allocate blocks as needed, and deallocate them.
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.
Heaps and basic data structures David Kauchak cs161 Summer 2009.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 25 Sorting.
Priority Queues, Heaps, and Heapsort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
CS 367 Introduction to Data Structures Lecture 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.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
8/3/2007CMSC 341 BTrees1 CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
Priority Queues, Heaps, and Heapsort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 23 Sorting.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Sorting and "Big Oh" ASFA AP Computer Science A SortingBigOh.
"Teachers open the door, but you must enter by yourself. "
CSE373: Data Structures & Algorithms Priority Queues
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
Andreas Klappenecker [partially based on the slides of Prof. Welch]
Hashing CSE 2011 Winter July 2018.
Quick-Sort 9/12/2018 3:26 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Hashing Exercises.
CMSC 341: Data Structures Priority Queues – Binary Heaps
CSE332: Data Abstractions Lecture 5: Binary Heaps, Continued
i206: Lecture 14: Heaps, Graphs intro.
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
B-Tree Insertions, Intro to Heaps
Heaps and the Heapsort Heaps and priority queues
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
CSE 332: Data Structures Priority Queues – Binary Heaps Part II
Data Structures and Analysis (COMP 410)
CSE373: Data Structures & Algorithms Lecture 7: Binary Heaps, Continued Dan Grossman Fall 2013.
Quick-Sort 4/25/2019 8:10 AM Quick-Sort     2
Amortized Analysis and Heaps Intro
Priority Queues (Heaps)
CS203 Lecture 15.
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.
Presentation transcript:

Amortized Analysis and Heaps Intro David Kauchak cs302 Spring 2013

Admin Homeworks 4 and 5 back soon How are the homeworks going?

Extensible array Sequential locations in memory in linear order Elements are accessed via index Access of particular indices is O(1) Say we want to implement an array that supports add (i.e. addToBack) ArrayList or Vector in Java arrays in Python, perl, Ruby, … How can we do it?

Extensible array Idea 1: Each time we call add, create a new array one element large, copy the data over and add the element Running time:Θ(n)

Extensible array Idea 2: Allocate extra, unused memory and save room to add elements For example: new ArrayList(2) allocated for actual array extra space for calls to add

Extensible array Idea 2: Allocate extra, unused memory and save room to add elements Adding an item: Running time:Θ(1)Problems?

Extensible array Idea 2: Allocate extra, unused memory and save room to add elements How much extra space do we allocate? Too little, and we might run out (e.g. add 15 items) Too much, and we waste lots of memory Ideas?

Extensible array Idea 3: Allocate some extra memory and when it fills up, allocate some more and copy For example: new ArrayList(2) …

Extensible array Idea 3: Allocate some extra memory and when it fills up, allocate some more and copy For example: new ArrayList(2) … Running time:Θ(n)

Extensible array Idea 3: Allocate some extra memory and when it fills up, allocate some more and copy For example: new ArrayList(2) … How much extra memory should we allocate?

Extensible array … Challenge: most of the calls to add will be O(1) How else might we talk about runtime? What is the average running time of add in the worst case? Note this is different than the average-case running time

Amortized analysis What does “amortize” mean?

Amortized analysis There are many situations where the worst case running time is bad However, if we average the operations over n operations, the average time is more reasonable This is called amortized analysis This is different than average-case running time, which requires probabilistic reasoning about input The worse case running time doesn’t change

Amortized analysis Many approaches for calculating the amortized analysis we’ll just look at the counting/aggregate method book has others aggregate method figure out the big-O runtime for a sequence of n calls divide by n to get the average run-time per call

Amortized analysis What is the aggregate cost of n calls? Let’s assume it’s O(1) and then prove it Base case: size 1 array, add an element: O(1) Inductive case: assume n-1 calls are O(1), show that nth call is O(1) Two cases: -array need to be doubled -array does need to be doubled

Amortized analysis What is the aggregate cost of n calls? Case 1: doesn’t need doubling just add the element into the current array O(1) Case 2: need doubling O(n) operation to copy all the data over Overall cost of n-insertions: n-1*O(1) + O(n) = O(n) Amortized cost: O(n)/n = O(1) We amortize (spread) the cost of the O(n) operation over all of the previous O(1) operations

Amortized analysis Another way we could have done the analysis would be to calculate the total cost over n operations amortized O(1) Assume we start with an empty array with 1 location. What is the cost to insert n items?

Amortized analysis vs. worse case What is the worse case of add? Still O(n) If you have an application that needs it to be O(1), this implementation will not work! amortized analysis give you the cost of n operations (i.e. average cost) not the cost of any individual operation

Extensible arrays What if instead of doubling the array, we add instead increase the array by a fixed amount (call it k) each time Is the amortized run-time still O(1)? No! Why?

Amortized analysis Consider the cost of n insertions for some constant k

Amortized analysis Consider the cost of n insertions for some constant k amortized O(n)!

Another set data structure We want to support fast lookup and insertion (i.e. faster than linear) Arrays can easily made to be fast for one or the other fast search: keep list sorted O(n) insert O(log n) search fast insert: extensible array O(1) insert (amortized) O(n) search

Another set data structure Idea: store data in a collection of arrays array i has size 2 i an array is either full or empty (never partially full) each array is stored in sorted order no relationship between arrays

Another set data structure Which arrays are full and empty are based on the number of elements specifically, binary representation of the number of elements 4 items = 100 = A2-full, A1-empty, A 0 -empty 11 items = 1011 = A 3 -full, A 2 -empty, A 1 -full, A 0 -full Lookup: binary search through each array Worse case runtime? A 0 : [5] A 1 : [4, 8] A 2 : empty A 3 : [2, 6, 9, 12, 13, 16, 20, 25]

Another set data structure Lookup: binary search through each array Worse case: all arrays are full number of arrays = number of digits = log n binary search cost for each array = O(log n) O(log n log n) A 0 : [5] A 1 : [4, 8] A 2 : empty A 3 : [2, 6, 9, 12, 13, 16, 20, 25]

Another set data structure Insert(A, item) starting at i = 0 current = [item] as long as the level i is full merge current with A i using merge procedure store to current A i = empty i++ A i = current

Insert 5 A 0 : empty Insert starting at i = 0 current = [item] as long as the level i is full merge current with A i using merge procedure store to current A i = empty i++ A i = current

Insert 5 A 0 : [5] Insert starting at i = 0 current = [item] as long as the level i is full merge current with A i using merge procedure store to current A i = empty i++ A i = current

Insert 6 A 0 : [5] Insert starting at i = 0 current = [item] as long as the level i is full merge current with A i using merge procedure store to current A i = empty i++ A i = current

Insert 6 A 0 : empty A 1 : [5, 6] Insert starting at i = 0 current = [item] as long as the level i is full merge current with A i using merge procedure store to current A i = empty i++ A i = current

Insert 12 A 0 : empty A 1 : [5, 6] Insert starting at i = 0 current = [item] as long as the level i is full merge current with A i using merge procedure store to current A i = empty i++ A i = current

Insert 12 A 0 : [12] A 1 : [5, 6] Insert starting at i = 0 current = [item] as long as the level i is full merge current with A i using merge procedure store to current A i = empty i++ A i = current

Insert 4 A 0 : [12] A 1 : [5, 6] Insert starting at i = 0 current = [item] as long as the level i is full merge current with A i using merge procedure store to current A i = empty i++ A i = current

Insert 4 A 0 : empty A 1 : empty A 2 : [4, 5, 6, 12] Insert starting at i = 0 current = [item] as long as the level i is full merge current with A i using merge procedure store to current A i = empty i++ A i = current

Insert 23 A 0 : empty A 1 : empty A 2 : [4, 5, 6, 12] Insert starting at i = 0 current = [item] as long as the level i is full merge current with A i using merge procedure store to current A i = empty i++ A i = current

Insert 23 A 0 : [23] A 1 : empty A 2 : [4, 5, 6, 12] Insert starting at i = 0 current = [item] as long as the level i is full merge current with A i using merge procedure store to current A i = empty i++ A i = current

Another set data structure Insert starting at i = 0 current = [item] as long as the level i is full merge current with A i using merge procedure store to current A i = empty i++ A i = current running time?

Insert running time Worse case merge at each level … + n/2 + n = O(n) There are many insertions that won’t fall into this worse case What is the amortized worse case for insertion?

insert: amortized analysis Consider inserting n numbers how many times will A 0 be empty? how many times will we need to merge with A 0 ? how many times will we need to merge with A 1 ? how many times will we need to merge with A 2 ? … how many times will we need to merge with A log n ?

insert: amortized analysis Consider inserting n numbers how many times will A 0 be empty?n/2 how many times will we need to merge with A 0 ?n/2 how many times will we need to merge with A 1 ?n/4 how many times will we need to merge with A 2 ?n/8 … how many times will we need to merge with A log n ?1 cost of each of these steps? times

insert: amortized analysis Consider inserting n numbers how many times will A 0 be empty?n/2O(1) how many times will we need to merge with A 0 ?n/22 how many times will we need to merge with A 1 ?n/44 how many times will we need to merge with A 2 ?n/88 … how many times will we need to merge with A log n ?1n total cost: timescost

insert: amortized analysis Consider inserting n numbers how many times will A 0 be empty?n/2O(1) how many times will we need to merge with A 0 ?n/22 how many times will we need to merge with A 1 ?n/44 how many times will we need to merge with A 2 ?n/88 … how many times will we need to merge with A log n ?1n total cost:log n levels * O(n) each level O(n log n) cost for n inserts O(log n) amortized cost! timescost

Binary heap A binary tree where the value of a parent is greater than or equal to the value of its children Additional restriction: all levels of the tree are complete except the last Max heap vs. min heap

Binary heap - operations Maximum(S) - return the largest element in the set ExtractMax(S) – Return and remove the largest element in the set Insert(S, val) – insert val into the set IncreaseElement(S, x, val) – increase the value of element x to val BuildHeap(A) – build a heap from an array of elements

Binary heap How can we represent a heap?

Binary heap - references parent ≥ child complete tree level does not indicate size all nodes in a heap are themselves heaps

Binary heap - array

Binary heap - array Left child of A[3]?

Binary heap - array Left child of A[3]? 2*3 = 6

Binary heap - array Parent of A[8]?

Binary heap - array Parent of A[8]?

Binary heap - array

Identify the valid heaps 8 [15, 12, 3, 11, 10, 2, 1, 7, 8] [20, 18, 10, 17, 16, 15, 9, 14, 13]