CPS 616 TRANSFORM-AND-CONQUER 7-1

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

CSC Design and Analysis of Algorithms

Lecture 7. Transform-and-Conquer

CSC Design and Analysis of Algorithms. Lecture 7. Transform and Conquer I Algorithm Design Technique. 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 8. Transform and Conquer II Algorithm Design Technique. Transform and Conquer

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

CS Elementary Graph Algorithms & Transform-and-Conquer

Transform & Conquer. Presorting

CSCE 411 Design and Analysis of Algorithms

Design and Analysis of Algorithms - Chapter 6 6

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

Algorithm Design Paradigms

LECTURE 3 ALGORITHM DESIGN PARADIGMS

Chapter 4. Divide-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Divide-and-Conquer. The most-well known algorithm design strategy: smaller instances. combining these solutions

CS Transform-and-Conquer

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

CSC 421: Algorithm Design Analysis. Spring 2013

Module 2: Classical Algorithm Design Techniques

CS Data Structures and Algorithm Analysis

MA/CSSE 473 Day 20. Recap: Josephus Problem

7. Sorting I. 7.1 Simple Sorting. Problem. Algorithm: IsSorted(A) 1 i j n. Simple Sorting

Chapter 4. Transform-and-conquer

Midterm solutions. n f 3 (n) = 3

Lecture 5. Treaps Find, insert, delete, split, and join in treaps Randomized search trees Randomized search tree time costs

Levitin second pages 2002/9/9 12:52 p. 192 (chap06) Windfall Software, PCA ZzTeX v8.7

Algorithms. AVL Tree

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

8. Sorting II. 8.1 Heapsort. Heapsort. [Max-]Heap 6. Heapsort, Quicksort, Mergesort. Binary tree with the following properties Wurzel

Unit-2 Divide and conquer 2016

CISC 235: Topic 4. Balanced Binary Search Trees

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

Self-Balancing Search Trees. Chapter 11

CSC 1700 Analysis of Algorithms: Heaps

Search Trees. Undirected graph Directed graph Tree Binary search tree

Balanced Search Trees. CS 3110 Fall 2010

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

CSC 8301 Design and Analysis of Algorithms: Heaps

EXAM ELEMENTARY MATH FOR GMT: ALGORITHM ANALYSIS NOVEMBER 7, 2013, 13:15-16:15, RUPPERT D

CSC Design and Analysis of Algorithms

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

CIS265/ Trees Red-Black Trees. Some of the following material is from:

Analysis of Algorithms

Searching, Sorting. part 1

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

Divide-and-Conquer. Dr. Yingwu Zhu

Section 4 SOLUTION: AVL Trees & B-Trees

Search Trees - 1 Venkatanatha Sarma Y

CSE100. Advanced Data Structures. Lecture 8. (Based on Paul Kube course materials)

Binary Search Trees. Analysis of Algorithms

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

AVL Tree Definition. An example of an AVL tree where the heights are shown next to the nodes. Adelson-Velsky and Landis

Lecture 11: Multiway and (2,4) Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

MA/CSSE 473 Day 20. Student Questions

Lecture 6: Analysis of Algorithms (CS )

Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

CS521 \ Notes for the Final Exam

COMP 251 Winter 2017 Online quizzes with answers

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

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

CSC 421: Algorithm Design Analysis. Spring 2017

AVL Trees Goodrich, Tamassia, Goldwasser AVL Trees 1

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Data Structures in Java

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

Questions Total Points Score

CS350: Data Structures Red-Black Trees

1 Introduction 2. 2 A Simple Algorithm 2. 3 A Fast Algorithm 2

Data Structures Brett Bernstein

1 The range query problem

AVL Trees (10.2) AVL Trees

Solutions to Exam Data structures (X and NV)

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

CS350: Data Structures AVL Trees

CS 206 Introduction to Computer Science II

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees

CS Divide and Conquer

Trees. (Trees) Data Structures and Programming Spring / 28

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

Chapter 12 Advanced Data Structures

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

Advanced Set Representation Methods

CS Divide and Conquer

CSCE 411 Design and Analysis of Algorithms

Balanced Binary Search Trees. Victor Gao

21# 33# 90# 91# 34# # 39# # # 31# 98# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10# #

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

Balanced Binary Search Trees

Chapter 3:- Divide and Conquer. Compiled By:- Sanjay Patel Assistant Professor, SVBIT.

Balanced Search Trees

Selection, Bubble, Insertion, Merge, Heap, Quick Bucket, Radix

