Chapter 6: Transform and Conquer

Slides:



Advertisements
Similar presentations
RAIK 283: Data Structures & Algorithms
Advertisements

Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)
Transform & Conquer Replacing One Problem With Another Saadiq Moolla.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. Problem Reduction Dr. M. Sakalli Marmara Unv, Levitin ’ s notes.
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.
Transform & Conquer Lecture 08 ITS033 – Programming & Algorithms
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.
MA/CSSE 473 Day 25 Transform and Conquer Student questions?
Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
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,
Design and Analysis of Algorithms - Chapter 61 Transform and Conquer Solve problem by transforming into: b a more convenient instance of the same problem.
Chapter 6 Transform-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
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.
Chapter 6 Transform-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
1 CSC 421: Algorithm Design Analysis Spring 2014 Transform & conquer  transform-and-conquer approach  presorting  balanced search trees, heaps  Horner's.
Chapter 21 Binary Heap.
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.
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.
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.
1 CSC 421: Algorithm Design Analysis Spring 2013 Transform & conquer  transform-and-conquer approach  presorting  balanced search trees, heaps  Horner's.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
Chapter 6 Transform-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
CSC 421: Algorithm Design Analysis
CSC 421: Algorithm Design Analysis
CSE373: Data Structures & Algorithms Priority Queues
AA Trees.
Heaps (8.3) CSE 2011 Winter May 2018.
Chapter 6 Transform-and-Conquer
October 30th – Priority QUeues
Week 11 - Friday CS221.
Bohyung Han CSE, POSTECH
Heap Sort Example Qamar Abbas.
Analysis & Design of Algorithms (CSCE 321)
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Sorting.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
CMSC 341: Data Structures Priority Queues – Binary Heaps
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.
Heaps and the Heapsort Heaps and priority queues
Tree Representation Heap.
Computer Science 2 Heaps.
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 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
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.
Transform and Conquer Transform and Conquer Transform and Conquer.
Data structure: Heap Representation change: Heapsort Problem reduction
Data structure: Heap Representation change: Heapsort Problem reduction
Transform and Conquer Transform and Conquer Transform and Conquer.
CSC 380: Design and Analysis of Algorithms
Chapter 12 Heap ADT © 2011 Pearson Addison-Wesley. All rights reserved.
Computer Algorithms CISC4080 CIS, Fordham Univ.
Heaps & Multi-way Search Trees
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:

Chapter 6: Transform and Conquer The Design and Analysis of Algorithms Chapter 6: Transform and Conquer

Chapter 6. Transform and Conquer Algorithms Basic Idea Instance simplification Presorting Search with presorting Element uniqueness with presorting Gaussian elimination Representation change Binary Search Trees Heaps Horner’s rule for polynomial evaluation Problem reduction Conclusion

Basic Idea 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) Reduce problem instance to smaller instance of the same problem and extend solution

Examples of Decrease-and-Conquer Algorithms Decrease by one: Insertion sort Graph search algorithms: DFS BFS Topological sorting Algorithms for generating permutations, subsets

Instance Simplification - Presorting Many problems involving lists are easier when list is sorted. searching computing the median (selection problem) checking if all elements are distinct (element uniqueness) Topological sorting helps solving some problems for dags. Presorting is used in many geometric algorithms. Efficiency of algorithms involving sorting depends on efficiency of sorting.

Search with presorting Problem: Search for a given K in A[0..n-1] Stage 1 Sort the array by an efficient sorting algorithm Stage 2 Apply binary search Efficiency: Θ(nlog n) + O(log n) = Θ(nlog n) Good or bad? Why do we have our dictionaries, telephone directories, etc. sorted?

Element Uniqueness with Presorting Presorting-based algorithm Stage 1: sort by efficient sorting algorithm (e.g. mergesort) Stage 2: scan array to check pairs of adjacent elements Efficiency: Θ(nlog n) + O(n) = Θ(nlog n) Brute force algorithm : Compare all pairs of elements Efficiency: O(n2)

Gaussian Elimination Given: A system of n linear equations in n unknowns with an arbitrary coefficient matrix. Transform to: An equivalent system of n linear equations in n unknowns with an upper triangular coefficient matrix. Solve the latter by substitutions starting with the last equation and moving up to the first one.

