Design and Analysis of Algorithms - Chapter 61 Transform and Conquer Solve problem by transforming into: b a more convenient instance of the same problem.

Slides:



Advertisements
Similar presentations
RAIK 283: Data Structures & Algorithms
Advertisements

COL 106 Shweta Agrawal and Amit Kumar
Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)
Tree Data Structures &Binary Search Tree 1. Trees Data Structures Tree  Nodes  Each node can have 0 or more children  A node can have at most one parent.
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.
Sorting Comparison-based algorithm review –You should know most of the algorithms –We will concentrate on their analyses –Special emphasis: Heapsort Lower.
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
6-0 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 6 LECTURE.
Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
CS 253: Algorithms Chapter 6 Heapsort Appendix B.5 Credit: Dr. George Bebis.
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.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
Course Review COMP171 Spring Hashing / Slide 2 Elementary Data Structures * Linked lists n Types: singular, doubly, circular n Operations: insert,
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Design and Analysis of Algorithms - Chapter 61 Transform and Conquer Solve problem by transforming into: b a more convenient instance of the same problem.
© 2006 Pearson Addison-Wesley. All rights reserved12 A-1 Chapter 12 Heaps.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Chapter 6 Transform-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 12 B Priority Queues. © 2004 Pearson Addison-Wesley. All rights reserved 12 B-2 The ADT Priority Queue: A Variation of the ADT Table The ADT priority.
Heaps and heapsort COMP171 Fall 2005 Part 2. Sorting III / Slide 2 Heap: array implementation Is it a good idea to store arbitrary.
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.
Theory of Algorithms: Transform and Conquer James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
CSC401: Analysis of Algorithms CSC401 – Analysis of Algorithms Chapter 5 – 2. Divide-and-Conquer Objectives: Introduce the Divide-and-conquer paradigm.
Chapter 6 Transform-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
1 CSC 421: Algorithm Design Analysis Spring 2014 Transform & conquer  transform-and-conquer approach  presorting  balanced search trees, heaps  Horner's.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
Chapter 21 Binary Heap.
Computer Sciences Department1. Sorting algorithm 3 Chapter 6 3Computer Sciences Department Sorting algorithm 1  insertion sort Sorting algorithm 2.
Transform and Conquer This group of techniques solves a problem by a transformation to a simpler/more convenient instance of the same problem (instance.
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.
CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
COSC2007 Data Structures II Chapter 12 Tables & Priority Queues III.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Heaps and Heapsort Prof. Sin-Min Lee Department of Computer Science San Jose State University.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Heaps & Priority Queues
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:
Lecture 14 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
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 CSC 421: Algorithm Design Analysis Spring 2013 Transform & conquer  transform-and-conquer approach  presorting  balanced search trees, heaps  Horner's.
Chapter 6 Transform-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Priority Queues, Heaps, and Heapsort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Chapter 6 Transform-and-Conquer
Bohyung Han CSE, POSTECH
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Chapter 6 Transform and Conquer.
Transform and Conquer This group of techniques solves a problem by a transformation to a simpler/more convenient instance [eg sorted] of the same problem.
Transform and Conquer This group of techniques solves a problem by a transformation to a simpler/more convenient instance of the same problem (instance.
Chapter 6: Transform and Conquer
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
Data structure: Heap Representation change: Heapsort Problem reduction
Chapter 12 Heap ADT © 2011 Pearson Addison-Wesley. All rights reserved.
Data structure: Heap Representation change: Heapsort Problem reduction
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:

Design and Analysis of Algorithms - Chapter 61 Transform and Conquer Solve problem by transforming into: b a more convenient instance of the same problem (instance implification) presortingpresorting Gaussian eliminationGaussian elimination b a different representation of the same instance (representation change) balanced search treesbalanced search trees heaps and heapsortheaps and heapsort polynomial evaluation by Horner’s rulepolynomial evaluation by Horner’s rule Fast Fourier TransformFast Fourier Transform b a different problem altogether (problem reduction) reductions to graph problemsreductions to graph problems linear programminglinear programming

Design and Analysis of Algorithms - Chapter 62 Instance simplification - Presorting Solve instance of problem by transforming into another simpler/easier instance of the same problem Presorting: Many problems involving lists are easier when list is sorted. b searching b computing the median (selection problem) b computing the mode b finding repeated elements

Design and Analysis of Algorithms - Chapter 63 Selection Problem Find the k th smallest element in A[1],…A[n]. Special cases: minimum: k = 1minimum: k = 1 maximum: k = nmaximum: k = n median: k = n/2median: k = n/2 b Presorting-based algorithm sort listsort list return A[k]return A[k] b Partition-based algorithm (Variable decrease & conquer): pivot/split at A[s] using partitioning algorithm from quicksortpivot/split at A[s] using partitioning algorithm from quicksort if s=k return A[s]if s=k return A[s] else if s<k repeat with sublist A[s+1],…A[n].else if s<k repeat with sublist A[s+1],…A[n]. else if s>k repeat with sublist A[1],…A[s-1].else if s>k repeat with sublist A[1],…A[s-1].

Design and Analysis of Algorithms - Chapter 64 Notes on Selection Problem b Presorting-based algorithm: Ω(nlgn) + Θ(1) = Ω(nlgn) b Partition-based algorithm (Variable decrease & conquer): worst case: T(n) =T(n-1) + (n+1) -> Θ(n 2 )worst case: T(n) =T(n-1) + (n+1) -> Θ(n 2 ) best case: Θ(n)best case: Θ(n) average case: T(n) =T(n/2) + (n+1) -> Θ(n)average case: T(n) =T(n/2) + (n+1) -> Θ(n) Bonus: also identifies the k smallest elements (not just the k th )Bonus: also identifies the k smallest elements (not just the k th ) b Special cases max, min: better, simpler linear algorithm (brute force) b Conclusion: Presorting does not help in this case.

Design and Analysis of Algorithms - Chapter 65 Finding repeated elements b Presorting-based algorithm: use mergesort (optimal): Θ(nlgn)use mergesort (optimal): Θ(nlgn) scan array to find repeated adjacent elements: Θ(n)scan array to find repeated adjacent elements: Θ(n) b Brute force algorithm: Θ(n 2 ) b Conclusion: Presorting yields significant improvement b Similar improvement for mode b What about searching? Θ(nlgn)

Design and Analysis of Algorithms - Chapter 66 Taxonomy of Searching Algorithms b Elementary searching algorithms sequential searchsequential search binary searchbinary search binary tree searchbinary tree search b Balanced tree searching AVL treesAVL trees red-black treesred-black trees multiway balanced trees (2-3 trees, trees, B trees)multiway balanced trees (2-3 trees, trees, B trees) b Hashing separate chainingseparate chaining open addressingopen addressing

Design and Analysis of Algorithms - Chapter 67 Balanced trees: AVL trees b For every node, difference in height between left and right subtree is at most 1 b AVL property is maintained through rotations, each time the tree becomes unbalanced b lg n ≤ h ≤ lg (n + 2) average: 1.01 lg n for large n b Disadvantage: needs extra storage for maintaining node balance b A similar idea: red-black trees (height of subtrees is allowed to differ by up to a factor of 2)

Design and Analysis of Algorithms - Chapter 68 AVL tree rotations b Small examples: 1, 2, 31, 2, 3 3, 2, 13, 2, 1 1, 3, 21, 3, 2 3, 1, 23, 1, 2 b Larger example: 4, 5, 7, 2, 1, 3, 6 b See figures 6.4, 6.5 for general cases of rotations;

Design and Analysis of Algorithms - Chapter 69 General case: single R-rotation

Design and Analysis of Algorithms - Chapter 610 Double LR-rotation

Design and Analysis of Algorithms - Chapter 611 Balance factor b Algorithm maintains balance factor for each node. For example:

Design and Analysis of Algorithms - Chapter 612 Heapsort Definition: A heap is a binary tree with the following conditions: b it is essentially complete: b The key at each node is ≥ keys at its children

Design and Analysis of Algorithms - Chapter 613 Definition implies: b Given n, there exists a unique binary tree with n nodes that is essentially complete, with h= lg n b The root has the largest key b The subtree rooted at any node of a heap is also a heap

Design and Analysis of Algorithms - Chapter 614 Heapsort Algorithm: 1. Build heap 2. Remove root –exchange with last (rightmost) leaf 3. Fix up heap (excluding last leaf) Repeat 2, 3 until heap contains just one node.

Design and Analysis of Algorithms - Chapter 615 Heap construction b Insert elements in the order given breadth-first in a binary tree b Starting with the last (rightmost) parental node, fix the heap rooted at it, if it does not satisfy the heap condition: 1.exchange it with its largest child 2.fix the subtree rooted at it (now in the child’s position) Example:

Design and Analysis of Algorithms - Chapter 616 Root deletion The root of a heap can be deleted and the heap fixed up as follows: b exchange the root with the last leaf b compare the new root (formerly the leaf) with each of its children and, if one of them is larger than the root, exchange it with the larger of the two. b continue the comparison/exchange with the children of the new root until it reaches a level of the tree where it is larger than both its children

Design and Analysis of Algorithms - Chapter 617 Representation b Use an array to store breadth-first traversal of heap tree: b Example: b Left child of node j is at 2j b Right child of node j is at 2j+1 b Parent of node j is at j /2 b Parental nodes are represented in the first n /2 locations

Design and Analysis of Algorithms - Chapter 618 Bottom-up heap construction algorithm

Design and Analysis of Algorithms - Chapter 619 Analysis of Heapsort See algorithm HeapBottomUp in section 6.4 b Fix heap with “problem” at height j: 2j comparisons b For subtree rooted at level i it does 2(h-i) comparisons b Total for heap construction phase: Σ 2(h-i) 2 i = 2 ( n – lg (n + 1)) = Θ(n) i=0 h-1 # nodes at level i

Design and Analysis of Algorithms - Chapter 620 Analysis of Heapsort (continued) Recall algorithm: 1.Build heap 2.Remove root –exchange with last (rightmost) leaf 3.Fix up heap (excluding last leaf) Repeat 2, 3 until heap contains just one node. Θ(n)Θ(n) Θ(log n) n – 1 times Total: Total: Θ(n) + Θ( n log n) = Θ(n log n) Note: Note: this is the worst case. Average case also Θ(n log n).

Design and Analysis of Algorithms - Chapter 621 Priority queues b A priority queue is the ADT of an ordered set with the operations: find element with highest priorityfind element with highest priority delete element with highest prioritydelete element with highest priority insert element with assigned priorityinsert element with assigned priority b Heaps are very good for implementing priority queues

Design and Analysis of Algorithms - Chapter 622 Insertion of a new element b Insert element at last position in heap. b Compare with its parent and if it violates heap condition exchange them exchange them b Continue comparing the new element with nodes up the tree until the heap condition is satisfied Example:Efficiency:

Design and Analysis of Algorithms - Chapter 623 Bottom-up vs. Top-down heap construction b Top down: Heaps can be constructed by successively inserting elements into an (initially) empty heap b Bottom-up: Put everything in and then fix it b Which one is better?