Algorithms. Algorithms 2.4 PRIORITY QUEUES. API and elementary implementations binary heaps heapsort event-driven simulation (see videos)

Size: px
Start display at page:

Download "Algorithms. Algorithms 2.4 PRIORITY QUEUES. API and elementary implementations binary heaps heapsort event-driven simulation (see videos)"

Transcription

1 lgorithms B DGWICK KVIN WYN.4 IIY QUU lgorithms F U H D I I N I and elementary implementations binary heaps heapsort event-driven simulation (see videos) B DGWICK KVIN WYN ast updated on /6/9 6:9

2 .4 IIY QUU lgorithms I and elementary implementations binary heaps heapsort event-driven simulation (see videos) B DGWICK KVIN WYN

3 Collections collection is a data type that stores a group of items. data type core operations data structure stack UH, linked list, resizing array queue NQUU, DQUU linked list, resizing array priority queue IN, D- binary heap symbol table U, G, D binary search tree, hash table set DD, CNIN, D binary search tree, hash table how me your code and conceal your data structures, and I shall continue to be mystified. how me your data structures, and I won t usually need your code; it ll be obvious. Fred Brooks

4 riority queue Collections allow adding and removing items. Which item to remove? tack. emove the item most recently added. Queue. emove the item least recently added. andomized queue. emove a random item. riority queue. emove the largest (or smallest) item. Generalizes: stack, queue, randomized queue. operation argument insert insert insert remove max insert insert insert remove max insert insert insert remove max return value size Q Q sequenc 4

5 riority queue I equirement. Keys are generic; they must also be Comparable. Key must be Comparable ( bounded type parameter ) public class axq<key extends Comparable<Key>> axq() create an empty priority queue axq(key[] a) create a priority queue with given keys void insert(key v) insert a key into the priority queue Key delax() return and remove a largest key boolean ismpty() is the priority queue empty? Key max() return a largest key int size() number of entries in the priority queue Note. Duplicate keys allowed; delax() picks any maximum key. 5

6 riority queue: applications vent-driven simulation. [ customers in a line, colliding particles ] Numerical computation. [ reducing roundoff error ] Discrete optimization. [ bin packing, scheduling ] rtificial intelligence. [ * search ] Computer networks. [ web cache ] Data compression. [ Huffman codes ] perating systems. [ load balancing, interrupt handling ] Graph searching. [ Dijkstra s algorithm, rim s algorithm ] Number theory. [ sum of powers ] pam filtering. [ Bayesian spam filter ] tatistics. [ online median in data stream ] 6

7 riority queue: elementary implementation xercise. In the worst case, what are the running times for IN and D- for a priority queue implemented with an unordered array? an ordered array? operation argument return value size contents (unordered) contents (ordered) insert insert insert remove max insert insert insert remove max insert insert insert remove max Q Q Q Q sequence of operations on a priority queue Q Q 7

8 riority queues: quiz In the worst case, what are the running times for IN and D- for a priority queue implemented with an ordered array?. and n ignore array resizing B. and log n C. log n and D. n and

9 riority queue: implementations cost summary Challenge. Implement all operations efficiently. implementation IN D- unordered array n n ordered array n goal log n log n log n order of growth of running time for priority queue with n items what might this mean? olution. omewhat-ordered array. 9

10 .4 IIY QUU lgorithms I and elementary implementations binary heaps heapsort event-driven simulation B DGWICK KVIN WYN

11 Complete binary tree Binary tree. mpty or node with links to left and right binary trees. Complete tree. very level (except possibly the last) is completely filled; the last level is filled from left to right. complete binary tree with n = 6 nodes (height = 4) roperty. Height of complete binary tree with n nodes is lg n. ecursive definition

12 complete binary tree in nature

13 Binary heap: representation Binary heap. rray representation of a heap-ordered complete binary tree. Heap-ordered binary tree. Keys in nodes. arent s key no smaller than children s keys. i a[i] - N I H G rray representation. Indices start at. ake nodes in level order. No explicit links needed! N 4 5 N 6 7 I H G 8 9 I 0 H G Heap representations

14 riority queues: quiz Which is the index of the parent of the item at index k in a binary heap?. k/ - B. k/ C. k/ + D. *k N I 8 9 I 0 H G i a[i] - N I H G 4

15 Binary heap: properties roposition. argest key is a[], which is root of binary tree. roposition. Can use array indices to move through tree. arent of node at k is at k/. Children of node at k are at k and k+. i a[i] - N I H G N I H G 4 5 N I 0 H G Heap representations 5

16 Binary heap demo Insert. dd node at end, then swim it up. emove the maximum. xchange root with node at end, then sink it down. heap ordered N H I G N H I G 6

17 Binary heap: swim / promotion cenario. key becomes larger than its parent s key. o eliminate the violation: xchange key in child with key in parent. epeat until heap order restored. private void swim(int k) { N 5 while (k > && less(k/, k)) { I H G violates heap order (larger key than parent) } } exch(k, k/); k = k/; parent of node at k is at k/ N 5 I H G eter principle. Node promoted to level of incompetence. 7

18 Binary heap: insertion Insert. dd node at end in bottom level; then, swim it up. Cost. t most + lg n compares. insert N H public void insert(key x) I G key to insert { pq[++n] = x; swim(n); } N H I G add key to heap violates heap order swim up N I G H 8

19 Binary heap: sink / demotion cenario. key becomes smaller than one (or both) of its children s. o eliminate the violation: xchange key in parent with key in larger child. epeat until heap order restored. why not smaller child? private void sink(int k) { while (*k <= n) children of node at k violates heap order (smaller than a child) H { are at *k and *k+ 5 int j = *k; I N G } if (j < n && less(j, j+)) j++; if (!less(k, j)) break; exch(k, j); k = j; I 0 5 H N G } op-down reheapify (sink) ower struggle. Better subordinate promoted. 9

20 Binary heap: delete the maximum Delete max. xchange root with node at end; then, sink it down. Cost. t most lg n compares. remove the maximum key to remove public Key delax() { N I G H exchange key with root Key max = pq[]; exch(, n--); sink(); H violates heap order } pq[n+] = null; return max; prevent loitering N I G remove node from heap sink down N H I G 0

