18
Design and Analysis of Algorithm Divide-and-ConquerAlgorithm Design Technique * Property of STI Page 1 of 18 TOPIC TITLE: Transform-and-Conquer Algorithm Design Technique Specific Objectives: At the end of the topic session, the students are expected to: Cognitive: 1. Define Transform-and-Conquer algorithm design technique. 2. Explain how Transform-and-Conquer algorithm design technique is applied to Presorting algorithm, Gaussian elimination, and Balanced Search Tree. 3. Understand the concept of problem reduction. Affective: 1. Listen to others with respect. 2. Participate in class discussions actively. 3. Share ideas to the class. MATERIALS/EQUIPMENT: o topic slides o OHP TOPIC PREPARATION: o Have the students research on the following: Definition of Transform-and-Conquer algorithm Importance of Presorting algorithm Uses of Gaussian elimination algorithm Application of Balanced Search tree using Transform-and- Conquer algorithm Concept of Problem reduction o The research work should be in a printed copy compiled in a folder with a title page. o Provide sample problems that can be solved using Transform-and- Conquer algorithm design technique. o It is imperative for the instructor to incorporate various kinds of teaching strategies while discussing the suggested topics. The instructor may use the suggested learning activities below to facilitate a thorough and creative discussion of the topic. o Prepare the slides to be presented in the class. o Prepare seatwork for the students to apply the lessons learned in Transform-and-Conquer algorithm design technique. TOPIC PRESENTATION: The topic will cover the application of Transform-and-Conquer algorithm design technique The following is the suggested flow of discussion for the course topic: 1. Ask the students to define Transform-and-Conquer algorithm design technique. 2. Discuss the application of Transform-and-Conquer Algorithm Design Technique in the following algorithms: a. Presorting algorithm b. Gaussian Elimination

MELJUN CORTES ALGORITHM Transform-And-Conquer Algorithm Design Technique II

Embed Size (px)

DESCRIPTION

MELJUN CORTES ALGORITHM Transform-And-Conquer Algorithm Design Technique II

Citation preview

Page 1: MELJUN CORTES ALGORITHM Transform-And-Conquer Algorithm Design Technique II

Design and Analysis of Algorithm

Divide-and-ConquerAlgorithm Design Technique * Property of STI Page 1 of 18

TOPIC TITLE: Transform-and-Conquer Algorithm Design Technique Specific Objectives: At the end of the topic session, the students are expected to: Cognitive:

1. Define Transform-and-Conquer algorithm design technique. 2. Explain how Transform-and-Conquer algorithm design technique is

applied to Presorting algorithm, Gaussian elimination, and Balanced Search Tree.

3. Understand the concept of problem reduction.

Affective:

1. Listen to others with respect. 2. Participate in class discussions actively. 3. Share ideas to the class.

MATERIALS/EQUIPMENT:

o topic slides o OHP

TOPIC PREPARATION:

o Have the students research on the following:

Definition of Transform-and-Conquer algorithm

Importance of Presorting algorithm

Uses of Gaussian elimination algorithm

Application of Balanced Search tree using Transform-and-Conquer algorithm

Concept of Problem reduction o The research work should be in a printed copy compiled in a folder

with a title page. o Provide sample problems that can be solved using Transform-and-

Conquer algorithm design technique. o It is imperative for the instructor to incorporate various kinds of

teaching strategies while discussing the suggested topics. The instructor may use the suggested learning activities below to facilitate a thorough and creative discussion of the topic.

o Prepare the slides to be presented in the class. o Prepare seatwork for the students to apply the lessons learned in

Transform-and-Conquer algorithm design technique.

TOPIC PRESENTATION: The topic will cover the application of Transform-and-Conquer algorithm design technique The following is the suggested flow of discussion for the course topic:

1. Ask the students to define Transform-and-Conquer algorithm design technique.

2. Discuss the application of Transform-and-Conquer Algorithm Design Technique in the following algorithms:

a. Presorting algorithm b. Gaussian Elimination

Page 2: MELJUN CORTES ALGORITHM Transform-And-Conquer Algorithm Design Technique II

Design and Analysis of Algorithm

Divide-and-ConquerAlgorithm Design Technique * Property of STI Page 2 of 18

