SlideShare a Scribd company logo
1 of 53
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
Transform and Conquer
This group of techniques solves a problem by a
transformation to
 a simpler/more convenient instance of the same
problem (instance simplification)
 a different representation of the same instance
(representation change)
 a different problem for which an algorithm is
already available (problem reduction)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 3
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, e.g.
 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.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 4
How fast can we sort ?
Efficiency of algorithms involving sorting depends on
efficiency of sorting.
Theorem (see Sec. 11.2): log2 n!  n log2 n comparisons are
necessary in the worst case to sort a list of size n by any
comparison-based algorithm.
Note: About nlog2 n comparisons are also sufficient to sort array
of size n (by mergesort).
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 5
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)
Good or bad?
Why do we have our dictionaries, telephone directories, etc.
sorted?
Presort: Element Uniqueness
 ALGORITHM PresortElementUniqueness(A[0..n − 1])
 //Solves the element uniqueness problem by sorting the array
first
 //Input: An array A[0..n − 1] of orderable elements
 //Output: Returns “true” if A has no equal elements, “false”
otherwise
 sort the array A
 for i ←0 to n − 2 do
 if A[i]= A[i + 1] return false
 return true
 ##########################################
 T (n) = Tsort(n) + Tscan(n) ∈ (n log n) + (n) = (n log n).
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 6
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 7
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)
 Another algorithm? Hashing
Presort: Computing a mode
 A mode:
 is a value that occurs most often in a given list of numbers.
For example, for 5, 1, 5, 7, 6, 5, 7, the mode is 5.
 (If several different values occur most often, any of them can
be considered a mode.)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 8
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Instance simplification – 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.
a11x1 + a12x2 + … + a1nxn = b1 a1,1x1+ a12x2 + … + a1nxn = b1
a21x1 + a22x2 + … + a2nxn = b2 a22x2 + … + a2nxn = b2
an1x1 + an2x2 + … + annxn = bn annxn = bn
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Gaussian Elimination (cont.)
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
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Example of Gaussian Elimination
Solve 2x1 - 4x2 + x3 = 6
3x1 - x2 + x3 = 11
x1 + x2 - x3 = -3
Gaussian elimination
2 -4 1 6 2 -4 1 6
3 -1 1 11 row2 – (3/2)*row1 0 5 -1/2 2
1 1 -1 -3 row3 – (1/2)*row1 0 3 -3/2 -6 row3–(3/5)*row2
2 -4 1 6
0 5 -1/2 2
0 0 -6/5 -36/5
Backward substitution
x3 = (-36/5) / (-6/5) = 6
x2 = (2+(1/2)*6) / 5 = 1
x1 = (6 – 6 + 4*1)/2 = 2
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Pseudocode and Efficiency of Gaussian Elimination
Stage 1: Reduction to an upper-triangular matrix
for i ← 1 to n-1 do
for j ← i+1 to n do
for k ← i to n+1 do
A[j, k] ← A[j, k] - A[i, k] * A[j, i] / A[i, i] //improve!
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]
Efficiency: Θ(n3) + Θ(n2) = Θ(n3)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 13
Searching Problem
Problem: Given a (multi)set S of keys and a search
key K, find an occurrence of K in S, if any
 Searching must be considered in the context of:
• file size (internal vs. external)
• dynamics of data (static vs. dynamic)
 Dictionary operations (dynamic data):
• find (search)
• insert
• delete
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 14
Taxonomy of Searching Algorithms
 List searching
• sequential search
• binary search
• interpolation search
 Tree searching
• binary search tree
• binary balanced trees: AVL trees, red-black trees
• multiway balanced trees: 2-3 trees, 2-3-4 trees, B trees
 Hashing
• open hashing (separate chaining)
• closed hashing (open addressing)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 15
Binary Search Tree
Arrange keys in a binary tree with the binary search
tree property:
K
<K >K
Example: 5, 3, 1, 10, 12, 7, 9
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 16
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
Efficiency depends of the tree’s height: log2 n  h  n-1,
with height average (random files) be about 3log2 n
Thus all three operations have
• worst case efficiency: (n)
• average case efficiency: (log n)
Bonus: inorder traversal produces sorted list
Balanced vs. Unbalanced Binary Search Tree
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 17
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 18
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
• G. M. Adelson-Velsky and E. M. Landis
• red-black trees
 to allow more than one key per node of a search tree