Gaussian Elimination a11x1 + a12x2 + … + a1nxn = b1 . . . . . an1x1 + an2x2 + … + annxn = bn a11x1+ a12x2 + … + a1nx n = b1 a22x2 + … + a2nx n = b2 annxn = bn Efficiency: Θ(n3)

Representation Change: Binary Search Trees Instead of linear representation use tree representation Arrange keys in a binary tree with the binary search tree property: K <K >K

Representation Change: Binary Search Trees Efficiency depends of the tree’s height: log2 n  h  n-1 Search, insert and delete: worst case efficiency: (n) average case efficiency: (log n)

Representation Change: Binary Search Trees Advantage: Inorder traversal produces sorted list Disadvantage: worst case efficiency is (n) Several representations have been developed to overcome the disadvantages of BSTs – AVL trees, multi-way search trees - 2-3 trees, 2-3-4 trees, red-black trees.

Representation Change: Heaps 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 The key at each node is ≥ keys at its children

Representation Change: Heaps a heap not a heap not a heap

Representation Change: Heaps Some Important Properties of a Heap Given n, there exists a unique binary tree with n nodes that is essentially complete, with h = log2 n 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

Heaps’ Array Representation Store heap’s elements in an array (whose elements indexed, for convenience, 1 to n) in top-down left-to-right order 9 1 5 3 4 2 1 2 3 4 5 6 9 5 3 1 4 2 Parental nodes are represented in the first n/2 locations Left child of node j is at 2j Right child of node j is at 2j+1 Parent of node j is at j/2

Heap Construction (bottom-up) Step 0: Initialize the structure with keys in the order given Step 1: 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 2: Repeat Step 1 for the preceding parental node

Heap Construction (bottom-up)

Heapsort Stage 1: Construct a heap for a given list of n keys Stage 2: Repeat operation of root removal n-1 times: Exchange keys in the root and in the last (rightmost) leaf Decrease heap size by 1 If necessary, swap new root with larger child until the heap condition holds

Analysis of Heapsort Stage 1: Build heap for a given list of n keys worst-case C(n) =  2(h-i) 2i = 2(n – log2(n+1) )   (n) (i = 0 to h-1) Note: 2i = nodes at level i

Analysis of Heapsort C(n) =  2log2i   (n log(n)) Stage 2: Repeat operation of root removal n-1 times (fix heap) worst-case C(n) =  2log2i   (n log(n)) i = 1 to h-1 Both worst-case and average-case efficiency: (nlogn)

Priority Queues A priority queue is the ADT of a set of elements with numerical priorities with the following operations: delete element with highest priority insert element with assigned priority Efficiency : deleteMin: (1), with (log (n)) for restructuring the heap after the delete operations insert an element:  (log (n))

Horner’s Rule Given a polynomial of degree n p(x) = anxn + a n-1 x n-1 + … + a1x1 + a0 and a specific value of x, find the value of p at that point.

Two Brute-Force Algorithms p  0 for i  n down to 0 do power  1 for k 1 to i do power  power * x p  p + ai * power return p Algorithm 2: p  a0; power  1 for k  1 to n do power  power * x p  p + ak * power return p Efficiency of algorithm 1: (n2) Efficiency of algorithm 2: (n)

Horner’s Rule p(x) = anxn + an-1 xn-1 + … + a1x1 + a0 = x(x(x(…(anx + an-1) + an-2) + …) + a0 Example: p(x) = 2x4 - x3 + 3x2 + x - 5 = = x(2x3 - x2 + 3x + 1) - 5 = = x(x(2x2 - x + 3) + 1) - 5 = = x(x(x(2x - 1) + 3) + 1) - 5

Horner’s Rule Pseudocode Efficiency of Horner’s Rule: # multiplications = # additions = n

Problem Reduction Transform the problem into a different problem for which an algorithm is already available. 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 Problem 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) reduction to graph problems (e.g., solving puzzles via state-space graphs)

Conclusion The Transform-and-Conquer paradigm is a powerful problem-solving strategy. The key to success is to be able to view the problem from different perspectives