c. Balanced search tree d. Problem reduction

Transform-and-Conquer Algorithm Design Technique

Page 1 of 30

Transform-and-Conquer Algorithm Design Technique These are the topics to be discussed under Transform-and-Conquer algorithm design technique.

o Definition of Transform-and-Conquer algorithm design technique o 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, Page 1 of 30]

What is Transform-and-Conquer Algorithm Design Technique? Page 2 of 30

What is Transform-and-Conquer Algorithm Design Technique? Transform-and-Conquer is an algorithm design technique used in solving a problem by modifying instance to a simple one applied in the same or

different problem.

Transform-and-Conquer involves three variations and these are as follows:

Instance simplification. This transforms 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 transforms the problem into a different problem for which an algorithm is already identified.

Transform-and-Conquer is applied in:

instance simplification through presorting and Gaussian elimination

representation change through balanced search trees, heaps and Horner’s rule

problem reduction through least common multiple and optimization [What is Transform-and-Conquer Algorithm Design Technique?, Pages 2-3 of 30]

Transform-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STI

Page 1 of 30

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

Design and Analysis of Algorithm

* Property of STI

Page 2 of 30

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

Page 3: MELJUN CORTES ALGORITHM Transform-And-Conquer Algorithm Design Technique II

Design and Analysis of Algorithm

Divide-and-ConquerAlgorithm Design Technique * Property of STI Page 3 of 18

Page 3 of 30

Presorting Algorithm Page 4 of 30

Page 5 of 30

Presorting Algorithm Presorting is considered as an old idea in Computer Science where data are sorted to make computation easier. Basically, it 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:

o searching o computing the median (selection problem) o checking if all elements are distinct (element uniqueness) o sorting o Geometric algorithms

The use of presorting algorithm is vital since it is already sorted, various problems about a list can be solved easily. In fact, the efficiency time of algorithms that engage sorting may depend on the efficiency of the sorting algorithm being applied. To make the application simple, we assume throughout the discussion that lists are implemented as arrays, since some sorting algorithms are easier to apply for the array representation. Example of Presorting Using Searching Algorithm To further understand the concept of presorting, let us examine a searching problem given the value v in an array of n sort table items. The brute force solution here is sequential search, which needs n comparison in the worst case. If the array is sorted, we can then apply the binary search, which requires only [log2n] + 1 comparison in a worst case. Assuming the most efficient n log n sort, the total running time in the worst case will be:

Certainly, if we are to search in the same list more than once, the time spent on sorting is justified. In addition, presorting algorithm is also applied in different geometric algorithms engaging with sets of points using presorting in one way or another. Therefore, points can be sorted by one of their coordinates or by their distance from a particular line or by some angle, and so on.

Transform-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STI

Page 3 of 30

Transform-and-Conquer is applied in:

instance simplification through presorting and

Gaussian elimination

representation change through balanced search

trees, heaps and Horner’s rule

problem reduction through least common multiple

and optimization

Transform-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STI

Page 4 of 30

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

Design and Analysis of Algorithm

* Property of STI

Page 5 of 30

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.

Page 4: MELJUN CORTES ALGORITHM Transform-And-Conquer Algorithm Design Technique II

Design and Analysis of Algorithm

Divide-and-ConquerAlgorithm Design Technique * Property of STI Page 4 of 18

[Presorting Algorithm, Pages 4-5 of 30]

Gaussian Elimination Page 6 of 30

Gaussian Elimination Gaussian elimination is considered as an instance simplification in the transform-and-conquer algorithm applied in linear equations. It was named after Carl Friedrich Gauss who contributed significantly to various fields,

including number theory, statistics, analysis, differential geometry, etc.

Given a problem associated with a system of n linear equations in n unknowns has the following equation:

a x + b y + c z + d w = h

In the equation, a, b, c, d, and h are known numbers, on the other hand, x, y, z, and w are unknown numbers which is basically referred to as the linear equation. If h = 0, the linear equation is said to be homogeneous since a linear system is a set of linear equations and a homogeneous linear system is a set of homogeneous linear equations. If the coefficients of one equation are relative to the coefficients of the other, we can simply say that the system has a single solution. Usually, the method for finding the solution is to use whichever equation to express one of the variables as function of the other and substitute the answer into the other equation, producing linear equation whose solution is then used for finding the value of the second variable. In different applications, we need to solve a system of n equation in n unknowns as illustrated in the following equations:

Figure 9.1 Sample equation for Gaussian elimination

We can simply say that n is the large number, theoretically, we can solve a given system by generalizing the substitution method for solving systems of two linear equations. However, the resulting algorithm would be intensely unwieldy. This is where we need to apply the Gaussian elimination to transform a system of n linear equation in n unknown to the corresponding system such as system with the same solution as the original one, then added with an upper triangular coefficient matrix, a matrix with all zeros below its main diagonal.

Transform-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STI

Page 6 of 30

named after Carl Friedrich Gauss

The following equations illustrates the Gaussian

Elimination algorithm:

Page 5: MELJUN CORTES ALGORITHM Transform-And-Conquer Algorithm Design Technique II

Design and Analysis of Algorithm

Divide-and-ConquerAlgorithm Design Technique * Property of STI Page 5 of 18

Page 7 of 30

Page 8 of 30

The figure below illustrates the application of Gaussian elimination application:

Figure 9.2 Illustration of the Gaussian Elimination

The matrix representation of the given equation is as follows:

Ax = B A’x = B’ Where:

Figure 9.3 Matrix representation for the equation

The idea is to transform the system of linear equations into its equivalent that eliminates coefficients so we end up with a triangular matrix. Maybe there will be a question in your mind on why do we need to transform? Well, it simply because the matrix with zeros in the lower triangle which is referred to as the upper-triangular matrix is easier to solve. Through transformation, we can solve the last equation first, substitute into the second to last, etc. working our way back to the first one. How can we get from a system with an arbitrary coefficient matrix A to an equivalent system with an upper-triangular coefficient matrix A1? The

Transform-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STI

Page 7 of 30

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

Design and Analysis of Algorithm

* Property of STI

Page 8 of 30

Matrix representation of the given equation:

Ax = B A’x = B’

where:

Solve the latter by substitutions starting with the last equation and moving up to the first one.

Page 6: MELJUN CORTES ALGORITHM Transform-And-Conquer Algorithm Design Technique II

Design and Analysis of Algorithm

Divide-and-ConquerAlgorithm Design Technique * Property of STI Page 6 of 18

Page 9 of 30

Page 10 of 30

Page 11 of 30

solution for this is following a series called elementary operations. The figure below shows the steps in elementary operations:

Elementary Operations

Figure 9.4 Illustration for the Elementary Operations

The figure shows that, since no elementary operation can modify a solution to a system that is achieved through a sequence of operations, we will come up with the same solution as the original one. Let us examine how we can get to a system with an upper-right triangular matrix. The first step is using a11 as pivot to make all x1 coefficients zeros in the equation below the first one. Specifically, we substitute the second equation with the difference between it and the first equation will be multiplied by a21/a11 to come up with an equation having a zero coefficient for x1. Similar process will be applied for the third, fourth and finally the nth equation by multiplying a31/a11, a41/a11 ….an1/a11 of the first equation, which correspondingly yields all the coefficients of x1 below the first equation zero. Afterwards, we will eliminate all the coefficients of x2 by subtracting a proper multiple of second equation for every n-1 variables eventually generates system with an upper-triangular coefficient matrix. Example of Gaussian Elimination

Apply Gaussian elimination using the equation below:

2x1 – x2 + x3 = 1 4x1 + x2 – x3 = 5 x1 + x2 + x3 = 0

To solve the given equation, we will start by creating the matrix: subtract 2 * row 1: Afterwards, subtract ½ * row 1:

Then, subtract ½ * row 2:

Transform-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STI

Page 9 of 30

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

Design and Analysis of Algorithm

* Property of STI

Page 10 of 30

Apply Gaussian elimination using the equation

below:

2x1 – x2 + x3 = 1

4x1 + x2 – x3 = 5

x1 + x2 + x3 = 0

Solution:

We will start by creating the matrix. Subtract 2 *

row 1. Afterwards, subtract ½ * row 1.

Transform-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STI

Page 11 of 30

Then subtract ½ * row 2:

