Transform and Conquer
Transform and Conquer
TECH V SEM
CSE
ACADEMIC YEAR: 2023-2024
• Textbook
•Introduction to Algorithms 3rd ,Cormen, Leiserson, Rivest
and Stein, The MIT Press,
•Fundamentals of Computer Algorithms, 2nd, Sartaj Sahni,
Ellis Horowitz, Sanguthevar Rajasekaran
• Others
•Introduction to Design & Analysis Computer Algorithm 3rd,
Sara Baase, Allen Van Gelder, Adison-Wesley, 2000.
•Algorithms, Richard Johnsonbaugh, Marcus Schaefer, Prentice
Hall, 2004.
•Introduction to The Design and Analysis of Algorithms 2nd
Edition, Anany Levitin, Adison-Wesley, 2007.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 6 6-7
Instance simplification - Presorting
Solve a problem’s instance by transforming it into
another simpler/easier instance of the same problem
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)
Also:
Topological sorting helps solving some problems for dags.
Presorting is used in many geometric algorithms.
Presorting is a special case of preprocessing.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 6 6-8
How fast can we sort ?
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 6 6-9
Searching with presorting
Presorting-based algorithm:
Stage 1 Sort the array by an efficient sorting algorithm
Stage 2 Apply binary search
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-10
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: O(n2)
<K >K 5
3 10
1 7 12
Example: 5, 3, 1, 10, 12, 7, 9
9
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 6 6-12
Dictionary Operations on Binary Search Trees
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
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 6 6-13
Balanced Search Trees
Attractiveness of binary search tree is marred by the bad (linear)
worst-case efficiency. Two ideas to overcome it are:
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 6 6-14
Balanced trees: AVL trees
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)
1 2
10 10
0 1 0 0
5 20 5 20
1 -1 0 1 -1
4 7 12 4 7
0 0 0 0
2 8 2 8
(a) (b)
1 0 0 -1 0 0
R LR
2 > 1 3 1 > 1 3
0 0
1 2
(a) (c)
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 - an example
0
8
1 2 1
6 6 6
1 0 2 0 R (5) 0 0
5 8 5 8 > 3 8
0 1 0 0
3 3 2 5
0
2
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.)
2 0
6 5
-1 0 LR (6) 0 -1
3 8 > 3 6
0 1 0 0 0
2 5 2 4 8
0
4
-1 0
5 5
0 -2 0 0
3 6 3 7
RL (6)
0 0 1 > 0 0 0 0
2 4 8 2 4 6 8
0
7
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 6 6-20