CSC Design and Analysis of Algorithms. Lecture 8. Transform and Conquer II Algorithm Design Technique. Transform and Conquer

Similar documents
CSC Design and Analysis of Algorithms. Lecture 8. Transform and Conquer II Algorithm Design Technique. Transform and Conquer

Lecture 7. Transform-and-Conquer

2. Instance simplification Solve a problem s instance by transforming it into another simpler/easier instance of the same problem

CSC Design and Analysis of Algorithms. Lecture 7. Transform and Conquer I Algorithm Design Technique. Transform and Conquer

CSC Design and Analysis of Algorithms

CPS 616 TRANSFORM-AND-CONQUER 7-1

CSC 1700 Analysis of Algorithms: Heaps

CSC 8301 Design and Analysis of Algorithms: Heaps

CSC Design and Analysis of Algorithms. Lecture 7. Transform and Conquer I Algorithm Design Technique. Transform and Conquer

Module 2: Classical Algorithm Design Techniques

Transform & Conquer. Presorting

CSC 421: Algorithm Design Analysis. Spring 2013

Design and Analysis of Algorithms - Chapter 6 6

Heaps and Priority Queues

Chapter 6 Heapsort 1

Properties of a heap (represented by an array A)

Sorting and Searching

Priority Queues and Binary Heaps

CSC Design and Analysis of Algorithms. Lecture 5. Decrease and Conquer Algorithm Design Technique. Decrease-and-Conquer

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

ADT Priority Queue. Heaps. A Heap Implementation of the ADT Priority Queue. Heapsort

1 Interlude: Is keeping the data sorted worth it? 2 Tree Heap and Priority queue

Sorting and Searching

Heapsort. Heap data structure

Heapsort. Why study Heapsort?

Overview of Presentation. Heapsort. Heap Properties. What is Heap? Building a Heap. Two Basic Procedure on Heap

CSE 241 Class 17. Jeremy Buhler. October 28, Ordered collections supported both, plus total ordering operations (pred and succ)

Heap: A binary heap is a complete binary tree in which each, node other than root is smaller than its parent. Heap example: Fig 1. NPTEL IIT Guwahati

Algorithms and Data Structures

Data Structures and Algorithms

BM267 - Introduction to Data Structures

9. Heap : Priority Queue

CS350: Data Structures Heaps and Priority Queues

Chapter 4. Transform-and-conquer

Data Structures and Algorithms for Engineers

Binary Heaps in Dynamic Arrays

AVL Trees Heaps And Complexity

Priority queues. Priority queues. Priority queue operations

CS 241 Analysis of Algorithms

Trees & Tree-Based Data Structures. Part 4: Heaps. Definition. Example. Properties. Example Min-Heap. Definition

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

Searching, Sorting. part 1

Definition of a Heap. Heaps. Priority Queues. Example. Implementation using a heap. Heap ADT

Module 2: Priority Queues

CS Elementary Graph Algorithms & Transform-and-Conquer

Module 2: Priority Queues

CSC Design and Analysis of Algorithms. Lecture 5. Decrease and Conquer Algorithm Design Technique. Decrease-and-Conquer

CSC 421: Algorithm Design Analysis. Spring 2017

Introduction to Algorithms 3 rd edition

Module 2: Priority Queues

CS 240 Data Structures and Data Management. Module 2: Priority Queues

DATA STRUCTURES AND ALGORITHMS

CS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22

The Heap Data Structure

Lecture 5: Sorting Part A

CS 240 Data Structures and Data Management. Module 2: Priority Queues

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?

Collection of priority-job pairs; priorities are comparable.

Heaps Goodrich, Tamassia. Heaps 1

Sorting. Two types of sort internal - all done in memory external - secondary storage may be used

Heaps. Heaps Priority Queue Revisit HeapSort

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

EST Solutions. Ans 1(a): KMP Algorithm for Preprocessing: KMP Algorithm for Searching:

Midterm solutions. n f 3 (n) = 3

Algorithms and Data Structures

Heaps. Heapsort. [Reading: CLRS 6] Laura Toma, csci2000, Bowdoin College

University of Waterloo CS240, Winter 2010 Assignment 2

Lecture: Analysis of Algorithms (CS )

Data Structures and Algorithms Chapter 4

Priority Queues, Heaps, and Heapsort

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

Heaps. 2/13/2006 Heaps 1

A data structure and associated algorithms, NOT GARBAGE COLLECTION

Partha Sarathi Manal

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Lower Bound on Comparison-based Sorting