Solution by backward substitutions:

X3 = (-2)/2 = -1, x2 = (3-(-3) x3)/3 =0,

and

x1 = (1-x3-(-1)x2)/2 = 1

Exchanging two equations of the system

Replacing an equation with its non-zero multiple

Replacing an equation with a sum or difference or this equation and some multiple of another equation

Page 7: MELJUN CORTES ALGORITHM Transform-And-Conquer Algorithm Design Technique II

Design and Analysis of Algorithm

Divide-and-ConquerAlgorithm Design Technique * Property of STI Page 7 of 18

Page 12 of 30

Now, we can get the answer by using backward substitutions:

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

The pseudo code below is the elimination stage of Gaussian elimination. // Gaussian Elimination (A(1..n, 1, …n], b[1..n]) // Uses Gaussian elimination to matrix A of a system’s coefficients //augmented with vector b of the system’s right-hand 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

If you read and understand the algorithm above, you will realize that there are two important tests that can be applied on this pseudocode.

1. First, we can say that the pseudocode is not always correct. For example, if A[i,i] = 0 is given, since we cannot divide by it, it cannot be used in the ith row as a pivot for the ith iteration of the algorithm. In this example, we should get advantage of the first elementary operation and switch the ith row with some row below it that has a nonzero coefficient in the ith column. Therefore, the system have a unique solution, which is a usual instance for systems in consideration, such row must exist. In addition, we should anticipate the opportunity that A[i,i] is so small and as a result the scaling factor A[j,i]/A[i,i] is so large that the new value of A[j,k] may turn out to be distorted by a round-off fault produced by the subtraction of two numbers of absolutely different magnitudes. To prevent this difficulty, look for a row with the largest absolute value of the coefficient in the ith column, replace it with the ith row, and then use the new A[i,i] as the iteration’s pivot. The alteration is referred to as the partial pivoting, which ensures that the magnitude of the scaling factor will never exceed 1.

2. The second observation is the reality that the innermost loop is written with an intense inefficiency. Can you discover it earlier before examining the following pseudocode, which features both

Transform-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STI

Page 12 of 30

Pseudo code:

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

// Uses Gaussian elimination to matrix A of a

system’s coefficients

//augmented with vector b of the system’s right-

hand 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

Page 8: MELJUN CORTES ALGORITHM Transform-And-Conquer Algorithm Design Technique II

Design and Analysis of Algorithm

Divide-and-ConquerAlgorithm Design Technique * Property of STI Page 8 of 18

Page 13 of 30

partial pivoting and get rid of this efficiency?

// Improved Gaussian elimination (A(1..n, 1, …n], b[1..n]) // Implements Gaussian elimination with partial pivoting //input: Matrix A[i, n+ 1] and column-vector b[1..n] //output: An equivalent upper-triangular matrix in place of A and the //corresponding right-hand side values in place of (n+1) column

The time efficiency of this algorithm in the innermost loop is represented in only one line:

A[j,k] A[j,k]- A[i,k] * temp This line includes one multiplication and one subtraction. In most computers, multiplication process is indisputably more expensive than addition/subtraction and for this reason it is multiplication that is typically cited as the algorithm’s basic operation. [Gaussian Elimination, Pages 6-13 of 30]

Application of Transform-and-Conquer Algorithm Design Technique in Balanced Search Trees Page 14 of 30

Application of Transform-and-Conquer Algorithm Design Technique in Balanced Search Trees In this section we will study the Balanced Search Tree with the application of Transform-and-Conquer algorithm. As we all know, the Balanced tree is the primary data structure for implementing dictionaries. 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.. The figure below illustrates a Binary search tree:

Figure 9.5 Binary Search tree

Transform-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STI

Page 13 of 30

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

// Uses Gaussian elimination to matrix A of a

system’s coefficients

//augmented with vector b of the system’s right-

hand 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

Design and Analysis of Algorithm

* Property of STI

Page 14 of 30

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.

Page 9: MELJUN CORTES ALGORITHM Transform-And-Conquer Algorithm Design Technique II

Design and Analysis of Algorithm

Divide-and-ConquerAlgorithm Design Technique * Property of STI Page 9 of 18

Page 15 of 30