• 2-3 trees
• 2-3-4 trees
• B-trees
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 19
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)
5 20
124 7
2
(a)
10
1
8
10
1
0
-1
0
0
5 20
4 7
2
(b)
10
2
8
00
1
0
-1
0
Tree (a) is an AVL tree; tree (b) is not an AVL tree
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 20
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.)
3
2
2
1
1
0
2
0
1
0
3
0
>
R
(a)
3
2
1
-1
2
0
2
0
1
0
3
0
>
LR
(c)
Single R-rotation Double LR-rotation
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 21
General case: Single R-rotation
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 22
General case: Double LR-rotation
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 23
AVL tree construction - an example
Construct an AVL tree for the list 5, 6, 8, 3, 2, 4, 7
5
-1
6
0
5
0
5
-2
6
-1
8
0
>
6
0
8
0
5
0
L(5)
6
1
5
1
3
0
8
0
6
2
5
2
3
1
2
0
8
0
>
R (5)
6
1
3
0
2
0
8
0
5
0
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 24
AVL tree construction - an example (cont.)
6
2
3
-1
2
0
5
1
4
0
8
0
>
LR (6)
5
0
3
0
2
0
4
0
6
-1
8
0
5
-1
3
0
2
0
4
0
6
-2
8
1
7
0
>
RL (6)
5
0
3
0
2
0
4
0
7
0
8
0
6
0
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 25
Analysis of AVL trees
 h  1.4404 log2 (n + 2) - 1.3277
average height: 1.01 log2n + 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
• complexity
 A similar idea: red-black trees (height of subtrees is allowed to
differ by up to a factor of 2)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 26
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
k1 < k2 < … < kn-1
< k1 [k1, k2 )  kn-1
 The most number of chidren is n ,the ,most number of keys
is n-1
 Node 1 node 2 node3 node 4
 N=4
 4 nodes
 3 keys
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 27
Key1 key2 key3
 Insert P DG H M E A C F S Y N I K L
 In order 4
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 28
P D G D P G D G P
D G P
I K L
S YA C E F H M N
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 29
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, it’s split into two with the middle key promoted to
the parent.
K K , K1 2
(K , K )1 2
2-node 3-node
< K > K< K > K 1 2
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 30
2-3 tree construction – an example
Construct a 2-3 tree the list 9, 5, 8, 3, 2, 4, 7
9
>
8
955, 9 5, 8, 9
8
93, 5
2, 3, 5
8
9
>
>
3, 8
92 5
3, 8
92 4, 5
3, 8
4, 5, 72 9
> 3, 5, 8
2 4 7 9
5
3
42
8
97
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 31
Analysis of 2-3 trees
 log3 (n + 1) - 1  h  log2 (n + 1) - 1
 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
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 32
Heaps and Heapsort
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
 1. There exists exactly one essentially complete binary tree
with n nodes. Its height is equal to log2 n .
 2. The root of a heap always contains its largest element.
 3. A node of a heap considered with all its descendants is
also a heap.
 4. A heap can be implemented as an array by recording its
elements in the topdown, left-to-right fashion. It is
convenient to store the heap’s elements in positions 1
through n of such an array, leaving H[0]either unused or
putting
thereasentinelwhosevalueisgreaterthaneveryelementinthehe
ap.Insuch a representation,
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 33
 Max heap properties:
 1-Value of any node is greater than its each child.
 2-It is balanced. Leaf nodes are at left.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 34
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 35
Illustration of the heap’s definition
10
5
4 2
7
1
10
5
2
7
1
10
5
6 2
7
1
a heap not a heap not a heap
Note: Heap’s elements are ordered top down (along any path
down from its root), but they are not ordered left to right
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 36
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
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 37
Heap’s Array Representation
Store heap’s elements in an array (whose elements indexed,
for convenience, 1 to n) in top-down left-to-right order
Example:
 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
 Parental nodes are represented in the first n/2 locations
9
1
5 3
4 2
1 2 3 4 5 6
9 5 3 1 4 2
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 38
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)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 39
Example of Heap Construction
7
2
9
6 5 8
>
2
9
6 5
8
7
2
9
6 5
8
7
2
9
6 5
8
7
>
9
2
6 5
8
7
9
6
2 5
8
7
>
Construct a heap for the list 2, 9, 7, 6, 5, 8
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 40
Pseudopodia of bottom-up heap construction
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 41
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
Heapsort
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 42
Sort the list 2, 9, 7, 6, 5, 8 by heapsort
Stage 1 (heap construction) Stage 2 (root/max removal)
1 9 7 6 5 8 9 6 8 2 5 7
2 9 8 6 5 7 7 6 8 2 5 | 9
2 9 8 6 5 7 8 6 7 2 5 | 9
9 2 8 6 5 7 5 6 7 2 | 8 9
9 6 8 2 5 7 7 6 5 2 | 8 9
2 6 5 | 7 8 9
6 2 5 | 7 8 9
5 2 | 6 7 8 9
5 2 | 6 7 8 9
2 | 5 6 7 8 9
Example of Sorting by Heapsort
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 43
Stage 1: Build heap for a given list of n keys
worst-case
C(n) =
Stage 2: Repeat operation of root removal n-1 times (fix heap)
worst-case
C(n) =
Both worst-case and average-case efficiency: (nlogn)
In-place: yes
Stability: no (e.g., 1 1)
 2(h-i) 2i = 2 ( n – log2(n + 1))  (n)