Augmenting Data Structures

CHAPTER 10 AVL TREES. 3 8 z 4

Overview of Sorting Algorithms

Trees. Reading: Weiss, Chapter 4. Cpt S 223, Fall 2007 Copyright: Washington State University

Transcription:

CPS 616 TRANSFORM-AND-CONQUER 7-1 TRANSFORM AND CONQUER Group of techniques to solve a problem by first transforming the problem into one of: 1. a simpler/more convenient instance of the same problem (instance simplification) 2. a different representation of the same instance (representation change) 3. a different problem for which an algorithm is already available (problem reduction) INSTANCE SIMPLIFICATION - PRESORTING Main idea: Many problems involving lists are easier when list is sorted: searching computing the median (selection problem) checking if all elements are distinct (element uniqueness) Also Topological sorting helps solving some problems for dags. Presorting is used in many geometric algorithms. Efficiency of sorting Efficiency of algorithms with pre-sorts depends on efficiency of sorting Theorem (see Sec. 11.2): log 2 n! n log 2 n comparisons are necessary in the worst case to sort a list of size n by any comparison-based algorithm. Note: About n log 2 n comparisons are also sufficient to sort array of size n (by mergesort, quicksort)

CPS 616 TRANSFORM-AND-CONQUER 7-2 Searching with presorting Problem: Search for a given K in A[0..n-1] Presorting-based algorithm: Stage 1 Sort the array by an efficient sorting algorithm Stage 2 Apply binary search Efficiency: Θ(nlog n) + O(log n) = Θ(nlog n) Linear search (without presorting): Element uniqueness with presorting Problem: are all the elements of an array unique? Brute force algorithm : Compare all pairs of elements Efficiency: O(n 2 ) 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) Computing a mode with presorting Definition: a mode is a value that occurs most often in a given list of numbers: e.g. for 2, 6, 7, 8, 6, 1, 6, 3, 6, 2 the mode is 6 Brute force algorithm: Check the frequency of occurrence of each element and select element with highest frequency Efficiency: O(n 2 ) Presorting-based algorithm Stage 1: sort by efficient sorting algorithm (e.g. mergesort) Stage 2: scan array counting equal adjacent elements Efficiency: Θ(nlog n) + O(n) = Θ(nlog n)

CPS 616 TRANSFORM-AND-CONQUER 7-3 INSTANCE SIMPLIFICATION - GAUSSIAN ELIMINATION Background To solve a set of equations: a 11 x 1 + a 12 x 2 = b 1 a 21 x 1 + a 22 x 2 = b 2 Transform the second equation as an equation with 1 variable: a 11 x 1 + a 12 x 2 = b 1 c 22 x 2 = d 2 Then second equation can be solved as x 2 = d 2 / c 22 And this value can be substituted back into first: a 11 x 1 + a 12 (d 2 / c 22 ) = b 1 Which can be solved as: x 1 = [ b 1 - a 12 (d 2 / c 22 ) ] / a 11 How to transform: into: a 11 x 1 + a 12 x 2 = b 1 a 11 x 1 + a 12 x 2 = b 1 a 21 x 1 + a 22 x 2 = b 2 c 22 x 2 = d 2 Multiply first equation by a 21 / a 11 a 21 x 1 + (a 12 a 21 / a 11 )x 2 = b 1 a 21 / a 11 Subtract this equation from the second on both sides: a 22 x 2 - (a 12 a 21 / a 11 )x 2 = b 2 - b 1 a 21 / a 11 We get: c 22 = a 22 - (a 12 a 21 / a 11 ) d 2 = b 2 - b 1 a 21 / a 11 Note that the matrix equivalent of a 11 x 1 + a 12 x 2 = b 1 a 21 x 1 + a 22 x 2 = b 2 is: ( a 11 a 12 a 21 a 22 ). ( x 1 x 2 ) = ( b 1 b 2 ) And the matrix equivalent of a 11 x 1 + a 12 x 2 = b 1 c 22 x 2 = d 2 is: ( a 11 a 12 0 c 22 ). ( x 1 x 2 ) = ( b 1 d 2 )

CPS 616 TRANSFORM-AND-CONQUER 7-4 Generalization Given: A system of n linear equations in n unknowns with an arbitrary coefficient matrix. a 11 x 1 + a 12 x 2 + + a 1n x n = b 1 a 21 x 1 + a 22 x 2 + + a 2n x n = b 2... a n1 x 1 + a n2 x 2 + + a nn x n = b n Transform to: An equivalent system of n linear equations in n unknowns with an upper triangular coefficient matrix. c 11 x 1 + c 12 x 2 + + c 1n x n = d 1 c 22 x 2 + + c 2n x n = d 2... c nn x n = d n Solve the latter by substitutions starting with the last equation and moving up to the first one. The transformation is accomplished by a sequence of elementary operations on the system s coefficient matrix (which don t change the system s solution): for i 1 to n-1 do replace each of the subsequent rows (i.e., rows i+1,, n) by a difference between that row and an appropriate multiple of the i-th row to make the new coefficient in the i-th column of that row 0 Pseudocode Stage 1: Reduction to an upper-triangular matrix for i 1 to n-1 do // remove multiple of row i from others below for j i+1 to n do // rows j from which row i is removed factor A[j, i] / A[i, i] for k i to n+1 do // columns of row j A[j, k] A[j, k] - A[i, k] * factor Stage 2: Back substitutions for j n downto 1 do t 0 for k j +1 to n do t t + A[j, k] * x[k] x[j] (A[j, n+1] - t) / A[j, j] //go from bottom row j to top //substitute solved x s into equation // solve equation for row

CPS 616 TRANSFORM-AND-CONQUER 7-5 Efficiency Θ(n 3 ) + Θ(n 2 ) = Θ(n 3 ) Example Solve 2x 1-4x 2 + x 3 = 6 2-4 1 6 3x 1 - x 2 + x 3 = 11 3-1 1 11 x 1 + x 2 - x 3 = -3 1 1-1 -3 Gaussian Elimination 2-4 1 6 row2 (3/2)*row1: 0 5-1/2 2 row3 (1/2)*row1: 0 3-3/2-6 2-4 1 6 0 5-1/2 2 row3 (3/5)*row2 : 0 0-6/5-36/5 Which represents: 2x 1-4x 2 + x 3 = 6 5x 2-1/2 x 3 = 2-6/5x 3 = -36/5 Backward substitution x 3 = (-36/5) / (-6/5) = 6 x 2 = (2+(1/2)*6) / 5 = 1 x 1 = (6 6 + 4*1)/2 = 2

CPS 616 TRANSFORM-AND-CONQUER 7-6 INSTANCE SIMPLIFICATION - BALANCED SEARCH TREES Review: Searching with BSTs Linear search for a key through an array is O(n) Instead arrange keys in a binary tree with the binary search tree property: If the tree is balanced, search for key is O(log n) Bonus: inorder traversal produces sorted list Review: Operations on BSTs Searching straightforward Insertion search for key, insert at leaf where search terminated Deletion 3 cases: deleting key at a leaf deleting key at node with single child deleting key at node with two children Efficiency depends of the tree s height: log 2 n h n-1, with height average (random files) be about 3log 2 n Thus all three operations have worst case efficiency: (n) average case efficiency: (log n)

CPS 616 TRANSFORM-AND-CONQUER 7-7 Balanced Search Trees Attractiveness of binary search tree is marred by the bad (linear) worst-case efficiency. Two ideas to overcome it are: Instance simplification: rebalance binary search tree when a new insertion makes the tree too unbalanced AVL trees: diff. between heights of L and R subtrees 1 red-black trees: height of 1 subtree 2* height of the other (see later) Representation change: allow more than one key per node of a search tree 2-3 trees 2-3-4 trees B-trees AVL Trees Definitions: The height of a tree is the number of edges between the root and the lowest leaf. The height of an empty tree is -1. The balance factor of the node of a tree is the difference between the heights of its left and right subtrees. An AVL tree is a binary search tree in which the absolute value of the balance factor of any node is at most 1

CPS 616 TRANSFORM-AND-CONQUER 7-8 Degenerate Trees Degenerate BST 100 Degenerate AVL Tree (half of the levels are fully balanced) 1 100 45 1 50 1 140 30 1 1 1 1 18 40 1 1 1 60 0 120 1 0 0 150 15 25 43 55 65 112 123 144 0 0 0 0 10 18 33 41 52 105 0 10 Analysis of Efficiency height 1.4404 log 2 (n + 2) - 1.3277 i.e. O(log n) average height: 1.01 log 2 n + 0.1 for large n (found empirically) Search and insertion are O(log n) Deletion is more complicated but is also O(log n) Disadvantages: frequent rotations (see later) complexity

CPS 616 TRANSFORM-AND-CONQUER 7-9 Rotations If a key insertion violates the balance requirement at some node, the subtree rooted at that node is transformed via one of the four rotations. (The rotation is always performed for a subtree rooted at an unbalanced node closest to the new leaf.)

CPS 616 TRANSFORM-AND-CONQUER 7-10

CPS 616 TRANSFORM-AND-CONQUER 7-11 AVL Tree Construction

CPS 616 TRANSFORM-AND-CONQUER 7-12 REPRESENTATION CHANGE - 2-3 TREES Multiway Search Trees Definition: A multiway search tree is a search tree that allows more than one key in the same node of the tree. Definition A node of a search tree is called an n-node if it contains n-1 ordered keys (which divide the entire key range into n intervals pointed to by the node s n links to its children): Note: Every node in a classical binary search tree is a 2-node 2-3 Trees Definition A 2-3 tree is a search tree that may have 2-nodes and 3-nodes is height-balanced (all leaves are on the same level)

CPS 616 TRANSFORM-AND-CONQUER 7-13 Extreme 2-3 Trees Max height when all nodes are 2-nodes 25 40 43 50 55 60 65 n = height 2 i i=0 = (2height+1 1) 2 1 = (2 height+1 1) 18 33 41 47 52 58 63 70 So height = log 2 (n+1)-1 Min height when all nodes are 3-nodes 50 100 20 40 150 200 5 10 45 48 110 130 210 215 25 30 65 85 160 180 55 60 90 95 70 75 height n = 2. 3 i = 2. (3height+1 1) = 3 1 i=0 (3 height+1 1) So height = log 3 (n+1)-1 Efficiency log 3 (n + 1) - 1 height log 2 (n + 1) - 1 Search, insertion, and deletion are in (log n)

CPS 616 TRANSFORM-AND-CONQUER 7-14 Construction: A 2-3 tree is constructed by successive insertions of keys given, with a new key always inserted into a leaf of the tree. If the leaf is a 3-node, it s split into two with the middle key promoted to the parent. The idea of 2-3 tree can be generalized by allowing more keys per node 2-3-4 trees B-trees However, 2-3 tree implementation is complicated. These trees can instead be implemented as red-black BSTs

CPS 616 TRANSFORM-AND-CONQUER 7-15 RED-BLACK TREES Definition 2-3 Trees can be represented as special BSTs using rules: Nodes and edges are either red or black A node is the same colour as its upper edge On any path from the root to a leaf the number of black edges is the same A red node that is not a leaf has 2 black children A black node that is not a leaf has either Two black children, or One child of each colour, or A single child which is a red leaf Conversion: A 3-node: 50 100 50 is converted into one of the two red-black trees 100 100 or 50 Examples A 2-3 Tree: M S Is converted into: M D F Q V F S A E H N O R T X Z D H Q V A E O R T X N Z Properties The height of the red-black tree is at most twice the height of the corresponding 2-3 tree. Therefore the logarithmic-time operations on 2-3 trees will be logarithmic time on the red-black implementations of those trees provided that each conversion operation (like splitting) can be done in constant time.

CPS 616 TRANSFORM-AND-CONQUER 7-16 REPRESENTATION CHANGE : POLYNOMIAL EVALUATION USING HORNER'S RULE Problem: (Handout 4) Horner's Rule Find the value of polynomial p(x) = a n x n + a n-1 x n-1 + + a 1 x 1 + a 0 at a point x = x 0 p(x) = a n x n + a n-1 x n-1 + + a 1 x 1 + a 0 = [(((a n x + a n-1 )x + a n-2 )x + a n-3 )x + + a 1 ] x + a 0 Algorithm: p a[n] for i n-1 downto 0 do p p x + a[i] return p Efficiency Example #multiplications = #additions = n p(x) = 2x 4 - x 3 + 3x 2 + x - 5 = (2x 3 - x 2 + 3x + 1) x - 5 = ((2x 2 - x + 3) x + 1) x - 5 = (((2x - 1) x + 3) x + 1) x 5 Arrange coefficients in table: 2-1 3 1-5 x=3 2x - 1 = 5 (2x - 1) x + 3 = 18 ((2x - 1) x + 3) x + 1) = 55 (((2x - 1) x + 3) x + 1) x 5 = 160

