0% found this document useful (0 votes)
269 views

7 Transform and Conquer

This document introduces the transform and conquer algorithm design technique. It discusses three variations of this technique: instance simplification, representation change, and problem reduction. Instance simplification modifies a problem instance into a simpler one, representation change transforms the problem into a different representation, and problem reduction changes the problem into a different but related problem. Examples provided include sorting to simplify searching, Horner's rule to evaluate polynomials, and binary exponentiation. The goal of this document is to explain how to apply transform and conquer techniques to algorithm problems.

Uploaded by

Nixon Somoza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
269 views

7 Transform and Conquer

This document introduces the transform and conquer algorithm design technique. It discusses three variations of this technique: instance simplification, representation change, and problem reduction. Instance simplification modifies a problem instance into a simpler one, representation change transforms the problem into a different representation, and problem reduction changes the problem into a different but related problem. Examples provided include sorting to simplify searching, Horner's rule to evaluate polynomials, and binary exponentiation. The goal of this document is to explain how to apply transform and conquer techniques to algorithm problems.

Uploaded by

Nixon Somoza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Course Packet 7

Transform and Conquer


Design Technique

Maria Lolita G. Masangcap


Introduction
This course packet introduces the topic transform and conquer design technique and
enumerates its importance in algorithm designing.
Also, it discusses the three major variations of transform and conquer design technique and
their applications.

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

At the end of this course packet, you will


be able to Define and implement the
concept of Transform and Conquer
algorithm technique in designing
algorithm applicable in problem solving.
Design and Analysis of
Algorithm
Transform and Conquer Design Technique

An algorithm design technique used in solving a problem by


modifying instance to a simple one applied in the same or different
problem

Modify problem instance


Conquer! Work back to
to something easier to
original Problem
solve
Transform and Conquer Design Technique
Simpler instance or
Problem’s another representation or
Solution
instance another problem’s
instance

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

Data are sorted to make computation easier

Solves instance of problem by transforming into another simplified


instance of the same problem

• 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

Problem: Check uniqueness of elements in an array

Brute Force way:


Take the first element and check whether it is in the rest of the array, take
the second element and check if it is in the rest of the array, and so on…..
Analysis: O(n2)
Instance Simplification
Element Uniqueness: Presorting Algorithm

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.

Worst Case: An array with no equal elements

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)

PresortMode (A[0, …, n-1])


5 1 5 7 6 5 7 Sort the array A
i=0
modefrequency = 0
Sorted while i<= n-1 do
runlength = 1; runvalue = A[i]
while i+runlength <= n -1 and A[i+runlength] = runvalue
1 5 5 5 6 7 7 runlength = runlength + 1
if runlength > modefrequency
modefrequency = runlength
While loops take linear time, so the overall modevalue = runvalue
runtime is dominated by the time of sorting! i = i+runlength
return movevalue
Analysis: O(n log n)
Representation Change
Problem: Evaluating Polynomial
𝑓 𝑥 = 𝑎! + 𝑎" 𝑥 + 𝑎# 𝑥 # + … + 𝑎$ 𝑥 $ at a given point x = 𝑥!
Example:
𝑓 𝑥 = 2𝑥 % − 𝑥 # − 6𝑥 + 5 at x = 3
= 2(3)% − (3)# − 6(3) + 5
= 2(3)% − (3)# − 6(3) + 5
= 2(27) − 9 − 18 + 5
= 54 − 9 − 18 + 5
= 32
Representation Change
Problem: Evaluating Polynomial
𝑓 𝑥 = 𝑎! + 𝑎" 𝑥 + 𝑎# 𝑥 # + … + 𝑎$ 𝑥 $ at a given point x = 𝑥!

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

Let n = bk … bi … b0 be n in binary (bi = 0 or 1 for all i)


• P(x) = bk xk + … + bi xi + … + b0
• n = P(2)

Ex. n = 13 11012 à 1(23) + 1(22) + 0(21) + 1(20)


• b3 = 1; b2 = 1; b1 = 0; b0 = 1
• an = a p(2) = a(..(bk2 + bk-1)2 + … + b1)2 + b0
Binary Exponentiation
an = a p(2) = a(..(bk2 + bk-1)2 + … + b1)2 + b0
Horner’s Rule for val = an = ap(2)
Given: a13 =a 1101
val = a
Iterations for i = k – 1 down to 0 do
val = val * val
b3 = 1 à val = a
if bi = 1 then val = val * a
b2 = 1 à val = (a*a)*a = a3
Analysis
b1 = 0 à val = (a3 * a3) = a6 number of multiplication is between
b0 = 1 à val = (a6 * a6) * a = a13 k and 2k which is only O(log n)
Binary Search Trees (BST)
A binary search tree (BST) is a binary tree that is
either empty or in which the data entry of every
node has a key and satisfies these conditions:
BST Properties
• The key value of the left child of a node is less
than the key value of its parent node;
• The key value of the right child of a node is
greater than the key value of its parent node; In a BST, no two
• The left and right subtrees of a root node are nodes may have
again binary search trees. equal key values.

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

