You are on page 1of 25

Chapter 6

Transform and Conquer

Transform and Conquer

Presorting
Many Questions about a list is easier to answer if the list is sorted. So Presorting is an idea to sort the list before applying the actual method on that. for sorting list :- O(n log n) (Merge sort)

Example 1
Checking element uniqueness in an array: Brute force:- algorithm compared pairs of the arrays element until either two equal elements were found or no more pair remains. Worst case efficiency O(n2)

Presorting-based algorithm Stage 1: sort by efficient sorting algorithm (e.g. merge sort) Stage 2: scan array to check pairs of adjacent elements

Example 2
Computing a Mode:A mode is a value that occur most often in a given list of numbers. Brute Force:- Scan complete list and count occurrence. Worst case :- O(n2) for comparison and (n-1) for finding highest occurrence element.

Efficiency:- O(n lg n)

Example 3 :- Searching
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) Good or bad? Why do we have our dictionaries, telephone directories, etc. sorted?

Copyright 2007 Pearson Addison-Wesley. All rights reserved.

A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 6

6-4

Balanced Search Trees


Attractiveness of binary search tree is marred by the bad (linear) worst-case efficiency. Two ideas to overcome it are: -to rebalance binary search tree when a new insertion makes the tree too unbalanced --AVL trees --red-black trees -to allow more than one key and two children --2-3 trees --2-3-4 trees --B-trees

Copyright 2007 Pearson Addison-Wesley. All rights reserved.

A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 6

6-14

Binary Search Trees (BST)


A data structure for efficient searching, insertion and deletion Binary search tree property
For every node X All the keys in its left subtree are smaller than the key value in X All the keys in its right subtree are larger than the key value in X

Binary Search Trees

A binary search tree

Not a binary search tree

Balanced trees: AVL(Adelson-Velskii and Landis) Tree


Definition An AVL tree is a binary search tree in which, for every node, the difference between the heights of its left and right subtrees, called the balance factor, is at most 1 (with the height of an empty tree defined as -1)

Tree (a) is an AVL tree; tree (b) is not an AVL tree


Copyright 2007 Pearson Addison-Wesley. All rights reserved.

A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 6

6-15

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.)

Single R-rotation

Double LR-rotation

Copyright 2007 Pearson Addison-Wesley. All rights reserved.

A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 6

6-16

General case: Single R-rotation

Copyright 2007 Pearson Addison-Wesley. All rights reserved.

A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 6

6-17

General case: Double LR-rotation

Copyright 2007 Pearson Addison-Wesley. All rights reserved.

A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 6

6-18

AVL tree construction


Construct an AVL tree for the list 5, 6, 8, 3, 2, 4, 7

Copyright 2007 Pearson Addison-Wesley. All rights reserved.

A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 6

6-19

AVL tree construction - an example (cont.)

Copyright 2007 Pearson Addison-Wesley. All rights reserved.

A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 6

6-20

Analysis of AVL trees


N(h-1) + N(h-2) N(h) --Search and insertion are O(log n) --Deletion is more complicated but is also O(log n) --Disadvantages: --frequent rotations --complexity

Copyright 2007 Pearson Addison-Wesley. All rights reserved.

A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 6

6-21

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 n1 ordered keys (which divide the entire key range into n intervals pointed to by the nodes n links to its children):

Note: Every node in a classical binary search tree is a 2-node

k1 < k2 < < kn-1

< k1

[k1, k2 )

kn-1

Copyright 2007 Pearson Addison-Wesley. All rights reserved.

A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 6

6-22

2-3 Tree
Definition A 2-3 tree is a search tree that -- may have 2-nodes and 3-nodes -- height-balanced (all leaves are on the same level)

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, its split into two with the middle key promoted to the parent.
Copyright 2007 Pearson Addison-Wesley. All rights reserved.

A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 6

6-23

2-3 tree construction an example


Construct a 2-3 tree the list 9, 5, 8, 3, 2, 4, 7

Copyright 2007 Pearson Addison-Wesley. All rights reserved.

A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 6

6-24

Analysis of 2-3 trees


-- Search, insertion, and deletion are in (log n)

-- The idea of 2-3 tree can be generalized by allowing more keys per node -- 2-3-4 trees -- B-trees

Copyright 2007 Pearson Addison-Wesley. All rights reserved.

A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 6

6-25

Minimum Maximum
Problem:- How many comparisons are necessary to determine the minimum of a set of n elements?

No of Comparisons = n-1

Better way for finding Minimum and Maximum: Strategy: Rather than procession each elements of the input but comparing it against the current minimum and maximum, at a cost of 2 comparisons per elements. We process elements in pairs. Compare pairs of element from input first with each other, and then compare the smaller to the current minimum and the larger to the current maximum, at a cost of comparisons for every 2 elements. Initial values for current minimum and maximum depends on whether n is odd or even. If n is odd, than initial value is first element and than process the rest on pairs. If n is even than initial value is comparison of first 2 element and rest in pairs. No of comparison :-

You might also like