CPS 616 TRANSFORM-AND-CONQUER 7-17 REPRESENTATION CHANGE : SYNTHETIC DIVISION = POLYNOMIAL DIVISION USING HORNER'S RULE Problem: Divide p(x) = a n x n + a n-1 x n-1 + + a 1 x 1 + a 0 by (x - x 0 ) Result: p(x) = (x - x 0 ) q(x) + remainder where q(x) = b n-1 x n-1 + + b 1 x 1 + b 0 Remainder Theorem: If the polynomial p(x) is divided by x - x 0, then the remainder is p(x 0 ). Application (substitution of remainder): p(x) = (x - x 0 ) q(x) + p(x 0 ) Find coefficients b i 's of q(x) p(x) = (x - x 0 ) (b n-1 x n-1 + + b 1 x 1 + b 0 ) + p(x 0 ) = x.(b n-1 x n-1 + + b 1 x 1 + b 0 ) - x 0. (b n-1 x n-1 + + b 1 x 1 + b 0 ) + p(x 0 ) = x.(b n-1 x n-1 + + b 1 x 1 + b 0 ) - x 0. (b n-1 x n-1 + + b 1 x 1 ) - b 0 x 0 + p(x 0 ) = a n x n + a n-1 x n-1 + + a 1 x 1 + a 0 So a 0 = p(x 0 ) - b 0 x 0 So b 0 = (p(x 0 ) - a 0 )/x 0 Example: Apply Horner's Rule to p(x0): p(x 0 ) = a n x 0 n + a n-1 x 0 n-1 + + a 1 x 0 1 + a 0 = [(((a n x 0 + a n-1 ) x 0 + a n-2 ) x 0 + a n-3 ) x 0 + + a 1 ] x 0 + a 0 Substitute back into formula for b 0 : b 0 = (p(x 0 ) - a 0 )/x 0 =([[(((a n x 0 + a n-1 ) x 0 + a n-2 ) x 0 + a n-3 ) x 0 + + a 1 ] x 0 + a 0 ] - a 0 )/x 0 =([[(((a n x 0 + a n-1 ) x 0 + a n-2 ) x 0 + a n-3 ) x 0 + + a 1 ] x 0 )/x 0 =[[(((a n x 0 + a n-1 ) x 0 + a n-2 ) x 0 + a n-3 ) x 0 + + a 1 ] This is the one before last step in the calculation of p(x 0 ) using Horner's rule! divide p(x) = 2x 4 - x 3 + 3x 2 + x - 5 by x-3 The steps to calculate p(3) were: 2 (times 3 0 ), 5, 18, 55, 160 Result: 2x 4 - x 3 + 3x 2 + x - 5= (x-3)(2x 3 + 5x 2 + 18x + 55)+160 Algorithm p a[n] for i n-1 downto 0 do b[i] p p p x + a[i] remainder p

CPS 616 TRANSFORM-AND-CONQUER 7-18 PROBLEM REDUCTION - EXPLANATION This variation of transform-and-conquer solves a problem by a 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. PROBLEM REDUCTION - LEAST COMMON MULTIPLE The least common multiple of two positive integers m and n, denoted lcm(m,n), is the smallest integer that is divisible by both m and n, E.g. lcm(25,60) = 300 Theorem: m.n = gcd(m,n).lcm(m,n) and so lcm(m, n) = Algorithm: Calculate gcd(m,n) first using Euclidian Algorithm Apply formula above PROBLEM REDUCTION - COUNTING PATHS IN A GRAPH m.n gcd (m,n) Problem: given a graph, how many paths of size n does it have? Represent graph by adjacency matrix A Theorem: the number of paths of length n from vertex i to j is A n [i,j] Algorithm: take the power of A n times - answers are in matrix A n

CPS 616 TRANSFORM-AND-CONQUER 7-19 PROBLEM REDUCTION - REDUCTION TO GRAPHS River crossing puzzle: Peasant must take cabbage, goat, and wolf across river using one boat which can only take one passenger other than himself. In his absence, the wolf might eat the goat and the goat might eat the cabbage. Is there a way for the peasant to transport all 3 safely to the other side? http://www.transum.org/software/river_crossing/level1.asp Reduction to graph: Draw a state-space graph: vertices are possible states of the problem, edges indicate how to move from one state to the other. This is the same as a statetransition diagram. Solution: find a minimal path from Pwgc to Pwgc using BFS

CPS 616 TRANSFORM-AND-CONQUER 7-20 PROBLEM REDUCTION - OPTIMISATION PROBLEMS Problem: Find min (or max) of a function How to find either of them in calculus? solve for f'(x)=0 This is a problem reduction as well: min of a graph reduced to solving an equation