You are on page 1of 30

Design and Analysis of Algorithm

Definition of Transform-and-Conquer algorithm


design technique
Application of Transform-and-Conquer algorithm
design technique in the following algorithms:

Presorting algorithm
Gaussian Elimination
Balanced search tree
Problem reduction

Transform-and-Conquer Algorithm Design Technique

* Property of STI
Page 1 of 30

Design and Analysis of Algorithm

an algorithm design technique in solving a


problem by modifying instance to a simple one
applied in the same or different problem
involves three variations :
1. Instance simplification
2.

Representation Change

3.

Problem Reduction

Transform-and-Conquer Algorithm Design Technique

* Property of STI
Page 2 of 30

Design and Analysis of Algorithm

Transform-and-Conquer is applied in:


instance simplification through presorting and
Gaussian elimination
representation change through balanced search
trees, heaps and Horners rule
problem reduction through least common multiple
and optimization

Transform-and-Conquer Algorithm Design Technique

* Property of STI
Page 3 of 30

Design and Analysis of Algorithm

Presorting
data are sorted to make computation easier
solves instance of problem by transforming into
another simplified instance of the same problem
Many problems involving lists are easier to be solved
when list is sorted.
searching
computing the median
checking if all elements are distinct (element
uniqueness)
sorting
Geometric algorithms

Transform-and-Conquer Algorithm Design Technique

* Property of STI
Page 4 of 30

Design and Analysis of Algorithm

If the array is sorted first, we can then apply the


binary search which requires only [log2n]+1
comparison in a worst case.
The total running time in the worst case will be:

Presorting algorithm is also applied in different


geometric algorithms engaging with sets of points.

Transform-and-Conquer Algorithm Design Technique

* Property of STI
Page 5 of 30

Design and Analysis of Algorithm

named after Carl Friedrich Gauss


The following equations illustrates the Gaussian
Elimination algorithm:

Transform-and-Conquer Algorithm Design Technique

* Property of STI
Page 6 of 30

Design and Analysis of Algorithm

The figure below is an illustration of the


application of Gaussian elimination application:

Solve the latter by substitutions starting with the last


equation and moving up to the first one.

Transform-and-Conquer Algorithm Design Technique

* Property of STI
Page 7 of 30

Design and Analysis of Algorithm

Matrix representation of the given equation:


Ax = B
Ax = B
where:

Transform-and-Conquer Algorithm Design Technique

* Property of STI
Page 8 of 30

Design and Analysis of Algorithm

The figure below shows the steps in elementary


operations:

Exchanging two equations of the system

Replacing an equation with its non-zero multiple

Replacing an equation with a sum or difference of


this equation and some multiple of another equation

Transform-and-Conquer Algorithm Design Technique

* Property of STI
Page 9 of 30

Design and Analysis of Algorithm

Apply Gaussian elimination using the equation below:

2x1 x2 + x3 = 1
4x1 + x2 x3 = 5
x1 + x 2 + x3 = 0
Solution:
We will start by creating the matrix. Subtract 2 *
row 1. Afterwards, subtract * row 1.

Transform-and-Conquer Algorithm Design Technique

* Property of STI
Page 10 of 30

Design and Analysis of Algorithm

Then subtract * row 2:

Solution by backward substitutions:


X3 = (-2)/2 = -1, x 2 = (3-(-3) x 3)/3 =0,
and
x1 = (1-x 3-(-1)x2)/2 = 1

Transform-and-Conquer Algorithm Design Technique

* Property of STI
Page 11 of 30

Design and Analysis of Algorithm

Pseudo code:
// Gaussian Elimination (A(1..n, 1, n], b[1..n])
// Uses Gaussian elimination to matrix A of a
systems coefficients
//augmented with vector b of the systems righthand side values
//input: Matrix A[i, n+ 1] and column vector b[1..n]
//output: An equivalent upper-triangular matrix in
place of A with the corresponding right-hand side
values in the (n+1) column

Transform-and-Conquer Algorithm Design Technique

* Property of STI
Page 12 of 30

Design and Analysis of Algorithm

// Gaussian Elimination (A(1..n, 1, n], b[1..n])

// Uses Gaussian elimination to matrix A of a


systems coefficients
//augmented with vector b of the systems righthand side values
//input: Matrix A[i, n+ 1] and column vector b[1..n]
//output: An equivalent upper-triangular matrix in
place of A with the corresponding right-hand side
values in the (n+1) column

Transform-and-Conquer Algorithm Design Technique

* Property of STI
Page 13 of 30

Design and Analysis of Algorithm

Binary search tree is a type of binary tree that is


composed of nodes that contains elements of a set
of ordered items.

All elements of the left subtree should be less than