Data Structures and Algorithms Week 4

Topic: Heaps and priority queues

Priority Queues. Lecture15: Heaps. Priority Queue ADT. Sequence based Priority Queue

Priority Queues and Heaps. Heaps of fun, for everyone!

Tirgul 4. Order statistics. Minimum & Maximum. Order Statistics. Heaps. minimum/maximum Selection. Overview Heapify Build-Heap

Heaps 2. Recall Priority Queue ADT. Heaps 3/19/14

CS165: Priority Queues, Heaps

CS106X Programming Abstractions in C++ Dr. Cynthia Bailey Lee

Lecture 13: AVL Trees and Binary Heaps

Recall: Properties of B-Trees

Binary heaps (chapters ) Leftist heaps

Chapter 6 Heap and Its Application

Heaps, Heapsort, Priority Queues

CSCE 411 Design and Analysis of Algorithms

COMP 251 Winter 2017 Online quizzes with answers

Algorithms, Spring 2014, CSE, OSU Lecture 2: Sorting

Heaps & Priority Queues. (Walls & Mirrors - Remainder of Chapter 11)

CPSC 311 Lecture Notes. Sorting and Order Statistics (Chapters 6-9)

Heaps and Priority Queues

Tables and Priority Queues

CSE 373 Data Structures and Algorithms. Lecture 15: Priority Queues (Heaps) III

Appendix A and B are also worth reading.

CS 240 Fall Mike Lam, Professor. Priority Queues and Heaps

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

Transcription:

CSC 301- Design and Analysis of Algorithms Lecture Transform and Conquer II Algorithm Design Technique Transform and Conquer This group of techniques solves a problem by a transformation to a simpler/more convenient instance of the same problem (instance simplification) to a different representation of the same instance (representation change) to a different problem for which an algorithm is already available (problem reduction) 1

Representation Change Search Trees (binary, AVL, - 3, - 3-4, B- trees) Heaps Horner s rule for polynomial evaluation Computing a n (binary exponentiation) Heaps and Heapsort Definition A heap is a binary tree with keys at its nodes (one key per node) such that: It is essentially complete, i.e., all its levels are full except possibly the last level, where only some rightmost keys may be missing (shape property) The key at each node is keys at its children (heap property)

Illustration of the heap s definition 10 10 10 5 5 5 4 1 1 6 1 a heap not a heap not a heap Note: Heap s elements are ordered top down (along any path down from its root), but they are not ordered left to right Some Important Properties of a Heap The root contains the largest key The subtree rooted at any node of a heap is also a heap A heap can be represented as an array 3

Heap s Array Representation Store heap s elements in an array (whose elements indexed, for convenience, 1 to n) in top-down left-to-right order Example: 5 3 1 3 4 5 6 5 3 1 4 1 4 Left child of node j is at j Right child of node j is at j+1 Parent of node j is at j/ Parental nodes are represented in the first n/ locations Heap Construction (bottom-up) Step 0: Initialize the structure with keys in the order given Step 1: (Heapify) Starting with the last (rightmost) parental node, fix the heap rooted at it, if it doesn t satisfy the heap condition: keep exchanging it with its largest child until the heap condition holds Step : Repeat Step 1 for the preceding parental node 4

Example of Heap Construction (bottom-up) Construct a heap for the list,,, 6, 5, > 6 5 6 5 6 5 > > 6 6 5 6 5 5 Bottom-up Heap Construction Algorithm Construct a heap for the list 1 3 4 5 6 6 5 H 6 5... 6 5 6 5 5

Pseudopodia of Bottom-up Heap Construction Heapsort Stage 1: Construct a heap for a given list of n keys Stage : Repeat operation of root removal n-1 times: Exchange keys in the root and in the last (rightmost) leaf Decrease heap size by 1 Heapify the tree: if necessary, swap new root with larger child until the heap condition holds 6

Example of Sorting by Heapsort Sort the list,,, 6, 5, by heapsort Stage 1 (heap construction) 6 5 1 3 4 5 6 6 5 Example of Sorting by Heapsort Sort the list,,, 6, 5, 1 3 4 5 6 6 5 Stage 1 (heap construction) Stage (root/max removal) 6 5 6 5 6 5 6 5 6 5 6 5 6 5

Example of Sorting by Heapsort Sort the list,,, 6, 5, by heapsort Stage 1 (heap construction) Stage (root/max removal) 6 5 6 5 6 5 6 5 6 5 6 5 6 5 5 6 6 5 6 5 6 5 6 5 6 5 6 5 5 6 5 6 Stage 1: Build heap for a given list of n keys worst-case C(n) = Analysis of Heapsort

Stage 1: Build heap for a given list of n keys worst-case h-1 C(n) = Σ (h-i) i = (n log (n + 1)) Θ(n) i=0 # nodes at level i Stage : Repeat operation of root removal n-1 times (fix heap) worst-case C(n) = Analysis of Heapsort Analysis of Heapsort Stage 1: Build heap for a given list of n keys worst-case h-1 C(n) = Σ (h-i) i = (n log (n + 1)) Θ(n) i=0 # nodes at level i Stage : Repeat operation of root removal n-1 times (fix heap) worst-case n-1 C(n) = Σ log i Θ(nlogn) i=1 Both worst-case and average-case efficiency: Θ(nlogn) In-place: yes Stability: no (e.g., 1 1)

Review of Major Sorting Algorithms Selection sort Insertion sort Mergesort Quicksort Heapsort strategy worst time avg. time in-place stability Priority Queue A priority queue is the ADT of a set of elements with numerical priorities with the following operations: find element with highest priority delete element with highest priority insert element with assigned priority (see below) Heap is a very efficient way for implementing priority queues Two ways to handle priority queue in which highest priority = smallest number 10

Insertion of a New Element into a Heap Insert the new element at last position in heap. Compare it with its parent and, if it violates heap condition, exchange them Continue comparing the new element with nodes up the tree until the heap condition is satisfied Example: Insert key 10 10 6 6 > 10 > 6 5 10 5 5 Efficiency: O(log n) Representation Change Search Trees (binary, AVL, - 3, - 3-4, B- trees) Heaps Horner s rule for polynomial evaluation Computing a n (binary exponentiation) 11

Horner s Rule For Polynomial Evaluation Given a polynomial of degree n p(x) = a n x n + a n-1 x n-1 + + a 1 x + a 0 and a specific value of x, find the value of p at that point. Two brute-force algorithms: p 0 p a 0 ; power 1 for i n downto 0 do for i 1 to n do power 1 power power * x for j 1 to i do p p + a i * power power power * x return p p p + a i * power return p Horner s Rule Example: p(x) = x 4 - x 3 + 3x + x - 5 = = x(x 3 - x + 3x + 1) - 5 = = x(x(x - x + 3) + 1) - 5 = = x(x(x(x - 1) + 3) + 1) - 5 Substitution into the last formula leads to a faster algorithm Same sequence of computations are obtained by simply arranging the coefficient in a table and proceeding as follows: coefficients -1 3 1-5 x=3 1

Horner s Rule pseudocode Efficiency of Horner s Rule: # multiplications = # additions = n Synthetic division of of p(x) by (x-x 0 ) Example: Let p(x) = x 4 - x 3 + 3x + x - 5. Find p(x):(x-3) Computing a n (revisited) Left-to-right binary exponentiation Initialize product accumulator by 1. Scan n s binary expansion from left to right and do the following: If the current binary digit is 0, square the accumulator (S) If the binary digit is 1, square the accumulator and multiply it by a (SM) Example: Compute a 13. Here, n = 13 = 1101. binary rep. of 13: 1 1 0 1 SM SM S SM accumulator: 1 1 *a=a a *a = a 3 (a 3 ) = a 6 (a 6 ) *a= a 13 (computed left-to-right) Efficiency: (b-1) M(n) (b-1) where b = log n + 1 13

Computing a n (cont.) Right-to-left binary exponentiation Scan n s binary expansion from right to left and compute a n as the product of terms a i corresponding to 1 s in this expansion. Example Compute a 13 by the right-to-left binary exponentiation. Here, n = 13 = 1101. 1 1 0 1 a a 4 a a : a i terms a * a 4 * a : product (computed right-to-left) Efficiency: same as that of left-to-right binary exponentiation Transform and Conquer Problem Reduction 14

Problem Reduction This variation of transform-and-conquer solves a problem by transforming it into different problem for which an algorithm is already available. To be of practical value, the combined time of the transformation and solving the other problem should be smaller than solving the problem as given by another method. Examples of Solving Problems by Reduction computing lcm(m, n) via computing gcd(m, n) counting number of paths of length 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) linear programming reduction to graph problems (e.g., solving puzzles via state-space graphs) 15

Homework Read Sec. 6.4, 6.5, and 6.6 Exercises 6.4: 3, 6,, Exercises 6.5: 4,, Exercises 6.6:, 16