i=0
h-1
# nodes at
level i

i=1
n-1
2log2 i  (nlogn)
Analysis of Heapsort
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 44
A priority queue is the ADT of a set of elements with
numerical priorities with the following operations:
• find element with highest priority
• delete element with highest priority
• insert element with assigned priority (see below)
 Heap is a very efficient way for implementing priority queues
 Two ways to handle priority queue in which
highest priority = smallest number
Priority Queue
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 45
Insertion of a New Element into a Heap
 Insert the new element at last position in heap.
 Compare it with its parent and, if it violates heap condition,
exchange them
 Continue comparing the new element with nodes up the tree
until the heap condition is satisfied
Example: Insert key 10
Efficiency: O(log n)
9
6
2 5
8
7 10
9
6
2 5
10
7 8
> >
10
6
2 5
9
7 8
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 46
Horner’s Rule For Polynomial Evaluation
Given a polynomial of degree n
p(x) = anxn + an-1xn-1 + … + a1x + a0
and a specific value of x, find the value of p at that point.
Two brute-force algorithms:
p  0 p  a0; power  1
for i  n downto 0 do for i  1 to n do
power  1 power  power * x
for j  1 to i do p  p + ai * power
power  power * x return p
p  p + ai * power
return p
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 47
Horner’s Rule
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
Substitution into the last formula leads to a faster algorithm
Same sequence of computations are obtained by simply
arranging the coefficient in a table and proceeding as follows:
coefficients 2 -1 3 1 -5
x=3
 x(x(x(2x - 1) + 3) + 1) – 5
 x(x (x (2x - 1) + 3) + 1) – 5 =2*3-1 =5
 x(x (x (2x - 1) + 3) + 1) – 5 = 3*5+3=18
 x (x (x (2x - 1) + 3) + 1) – 5 =3*18+1=55
 x(x (x (2x - 1) + 3) + 1) – 5 = 3*55-5=16
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 48
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 49
Horner’s Rule pseudocode
Efficiency of Horner’s Rule: # multiplications = # additions = n
Synthetic division of of p(x) by (x-x0)
Example: Let p(x) = 2x4 - x3 + 3x2 + x - 5. Find p(x):(x-3)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 50
Computing an (revisited)
Left-to-right binary exponentiation
Initialize product accumulator by 1.
Scan n’s binary expansion from left to right and do the
following:
If the current binary digit is 0, square the accumulator (S);
if the binary digit is 1, square the accumulator and multiply it
by a (SM).
Example: Compute a13. Here, n = 13 = 11012
binary rep. of 13: 1 1 0 1
SM SM S SM
accumulator: 1 12*a=a a2*a = a3 (a3)2 = a6 (a6)2*a= a13
(computed left-to-right)
Efficiency: b ≤ M(n) ≤ 2b where b = log2 n + 1
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 51
Computing an (cont.)
Right-to-left binary exponentiation
Scan n’s binary expansion from right to left and compute an as
the product of terms a2 i
corresponding to 1’s in this expansion.
Example Compute a13 by the right-to-left binary exponentiation.
Here, n = 13 = 11012.
1 1 0 1
a8 a4 a2 a : a2 i
terms
a8 * a4 * a : product
(computed right-to-left)
Efficiency: same as that of left-to-right binary exponentiation
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 52
Problem Reduction
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.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 53
Examples of Solving Problems by 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)
 linear programming
 reduction to graph problems (e.g., solving puzzles via state-
space graphs)

More Related Content

What's hot (20)

Kernel Method
Kernel MethodKernel Method
Kernel Method
 
Data Structures Chapter-4
Data Structures Chapter-4Data Structures Chapter-4
Data Structures Chapter-4
 
Kruskal's algorithm
Kruskal's algorithmKruskal's algorithm
Kruskal's algorithm
 
Matrix Groups and Symmetry
Matrix Groups and SymmetryMatrix Groups and Symmetry
Matrix Groups and Symmetry
 
Clustering
ClusteringClustering
Clustering
 
Graph mining ppt
Graph mining pptGraph mining ppt
Graph mining ppt
 
DFS and BFS
DFS and BFSDFS and BFS
DFS and BFS
 
Artificial Intelligence - Game of Nim
Artificial Intelligence - Game of NimArtificial Intelligence - Game of Nim
Artificial Intelligence - Game of Nim
 
UNIT III NON LINEAR DATA STRUCTURES – TREES
UNIT III 	NON LINEAR DATA STRUCTURES – TREESUNIT III 	NON LINEAR DATA STRUCTURES – TREES
UNIT III NON LINEAR DATA STRUCTURES – TREES
 
