7 Transform and Conquer
7 Transform and Conquer
It explains how this technique is applied in solving different types of computing problems.
At the end of this activity, students are expected to apply transform and conquer design
technique to the given problems.
Also, students are expected to design an algorithm implementing the concept of transform
and conquer in solving a computing problem.
Introduction
Instance Simplification
• This transform the problem into a simpler or more convenient instance of the same problem
Representation Change
• This transforms the problem into a different representation of the same instance
Problem Reduction
• This transform the problem into a different problem for which an algorithm is already identified
Transform and Conquer Design Technique
We transform into:
Simpler
• Presorting
instance
• Horner’s Rule
Another
• Binary Exponentiation
representation
• Balanced Search Tree
Another • Computing Least Common Multiple
problem’s
Instance • Reduction to a Graph Representation
Instance Simplification
Presorting Algorithm
• Searching
Many problems • Computing median
involving lists are • Checking if all elements are distinct (element
easier to be solved uniqueness)
• Sorting
when list is sorted • Geometrics algorithms
Instance Simplification
Element Uniqueness: Brute Force Way
In sorting the elements, use good sorting algorithm like mergesort or quicksort!
Analysis: T(Sorting) + T(Scan)
Let’s compare: O(n log n) vs O(n 2)
O(n log n) + O(n) à O(n log n)
Instance Simplification
Getting the Mode of a List: Brute Force Way
Problem: Computing a mode (A value that occurs most often in a list)
5 1 5 7 6 5 7 Mode is? 5
Brute Force Scan the list and compute the frequencies of all distinct values, then find the value
with the highest frequency.
way:
Store the value already encountered along with their frequencies, in a separate list.
Analysis: O(n2)
Instance Simplification
Getting the Mode of a List: with Presorting Algorithm
Problem: Computing a mode (A value that occurs most often in a list)
5 1 5 7 6 5 7 Mode is? 5
If we sort the array first, all equal values will be adjacent to each other.
To compute the mode, all we need to know is to find the longest run of
adjacent equal values in the sorted array.
1 5 5 5 6 7 7 Sorted
Instance Simplification
Getting the Mode of a List: with Presorting Algorithm
Problem: Computing a mode (A value that occurs most often in a list)
Horner’s Rule
• Re-invented by W. Horner in early 19th century
• Approach: Representation Change
• Convert formula to a new one by successively taking x as a
common factor in the remaining polynomial of diminishing
degrees
𝑓 𝑥 = 𝑎! + 𝑥! (𝑎" + 𝑥! (𝑎# + 𝑥! (𝑎% + … + (𝑎$&" + 𝑎$ 𝑥! ) … )
Representation Change
Problem: Evaluating Polynomial
𝑓 𝑥 = 𝑎! + 𝑎" 𝑥 + 𝑎# 𝑥 # + … + 𝑎$ 𝑥 $ at a given point x = 𝑥!
Horner’s Rule
𝑓 𝑥 = 𝑎! + 𝑥! (𝑎" + 𝑥! (𝑎# + 𝑥! (𝑎% + … + (𝑎$&" + 𝑎$ 𝑥! ) … )
𝑓 𝑥 = 𝑎0 + 𝑎1𝑥 + 𝑎2𝑥 2 + 𝑎3𝑥 3
3 2 1 0
b3 = a3 b2 = a2 + x0b3 b1 = a1 + x0b2 b0 = a0 + x0b1
Representation Change
Problem: Evaluating Polynomial
𝑓 𝑥 = 𝑎! + 𝑎" 𝑥 + 𝑎# 𝑥 # + … + 𝑎$ 𝑥 $ at a given point x = 𝑥!
Horner’s Rule
𝑓 𝑥 = 𝑎! + 𝑥! (𝑎" + 𝑥! (𝑎# + 𝑥! (𝑎% + … + (𝑎$&" + 𝑎$ 𝑥! ) … )
𝑓 𝑥 = 𝑎0 + 𝑎1𝑥 + 𝑎2𝑥 2 + 𝑎3𝑥 3
3 2 1 0
b3 = a3 b2 = a2 + x0b3 b1 = a1 + x0b2 b0 = a0 + x0b1
Representation Change
Horner’s Rule Algorithm Example
val = a[n] f(x) = 2x3 – x2 – 6x + 5 at x = 3 n=3
for i = n – 1 down to 0 a[3] = 2 val = 2
val = x * val + a[i] a[2] = -1 val = 3(2) + (- 1) = 5
return val a[1] = -6 val = 3(5) + (-6) = 9
a[0] = 5 val = 3(9) + 5 = 32
f(3) = 32
Analysis: O(n)
Representation Change
Problem: Binary Exponentiation
Calculate an
• Part of primality testing algorithms for encryption
Evaluate P(x) = xn at x = a
• If using Horner’s rule, it becomes brute-force
exponentiation (n multiplications) – Any faster method?
• Direct representation change can be applied
Binary Exponentiation
BST Example
Representation Change
Balanced Search Tree
With an unbalanced tree,
AVL tree
searching a key will take too much • named after its creator Adelson, Velski, and
Landis
time to perform; thus, a need to
• It keeps its balance by checking the height of its
balance out an existing BST arises. left and right subtrees.
• For each node of an AVL tree, the balance factor
Balanced tree is a tree structure or the difference of the height of its left and right
of reduced height with the same subtree must not be more than one (1).
• Its balance factor is either -1, 0, and 1
degree and number of nodes considering also that each subtree is balanced.
among all trees. • The formula for getting the balance factor is
presented below: BL = HL - HR
Representation Change
AVL tree Allowing more than one
element in a node of a search
• named after its creator Adelson, Velski, and
Landis tree to change its position –
• It keeps its balance by checking the height of its Representation Change!
left and right subtrees.
• For each node of an AVL tree, the balance factor Analysis
or the difference of the height of its left and right
subtree must not be more than one (1). There is a gain in the time
• Its balance factor is either -1, 0, and 1 efficiency of searching,
considering also that each subtree is balanced. insertion, and deletion.
• The formula for getting the balance factor is Average case: O(log n)
presented below: BL = HL - HR
AVL Tree
Tree (a) is balanced since all nodes have no more than one (1) while
trees (b) and (c) are not balanced.
AVL Tree
Solution to Problem 1
Problem Reduction
Problem: Get the LCM of two integers
Least Common Multiple (LCM) is the lowest integer that both numbers
divide perfectly
Example:
Say m = 24, and n = 60, compute LCM -> lcm(m, n)
24 2 * 2 * 2 * 3 60 2 * 2 * 3 * 5 Using the old method
Take product of all common factors of m and
n, and all factors of m not in n, and all factors
of n not in m
lcm (24, 60) = (2 * 2 * 3) * 2 * 5 = 120
Least Common Multiple (LCM)
Problem Reduction
Algo A
Solution to
Problem 1 Problem 2
Problem 2
To be solved Solvable by Algo A
Problem 1: LCM
lcm (24, 60) = (2*2*3)*2*5 = 120
Notice that
24*60 = (2*2*2*3) * (2*2*3*5) = (2*2*3)2 *2*5
(2*2*3)2 *2*5 = (2*2*3) * (2*2*3) * 2*5
= 12 * 12 * 10 So,
(2*2*3)2 *2*5 = 12 * 120 m*n = gcd(m, n) * lcm(m, n)
Least Common Multiple (LCM)
Problem Reduction
Algo A
Solution to
Problem 1 Problem 2
Problem 2
To be solved Solvable by Algo A
Any
Question???
CP7 Transform and Conquer Design Technique