MA/CSSE 473 Day 22 Binary Heaps Heapsort Answers to student questions Exam 2 Tuesday, Nov 4 in class.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

Chapter 4: Trees Part II - AVL Tree
Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Transform & Conquer Replacing One Problem With Another Saadiq Moolla.
Chapter 6: Transform and Conquer
COSC 3100 Transform and Conquer
MA/CSSE 473 Day 25 Transform and Conquer Student questions?
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
CS 253: Algorithms Chapter 6 Heapsort Appendix B.5 Credit: Dr. George Bebis.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
More sorting algorithms: Heap sort & Radix sort. Heap Data Structure and Heap Sort (Chapter 7.6)
DAST, Spring © L. Joskowicz 1 Data Structures – LECTURE 1 Introduction Motivation: algorithms and abstract data types Easy problems, hard problems.
TCSS 343, version 1.1 Algorithms, Design and Analysis Transform and Conquer Algorithms Presorting HeapSort.
Midterm Exam Two Tuesday, November 25 st In class cumulative.
3-Sorting-Intro-Heapsort1 Sorting Dan Barrish-Flood.
Heapsort CIS 606 Spring Overview Heapsort – O(n lg n) worst case—like merge sort. – Sorts in place—like insertion sort. – Combines the best of both.
Heapsort Based off slides by: David Matuszek
MA/CSSE 473 Day 20 Josephus problem Transform and conquer examples.
CS 3610 Midterm Review.
Christopher Moh 2005 Application of Data Structures.
Heaps, Heapsort, Priority Queues. Sorting So Far Heap: Data structure and associated algorithms, Not garbage collection context.
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
1 CSC 421: Algorithm Design Analysis Spring 2014 Transform & conquer  transform-and-conquer approach  presorting  balanced search trees, heaps  Horner's.
Binary Heap.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
Chapter 21 Binary Heap.
Computer Sciences Department1. Sorting algorithm 3 Chapter 6 3Computer Sciences Department Sorting algorithm 1  insertion sort Sorting algorithm 2.
MA/CSSE 473 Day 27 Hash table review Intro to string searching.
MA/CSSE 473 Day 21 AVL Tree Maximum height 2-3 Trees Student questions?
MA/CSSE 473 Day 28 Dynamic Programming Binomial Coefficients Warshall's algorithm Student questions?
MA/CSSE 473 Day 23 Student questions Space-time tradeoffs Hash tables review String search algorithms intro.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
MA/CSSE 473 Days Answers to student questions Prim's Algorithm details and data structures Kruskal details.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is.
Priority Queues, Heaps, and Heapsort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
1Computer Sciences. 2 HEAP SORT TUTORIAL 4 Objective O(n lg n) worst case like merge sort. Sorts in place like insertion sort. A heap can be stored as.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
1 CSC 421: Algorithm Design Analysis Spring 2013 Transform & conquer  transform-and-conquer approach  presorting  balanced search trees, heaps  Horner's.
MA/CSSE 473 Day 14 Strassen's Algorithm: Matrix Multiplication Decrease and Conquer DFS.
MA/CSSE 473 Day 06 DivisionPrimes Modular Arithmetic.
MA/CSSE 473 Day 05 More induction Factors and Primes Recursive division algorithm.
MA/CSSE 473 Day 05 Factors and Primes Recursive division algorithm.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
CSC 421: Algorithm Design Analysis
CSC 421: Algorithm Design Analysis
MA/CSSE 473 Day 23 Review of Binary Heaps and Heapsort
MA/CSSE 473 Day 20 Finish Josephus
"Teachers open the door, but you must enter by yourself. "
MA/CSSE 473 Day 05 Factors and Primes Recursive division algorithm.
MA/CSSE 473 Day 36 Student Questions More on Minimal Spanning Trees
Priority Queues An abstract data type (ADT) Similar to a queue
Heaps, Heap Sort and Priority Queues
Data Structures & Algorithms
MA/CSSE 473 Day 24 Student questions Space-time tradeoffs
MA/CSSE 473 Day 21 AVL Tree Maximum height 2-3 Trees
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Ch 6: Heapsort Ming-Te Chi
CS Data Structure: Heaps.
Chapter 6: Transform and Conquer
Data structure: Heap Representation change: Heapsort Problem reduction
Topic 5: Heap data structure heap sort Priority queue
Data structure: Heap Representation change: Heapsort Problem reduction
Priority Queues CSE 373 Data Structures.
Heaps & Multi-way Search Trees
Data structure: Heap Representation change: Heapsort Problem reduction
Presentation transcript:

MA/CSSE 473 Day 22 Binary Heaps Heapsort Answers to student questions Exam 2 Tuesday, Nov 4 in class

Binary (max) Heap Quick Review An almost-complete Binary Tree –All levels, except possibly the last, are full –On the last level all nodes are as far left as possible –No parent is smaller than either of its children –A great way to represent a Priority Queue Representing a binary heap as an array: See also Weiss, Chapter 21 (Weiss does min heaps) Representation change example

Insertion and RemoveMax Insertion: –Insert at the next position (end of the array) to maintain an almost-complete tree, then "percolate up" within the tree to restore heap property. RemoveMax: –Move last element of the heap to replace the root, then "percolate down" to restore heap property. Both operations are Ѳ(log n). Many more details (done for min-heaps): – hulman.edu/class/csse/csse230/201230/Slides/18- Heaps.pdfhttp:// hulman.edu/class/csse/csse230/201230/Slides/18- Heaps.pdf

Heap utilitiy functions Code is on-line, linked from the schedule page

HeapSort Arrange array into a heap. (details next slide) for i = n downto 2: a[1]  a[i], then "reheapify" a[1]..a[i-1] Animation: m/heapsort.html m/heapsort.html Faster heap building algorithm: buildheap _structure/HeapSort/heap_applet.html _structure/HeapSort/heap_applet.html

HeapSort Code

Recap: HeapSort: Build Initial Heap Two approaches: –for i = 2 to n percolateUp(i) –for j = n/2 downto 1 percolateDown(j) Which is faster, and why? What does this say about overall big-theta running time for HeapSort?

TRANSFORM AND CONQUER Polynomial Evaluation Problem Reductiion

Recap: Horner's Rule We discussed it in class previously It involves a representation change. Instead of a n x n + a n-1 x n-1 + …. + a 1 x + a 0, which requires a lot of multiplications, we write ( … (a n x + a n-1 )x + … +a 1 )x + a 0 code on next slide

Recap: Horner's Rule Code This is clearly Ѳ(n).

Problem Reduction Express an instance of a problem in terms of an instance of another problem that we already know how to solve. There needs to be a one-to-one mapping between problems in the original domain and problems in the new domain. Example: In quickhull, we reduced the problem of determining whether a point is to the left of a line to the problem of computing a simple 3x3 determinant. Example: Moldy chocolate problem in HW 9. The big question: What problem to reduce it to? (You'll answer that one in the homework)

Least Common Multiple Let m and n be integers. Find their LCM. Factoring is hard. But we can reduce the LCM problem to the GCD problem, and then use Euclid's algorithm. Note that lcm(m,n)∙gcd(m,n) = m∙n This makes it easy to find lcm(m,n)

Paths and Adjacency Matrices We can count paths from A to B in a graph by looking at powers of the graph's adjacency matrix. For this example, I used the applet from which is no longer accessible

Linear programming We want to maximize/minimize a linear function, subject to constraints, which are linear equations or inequalities involving the n variables x 1,…,x n. The constraints define a region, so we seek to maximize the function within that region. If the function has a maximum or minimum in the region it happens at one of the vertices of the convex hull of the region. The simplex method is a well-known algorithm for solving linear programming problems. We will not deal with it in this course. The Operations Research courses cover linear programming in some detail.

Integer Programming A linear programming problem is called an integer programming problem if the values of the variables must all be integers. The knapsack problem can be reduced to an integer programming problem: maximize subject to the constraints and x i  {0, 1} for i=1, …, n

SPACE-TIME TRADEOFFS Sometimes using a little more space saves a lot of time

Space vs time tradeoffs Often we can find a faster algorithm if we are willing to use additional space. Examples:

Space vs time tradeoffs Often we can find a faster algorithm if we are willing to use additional space. Give some examples (quiz question) Examples: –Binary heap vs simple sorted array. Uses one extra array position –Merge sort –Radix sort and Bucket Sort –Anagram finder –Binary Search Tree (extra space for the pointers) –AVL Tree (extra space for the balance code)