Professional Documents
Culture Documents
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?
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 6
6-4
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 6
6-14
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
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 6
6-16
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 6
6-17
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 6
6-18
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 6
6-19
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 6
6-20
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 6
6-21
< k1
[k1, k2 )
kn-1
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
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 6
6-24
-- The idea of 2-3 tree can be generalized by allowing more keys per node -- 2-3-4 trees -- B-trees
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 :-