Page 16 of 30

This transformation of elements from a set of binary search tree is an example of representation change technique. The advantage of using a transformation is the time efficiency of searching, inserting, and deleting,

which is equivalent to (log n) but only in the average case.

This section focuses on the application of AVL tree using the Transform-and-Conquer algorithm design technique. AVL Tree AVL (Adelson-Velskii and Landis) Tree was developed by two Russian mathematicians in 1962. It is a height balanced binary search tree where the heights of the subtrees must differ by not more than 1. To illustrate the importance of the tree being balanced, let’s examine two different trees containing the same data. The first example of a tree is a search tree where each node is greater than its predecessor.

Figure 9.6 Illustration of the first tree example

If you study the given tree figure, you will observe that it is equivalent to a linear list in a binary tree’s clothing. It will take two tests to find node 13, three tests to find node 15, and eight tests to locate node 52. In other words, the search effort for this particular binary tree is O(n). The second tree is an example of AVL tree, as illustrated in the figure below:

Figure 9.7 Illustration of the second tree example

Although we have labeled it as an AVL tree, it appears like a binary tree since both trees are built on the same structure. This time, locating nodes 9 and 15 require four tests and locating nodes 21 and 53 require three

Transform-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STI

Page 15 of 30

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

Design and Analysis of Algorithm

* Property of STI

Page 16 of 30

The second example of AVL tree is illustrated

below.

Page 10: MELJUN CORTES ALGORITHM Transform-And-Conquer Algorithm Design Technique II

Design and Analysis of Algorithm

Divide-and-ConquerAlgorithm Design Technique * Property of STI Page 10 of 18

Page 17 of 30

Page 18 of 30

tests. In other words, the maximum search effort for the tree is either three or four. Its search effort is O(log2n). These two small trees examples, we realize that the worst search effort was decreased from eight tests to four tests by simply balancing the tree. For a tree with 1000 nodes, the worst case for completely unbalanced tree is 1000, while the worst case for a nearly complete tree is 10. Thus, balancing a tree can lead to important performance improvement. Determining Unbalanced Tree Insertion or deletion of nodes in a binary search tree causes the tree to become unbalanced. If this happens, it is required to balance the tree by rotating nodes either to the left or to the right. To help us determine if we are required to balance the given AVL tree, we should consider the following cases:

Left of left case– this is where a subtree of a given tree that is left high node has also become the left high node. The figure below illustrates a sample AVL tree before inserting 8.

Figure 9.8 Sample AVL tree before inserting node 8

Once the node 8 is inserted, the AVL tree will become an unbalanced tree left of left case since the left high node has also become the left high node. The figure below illustrates an AVL after inserting 8.

Figure 9.9 Left of left case after insertion of node 8 in an unbalance AVL tree

Transform-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STI

Page 17 of 30

Four cases of AVL tree:

Left of left

Right of right

Left of right

Right of left

Transform-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STI

Page 18 of 30

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

AFTER INSERTING 8

BEFORE INSERTING 8

LH

LH

LH

LH

H

Out of

balance

at 20

LH

LH

Page 11: MELJUN CORTES ALGORITHM Transform-And-Conquer Algorithm Design Technique II

Design and Analysis of Algorithm

Divide-and-ConquerAlgorithm Design Technique * Property of STI Page 11 of 18

Page 19 of 30

The figure shows that in node 22, the tree is out of balance since the left subtree node 20 is left high and it is on the left branch of node 22, which is also a left high.

Right of right case – this where a subtree that is right high has also become the right high. It is actually the mirror of left of left case. In our example, we have an original AVL before inserting node 38, as illustrated below:

Figure 9.10 Right of right case before inserting node 38 in an AVL tree

The figure below illustrates Right of right case of an AVL after inserting node 38. The tree will become an unbalanced tree right of right case since the right high node has also become the right high node.

Figure 9.11 Right of right case after insertion node 38 in an AVL tree

Right of left case – this is where a subtree that is left high node has become the right high node. In this case, the root node is left high

Transform-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STI

Page 19 of 30

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.

AFTER INSERTING 38

BEFORE INSERTING 38

RH

RH

RH

RH

RH

RH