21 Binary heap: Java implementation public class axq<key extends Comparable<Key>> { private Key[] pq; private int n; public axq(int capacity) { pq = (Key[]) new Comparable[capacity+]; } fixed capacity (for simplicity) public boolean ismpty() { return n == 0; } public void insert(key key) // see previous code public Key delax() // see previous code Q ops private void swim(int k) private void sink(int k) // see previous code // see previous code heap helper functions } private boolean less(int i, int j) { return pq[i].compareo(pq[j]) < 0; } private void exch(int i, int j) { Key t = pq[i]; pq[i] = pq[j]; pq[j] = t; } array helper functions

22 riority queue: implementations cost summary implementation IN D- unordered array n n ordered array n binary heap log n log n order of growth of running time for priority queue with n items

23 Binary heap: considerations Underflow and overflow. Underflow: throw exception if deleting from empty Q. verflow: add no-arg constructor and use resizing array. inimum-oriented priority queue. eplace less() with greater(). Implement greater(). leads to log n amortized time per op (how to make worst case?) ther operations. emove an arbitrary item. Change the priority of an item. can implement efficiently with sink() and swim() [ stay tuned for rim/dijkstra ] Immutability of keys. ssumption: client does not change keys while they re on the Q. Best practice: use immutable keys.

24 Immutability: implementing in Java Data type. et of values and operations on those values. Immutable data type. Can t change the data type value once created. public final class Vector { private final int n; private final double[] data; } public Vector(double[] data) { this.n = data.length; this.data = new double[n]; for (int i = 0; i < n; i++) this.data[i] = data[i]; } instance methods don t change instance variables Immutable in Java. tring, Integer, Double, Color, File, utable in Java. tringbuilder, tack, U, arrays, instance variables private and final (neither necessary nor sufficient, but good programming practice) defensive copy of mutable instance variables 4

25 Immutability: properties Data type. et of values and operations on those values. Immutable data type. Can t change the data type value once created. dvantages. implifies debugging. implifies concurrent programming. ore secure in presence of hostile code. afe to use as key in priority queue or symbol table. Disadvantage. ust create new object for each data-type value. 5

26 6 Binary heap: practical improvement ultiway heaps. Complete d-way tree. arent s key no smaller than its children s keys. Fact. Height of complete d-way tree on n nodes is ~ log d n. -way heap Y Z K I G D B J F H V C W Q N

27 riority queues: quiz In the worst case, how many compares to IN and D- in a d-way heap?. ~ log d n and ~ log d n B. ~ log d n and ~ d log d n C. ~ d log d n and ~ log d n D. ~ d log d n and ~ d log d n 7

28 riority queue: implementation cost summary implementation IN D- unordered array n n ordered array n binary heap log n log n d-ary heap log d n d log d n sweet spot: d = 4 Fibonacci log n Brodal queue log n impossible why impossible? amortized order-of-growth of running time for priority queue with n items 8

29 Impossibility of priority queue with constant-time IN & D- xercise. ssume there is a priority queue which makes a constant number of compares in the worst case for both IN and D-. Design a sorting algorithm that uses this priority queue. How many compares does it perform in the worst case? 9

30 .4 IIY QUU lgorithms I and elementary implementations binary heaps heapsort event-driven simulation B DGWICK KVIN WYN

31 riority queues: quiz 4 What are the properties of this sorting algorithm? public void sort(tring[] a) { int n = a.length; axq<tring> pq = new axq<tring>(); for (int i = 0; i < n; i++) pq.insert(a[i]); for (int i = n-; i >= 0; i--) a[i] = pq.delax(); }. n log n compares in the worst case. B. In-place. C. table. D. ll of the above.

32 Heapsort Basic plan for in-place sort. View input array as a complete binary tree. Heap construction: build a max-heap with all n keys. ortdown: repeatedly remove the maximum key. keys in arbitrary order build max heap (in place) sorted result (in place)

33 Heapsort demo Heap construction. Build max heap using bottom-up method. for now, assume array entries are indexed to n array in arbitrary order

34 Heapsort demo ortdown. epeatedly delete the largest remaining item. array in sorted order

35 5 Heapsort: heap construction First pass. Build heap using bottom-up method. for (int k = n/; k >= ; k--) sink(a, k, n); starting point (arbitrary order) sink(, ) sink(5, ) heap construction starting point (heap-ordered) starting point (arbitrary order) sink(5, ) sink(4, ) exch(, 6) sink(, 5) exch(, 5) sink(, 4) exch(, 4) sink(, ) sortdown exch(, ) sink(, 0) exch(, 0) sink(, 9) heap construction starting point (heap-ordered) starting point (arbitrary order) sink(5, ) sink(4, ) sink(, ) sink(, ) sink(, ) exch(, 5) sink(, 4) exch(, 4) sink(, ) exch(, ) sink(, ) exch(, ) sink(, ) exch(, ) sink(, 0) exch(, 0) sink(, 9) exch(, 9) sink(, 8) exch(, 8) sink(, 7) exch(, 7) sink(, 6) Heapsort: constructing (left) and sorting down (right) a heap result (heap-ordered) resul sink(5, ) sink(4, ) sink(, ) sink(, ) sink(, ) exch(, 5) sink(, 4) exch(, 4) sink(, ) exch(, ) sink(, ) exch(, ) sink(, ) exch(, ) sink(, 0) exch(, 0) sink(, 9) exch(, 9) sink(, 8) exch(, 8) sink(, 7) exch(, 7) sink(, 6) Heapsort: constructing (left) and sorting down (right) a heap result (heap-ordered) resul Key insight. fter sink(a, k, n) completes, the subtree rooted at k is a heap.

36 6 Heapsort: sortdown econd pass. emove the maximum, one at a time. eave in array, instead of nulling out. while (n > ) { exch(a,, n--); sink(a,, n); } starting point (heap-ordered) exch(, ) sink(, 0) exch(, 0) sink(, 9) Heapsort: constructing (left) and sorting down (right) a heap heap construction result (heap-ordered) result (sorted) starting point (heap-ordered) starting point (arbitrary order) sink(5, ) sink(4, ) sink(, ) sink(, ) sink(, ) exch(, 6) sink(, 5) exch(, 5) sink(, 4) exch(, 4) sink(, ) exch(, ) sink(, ) exch(, ) sink(, ) sortdown exch(, ) sink(, 0) exch(, 0) sink(, 9) exch(, 9) sink(, 8) exch(, 8) sink(, 7) exch(, 7) sink(, 6) Heapsort: constructing (left) and sorting down (right) a heap heap construction result (heap-ordered) result (sorted) starting point (heap-ordered) starting point (arbitrary order) sink(5, ) sink(4, ) sink(, ) sink(, ) sink(, ) exch(, 6) sink(, 5) exch(, 5) sink(, 4) exch(, 4) sink(, ) exch(, ) sink(, ) exch(, ) sink(, ) sortdown exch(, ) sink(, 0) exch(, 0) sink(, 9) exch(, 9) sink(, 8) exch(, 8) sink(, 7) exch(, 7) sink(, 6) Heapsort: constructing (left) and sorting down (right) a heap heap construction result (heap-ordered) result (sorted) starting point (heap-ordered) starting point (arbitrary order) sink(5, ) sink(4, ) sink(, ) sink(, ) sink(, ) exch(, 6) sink(, 5) exch(, 5) sink(, 4) exch(, 4) sink(, ) exch(, ) sink(, ) exch(, ) sink(, ) sortdown exch(, ) sink(, 0) exch(, 0) sink(, 9) exch(, 9) sink(, 8) exch(, 8) sink(, 7) exch(, 7) sink(, 6) Heapsort: constructing (left) and sorting down (right) a heap heap construction result (heap-ordered) result (sorted) starting point (heap-ordered) starting point (arbitrary order) sink(5, ) sink(4, ) sink(, ) sink(, ) sink(, ) exch(, 6) sink(, 5) exch(, 5) sink(, 4) exch(, 4) sink(, ) exch(, ) sink(, ) exch(, ) sink(, ) sortdown exch(, ) sink(, 0) exch(, 0) sink(, 9) exch(, 9) sink(, 8) exch(, 8) sink(, 7) exch(, 7) sink(, 6) Heapsort: constructing (left) and sorting down (right) a heap heap construction result (heap-ordered) result (sorted) starting point (heap-ordered) starting point (arbitrary order) sink(5, ) sink(4, ) sink(, ) sink(, ) sink(, ) exch(, 6) sink(, 5) exch(, 5) sink(, 4) exch(, 4) sink(, ) exch(, ) sink(, ) exch(, ) sink(, ) sortdown exch(, ) sink(, 0) exch(, 0) sink(, 9) exch(, 9) sink(, 8) exch(, 8) sink(, 7) exch(, 7) sink(, 6) Heapsort: constructing (left) and sorting down (right) a heap heap construction result (heap-ordered) result (sorted) starting point (heap-ordered) starting point (arbitrary order) sink(5, ) sink(4, ) sink(, ) sink(, ) sink(, ) exch(, 6) sink(, 5) exch(, 5) sink(, 4) exch(, 4) sink(, ) exch(, ) sink(, ) exch(, ) sink(, ) sortdown exch(, ) sink(, 0) exch(, 0) sink(, 9) exch(, 9) sink(, 8) exch(, 8) sink(, 7) exch(, 7) sink(, 6) Heapsort: constructing (left) and sorting down (right) a heap heap construction result (heap-ordered) result (sorted) starting point (heap-ordered) starting point (arbitrary order) sink(5, ) sink(4, ) sink(, ) sink(, ) sink(, ) exch(, 6) sink(, 5) exch(, 5) sink(, 4) exch(, 4) sink(, ) exch(, ) sink(, ) exch(, ) sink(, ) sortdown exch(, ) sink(, 0) exch(, 0) sink(, 9) exch(, 9) sink(, 8) exch(, 8) sink(, 7) exch(, 7) sink(, 6) Heapsort: constructing (left) and sorting down (right) a heap heap construction result (heap-ordered) result (sorted) starting point (heap-ordered) starting point (arbitrary order) sink(5, ) sink(4, ) sink(, ) sink(, ) sink(, ) exch(, 6) sink(, 5) exch(, 5) sink(, 4) exch(, 4) sink(, ) exch(, ) sink(, ) exch(, ) sink(, ) sortdown exch(, ) sink(, 0) exch(, 0) sink(, 9) exch(, 9) sink(, 8) exch(, 8) sink(, 7) exch(, 7) sink(, 6) Heapsort: constructing (left) and sorting down (right) a heap heap construction result (heap-ordered) result (sorted) starting point (heap-ordered) starting point (arbitrary order) sink(5, ) sink(4, ) sink(, ) sink(, ) sink(, ) exch(, 6) sink(, 5) exch(, 5) sink(, 4) exch(, 4) sink(, ) exch(, ) sink(, ) exch(, ) sink(, ) sortdown exch(, ) sink(, 0) exch(, 0) sink(, 9) exch(, 9) sink(, 8) exch(, 8) sink(, 7) exch(, 7) sink(, 6) Heapsort: constructing (left) and sorting down (right) a heap heap construction result (heap-ordered) result (sorted) starting point (heap-ordered) starting point (arbitrary order) Key insight. fter each iteration, the array consists of a heap-ordered subarray followed by a sub-array in final order

37 Heapsort: Java implementation public class Heap { public static void sort(comparable[] a) { int n = a.length; for (int k = n/; k >= ; k--) sink(a, k, n); while (n > ) { exch(a,, n); sink(a,, --n); } } private static void sink(comparable[] a, int k, int n) { /* as before */ } but make static (and pass arguments) } private static boolean less(comparable[] a, int i, int j) { /* as before */ } private static void exch(bject[] a, int i, int j) { /* as before */ } but convert from -based indexing to 0-base indexing 7

38 Heapsort: trace a[i] N k initial values 5 4 heap-ordered sorted result Heapsort trace (array contents just after each sink) 8

39 Heapsort: mathematical analysis roposition. Heap construction makes n exchanges and n compares. f sketch. [assume n = h+ ] max number of exchanges to sink node binary heap of height h = a tricky sum (see C 40) h + (h ) + 4(h ) + 8(h ) h (0) = h+ h = n (h ) n 9

40 Heapsort: mathematical analysis roposition. Heap construction makes n exchanges and n compares. roposition. Heapsort uses n lg n compares and exchanges. algorithm can be improved to ~ n lg n (but no such variant is known to be practical) ignificance. In-place sorting algorithm with n log n worst-case. ergesort: no, linear extra space. Quicksort: no, quadratic time in worst case. Heapsort: yes! in-place merge possible, not practical n log n worst-case quicksort possible, not practical Bottom line. Heapsort is optimal for both time and space, but: Inner loop longer than quicksort s. akes poor use of cache. Not stable. can be improved using advanced caching tricks 40

41 orting algorithms: summary inplace? stable? best average worst remarks selection ½ n ½ n ½ n n exchanges insertion n ¼ n ½ n use for small n or partially ordered merge ½ n lg n n lg n n lg n n log n guarantee; stable quick n lg n n ln n ½ n n log n probabilistic guarantee; fastest in practice -way quick n n ln n ½ n improves quicksort when duplicate keys heap n n lg n n lg n n log n guarantee; in-place? n n lg n n lg n holy sorting grail 4

Algorithms. Algorithms 2.4 PRIORITY QUEUES. API and elementary implementations binary heaps heapsort event-driven simulation

Algorithms. Algorithms 2.4 PRIORITY QUEUES. API and elementary implementations binary heaps heapsort event-driven simulation lgorithms B DGWICK KVIN WYN.4 IIY QUU lgorithms F U H D I I N I and elementary implementations binary heaps heapsort event-driven simulation see videos B DGWICK KVIN WYN http://algs4.cs.princeton.edu ast

More information

Algorithms. Algorithms 2.4 PRIORITY QUEUES. API and elementary implementations binary heaps heapsort event-driven simulation (see videos)

Algorithms. Algorithms 2.4 PRIORITY QUEUES. API and elementary implementations binary heaps heapsort event-driven simulation (see videos) lgorithms B DGWICK KVIN WYN.4 IIY QUU lgorithms F U H D I I N I and elementary implementations binary heaps heapsort event-driven simulation (see videos) B DGWICK KVIN WYN https://algs4.cs.princeton.edu

More information

PRIORITY QUEUES AND HEAPSORT

PRIORITY QUEUES AND HEAPSORT cknowledgement: he course slides are adapted from the slides prepared by. edgewick and K. Wayne of rinceton University. BB 202 - GIH D. F CU NGINING IIY QUU ND H Heapsort I lementary implementations Binary

More information

PRIORITY QUEUES AND HEAPSORT

PRIORITY QUEUES AND HEAPSORT BB 0 - D. F CU lementary implementations Binary heaps DY Y QUU D cknowledgement: he course slides are adapted from the slides prepared by. edgewick and K. Wayne of rinceton University. riority queue Collections.

More information

PRIORITY QUEUES AND HEAPSORT

PRIORITY QUEUES AND HEAPSORT BB 0 - D. F CU lementary implementations Binary heaps DY Y QUU D ar., 07 cknowledgement: he course slides are adapted from the slides prepared by. edgewick and K. Wayne of rinceton University. riority

More information

Algorithms. Algorithms 2.4 PRIORITY QUEUES. API and elementary implementations binary heaps heapsort event-driven simulation

Algorithms. Algorithms 2.4 PRIORITY QUEUES. API and elementary implementations binary heaps heapsort event-driven simulation lgorithms B SDWICK KVI WY 2.4 IIY QUUS lgorithms F U H D I I I and elementary implementations binary heaps heapsort event-driven simulation B SDWICK KVI WY http://algs4.cs.princeton.edu 2.4 IIY QUUS lgorithms

More information

Algorithms. Algorithms 2.4 PRIORITY QUEUES. API and elementary implementations binary heaps heapsort event-driven simulation See video/book/booksite

Algorithms. Algorithms 2.4 PRIORITY QUEUES. API and elementary implementations binary heaps heapsort event-driven simulation See video/book/booksite lgorithms B DGWICK KVIN WYN 2.4 IIY QUU lgorithms F U H D I I N B DGWICK KVIN WYN I and elementary implementations binary heaps heapsort event-driven simulation ee video/book/booksite http://algs4.cs.princeton.edu

More information

cs2010: algorithms and data structures

cs2010: algorithms and data structures cs2010: algorithms and data structures Lecture 9: Priority Queues Vasileios Koutavas School of Computer Science and Statistics Trinity College Dublin Algorithms ROBERT SEDGEWICK KEVIN WAYNE 2.4 PRIORITY

More information

Algorithms. Algorithms 2.4 PRIORITY QUEUES. API and elementary implementations binary heaps heapsort event-driven simulation

Algorithms. Algorithms 2.4 PRIORITY QUEUES. API and elementary implementations binary heaps heapsort event-driven simulation lgorithms B DGWICK KVIN WYN 2.4 IIY QUU lgorithms F U H D I I N I and elementary implementations binary heaps heapsort event-driven simulation B DGWICK KVIN WYN http://algs4.cs.princeton.edu 2.4 IIY QUU

More information

Algorithms. Algorithms. Algorithms 2.4 P RIORITY Q UEUES. API and elementary implementations. API and elementary implementations binary heaps

Algorithms. Algorithms. Algorithms 2.4 P RIORITY Q UEUES. API and elementary implementations. API and elementary implementations binary heaps lgorithms lgorithms F U D B DWCK K V W Y.4 Y Q UU.4 Y Q UU and elementary implementations and elementary implementations binary heaps binary heaps heapsort lgorithms event-driven simulation heapsort event-driven

More information

Algorithms. Algorithms. Algorithms 2.4 P RIORITY Q UEUES. API and elementary implementations. API and elementary implementations binary heaps

Algorithms. Algorithms. Algorithms 2.4 P RIORITY Q UEUES. API and elementary implementations. API and elementary implementations binary heaps lgorithms lgorithms F U D B DWCK K V W Y.4 Y Q UU.4 Y Q UU and elementary implementations and elementary implementations binary heaps binary heaps heapsort lgorithms event-driven simulation heapsort event-driven

More information

Priority Queues 1 / 15

Priority Queues 1 / 15 Priority Queues 1 / 15 Outline 1 API 2 Elementary Implementations 3 Heap Definitions 4 Algorithms on Heaps 5 Heap Sort 6 Heap Sort and Other Sorts 2 / 15 API Many applications require that we process items

More information

COS 226 Algorithms and Data Structures Fall Midterm Solutions

COS 226 Algorithms and Data Structures Fall Midterm Solutions 1 COS 226 Algorithms and Data Structures Fall 2010 Midterm Solutions 1. Analysis of algorithms. (a) For each expression in the left column, give the best matching description from the right column. B.

More information

PRIORITY QUEUES AND HEAPS

PRIORITY QUEUES AND HEAPS 10//1 Reminder: A Collision Detection Due tonight by midnight PRIORITY QUEUES AND HEAPS Lecture 1 CS10 Fall 01 3 Readings and Homework Read Chapter A Heap Implementation to learn about heaps Exercise:

More information

DO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N.

DO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. CS61B Fall 2011 UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division Test #2 Solutions P. N. Hilfinger 1. [3 points] Consider insertion sort, merge

More information

COS 226 Algorithms and Data Structures Fall Midterm

COS 226 Algorithms and Data Structures Fall Midterm COS 226 Algorithms and Data Structures Fall 2017 Midterm This exam has 10 questions (including question 0) worth a total of 55 points. You have 0 minutes. This exam is preprocessed by a computer, so please

More information

COS 226 Midterm Exam, Spring 2009

COS 226 Midterm Exam, Spring 2009 NAME: login ID: precept: COS 226 Midterm Exam, Spring 2009 This test is 10 questions, weighted as indicated. The exam is closed book, except that you are allowed to use a one page cheatsheet. No calculators

More information

input sort left half sort right half merge results Mergesort overview

input sort left half sort right half merge results Mergesort overview Algorithms OBT S DGWICK K VIN W AYN Two classic sorting algorithms: mergesort and quicksort Critical components in the world s computational infrastructure. Full scientific understanding of their properties

More information

Sorting and Searching

Sorting and Searching Sorting and Searching Lecture 2: Priority Queues, Heaps, and Heapsort Lecture 2: Priority Queues, Heaps, and Heapsort Sorting and Searching 1 / 24 Priority Queue: Motivating Example 3 jobs have been submitted

More information

PRIORITY QUEUES AND HEAPS

PRIORITY QUEUES AND HEAPS 10//1 Readings and Homework Read Chapter A Heap Implementation to learn about heaps PRIORITY QUEUES AND HEAPS Lecture 17 CS10 Fall 01 Exercise: Salespeople often make matrices that show all the great features

More information

Sorting and Searching

Sorting and Searching Sorting and Searching Lecture 2: Priority Queues, Heaps, and Heapsort Lecture 2: Priority Queues, Heaps, and Heapsort Sorting and Searching 1 / 24 Priority Queue: Motivating Example 3 jobs have been submitted

More information

Algorithms and Theory of Computation. Lecture 7: Priority Queue

Algorithms and Theory of Computation. Lecture 7: Priority Queue Algorithms and Theory of Computation Lecture 7: Priority Queue Xiaohui Bei MAS 714 September 5, 2017 Nanyang Technological University MAS 714 September 5, 2017 1 / 15 Priority Queues Priority Queues Store

More information

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority. 3. Priority Queues 3. Priority Queues ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority. Malek Mouhoub, CS340 Winter 2007 1 3. Priority Queues

More information

The smallest element is the first one removed. (You could also define a largest-first-out priority queue)

The smallest element is the first one removed. (You could also define a largest-first-out priority queue) 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 is the first one removed (You could also define a largest-first-out

More information

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17 01.433/33 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/2/1.1 Introduction In this lecture we ll talk about a useful abstraction, priority queues, which are

More information

COS 226 Algorithms and Data Structures Fall Midterm

COS 226 Algorithms and Data Structures Fall Midterm COS 226 Algorithms and Data Structures Fall 2014 Midterm This test has 9 questions worth a total of 55 points. You have 80 minutes. The exam is closed book, except that you are allowed to use a one page

More information

COS 226 Algorithms and Data Structures Spring Midterm

COS 226 Algorithms and Data Structures Spring Midterm COS 226 Algorithms and Data Structures Spring 2015 Midterm This test has 9 questions worth a total of 55 points. You have 0 minutes. The exam is closed book, except that you are allowed to use a one page

More information

CS 2223 B15 Term. Homework 4

CS 2223 B15 Term. Homework 4 CS 2223 B15 Term. Homework 4 Homework Instructions This homework is to be completed individually. If you have any questions as to what constitutes improper behavior, review the examples as I have posted

More information

Algorithms and Data Structures

Algorithms and Data Structures Algorithms and Data Structures Dr. Malek Mouhoub Department of Computer Science University of Regina Fall 2002 Malek Mouhoub, CS3620 Fall 2002 1 6. Priority Queues 6. Priority Queues ffl ADT Stack : LIFO.

More information

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Advanced Java Concepts Unit 5: Trees. Notes and Exercises dvanced Java Concepts Unit 5: Trees. Notes and Exercises Tree is a data structure like the figure shown below. We don t usually care about unordered trees but that s where we ll start. Later we will focus

More information

HEAPS & PRIORITY QUEUES

HEAPS & PRIORITY QUEUES HEAPS & PRIORITY QUEUES Lecture 13 CS2110 Spring 2018 Announcements 2 A4 goes out today! Prelim 1: regrades are open a few rubrics have changed No Recitations next week (Fall Break Mon & Tue) We ll spend

More information

Lecture 5: Sorting Part A

Lecture 5: Sorting Part A Lecture 5: Sorting Part A Heapsort Running time O(n lg n), like merge sort Sorts in place (as insertion sort), only constant number of array elements are stored outside the input array at any time Combines

More information

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof. Priority Queues 1 Introduction Many applications require a special type of queuing in which items are pushed onto the queue by order of arrival, but removed from the queue based on some other priority

More information

Algorithms and Data Structures

Algorithms and Data Structures Algorithms and Data Structures Priority Queues Ulf Leser Special Scenarios for Searching Up to now, we assumed that all elements of a list are equally important and that any of them could be searched next

More information

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A; How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A; } if (n

More information

The beautiful binary heap.

The beautiful binary heap. The beautiful binary heap. Weiss has a chapter on the binary heap - chapter 20, pp581-601. It s a very neat implementation of the binary tree idea, using an array. We can use the binary heap to sort an

More information

Algorithms. Algorithms 3.2 BINARY SEARCH TREES. BSTs ordered operations iteration deletion (see book or videos) ROBERT SEDGEWICK KEVIN WAYNE

Algorithms. Algorithms 3.2 BINARY SEARCH TREES. BSTs ordered operations iteration deletion (see book or videos) ROBERT SEDGEWICK KEVIN WAYNE lgorithms OBT DGWIK KVIN WYN 3.2 BINY T lgorithms F O U T D I T I O N BTs ordered operations iteration deletion (see book or videos) OBT DGWIK KVIN WYN https://algs4.cs.princeton.edu Last updated on 10/9/18

More information

Sorting Applications 1 / 9

Sorting Applications 1 / 9 Sorting Applications 1 / 9 Outline 1 Sorting Various Types of Data 2 Which Sorting Algorithm Should I Use? 3 Reductions 4 A Brief Survey of Sorting Applications 2 / 9 Sorting Various Types of Data Our

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms Spring 2017-2018 Outline 1 Priority Queues Outline Priority Queues 1 Priority Queues Jumping the Queue Priority Queues In normal queue, the mode of selection is first in,

More information

Heapsort. Heap data structure

Heapsort. Heap data structure Heapsort Heap data structure. Heap A (not garbage-collected storage) is a nearly complete binary tree.. Height of node = # of edges on a longest simple path from the node down to a leaf.. Height of heap

More information

Summer Final Exam Review Session August 5, 2009

Summer Final Exam Review Session August 5, 2009 15-111 Summer 2 2009 Final Exam Review Session August 5, 2009 Exam Notes The exam is from 10:30 to 1:30 PM in Wean Hall 5419A. The exam will be primarily conceptual. The major emphasis is on understanding

More information

CS 310 Advanced Data Structures and Algorithms

CS 310 Advanced Data Structures and Algorithms CS 310 Advanced Data Structures and Algorithms Sorting June 13, 2017 Tong Wang UMass Boston CS 310 June 13, 2017 1 / 42 Sorting One of the most fundamental problems in CS Input: a series of elements with

More information

Computer Science 210 Data Structures Siena College Fall Topic Notes: Priority Queues and Heaps

Computer Science 210 Data Structures Siena College Fall Topic Notes: Priority Queues and Heaps Computer Science 0 Data Structures Siena College Fall 08 Topic Notes: Priority Queues and Heaps Heaps and Priority Queues From here, we will look at some ways that trees are used in other structures. First,

More information

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers? Review for Final (Chapter 6 13, 15) 6. Template functions & classes 1) What is the primary purpose of template functions? A. To allow a single function to be used with varying types of arguments B. To

More information

Priority Queues Heaps Heapsort

Priority Queues Heaps Heapsort Priority Queues Heaps Heapsort After this lesson, you should be able to apply the binary heap insertion and deletion algorithms by hand implement the binary heap insertion and deletion algorithms explain

More information

SORTING. Comparison of Quadratic Sorts

SORTING. Comparison of Quadratic Sorts SORTING Chapter 8 Comparison of Quadratic Sorts 2 1 Merge Sort Section 8.7 Merge A merge is a common data processing operation performed on two ordered sequences of data. The result is a third ordered

More information

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs Computational Optimization ISE 407 Lecture 16 Dr. Ted Ralphs ISE 407 Lecture 16 1 References for Today s Lecture Required reading Sections 6.5-6.7 References CLRS Chapter 22 R. Sedgewick, Algorithms in

More information

PRIORITY QUEUES AND HEAPS

PRIORITY QUEUES AND HEAPS PRIORITY QUEUES AND HEAPS Lecture 17 CS2110 Spring 201 Readings and Homework 2 Read Chapter 2 A Heap Implementation to learn about heaps Exercise: Salespeople often make matrices that show all the great

More information

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division CS61B Fall 2011 P. N. Hilfinger Test #2 (with corrections) READ THIS PAGE FIRST. Please do

More information

CH 8. HEAPS AND PRIORITY QUEUES

CH 8. HEAPS AND PRIORITY QUEUES CH 8. HEAPS AND PRIORITY QUEUES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM NANCY

More information

CH. 8 PRIORITY QUEUES AND HEAPS

CH. 8 PRIORITY QUEUES AND HEAPS CH. 8 PRIORITY QUEUES AND HEAPS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM NANCY

More information

DO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N.

DO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. CS61B Fall 2013 UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division Test #2 Solutions DO NOT P. N. Hilfinger REPRODUCE 1 Test #2 Solution 2 Problems

More information

Trees 1: introduction to Binary Trees & Heaps. trees 1

Trees 1: introduction to Binary Trees & Heaps. trees 1 Trees 1: introduction to Binary Trees & Heaps trees 1 Basic terminology Finite set of nodes (may be empty -- 0 nodes), which contain data First node in tree is called the root trees 2 Basic terminology

More information

Properties of a heap (represented by an array A)

Properties of a heap (represented by an array A) Chapter 6. HeapSort Sorting Problem Input: A sequence of n numbers < a1, a2,..., an > Output: A permutation (reordering) of the input sequence such that ' ' ' < a a a > 1 2... n HeapSort O(n lg n) worst

More information

Comparisons. Heaps. Heaps. Heaps. Sorting Revisited. Heaps. So far we talked about two algorithms to sort an array of numbers

Comparisons. Heaps. Heaps. Heaps. Sorting Revisited. Heaps. So far we talked about two algorithms to sort an array of numbers So far we have studied: Comparisons Tree is completely filled on all levels except possibly the lowest, which is filled from the left up to a point Insertion Sort Merge Sort Worst case Θ(n ) Θ(nlgn) Best

More information

CS102 Binary Search Trees

CS102 Binary Search Trees CS102 Binary Search Trees Prof Tejada 1 To speed up insertion, removal and search, modify the idea of a Binary Tree to create a Binary Search Tree (BST) Binary Search Trees Binary Search Trees have one

More information

Lecture 13: AVL Trees and Binary Heaps

Lecture 13: AVL Trees and Binary Heaps Data Structures Brett Bernstein Lecture 13: AVL Trees and Binary Heaps Review Exercises 1. ( ) Interview question: Given an array show how to shue it randomly so that any possible reordering is equally

More information

CE 221 Data Structures and Algorithms

CE 221 Data Structures and Algorithms CE 2 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, 6.1 6.3 Izmir University of Economics 1 A kind of queue Priority Queue (Heap) Dequeue gets element with the

More information

Abstract vs concrete data structures HEAPS AND PRIORITY QUEUES. Abstract vs concrete data structures. Concrete Data Types. Concrete data structures

Abstract vs concrete data structures HEAPS AND PRIORITY QUEUES. Abstract vs concrete data structures. Concrete Data Types. Concrete data structures 10/1/17 Abstract vs concrete data structures 2 Abstract data structures are interfaces they specify only interface (method names and specs) not implementation (method bodies, fields, ) HEAPS AND PRIORITY

More information

Tables, Priority Queues, Heaps

Tables, Priority Queues, Heaps Tables, Priority Queues, Heaps Table ADT purpose, implementations Priority Queue ADT variation on Table ADT Heaps purpose, implementation heapsort EECS 268 Programming II 1 Table ADT A table in generic

More information

COMP Data Structures

COMP Data Structures COMP 2140 - Data Structures Shahin Kamali Topic 5 - Sorting University of Manitoba Based on notes by S. Durocher. COMP 2140 - Data Structures 1 / 55 Overview Review: Insertion Sort Merge Sort Quicksort

More information

Binary heaps (chapters ) Leftist heaps

Binary heaps (chapters ) Leftist heaps Binary heaps (chapters 20.3 20.5) Leftist heaps Binary heaps are arrays! A binary heap is really implemented using an array! 8 18 29 20 28 39 66 Possible because of completeness property 37 26 76 32 74

More information

CS 61B Midterm 2 Guerrilla Section Spring 2018 March 17, 2018

CS 61B Midterm 2 Guerrilla Section Spring 2018 March 17, 2018 CS 61B Midterm 2 Guerrilla Section Spring 2018 March 17, 2018 Instructions Form a small group. Start on the first problem. Check off with a helper or discuss your solution process with another group once

More information

University of Waterloo CS240 Winter 2018 Assignment 2. Due Date: Wednesday, Jan. 31st (Part 1) resp. Feb. 7th (Part 2), at 5pm

University of Waterloo CS240 Winter 2018 Assignment 2. Due Date: Wednesday, Jan. 31st (Part 1) resp. Feb. 7th (Part 2), at 5pm University of Waterloo CS240 Winter 2018 Assignment 2 version: 2018-02-04 15:38 Due Date: Wednesday, Jan. 31st (Part 1) resp. Feb. 7th (Part 2), at 5pm Please read the guidelines on submissions: http://www.student.cs.uwaterloo.ca/~cs240/

More information

CS 445: Data Structures Final Examination: Study Guide

CS 445: Data Structures Final Examination: Study Guide CS 445: Data Structures Final Examination: Study Guide Java prerequisites Classes, objects, and references Access modifiers Arguments and parameters Garbage collection Self-test questions: Appendix C Designing

More information

3.2 BINARY SEARCH TREES. BSTs ordered operations iteration deletion. Algorithms ROBERT SEDGEWICK KEVIN WAYNE.

3.2 BINARY SEARCH TREES. BSTs ordered operations iteration deletion. Algorithms ROBERT SEDGEWICK KEVIN WAYNE. 3.2 BINY T lgorithms BTs ordered operations iteration deletion OBT DGWIK KVIN WYN http://algs4.cs.princeton.edu Binary search trees Definition. BT is a binary tree in symmetric order. binary tree is either:

More information

void insert( Type const & ) void push_front( Type const & )

void insert( Type const & ) void push_front( Type const & ) 6.1 Binary Search Trees A binary search tree is a data structure that can be used for storing sorted data. We will begin by discussing an Abstract Sorted List or Sorted List ADT and then proceed to describe

More information

Priority Queues (Heaps)

Priority Queues (Heaps) Priority Queues (Heaps) 1 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted order. Often we collect a set of items and process the

More information

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages Priority Queues and Binary Heaps See Chapter 21 of the text, pages 807-839. A priority queue is a queue-like data structure that assumes data is comparable in some way (or at least has some field on which

More information

CSE 214 Computer Science II Heaps and Priority Queues

CSE 214 Computer Science II Heaps and Priority Queues CSE 214 Computer Science II Heaps and Priority Queues Spring 2018 Stony Brook University Instructor: Shebuti Rayana shebuti.rayana@stonybrook.edu http://www3.cs.stonybrook.edu/~cse214/sec02/ Introduction

More information

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES 2 5 6 9 7 Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H., Wiley, 2014

More information

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages Priority Queues and Binary Heaps See Chapter 21 of the text, pages 807-839. A priority queue is a queue-like data structure that assumes data is comparable in some way (or at least has some field on which

More information

Comparisons. Θ(n 2 ) Θ(n) Sorting Revisited. So far we talked about two algorithms to sort an array of numbers. What is the advantage of merge sort?

Comparisons. Θ(n 2 ) Θ(n) Sorting Revisited. So far we talked about two algorithms to sort an array of numbers. What is the advantage of merge sort? So far we have studied: Comparisons Insertion Sort Merge Sort Worst case Θ(n 2 ) Θ(nlgn) Best case Θ(n) Θ(nlgn) Sorting Revisited So far we talked about two algorithms to sort an array of numbers What

More information

Announcements HEAPS & PRIORITY QUEUES. Abstract vs concrete data structures. Concrete data structures. Abstract data structures

Announcements HEAPS & PRIORITY QUEUES. Abstract vs concrete data structures. Concrete data structures. Abstract data structures Announcements A due TOMORROW. Late deadline is Sunday. A5 released. Due next Thursday. Deadline for Prelim 1 regrade requests is tomorrow. Remember to complete your TA evaluations by tonight. HEAPS & PRIORITY

More information

CS2012 Programming Techniques II

CS2012 Programming Techniques II 14/02/2014 C2012 Programming Techniques II Vasileios Koutavas Lecture 14 1 BTs ordered operations deletion 27 T implementations: summary implementation guarantee average case search insert delete search

More information

Adding a Node to (Min) Heap. Lecture16: Heap Sort. Priority Queue Sort. Delete a Node from (Min) Heap. Step 1: Add node at the end

Adding a Node to (Min) Heap. Lecture16: Heap Sort. Priority Queue Sort. Delete a Node from (Min) Heap. Step 1: Add node at the end Adding a Node to (Min) Heap (F) Lecture16: Heap Sort Takes log steps if nodes are added Step 1: Add node at the end Bohyung Han CSE, POSTECH bhhan@postech.ac.kr Step 2: Make swap if parent is bigger Step

More information

CSE 2123: Collections: Priority Queues. Jeremy Morris

CSE 2123: Collections: Priority Queues. Jeremy Morris CSE 2123: Collections: Priority Queues Jeremy Morris 1 Collections Priority Queue Recall: A queue is a specific type of collection Keeps elements in a particular order We ve seen two examples FIFO queues

More information

) $ f ( n) " %( g( n)

) $ f ( n)  %( g( n) CSE 0 Name Test Spring 008 Last Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. The time to compute the sum of the n elements of an integer array is: # A.

More information

& ( D. " mnp ' ( ) n 3. n 2. ( ) C. " n

& ( D.  mnp ' ( ) n 3. n 2. ( ) C.  n CSE Name Test Summer Last Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. The time to multiply two n " n matrices is: A. " n C. "% n B. " max( m,n, p). The

More information

9. Heap : Priority Queue

9. Heap : Priority Queue 9. Heap : Priority Queue Where We Are? Array Linked list Stack Queue Tree Binary Tree Heap Binary Search Tree Priority Queue Queue Queue operation is based on the order of arrivals of elements FIFO(First-In

More information

Stores a collection of elements each with an associated key value

Stores a collection of elements each with an associated key value CH9. PRIORITY QUEUES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER (WILEY 201) PRIORITY QUEUES Stores a collection

More information

Heaps and Priority Queues

Heaps and Priority Queues Unit 9, Part Heaps and Priority Queues Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Priority Queue A priority queue (PQ) is a collection in which each item has an associated number

More information

CS711008Z Algorithm Design and Analysis

CS711008Z Algorithm Design and Analysis CS711008Z Algorithm Design and Analysis Lecture 7. Binary heap, binomial heap, and Fibonacci heap Dongbo Bu Institute of Computing Technology Chinese Academy of Sciences, Beijing, China 1 / 108 Outline

More information

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators) Name: Email address: Quiz Section: CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators) Instructions: Read the directions for each question carefully before answering. We will

More information

CSE 100 Advanced Data Structures

CSE 100 Advanced Data Structures CSE 100 Advanced Data Structures Overview of course requirements Outline of CSE 100 topics Review of trees Helpful hints for team programming Information about computer accounts Page 1 of 25 CSE 100 web

More information

Sorting. Popular algorithms: Many algorithms for sorting in parallel also exist.

Sorting. Popular algorithms: Many algorithms for sorting in parallel also exist. Sorting Popular algorithms: Selection sort* Insertion sort* Bubble sort* Quick sort* Comb-sort Shell-sort Heap sort* Merge sort* Counting-sort Radix-sort Bucket-sort Tim-sort Many algorithms for sorting

More information

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology Introduction Chapter 4 Trees for large input, even linear access time may be prohibitive we need data structures that exhibit average running times closer to O(log N) binary search tree 2 Terminology recursive

More information

( ) + n. ( ) = n "1) + n. ( ) = T n 2. ( ) = 2T n 2. ( ) = T( n 2 ) +1

( ) + n. ( ) = n 1) + n. ( ) = T n 2. ( ) = 2T n 2. ( ) = T( n 2 ) +1 CSE 0 Name Test Summer 00 Last Digits of Student ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. Suppose you are sorting millions of keys that consist of three decimal

More information

Operations. Priority Queues & Heaps. Prio-Q as Array. Niave Solution: Prio-Q As Array. Prio-Q as Sorted Array. Prio-Q as Sorted Array

Operations. Priority Queues & Heaps. Prio-Q as Array. Niave Solution: Prio-Q As Array. Prio-Q as Sorted Array. Prio-Q as Sorted Array Operations Priority Queues & Heaps : insert item with a priority : remove the item with highest priority, and break ties with FIFO ordering For simplicity: we use an int as the object and the priority

More information

Lecture 14: Binary Search Trees (2)

Lecture 14: Binary Search Trees (2) cs2010: algorithms and data structures Lecture 14: Binary earch Trees (2) Vasileios Koutavas chool of omputer cience and tatistics Trinity ollege Dublin lgorithms OBT DGWIK KVIN WYN 3.2 BINY T lgorithms

More information

Partha Sarathi Manal

Partha Sarathi Manal MA 515: Introduction to Algorithms & MA353 : Design and Analysis of Algorithms [3-0-0-6] Lecture 11 http://www.iitg.ernet.in/psm/indexing_ma353/y09/index.html Partha Sarathi Manal psm@iitg.ernet.in Dept.

More information

Heaps and Priority Queues

Heaps and Priority Queues Heaps and Priority Queues (A Data Structure Intermezzo) Frits Vaandrager Heapsort Running time is O(n lg n) Sorts in place Introduces an algorithm design technique» Create data structure (heap) to manage

More information

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Advanced Java Concepts Unit 5: Trees. Notes and Exercises Advanced Java Concepts Unit 5: Trees. Notes and Exercises A Tree is a data structure like the figure shown below. We don t usually care about unordered trees but that s where we ll start. Later we will

More information

Chapter 5 Data Structures Algorithm Theory WS 2016/17 Fabian Kuhn

Chapter 5 Data Structures Algorithm Theory WS 2016/17 Fabian Kuhn Chapter 5 Data Structures Algorithm Theory WS 06/ Fabian Kuhn Examples Dictionary: Operations: insert(key,value), delete(key), find(key) Implementations: Linked list: all operations take O(n) time (n:

More information

Figure 1: A complete binary tree.

Figure 1: A complete binary tree. The Binary Heap A binary heap is a data structure that implements the abstract data type priority queue. That is, a binary heap stores a set of elements with a total order (that means that every two elements

More information

The Heap Data Structure

The Heap Data Structure The Heap Data Structure Def: A heap is a nearly complete binary tree with the following two properties: Structural property: all levels are full, except possibly the last one, which is filled from left

More information

You must print this PDF and write your answers neatly by hand. You should hand in the assignment before recitation begins.

You must print this PDF and write your answers neatly by hand. You should hand in the assignment before recitation begins. 15-122 Homework 5 Page 1 of 15 15-122 : Principles of Imperative Computation, Summer 1 2014 Written Homework 5 Due: Thursday, June 19 before recitation Name: Andrew ID: Recitation: Binary search trees,

More information

l Heaps very popular abstract data structure, where each object has a key value (the priority), and the operations are:

l Heaps very popular abstract data structure, where each object has a key value (the priority), and the operations are: DDS-Heaps 1 Heaps - basics l Heaps very popular abstract data structure, where each object has a key value (the priority), and the operations are: l insert an object, find the object of minimum key (find

More information

CS711008Z Algorithm Design and Analysis

CS711008Z Algorithm Design and Analysis CS700Z Algorithm Design and Analysis Lecture 7 Binary heap, binomial heap, and Fibonacci heap Dongbo Bu Institute of Computing Technology Chinese Academy of Sciences, Beijing, China 1 / Outline Introduction

More information

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs Algorithms in Systems Engineering ISE 172 Lecture 16 Dr. Ted Ralphs ISE 172 Lecture 16 1 References for Today s Lecture Required reading Sections 6.5-6.7 References CLRS Chapter 22 R. Sedgewick, Algorithms

More information