(a) (b) (c)


BLB = HL – HR = 1 – 1 = 0 BLC = HL – HR = 2 – 0 = 2 BLA = HL – HR = 2 – 0 = 2
BLA = HL – HR = 0 – 0 = 0 BLB = HL – HR = 1 – 0 = 1 BLB = HL – HR = 1 – 0 = 1
BLC = HL – HR = 0 – 0 = 0 BLA = HL – HR = 0 – 0 = 0 BLC = HL – HR = 0 – 0 = 0

Tree (a) is balanced since all nodes have no more than one (1) while
trees (b) and (c) are not balanced.
AVL Tree

The following are the


Every time that these four kinds of
Inserting and
procedures will be rotations that may be
deleting of nodes in To keep the tree
performed, the need executed to maintain
an AVL tree is similar balanced, some
to maintain the the tree balanced:
also to the rotation techniques
balanced factor not • Left Rotation
procedures done in are performed.
more than to one (1) • Right Rotation
BST.
must be done also. • Left Right Rotation
• Right Left Rotation
AVL Tree
• This kind of rotation is performed if the tree becomes
Left Rotation unbalanced when a node is added as right child of a right
subtree.

(a) (b) (c)


Right Unbalanced Tree Left Rotation Balanced Tree
Retrieved from https://www.tutorialspoint.com/
AVL Tree
• A single right rotation is needed if a tree becomes
Right Rotation unbalanced when a node is added as a left child of a left
subtree.

(a) (b) (c)


Left Unbalanced Tree Right Rotation Balanced Tree
Retrieved from https://www.tutorialspoint.com/
AVL Tree
Left-Right Rotation
• This kind of rotation is actually a
combination of left rotation followed by
the right rotation. This double rotation is
needed if a node is inserted in the right
subtree of a left subtree causing the root
node to have a balanced factor more
than one (1).

(a) Unbalanced Tree, (b) Left Rotation, (c) Still


Unbalanced Tree from Left Rotation, (d) Right
Rotation, and (e) Balanced Tree

Retrieved from https://www.tutorialspoint.com/


AVL Tree
Right-Left Rotation
• This kind of rotation is actually a
combination of right rotation followed by
a left rotation. This double rotation is
needed if a node is inserted in the left
subtree of a right subtree causing the
root node to have a balanced factor more
than one (1).

(a) Unbalanced Tree, (b) Right Rotation, (c)


Still Unbalanced Tree from Right Rotation, (d)
Left Rotation, and (e) Balanced Tree

Retrieved from https://www.tutorialspoint.com/


Building an AVL Tree
Suppose the keys 40, 20, 10, 25, 30, 22, and 50 are to be inserted in order into
an initially empty AVL tree. Remember that AVL tree is also a BST.
Building an AVL Tree
Suppose the keys 40, 20, 10, 25, 30, 22, and 50 are to be inserted in order into
an initially empty AVL tree. Remember that AVL tree is also a BST.
Building an AVL Tree
Suppose the keys 40, 20, 10, 25, 30, 22, and 50 are to be inserted in order into
an initially empty AVL tree. Remember that AVL tree is also a BST.
Building an AVL Tree
Suppose the keys 40, 20, 10, 25, 30, 22, and 50 are to be inserted in order into
an initially empty AVL tree. Remember that AVL tree is also a BST.
Problem Reduction

A process of transforming a given Problem 1 to be solved Reduction

algorithm to another problem


Problem 2 that can be
using a known algorithm solved by Algorithm A
Algorithm A

Solution for Problem 2 Clean up

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

m*n gcd(m, n) * lcm(m, n) Problem 2: GCD (Euclid’s Algorithm)


=
gcd(m, n) gcd(m, n) 60 ∗ 24
𝑙𝑐𝑚 60, 24 =
m*n gc d( 60, 24)
= lcm(m, n)
gcd(m, n) 1440
𝑙𝑐𝑚 60, 24 =
12
𝑙𝑐𝑚 60, 24 = 120
Reduction to Graphs Problems
Solutions are designed after reduction to a
graph representation

Problem: River Crossing Puzzle


• (P)easant, (w)olf, (g)oat, (c)abbage, (ll) river
• All must cross river
• Constraints:
• One boat
• Peasant + at most one more in a boat
• Without the peasant:
• Wolf would eat goat
• Goat would eat cabbage
Q&A

Any
Question???
CP7 Transform and Conquer Design Technique

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy