Data structure: Heap Representation change: Heapsort Problem reduction

Slides:



Advertisements
Similar presentations
RAIK 283: Data Structures & Algorithms
Advertisements

Linear Programming (LP) (Chap.29)
Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)
BY Lecturer: Aisha Dawood. Heapsort  O(n log n) worst case like merge sort.  Sorts in place like insertion sort.  Combines the best of both algorithms.
Analysis of Algorithms
Chapter 6: Transform and Conquer
Design and Analysis of Algorithms - Chapter 61 Transform and Conquer Solve problem by transforming into: b a more convenient instance of the same problem.
COSC 3100 Transform and Conquer
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
CS 253: Algorithms Chapter 6 Heapsort Appendix B.5 Credit: Dr. George Bebis.
Analysis of Algorithms CS 477/677
More sorting algorithms: Heap sort & Radix sort. Heap Data Structure and Heap Sort (Chapter 7.6)
TCSS 343, version 1.1 Algorithms, Design and Analysis Transform and Conquer Algorithms Presorting HeapSort.
Comp 122, Spring 2004 Heapsort. heapsort - 2 Lin / Devi Comp 122 Heapsort  Combines the better attributes of merge sort and insertion sort. »Like merge.
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.
Heaps, Heapsort, Priority Queues. Sorting So Far Heap: Data structure and associated algorithms, Not garbage collection context.
Binary Heap.
Chapter 21 Binary Heap.
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
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. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
David Luebke 1 12/23/2015 Heaps & Priority Queues.
Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
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.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
1 Heap Sort. A Heap is a Binary Tree Height of tree = longest path from root to leaf =  (lgn) A heap is a binary tree satisfying the heap condition:
Sept Heapsort What is a heap? Max-heap? Min-heap? Maintenance of Max-heaps -MaxHeapify -BuildMaxHeap Heapsort -Heapsort -Analysis Priority queues.
6.Heapsort. Computer Theory Lab. Chapter 6P.2 Why sorting 1. Sometimes the need to sort information is inherent in a application. 2. Algorithms often.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Sorting With Priority Queue In-place Extra O(N) space
Priority Queues A priority queue is an ADT where:
"Teachers open the door, but you must enter by yourself. "
Linear Programming Many problems take the form of maximizing or minimizing an objective, given limited resources and competing constraints. specify the.
Heaps, Heapsort, and Priority Queues
Priority Queues An abstract data type (ADT) Similar to a queue
Heaps, Heap Sort and Priority Queues
Priority Queues Chuan-Ming Liu
Heapsort.
Heap Sort Example Qamar Abbas.
Introduction to Algorithms
The Heap Data Structure
Sorting.
Part-D1 Priority Queues
Heaps, Heapsort, and Priority Queues
CS200: Algorithm Analysis
Ch 6: Heapsort Ming-Te Chi
Transform and Conquer This group of techniques solves a problem by a transformation to a simpler/more convenient instance of the same problem (instance.
i206: Lecture 14: Heaps, Graphs intro.
Heaps 11/27/ :05 PM Heaps Heaps.
Heapsort.
Tree Representation Heap.
Lecture 3 / 4 Algorithm Analysis
Heap Sort The Heap Data Structure
"Teachers open the door, but you must enter by yourself. "
Chapter 6: Transform and Conquer
Chapter 11 Limitations of Algorithm Power
Transform and Conquer This group of techniques solves a problem by a transformation to a simpler/more convenient instance of the same problem (instance.
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
HEAPS.
Heapsort Sorting in place
Algorithms: Design and Analysis
Important Problem Types and Fundamental Data Structures
Heaps & Multi-way Search Trees
Priority Queues Binary Heaps
Asst. Prof. Dr. İlker Kocabaş
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:

Data structure: Heap Representation change: Heapsort Problem reduction Transform and Conquer Data structure: Heap Representation change: Heapsort Problem reduction

Expected Outcomes Students should be able to Explain the idea of heapsort Explain the ideas of problem reduction Analyze the time complexity of heapsort

Heaps A heap can be defined as a binary tree with keys assigned to its nodes provided the following two conditions are met: The tree’s shape requirement: The binary tree is essentially complete, that is, all its levels are full except possibly the last level, where only some rightmost leaves may be missing. The parental dominance requirement: The key at each node is ≥ the keys at its children.

Illustration of the heap’s definition a heap not a heap not a heap Second one is not heap because the tree’s shape requirement is violated and third one because parental dominance requirement fails for the node with key 5. Note that key values in a heap are ordered top down; that is a sequence of values on any path from the root to a leaf is decreasing.

Heaps and Heapsort Not only is the heap structure useful for heapsort, but it also makes an efficient priority queue. Heapsort In place O(nlogn) A priority queue is the ADT for maintaining a set S of elements, each with an associated value called a key/priority. It supports the following operations: find element with highest priority delete element with highest priority insert element with assigned priority

Properties of Heaps (1) 9 Heap and its array representation. Conceptually, we can think of a heap as a binary tree. But in practice, it is easier and more efficient to implement a heap using an array. Store heap’s elements in an array (whose elements indexed, for convenience, 1 to n) in top-down left-to-right order Relationships between indexes of parents and children. 5 3 1 4 2 1 2 3 4 5 6 9 5 3 1 4 2 PARENT(i) LEFT(i) RIGHT(i) return 2i return 2i+1 return i/2 Parental nodes are represented in the first n/2 locations

Properties of Heaps (2) Max-heap property and min-heap property Max-heap: for every node other than root, A[PARENT(i)] >= A(i) Min-heap: for every node other than root, A[PARENT(i)] <= A(i) The root has the largest key (for a max-heap) The subtree rooted at any node of a heap is also a heap Given a heap with n nodes, the height of the heap, h = log n . - Height of a node: the number of edges on the longest simple downward path from the node to a leaf. - Height of a tree: the height of its root. - level of a node: A node’s level + its height = h, the tree’s height.

Bottom-up Heap construction How can we construct a heap for a given list of keys? There are two principal alternatives for doing that. The first is bottom-up heap construction algorithm as given below: Build an essentially complete binary tree by inserting n keys in the given order. Heapifies a series of trees Starting with the last (rightmost) parental node, heapify/fix the subtree rooted at it: if the parental dominance condition does not hold for the key at this node: exchange its key with the key of its larger child Heapify/fix the subtree rooted at it (now in the child’s position) Proceed to do the same for the node’s immediate predecessor. Stops after this is done for the tree’s root.

Example of Heap Construction Construct a heap for the list 2, 9, 7, 6, 5, 8 Try Example: 4 1 3 2 16 9 10 14 8 7 16 14 10 8 7 9 3 2 4 1

Bottom-up heap construction algorithm(A Recursive version) ALGORITHM HeapBottomUp(H[1..n]) //Constructs a heap from the elements //of a given array by the bottom-up algorithm //Input: An array H[1..n] of orderable items //Output: A heap H[1..n] for i  n/2 downto 1 do MaxHeapify(H, i) Given a heap of n nodes, what’s the index of the last parent? n/2 ALGORITHM MaxHeapify(H, i) l  LEFT(i) r  RIGHT(i) if l <= n and H[l] > H[i] then largest  l else largest  i if r <= n and H[r] > H[largest] then largest  r if largest  i then exchange H[i] H[largest] MaxHeapify(H, largest) // if left child exists and > H[i] // if R child exists and > H[largest] // heapify the subtree

Bottom-up heap construction algorithm // from the last parent down to 1, heapify the subtree rooted at i // k: the root of the subtree to be heapified; v: the key of the root // if not a heap yet and the left child exists // find the larger child, j: its index. // if the key of the root > that of the larger child, done. // exchange the key with the key of the larger child // again, k: the root of the subtree to be heapified; v: the key of the root

Worst-Case Efficiency a full tree; each key on a certain level will travel to the leaf. Fix a subtree rooted at height j: 2j comparisons Fix a subtree rooted at level i : comparisons A node’s level + its height = h, the tree’s height. Total for heap construction phase: 2(h-i) h-1 Σ 2(h-i) 2i = 2 ( n – log (n + 1)) = Θ(n) i=0 # nodes at level i

Bottom-up vs. Top-down Heap Construction Bottom-up: Put everything in the array and then heapify/fix the trees in a bottom-up way. Top-down: Heaps can be constructed by successively inserting elements (see the next slide) into an (initially) empty heap.

Insertion of a New Element The algorithm Insert element at the last position in heap. Compare with its parent, and exchange them if it violates the parental dominance condition. Continue comparing the new element with nodes up the tree until the parental dominance condition is satisfied. Example 1: add 10 to a heap: 9 6 8 2 5 7 Efficiency: Inserting one new element to a heap with n-1 nodes requires no more comparisons than the heap’s height Example 2: Use the top-down method to build a heap for numbers 2 9 7 6 5 8 Questions What is the efficiency for a top-down heap construction algorithm for a heap of size n? Which one is better, a bottom-up or a top-down heap construction? h  O(logn)

Root Deletion The root of a heap can be deleted and the heap fixed up as follows: Exchange the root with the last leaf Decrease the heap’s size by 1 Heapify the smaller tree in exactly the same way we did it in MaxHeapify(). . It can’t make key comparison more than twice the heap’s height Efficiency: Example: 9 8 6 2 5 1 2h  Θ(logn)

Update keys How to do it?

Heapsort Algorithm The algorithm An example: 2 9 7 6 5 8 (Heap construction) Build heap for a given array (either bottom-up or top-down) (Maximum deletion ) Apply the root-deletion operation n-1 times to the remaining heap until heap contains just one node. An example: 2 9 7 6 5 8

Analysis of Heapsort Recall algorithm: Bottom-up heap construction Root deletion Repeat 2 until heap contains just one node. Θ(n) Θ(log n) n – 1 times Total: Θ(n) + Θ( n log n) = Θ(n log n) Note: this is the worst case. Average case also Θ(n log n).

Problem Reduction Problem Reduction Linear programming Formally, If you need to solve a problem, reduce it to another problem that you know how to solve. Linear programming A problem of optimizing a linear function of several variables subject to constraints in the form of linear equations and linear inequalities. Formally, Maximize(or minimize) c1x1+ …cnxn Subject to ai1x1+…+ ainxn ≤ (or ≥ or =) bi, for i=1…n x1 ≥ 0, …, xn ≥ 0 Reduction to graph problems

Linear Programming—Example 1: Investment Problem Scenario A university endowment needs to invest $100million Three types of investment: Stocks (expected interest: 10%) Bonds (expected interest: 7%) Cash (expected interest: 3%) Constraints The investment in stocks is no more than 1/3 of the money invested in bonds At least 25% of the total amount invested in stocks and bonds must be invested in cash Objective: An investment that maximizes the return

Example 1 (cont’) Maximize 0.10x + 0.07y + 0.03z subject to x + y + z = 100 x (1/3)y z  0.25(x + y) x  0, y  0, z  0

Linear Programming—Example 2 : Election Problem Objective: Figure out the minimum amount of money that you need to spend in order to win 50,000 urban votes 100,000 suburban votes 25,000 rural votes Scenario: A politician that tries to win an election. Three types of areas of the district: urban (100,000 voters), suburban (200,000 voters), and rural(50,000 voters). Primary issues: Building more roads Gun control Farm subsidies Gasoline tax Advertisement fee For every $1,000… constraints: Policy Urban Suburban rural Build roads -2 5 3 Gun control 8 2 -5 Farm subsidies 10 Gasoline tax

Example 2 (cont’) Minimize x + y + z + w x: the number of thousand of dollars spent on advertising on building roads y: the number of thousand of dollars spent on advertising on gun control z: the number of thousand of dollars spent on advertising on farm subsidies w: the number of thousand of dollars spent on advertising on gasoline taxes Minimize x + y + z + w subject to –2x + 8y + 0z + 10w  50 5x + 2y + 0z + 0w  100 3x – 5y + 10z - 2w  25 x, y, z, w  0

Linear Programming—Example 3: Knapsack Problem (Continuous/Fraction Version) Scenario Given n items: weights: w1 w2 … wn values: v1 v2 … vn a knapsack of capacity W Constraints Any fraction of any item can be put into the knapsack. All the items must fit into the knapsack. Objective: Find the most valuable subset of the items

Example 3 (cont’) Maximize subject to 0  xj  1 for j = 1,…, n.

Linear Programming—Example 3: Knapsack Problem (Discrete Version) Scenario Given n items: weights: w1 w2 … wn values: v1 v2 … vn a knapsack of capacity W Constraints an item can either be put into the knapsack in its entirely or not be put into the knapsack. All the items must fit into the knapsack. Objective: Find the most valuable subset of the items

Example 3 (cont’) Maximize subject to xj  {0,1} for j = 1,…, n.

Algorithms for Linear Programming Simplex algorithm: exponential time. Ellipsoid algorithm: polynomial time. Interior-point methods: polynomial time. Integer linear programming problem no polynomial solution. requires the variables to be integers.

Examples of Solving Problems by Reduction computing lcm(m, n) via computing gcd(m, n) counting number of paths of length at most n in a graph by raising the graph’s adjacency matrix to the n-th power transforming a maximization problem to a minimization problem and vice versa (also, min-heap construction) reduction to graph problems (e.g., solving River-crossing puzzle via state-space graphs)