Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)

Slides:



Advertisements
Similar presentations
RAIK 283: Data Structures & Algorithms
Advertisements

Chapter 12 Binary Search Trees
COL 106 Shweta Agrawal and Amit Kumar
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.
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.
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
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
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Section 8.8 Heapsort.  Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations  Heapsort does not require any additional.
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.
BST Data Structure A BST node contains: A BST contains
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.
Theory of Algorithms: Transform and Conquer James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape.
PRIORITY QUEUES (HEAPS). Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important.
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.
Heaps, Heapsort, Priority Queues. Sorting So Far Heap: Data structure and associated algorithms, Not garbage collection context.
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 13 B Advanced Implementations of Tables – Balanced BSTs.
Chapter 21 Binary Heap.
Adapted from instructor resource slides Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All.
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.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
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
Balanced Search Trees Chapter 19 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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:
Internal and External Sorting External Searching
Lecture 14 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Foundation of Computing Systems Lecture 4 Trees: Part I.
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.
AA Trees.
Chapter 6 Transform-and-Conquer
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.
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Chapter 6: Transform and Conquer
Advanced Implementation of Tables
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
Final Review Dr. Yingwu Zhu.
Data structure: Heap Representation change: Heapsort Problem reduction
Chapter 12 Heap ADT © 2011 Pearson Addison-Wesley. All rights reserved.
Heaps and priority queues
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:

Transform and Conquer Chapter 6

Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification) –presorting –Gaussian elimination

Transform and Conquer Solve problem by transforming into: a different representation of the same instance (representation change) –balanced search trees –heaps and heapsort –polynomial evaluation by Horner’s rule –Fast Fourier Transform

Transform and Conquer Solve problem by transforming into: a different problem altogether (problem reduction) –reductions to graph problems –linear programming

Instance simplification - Presorting Solve instance of problem by transforming into another simpler/easier instance of the same problem

Instance simplification - Presorting Presorting: Many problems involving lists are easier when list is sorted. searching computing the median (selection problem) computing the mode finding repeated elements

Selection Problem Find the k th smallest element in A[1],…A[n]. Special cases: –minimum: k = 1 –maximum: k = n –median: k = n/2 Presorting-based algorithm –sort list –return A[k]

Selection Problem Partition-based algorithm (Variable decrease & conquer): –pivot/split at A[s] using partitioning algorithm from quicksort –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[1],…A[s-1]. Θ(n) average case

Notes on Selection Problem Presorting-based algorithm: Θ(n lgn) + Θ(1) = Θ(n lgn) Partition-based algorithm: Θ(n) Conclusion: Pre-sorting does NOT yield an advantage

Finding repeated elements Presorting-based algorithm: –use mergesort (or quicksort): Θ(n lgn) –scan array to find repeated adjacent elements: Θ(n) Brute force algorithm: Θ(n 2 ) Conclusion: Presorting yields significant improvement

Taxonomy of Searching Algorithms Elementary searching algorithms –sequential search –binary search –binary tree search

Taxonomy of Searching Algorithms Balanced tree searching –AVL trees –red-black trees –multiway balanced trees (2-3 trees, trees, B trees)

Taxonomy of Searching Algorithms Hashing –separate chaining –open addressing

Balanced trees: AVL trees For every node, difference in height between left and right sub-tree is at most 1 AVL property is maintained through rotations, each time the tree becomes unbalanced lg n ≤ h ≤ lg (n + 2) average: 1.01 lg n for large n

Balanced trees: AVL trees Disadvantage: needs extra storage for maintaining node balance A similar idea: red-black trees (height of subtrees is allowed to differ by up to a factor of 2)

General case: single R-rotation

Double LR-rotation

Balance factor Algorithm maintains balance factor for each node. For example:

Heaps Definition: A heap is a binary tree with the following conditions: it is essentially complete: The key at each node is ≥ keys at its children

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

Heap construction Insert elements in the order given breadth-first in a binary tree 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)

Root deletion The root of a heap can be deleted and the heap fixed up as follows: exchange the root with the last leaf 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. 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

Representation Use an array to store breadth-first traversal of heap tree: Example: Left child of node j is at 2j Right child of node j is at 2j+1 Parent of node j is at j /

Priority queues A priority queue is the ADT of an ordered set with the operations: –find element with highest priority –delete element with highest priority –insert element with assigned priority Heaps are very good for implementing priority queues

Insertion of a new element Insert element at last position in heap. Compare with its parent and if it violates heap condition exchange them exchange them Continue comparing the new element with nodes up the tree until the heap condition is satisfied

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