Page 12: MELJUN CORTES ALGORITHM Transform-And-Conquer Algorithm Design Technique II

Design and Analysis of Algorithm

Divide-and-ConquerAlgorithm Design Technique * Property of STI Page 12 of 18

Page 20 of 30

Page 21 of 30

and the left subtree is right high node, which simply means the right of left of the tree. The figure below illustrates an AVL tree before inserting node 15.

Figure 9.12Right of left case before inserting node 15 in an AVL tree

The figure below illustrates an AVL tree after inserting node 15 where we encounter the right of high case after the insertion of new node.

Figure 9.13 Right of left case after inserting node 15 in an AVL tree

Left of right case - this is where a subtree that is right high has become the left high. In this case, the root node is right high and the right subtree is left high node which simply means the left of right of the tree.

Transform-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STI

Page 20 of 30

Right of Left - a subtree that is left high has

become the right high

Example: The figure below illustrates the right of

left case.

AFTER INSERTING 15

BEFORE INSERTING 15

RH

LH

Transform-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STI

Page 21 of 30

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

RH

LH

Page 13: MELJUN CORTES ALGORITHM Transform-And-Conquer Algorithm Design Technique II

Design and Analysis of Algorithm

Divide-and-ConquerAlgorithm Design Technique * Property of STI Page 13 of 18

Page 22 of 30

The figure below illustrates an AVL tree before inserting node 18.

Figure 9.14 Left of Right case before inserting node 18 in an AVL tree

The figure below illustrates an AVL tree after inserting 18 where we encounter the left of high case after the insertion of new node.

Figure 9.15 Left of Right case after inserting node 18 in an AVL tree

After familiarizing with the different cases that caused the AVL tree to become an unbalanced tree, it is also important that we know how to solve these cases through different rotations. Rotation is referred to as the local transformation of a subtree rooted as a node whose balance has become either +2 or -2. The following are the types of rotations that can be applied to solve unbalanced trees.

1. Single right rotation – this 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.

Transform-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STI

Page 22 of 30

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

LH

RH

Page 14: MELJUN CORTES ALGORITHM Transform-And-Conquer Algorithm Design Technique II

Design and Analysis of Algorithm

Divide-and-ConquerAlgorithm Design Technique * Property of STI Page 14 of 18

Page 23 of 30

Page 24 of 30

Figure 9.16 Balancing the tree using single right rotation in a Left of left case unbalance

tree

To solve the left of left case, we can use the single right rotation to balance the tree. Node 20 will be rotated to the right to make node 14 the root and balance the tree.

2. Single left rotation – this is the same with the single left rotation but

in opposite direction.

Figure 9.17 Balancing the tree using single left rotation in a Right of right case unbalance

tree

3. Double left-right rotation – this rotation solves the Right of Left case of unbalanced tree. To balance a Right of Left case as in the figure, rotate the left subtree (10) of the unbalanced node (20) to the left to create a left-of-left situation then rotate the unbalanced node to the right to balance the tree.

Transform-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STI

Page 23 of 30

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

Design and Analysis of Algorithm

* Property of STI

Page 24 of 30

Single right rotation - it the same with the single

left rotation but in opposite direction

Example: The figure below illustrates the single

right rotation.

Page 15: MELJUN CORTES ALGORITHM Transform-And-Conquer Algorithm Design Technique II

Design and Analysis of Algorithm

Divide-and-ConquerAlgorithm Design Technique * Property of STI Page 15 of 18

Page 25 of 30

Page 26 of 30

Page 27 of 30

Figure 9.18 Balancing the tree using double left-right rotation in a right of left case

unbalance tree

Double right-left rotation – this rotation solves the Left of Right case of unbalanced tree. To balance a Left of Right case as in the figure, rotate the right subtree (20) of the unbalanced node (10) to the right to create a right-of-right situation then rotate the unbalanced node to the left to balance the tree.

Figure 9.19 Rig Balancing the tree using double right-left rotation in left of right case

unbalance tree

Analysis of Algorithm for Binary Search Using the Binary Search Tree Height is one of the most important characteristics of a tree and as previously stated, AVL tree is a height balanced tree. Therefore, it is bounded by logarithmic functions. The height h of any AVL tree with n nodes satisfies the inequalities.