the root while all elements of the right subtree
should be greater than or equal to the root.

Transform-and-Conquer Algorithm Design Technique

* Property of STI
Page 14 of 30

Design and Analysis of Algorithm

AVL Tree

Adelson-Velskii and Landis Tree


the heights of the subtrees must differ by not
more than 1

The first example of a tree is a search tree where


each node is greater than its predecessor.

Transform-and-Conquer Algorithm Design Technique

* Property of STI
Page 15 of 30

Design and Analysis of Algorithm

The second example of AVL tree is illustrated


below.

Transform-and-Conquer Algorithm Design Technique

* Property of STI
Page 16 of 30

Design and Analysis of Algorithm

Four cases of AVL tree:

Left of left

Right of right

Left of right

Right of left

Transform-and-Conquer Algorithm Design Technique

* Property of STI
Page 17 of 30

Design and Analysis of Algorithm

Left of left - a subtree of a given tree that is left


high node has also become the left high node
Example: The figure below illustrates a sample
AVL tree before inserting 8.

Out of
balance
at 20

LH

BEFORE
INSERTING 8
LH

LH

AFTER
INSERTING 8
Transform-and-Conquer Algorithm Design Technique

* Property of STI
Page 18 of 30

Design and Analysis of Algorithm

Right of right - a subtree that is right high has


also become the right high
Example: The figure below illustrates the right of
right case.

BEFORE
INSERTING 38

RH

RH

RH

AFTER
INSERTING 38
Transform-and-Conquer Algorithm Design Technique

* Property of STI
Page 19 of 30

Design and Analysis of Algorithm

Right of Left - a subtree that is left high has


become the right high
Example: The figure below illustrates the right of
left case.

BEFORE
INSERTING 15

RH

LH

AFTER
INSERTING 15
Transform-and-Conquer Algorithm Design Technique

* Property of STI
Page 20 of 30

Design and Analysis of Algorithm

Left of Right - a subtree that is right high has


become the left high
Example: The figure below illustrates the left of
right case.

AFTER
INSERTING 18

BEFORE
INSERTING 18

LH

RH

Transform-and-Conquer Algorithm Design Technique

* Property of STI
Page 21 of 30

Design and Analysis of Algorithm

Types of rotations that can be applied to solve the


unbalancing of trees.

Single left rotation

Single right rotation

Double left-right rotation

Double right-left rotation

Transform-and-Conquer Algorithm Design Technique

* Property of STI
Page 22 of 30

Design and Analysis of Algorithm

Single left rotation - it solves the left of left case in


an unbalanced tree by rotating the edge
connecting the root and its left child in the binary
tree

Example: The figure below illustrates the single


left rotation.

Transform-and-Conquer Algorithm Design Technique

* Property of STI
Page 23 of 30

Design and Analysis of Algorithm

Single right rotation - it the same with the single


left rotation but in opposite direction

Example: The figure below illustrates the single


right rotation.

Transform-and-Conquer Algorithm Design Technique

* Property of STI
Page 24 of 30

Design and Analysis of Algorithm

Double left-right rotation - solves the Right of Left


case of unbalanced tree

Example: The figure below illustrates the double


left-right rotation

Transform-and-Conquer Algorithm Design Technique

* Property of STI
Page 25 of 30

Design and Analysis of Algorithm

Double right-left rotation - solves the Left of Right


case of unbalanced tree

Example: The figure below illustrates the double


right-left rotation

Transform-and-Conquer Algorithm Design Technique

* Property of STI
Page 26 of 30

Design and Analysis of Algorithm

Analysis of algorithm for Binary search using the


Binary Search tree

[log2n] h < 1.4405 log2 (n + 2) 1.3277


The inequalities immediately imply that the
operations of searching and insertion are (log n)
in the worst case.

Transform-and-Conquer Algorithm Design Technique

* Property of STI
Page 27 of 30

Design and Analysis of Algorithm

Problem reduction process of transforming a


given problem to another problem using a known
algorithm

Illustration of Problem reduction strategy:

Transform-and-Conquer Algorithm Design Technique

* Property of STI
Page 28 of 30

Design and Analysis of Algorithm

Example for Problem Reduction

Problem: Spacing to Center Text Problem


Solution:
Given three numbers a, b, and c, where:
o a is the width of a normal space
o b is the width of an option-space
o c is the amount we want to indent
Find two more numbers
x is the number of normal spaces to be
used
y is the number of option-spaces to be
used

So that ax + by is as close as possible to c.

Transform-and-Conquer Algorithm Design Technique

* Property of STI
Page 29 of 30

Design and Analysis of Algorithm

Any
Questions?

Transform-and-Conquer Algorithm Design Technique

* Property of STI
Page 30 of 30