Data Mining: Mining ,associations, and correlations
Data Mining: Mining ,associations, and correlationsData Mining: Mining ,associations, and correlations
Data Mining: Mining ,associations, and correlations
 
Branch & bound
Branch & boundBranch & bound
Branch & bound
 
Trees and graphs
Trees and graphsTrees and graphs
Trees and graphs
 
5.5 back track
5.5 back track5.5 back track
5.5 back track
 
Artificial Intelligence -- Search Algorithms
Artificial Intelligence-- Search Algorithms Artificial Intelligence-- Search Algorithms
Artificial Intelligence -- Search Algorithms
 
Machine learning clustering
Machine learning clusteringMachine learning clustering
Machine learning clustering
 
DFS & BFS Graph
DFS & BFS GraphDFS & BFS Graph
DFS & BFS Graph
 
Unsupervised learning
Unsupervised learningUnsupervised learning
Unsupervised learning
 
Red black tree
Red black treeRed black tree
Red black tree
 
Recurrences
RecurrencesRecurrences
Recurrences
 
Gauss elimination
Gauss eliminationGauss elimination
Gauss elimination
 

Similar to Ch6 transform and conquer

ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02
ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02
ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02Shanmuganathan C
 
ch03-2018.02.02.pptx
ch03-2018.02.02.pptxch03-2018.02.02.pptx
ch03-2018.02.02.pptxSYAMDAVULURI
 
Algo-Exercises-2-hash-AVL-Tree.ppt
Algo-Exercises-2-hash-AVL-Tree.pptAlgo-Exercises-2-hash-AVL-Tree.ppt
Algo-Exercises-2-hash-AVL-Tree.pptHebaSamy22
 
lecture 1(Fundamental of algorithms).pptx
lecture 1(Fundamental of algorithms).pptxlecture 1(Fundamental of algorithms).pptx
lecture 1(Fundamental of algorithms).pptxTejaYadav27
 
Anti-differentiating Approximation Algorithms: PageRank and MinCut
Anti-differentiating Approximation Algorithms: PageRank and MinCutAnti-differentiating Approximation Algorithms: PageRank and MinCut
Anti-differentiating Approximation Algorithms: PageRank and MinCutDavid Gleich
 
Fast relaxation methods for the matrix exponential
Fast relaxation methods for the matrix exponential Fast relaxation methods for the matrix exponential
Fast relaxation methods for the matrix exponential David Gleich
 
ch08-2019-03-27 (1).ppt
ch08-2019-03-27 (1).pptch08-2019-03-27 (1).ppt
ch08-2019-03-27 (1).pptSaimaShaheen14
 
Understanding the Machine Learning Algorithms
Understanding the Machine Learning AlgorithmsUnderstanding the Machine Learning Algorithms
Understanding the Machine Learning AlgorithmsRupak Roy
 
Mncs 16-10-1주-변승규-introduction to the machine learning #2
Mncs 16-10-1주-변승규-introduction to the machine learning #2Mncs 16-10-1주-변승규-introduction to the machine learning #2
Mncs 16-10-1주-변승규-introduction to the machine learning #2Seung-gyu Byeon
 
Introduction to Supervised ML Concepts and Algorithms
Introduction to Supervised ML Concepts and AlgorithmsIntroduction to Supervised ML Concepts and Algorithms
Introduction to Supervised ML Concepts and AlgorithmsNBER
 
MODULE_05-Matrix Decomposition.pptx
MODULE_05-Matrix Decomposition.pptxMODULE_05-Matrix Decomposition.pptx
MODULE_05-Matrix Decomposition.pptxAlokSingh205089
 
Data Mining Lecture_10(b).pptx
Data Mining Lecture_10(b).pptxData Mining Lecture_10(b).pptx
Data Mining Lecture_10(b).pptxSubrata Kumer Paul
 
Lecture 11 diagonalization & complex eigenvalues - 5-3 & 5-5
Lecture  11   diagonalization & complex eigenvalues -  5-3 & 5-5Lecture  11   diagonalization & complex eigenvalues -  5-3 & 5-5
Lecture 11 diagonalization & complex eigenvalues - 5-3 & 5-5njit-ronbrown
 

Similar to Ch6 transform and conquer (20)

ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02
ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02
ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02
 
ch03-2018.02.02.pptx
ch03-2018.02.02.pptxch03-2018.02.02.pptx
ch03-2018.02.02.pptx
 
ch11-04-27-15.ppt
ch11-04-27-15.pptch11-04-27-15.ppt
ch11-04-27-15.ppt
 
Algo-Exercises-2-hash-AVL-Tree.ppt
Algo-Exercises-2-hash-AVL-Tree.pptAlgo-Exercises-2-hash-AVL-Tree.ppt
Algo-Exercises-2-hash-AVL-Tree.ppt
 
lecture 1(Fundamental of algorithms).pptx
lecture 1(Fundamental of algorithms).pptxlecture 1(Fundamental of algorithms).pptx
lecture 1(Fundamental of algorithms).pptx
 
Ch04n
Ch04nCh04n
Ch04n
 
Anti-differentiating Approximation Algorithms: PageRank and MinCut
Anti-differentiating Approximation Algorithms: PageRank and MinCutAnti-differentiating Approximation Algorithms: PageRank and MinCut
Anti-differentiating Approximation Algorithms: PageRank and MinCut
 
Fast relaxation methods for the matrix exponential
Fast relaxation methods for the matrix exponential Fast relaxation methods for the matrix exponential
Fast relaxation methods for the matrix exponential
 
ch08-2019-03-27 (1).ppt
ch08-2019-03-27 (1).pptch08-2019-03-27 (1).ppt
ch08-2019-03-27 (1).ppt
 
5261506.ppt
5261506.ppt5261506.ppt
5261506.ppt
 
nber_slides.pdf
nber_slides.pdfnber_slides.pdf
nber_slides.pdf
 
Understanding the Machine Learning Algorithms
Understanding the Machine Learning AlgorithmsUnderstanding the Machine Learning Algorithms
Understanding the Machine Learning Algorithms
 
Mncs 16-10-1주-변승규-introduction to the machine learning #2
Mncs 16-10-1주-변승규-introduction to the machine learning #2Mncs 16-10-1주-변승규-introduction to the machine learning #2
Mncs 16-10-1주-변승규-introduction to the machine learning #2
 
Introduction to Supervised ML Concepts and Algorithms
Introduction to Supervised ML Concepts and AlgorithmsIntroduction to Supervised ML Concepts and Algorithms
Introduction to Supervised ML Concepts and Algorithms
 
Paper3a
Paper3aPaper3a
Paper3a
 
MODULE_05-Matrix Decomposition.pptx
MODULE_05-Matrix Decomposition.pptxMODULE_05-Matrix Decomposition.pptx
MODULE_05-Matrix Decomposition.pptx
 
Eu33884888
Eu33884888Eu33884888
Eu33884888
 
Eu33884888
Eu33884888Eu33884888
Eu33884888
 
Data Mining Lecture_10(b).pptx
Data Mining Lecture_10(b).pptxData Mining Lecture_10(b).pptx
Data Mining Lecture_10(b).pptx
 
Lecture 11 diagonalization & complex eigenvalues - 5-3 & 5-5
Lecture  11   diagonalization & complex eigenvalues -  5-3 & 5-5Lecture  11   diagonalization & complex eigenvalues -  5-3 & 5-5
Lecture 11 diagonalization & complex eigenvalues - 5-3 & 5-5
 

Recently uploaded

Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)amitlee9823
 
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...amitlee9823
 
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...Call Girls in Nagpur High Profile
 
Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...amitlee9823
 
Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Call Girls in Nagpur High Profile
 
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)amitlee9823
 
infant assessment fdbbdbdddinal ppt.pptx
infant assessment fdbbdbdddinal ppt.pptxinfant assessment fdbbdbdddinal ppt.pptx
infant assessment fdbbdbdddinal ppt.pptxsuhanimunjal27
 
Design Inspiration for College by Slidesgo.pptx
Design Inspiration for College by Slidesgo.pptxDesign Inspiration for College by Slidesgo.pptx
Design Inspiration for College by Slidesgo.pptxTusharBahuguna2
 
Top Rated Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
Top Rated  Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...Top Rated  Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
Top Rated Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...Call Girls in Nagpur High Profile
 
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...kumaririma588
 
CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service 🧵
CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service  🧵CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service  🧵
CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service 🧵anilsa9823
 
Tapestry Clothing Brands: Collapsing the Funnel
Tapestry Clothing Brands: Collapsing the FunnelTapestry Clothing Brands: Collapsing the Funnel
Tapestry Clothing Brands: Collapsing the Funneljen_giacalone
 
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...Delhi Call girls
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...
UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...
UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...RitikaRoy32
 
Stark Industries Marketing Plan (1).pptx
Stark Industries Marketing Plan (1).pptxStark Industries Marketing Plan (1).pptx
Stark Industries Marketing Plan (1).pptxjeswinjees
 
SD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxSD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxjanettecruzeiro1
 
The history of music videos a level presentation
The history of music videos a level presentationThe history of music videos a level presentation
The history of music videos a level presentationamedia6
 
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...Pooja Nehwal
 

Recently uploaded (20)

Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
 
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
 
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
 
Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
 
Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
 
young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
 
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
 
infant assessment fdbbdbdddinal ppt.pptx
infant assessment fdbbdbdddinal ppt.pptxinfant assessment fdbbdbdddinal ppt.pptx
infant assessment fdbbdbdddinal ppt.pptx
 
Design Inspiration for College by Slidesgo.pptx
Design Inspiration for College by Slidesgo.pptxDesign Inspiration for College by Slidesgo.pptx
Design Inspiration for College by Slidesgo.pptx
 
Top Rated Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
Top Rated  Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...Top Rated  Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
Top Rated Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
 
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...
 
CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service 🧵
CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service  🧵CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service  🧵
CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service 🧵
 
Tapestry Clothing Brands: Collapsing the Funnel
Tapestry Clothing Brands: Collapsing the FunnelTapestry Clothing Brands: Collapsing the Funnel
Tapestry Clothing Brands: Collapsing the Funnel
 
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...
UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...
UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...
 
Stark Industries Marketing Plan (1).pptx
Stark Industries Marketing Plan (1).pptxStark Industries Marketing Plan (1).pptx
Stark Industries Marketing Plan (1).pptx
 
SD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxSD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptx
 
The history of music videos a level presentation
The history of music videos a level presentationThe history of music videos a level presentation
The history of music videos a level presentation
 
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
 

Ch6 transform and conquer

  • 1. 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 Transform and Conquer This group of techniques solves a problem by a transformation to  a simpler/more convenient instance of the same problem (instance simplification)  a different representation of the same instance (representation change)  a different problem for which an algorithm is already available (problem reduction)
  • 2. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2
  • 3. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 3 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, e.g.  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.
  • 4. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 4 How fast can we sort ? Efficiency of algorithms involving sorting depends on efficiency of sorting. Theorem (see Sec. 11.2): log2 n!  n log2 n comparisons are necessary in the worst case to sort a list of size n by any comparison-based algorithm. Note: About nlog2 n comparisons are also sufficient to sort array of size n (by mergesort).
  • 5. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 5 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) Good or bad? Why do we have our dictionaries, telephone directories, etc. sorted?
  • 6. Presort: Element Uniqueness  ALGORITHM PresortElementUniqueness(A[0..n − 1])  //Solves the element uniqueness problem by sorting the array first  //Input: An array A[0..n − 1] of orderable elements  //Output: Returns “true” if A has no equal elements, “false” otherwise  sort the array A  for i ←0 to n − 2 do  if A[i]= A[i + 1] return false  return true  ##########################################  T (n) = Tsort(n) + Tscan(n) ∈ (n log n) + (n) = (n log n). A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 6
  • 7. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 7 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)  Another algorithm? Hashing
  • 8. Presort: Computing a mode  A mode:  is a value that occurs most often in a given list of numbers. For example, for 5, 1, 5, 7, 6, 5, 7, the mode is 5.  (If several different values occur most often, any of them can be considered a mode.) A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 8
  • 9. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Instance simplification – 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. a11x1 + a12x2 + … + a1nxn = b1 a1,1x1+ a12x2 + … + a1nxn = b1 a21x1 + a22x2 + … + a2nxn = b2 a22x2 + … + a2nxn = b2 an1x1 + an2x2 + … + annxn = bn annxn = bn
  • 10. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Gaussian Elimination (cont.) 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
  • 11. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Example of Gaussian Elimination Solve 2x1 - 4x2 + x3 = 6 3x1 - x2 + x3 = 11 x1 + x2 - x3 = -3 Gaussian elimination 2 -4 1 6 2 -4 1 6 3 -1 1 11 row2 – (3/2)*row1 0 5 -1/2 2 1 1 -1 -3 row3 – (1/2)*row1 0 3 -3/2 -6 row3–(3/5)*row2 2 -4 1 6 0 5 -1/2 2 0 0 -6/5 -36/5 Backward substitution x3 = (-36/5) / (-6/5) = 6 x2 = (2+(1/2)*6) / 5 = 1 x1 = (6 – 6 + 4*1)/2 = 2
  • 12. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Pseudocode and Efficiency of Gaussian Elimination Stage 1: Reduction to an upper-triangular matrix for i ← 1 to n-1 do for j ← i+1 to n do for k ← i to n+1 do A[j, k] ← A[j, k] - A[i, k] * A[j, i] / A[i, i] //improve! 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] Efficiency: Θ(n3) + Θ(n2) = Θ(n3)
  • 13. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 13 Searching Problem Problem: Given a (multi)set S of keys and a search key K, find an occurrence of K in S, if any  Searching must be considered in the context of: • file size (internal vs. external) • dynamics of data (static vs. dynamic)  Dictionary operations (dynamic data): • find (search) • insert • delete
  • 14. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 14 Taxonomy of Searching Algorithms  List searching • sequential search • binary search • interpolation search  Tree searching • binary search tree • binary balanced trees: AVL trees, red-black trees • multiway balanced trees: 2-3 trees, 2-3-4 trees, B trees  Hashing • open hashing (separate chaining) • closed hashing (open addressing)
  • 15. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 15 Binary Search Tree Arrange keys in a binary tree with the binary search tree property: K <K >K Example: 5, 3, 1, 10, 12, 7, 9
  • 16. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 16 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 Efficiency depends of the tree’s height: log2 n  h  n-1, with height average (random files) be about 3log2 n Thus all three operations have • worst case efficiency: (n) • average case efficiency: (log n) Bonus: inorder traversal produces sorted list
  • 17. Balanced vs. Unbalanced Binary Search Tree A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 17
  • 18. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 18 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 • G. M. Adelson-Velsky and E. M. Landis • red-black trees  to allow more than one key per node of a search tree • 2-3 trees • 2-3-4 trees • B-trees
  • 19. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 19 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) 5 20 124 7 2 (a) 10 1 8 10 1 0 -1 0 0 5 20 4 7 2 (b) 10 2 8 00 1 0 -1 0 Tree (a) is an AVL tree; tree (b) is not an AVL tree
  • 20. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 20 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.) 3 2 2 1 1 0 2 0 1 0 3 0 > R (a) 3 2 1 -1 2 0 2 0 1 0 3 0 > LR (c) Single R-rotation Double LR-rotation
  • 21. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 21 General case: Single R-rotation
  • 22. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 22 General case: Double LR-rotation
  • 23. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 23 AVL tree construction - an example Construct an AVL tree for the list 5, 6, 8, 3, 2, 4, 7 5 -1 6 0 5 0 5 -2 6 -1 8 0 > 6 0 8 0 5 0 L(5) 6 1 5 1 3 0 8 0 6 2 5 2 3 1 2 0 8 0 > R (5) 6 1 3 0 2 0 8 0 5 0
  • 24. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 24 AVL tree construction - an example (cont.) 6 2 3 -1 2 0 5 1 4 0 8 0 > LR (6) 5 0 3 0 2 0 4 0 6 -1 8 0 5 -1 3 0 2 0 4 0 6 -2 8 1 7 0 > RL (6) 5 0 3 0 2 0 4 0 7 0 8 0 6 0
  • 25. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 25 Analysis of AVL trees  h  1.4404 log2 (n + 2) - 1.3277 average height: 1.01 log2n + 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 • complexity  A similar idea: red-black trees (height of subtrees is allowed to differ by up to a factor of 2)
  • 26. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 26 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 k1 < k2 < … < kn-1 < k1 [k1, k2 )  kn-1
  • 27.  The most number of chidren is n ,the ,most number of keys is n-1  Node 1 node 2 node3 node 4  N=4  4 nodes  3 keys A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 27 Key1 key2 key3
  • 28.  Insert P DG H M E A C F S Y N I K L  In order 4 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 28 P D G D P G D G P D G P I K L S YA C E F H M N
  • 29. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 29 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, it’s split into two with the middle key promoted to the parent. K K , K1 2 (K , K )1 2 2-node 3-node < K > K< K > K 1 2
  • 30. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 30 2-3 tree construction – an example Construct a 2-3 tree the list 9, 5, 8, 3, 2, 4, 7 9 > 8 955, 9 5, 8, 9 8 93, 5 2, 3, 5 8 9 > > 3, 8 92 5 3, 8 92 4, 5 3, 8 4, 5, 72 9 > 3, 5, 8 2 4 7 9 5 3 42 8 97
  • 31. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 31 Analysis of 2-3 trees  log3 (n + 1) - 1  h  log2 (n + 1) - 1  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
  • 32. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 32 Heaps and Heapsort 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
  • 33.  1. There exists exactly one essentially complete binary tree with n nodes. Its height is equal to log2 n .  2. The root of a heap always contains its largest element.  3. A node of a heap considered with all its descendants is also a heap.  4. A heap can be implemented as an array by recording its elements in the topdown, left-to-right fashion. It is convenient to store the heap’s elements in positions 1 through n of such an array, leaving H[0]either unused or putting thereasentinelwhosevalueisgreaterthaneveryelementinthehe ap.Insuch a representation, A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 33
  • 34.  Max heap properties:  1-Value of any node is greater than its each child.  2-It is balanced. Leaf nodes are at left. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 34
  • 35. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 35 Illustration of the heap’s definition 10 5 4 2 7 1 10 5 2 7 1 10 5 6 2 7 1 a heap not a heap not a heap Note: Heap’s elements are ordered top down (along any path down from its root), but they are not ordered left to right
  • 36. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 36 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
  • 37. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 37 Heap’s Array Representation Store heap’s elements in an array (whose elements indexed, for convenience, 1 to n) in top-down left-to-right order Example:  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  Parental nodes are represented in the first n/2 locations 9 1 5 3 4 2 1 2 3 4 5 6 9 5 3 1 4 2
  • 38. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 38 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)
  • 39. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 39 Example of Heap Construction 7 2 9 6 5 8 > 2 9 6 5 8 7 2 9 6 5 8 7 2 9 6 5 8 7 > 9 2 6 5 8 7 9 6 2 5 8 7 > Construct a heap for the list 2, 9, 7, 6, 5, 8
  • 40. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 40 Pseudopodia of bottom-up heap construction
  • 41. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 41 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 Heapsort
  • 42. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 42 Sort the list 2, 9, 7, 6, 5, 8 by heapsort Stage 1 (heap construction) Stage 2 (root/max removal) 1 9 7 6 5 8 9 6 8 2 5 7 2 9 8 6 5 7 7 6 8 2 5 | 9 2 9 8 6 5 7 8 6 7 2 5 | 9 9 2 8 6 5 7 5 6 7 2 | 8 9 9 6 8 2 5 7 7 6 5 2 | 8 9 2 6 5 | 7 8 9 6 2 5 | 7 8 9 5 2 | 6 7 8 9 5 2 | 6 7 8 9 2 | 5 6 7 8 9 Example of Sorting by Heapsort
  • 43. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 43 Stage 1: Build heap for a given list of n keys worst-case C(n) = Stage 2: Repeat operation of root removal n-1 times (fix heap) worst-case C(n) = Both worst-case and average-case efficiency: (nlogn) In-place: yes Stability: no (e.g., 1 1)  2(h-i) 2i = 2 ( n – log2(n + 1))  (n) i=0 h-1 # nodes at level i  i=1 n-1 2log2 i  (nlogn) Analysis of Heapsort
  • 44. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 44 A priority queue is the ADT of a set of elements with numerical priorities with the following operations: • find element with highest priority • delete element with highest priority • insert element with assigned priority (see below)  Heap is a very efficient way for implementing priority queues  Two ways to handle priority queue in which highest priority = smallest number Priority Queue
  • 45. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 45 Insertion of a New Element into a Heap  Insert the new element at last position in heap.  Compare it with its parent and, if it violates heap condition, exchange them  Continue comparing the new element with nodes up the tree until the heap condition is satisfied Example: Insert key 10 Efficiency: O(log n) 9 6 2 5 8 7 10 9 6 2 5 10 7 8 > > 10 6 2 5 9 7 8
  • 46. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 46 Horner’s Rule For Polynomial Evaluation Given a polynomial of degree n p(x) = anxn + an-1xn-1 + … + a1x + a0 and a specific value of x, find the value of p at that point. Two brute-force algorithms: p  0 p  a0; power  1 for i  n downto 0 do for i  1 to n do power  1 power  power * x for j  1 to i do p  p + ai * power power  power * x return p p  p + ai * power return p
  • 47. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 47 Horner’s Rule 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 Substitution into the last formula leads to a faster algorithm Same sequence of computations are obtained by simply arranging the coefficient in a table and proceeding as follows: coefficients 2 -1 3 1 -5 x=3
  • 48.  x(x(x(2x - 1) + 3) + 1) – 5  x(x (x (2x - 1) + 3) + 1) – 5 =2*3-1 =5  x(x (x (2x - 1) + 3) + 1) – 5 = 3*5+3=18  x (x (x (2x - 1) + 3) + 1) – 5 =3*18+1=55  x(x (x (2x - 1) + 3) + 1) – 5 = 3*55-5=16 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 48
  • 49. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 49 Horner’s Rule pseudocode Efficiency of Horner’s Rule: # multiplications = # additions = n Synthetic division of of p(x) by (x-x0) Example: Let p(x) = 2x4 - x3 + 3x2 + x - 5. Find p(x):(x-3)
  • 50. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 50 Computing an (revisited) Left-to-right binary exponentiation Initialize product accumulator by 1. Scan n’s binary expansion from left to right and do the following: If the current binary digit is 0, square the accumulator (S); if the binary digit is 1, square the accumulator and multiply it by a (SM). Example: Compute a13. Here, n = 13 = 11012 binary rep. of 13: 1 1 0 1 SM SM S SM accumulator: 1 12*a=a a2*a = a3 (a3)2 = a6 (a6)2*a= a13 (computed left-to-right) Efficiency: b ≤ M(n) ≤ 2b where b = log2 n + 1
  • 51. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 51 Computing an (cont.) Right-to-left binary exponentiation Scan n’s binary expansion from right to left and compute an as the product of terms a2 i corresponding to 1’s in this expansion. Example Compute a13 by the right-to-left binary exponentiation. Here, n = 13 = 11012. 1 1 0 1 a8 a4 a2 a : a2 i terms a8 * a4 * a : product (computed right-to-left) Efficiency: same as that of left-to-right binary exponentiation
  • 52. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 52 Problem Reduction 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.
  • 53. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 53 Examples of Solving Problems by 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)  linear programming  reduction to graph problems (e.g., solving puzzles via state- space graphs)