[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. [Application of Transform-and-Conquer Algorithm Design Technique

Transform-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STI

Page 25 of 30

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

Design and Analysis of Algorithm

* Property of STI

Page 26 of 30

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

Design and Analysis of Algorithm

* Property of STI

Page 27 of 30

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.

Page 16: MELJUN CORTES ALGORITHM Transform-And-Conquer Algorithm Design Technique II

Design and Analysis of Algorithm

Divide-and-ConquerAlgorithm Design Technique * Property of STI Page 16 of 18

in Binary Search Tree, Pages 14-27 of 30]

Problem Reduction Page 28 of 30

Page 29 of 30

Problem Reduction There are instances that problems only look like difficult to answer. A difficult problem may perhaps the one that can be reduced to a number of easier problems. And once each of the simple problem is solved, the difficult problem is also solved. This is actually the fundamental perception behind the problem reduction algorithm design technique. Problem reduction is referred to as the process of transforming a given problem to another problem that can be solved by a known algorithm. [LEV07]. The figure below illustrates the Problem reduction strategy:

Figure 9.20 Problem reduction strategy

The idea of problem reduction plays a major role in theoretical computer science, where it is used to classify problems according to their complexity. Example for Problem Reduction To clearly understand problem reduction, let us consider Spacing to Center Text problem. To center text, we need to indent it ourselves by using an appropriate number of space characters. The complication is that we have two types of spaces that vary in width: the usual space and option-space (also known as non-breaking space). Given three numbers a, b, and c, where:

a is the width of a normal space

b is the width of an option-space

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. [Problem Reduction, Pages 28-29 of 30]

Transform-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STI

Page 28 of 30

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

Design and Analysis of Algorithm

* Property of STI

Page 29 of 30

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.

Page 17: MELJUN CORTES ALGORITHM Transform-And-Conquer Algorithm Design Technique II

Design and Analysis of Algorithm

Divide-and-ConquerAlgorithm Design Technique * Property of STI Page 17 of 18

SEATWORK: Balance the following tree:

Answer: This tree is a Right-of-Left case of unbalanced tree. To balance it, we are to use the Double Left-Right Rotation. To do so, rotate the left subtree, which is 57, of the unbalanced node, which is 72, to the left to create a Left-of-Left case then rotate the unbalanced node, 72, to the right to balance the tree.

GENERALIZATION:

o Transform-and-Conquer is an algorithm design technique in solving a problem by modifying instance to a simple one applied in the same

or different problem. o Presorting is considered as an old idea in computer science where

you sort the data and that allows you to more easily compute some answer.

o Gaussian Elimination is an algorithm solving a system of n linear equations in n unknowns and was named after Carl Gauss who contributed significantly to various fields, including number theory, statistics, analysis, differential geometry, geodesy, electrostatics, astronomy and optics.

o Binary search tree is a type of binary tree that is composed of nodes that contains elements of a set of ordered items because all

72

57 82

52 62

67

1st rotate

57 left

72

62 82

57 67

52

2nd

rotate 72 right

Page 18: MELJUN CORTES ALGORITHM Transform-And-Conquer Algorithm Design Technique II

Design and Analysis of Algorithm

Divide-and-ConquerAlgorithm Design Technique * Property of STI Page 18 of 18

of the elements of the left subtree should be less than the root and the elements of the right subtree should be greater than or equal to the root.

o AVL (Adelson-Velskii tree and Landis) tree which requires the difference between the height of left and right subtrees.

o The types of cases in AVL tree are the left of left, right of right, left of right, and right of left cases.

o The types of rotation in AVL tree are single left rotation, single right rotation, double left-right rotation and double right-left rotation.

o Problem reduction calls for transforming a given problem to another problem that can be solved by a known algorithm. Two types of graph data structure are directed graph (Digraph) and undirected graph.

REFERENCES:

http://www1.pacific.edu/~mdoherty/comp157/lectures/071017b_lecture.pdf

http://www.math.uaa.alaska.edu/~afkjm/cs351/handouts/transform-conquer.pdf

http://en.wikipedia.org Anany Levitin,(2007), The design and analysis of algorithm (2nd

ed.), Pearson Education Inc.