You are on page 1of 19

# Design and Analysis of Algorithm

## TOPIC TITLE: Divide-and-Conquer Algorithm Design Technique

Specific Objectives:
At the end of the topic session, the students are expected to:
Cognitive:
1. Define Divide-and-Conquer algorithm design technique.
technique
2. Identify the importance of Divide-and-Conquer algorithm design
technique in solving different problem types.
3. Explain how Divide-and-Conquer algorithm design technique is
applied to sorting algorithms, specifically Merge and Quick sort.
4. Discuss the application of Divide-and-Conquer algorithm design
technique in Binary Search and Binary Tree traversals.
traversals
Affective:
1. Listen to others with respect.
2. Participate in class discussions actively.
3. Share ideas to the class.
MATERIALS/EQUIPMENT:
o
o

topic slides
OHP

TOPIC PREPARATION:
o

o
o
o

o
o

## Have the students research on the following:

Divide-and-Conquer algorithm and its application
Merge sort and Quick sort algorithm
Binary search algorithm and Binary tree traversals
traversal
The research work should be in a printed copy compiled in a
folder with a title page.
Provide sample problems that can be solved by using Divideand-Conquer algorithm design technique.
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
e a thorough and creative discussion of the topic.
Prepare the slides to be presented in the class.
Prepare seatwork for the students
ts to apply the lessons learned
in Divide-and-Conquer algorithm design technique.

Divide-and-ConquerAlgorithm
ConquerAlgorithm Design Technique

* Property of STI
Page 1 of 19

## Design and Analysis of Algorithm

TOPIC PRESENTATION:
The topic will cover the application of Divide-and-Conquer
Conquer algorithm
design technique
technique.
The following is the suggested flow of discussion for the course topic:
1. Identify the topics to be covered.
2. Ask the students to define Divide-and-Conquer algorithm design
technique and cite some algorithms where it can be applied.
applied
3. Discuss Divide-and-Conquer algorithm design technique and
provide an example.
4. Enumerate the importance of Divide-and-Conquer
Conquer algorithm
design technique.
5. Ask the students to differentiate Merge Sort from Quick Sort.
6. Discuss merge sort and quick sort by explaining how DivideDivide
and-Conquer
Conquer algorithm design technique is applied to these
types of sorting algorithm.
and binary tree traversals.
8. Explain the application of Divide-and-Conquer
Conquer algorithm design
technique to binary search and binary tree traversals.
Divide-and-Conquer Algorithm
Design Technique
Page 1 of 28

Divide
Divide-and-Conquer Design Technique
These are the topics to be discussed under Divide-and-Conquer
Divide
algorithm design ttechnique.

## Design and Analysis of Algorithm

Divide-and-Conquer
Algorithm Design Technique
c Definition of Divide-and-Conquer algorithm design
technique
c Importance of Divide-and-Conquer algorithm
design technique
c Application of Divide-and-Conquer algorithm
design technique in the following algorithms:
q Merge sort
q Quick sort
q Binary search
q Binary tree traversals

o
o
o

## Definition of Divide-and-Conquer algorithm design technique

t
Importance of Divide-and-Conquer algorithm design technique
Application of Divide-and-Conquer algorithm design
esign technique in
the following algorithms:
o Merge sort
o Quick sort
o Binary search
o Binary tree traversals

[Divide
Divide-and-Conquer Algorithm Design Technique, Page 1 of 28]

## Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 1 of 28

Divide-and-ConquerAlgorithm
ConquerAlgorithm Design Technique

* Property of STI
Page 2 of 19

## What is Divide-and-Conquer Algorithm Design Technique?

Technique
What is Divide-and-Conquer
Algorithm Design Technique?

Page 2 of 28

Divide
Divide-and-Conquer is a general
neral algorithm design technique that solves
a problems instance by dividing it into several smaller instances (ideally
of equal size), solving each of them recursively, and then combining
their solutions to get a solution to the original instance of the problem
[LEV07].

## What is Divide-andConquer Algorithm Design

Technique?
c Divide-and-Conquer
q

## solves a problems instance by dividing it into

several smaller instances, solving each of
them recursively, and then combining their
solutions to get a solution to the original
instance of the problem [LEV07].

## Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 2 of 28

## It simply means that it is done by breaking down a specific problem into

sub-problems
problems of the same type repeatedly, until it become simple
enough to be solved directly. Afterwards, solutions to the sub-problems
sub
are then combined to give a solution to the original problem.
problem
On the other hand, the capability to comprehend and design Divide-andConquer algorithms is a skill that should be practiced to become
proficient. For instance, in proving a theorem by induction,
inducti
it is often
needed to substitute the original problem by a more general or complex
problem for us to get the recursion going, and there is no systematic
method for finding the proper generalization. The correctness of a divide
and conquer algorithm is usually proved by mathematical induction and
its computational cost which is determined by solving recurrence
relations
relations.
General Divide
Divide-and-Conquer Algorithm
Step 1: If the problem size is small, solve this problem directly;
otherwise, split the original problem into 2 sub-problems
problems with equal
sizes.
Step 2: Recursively solve these 2 sub-problems
problems by applying this
algorithm.
Step 3: Merge the solutions of the 2 sub-problems
problems into a solution of
the original problem.
A simple example of Divide-and-Conquer algorithm

What is Divide-and-Conquer
Algorithm Design Technique?
Page 3 of 28
Design and Analysis of Algorithm

Technique?
c Example:
q

## Lets take a simple example of Divide

Divide-and-Conquer
Conquer algorithm for
computing the sum of n numbers a0.an-1, If n>1, we can divide the
problem into two instances of the same problem: to compute the sum of
the first [n/2] numbers and to compute the sum of the remaining [n/2]
numbers. Of course if n=1, we simply return a0 as the answer. One of
these two sums is computed (by applying the same method recursively),
we can add their values to get the sum question:

## a0 + ...+ an-1 = (a0 + + a[n/2]-1) + (a[n/2]++an-1)

Maybe we can ask if this is the efficient way to compute the sum of n
numbers. Not all divide-and-conquer
conquer algorithms are necessary to more
efficient than any algorithm even a brute force algorithm solution. But
the spent on time executing the divide-and-conquer
conquer algorithm turns out
to be smaller and it yields some of the most important and efficient
algorithms in computer science.

## Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 3 of 28

## The figure below illustrates an example of Divide

Divide-and-Conquer
Conquer algorithm
in typical case:

Divide-and-ConquerAlgorithm
ConquerAlgorithm Design Technique

* Property of STI
Page 3 of 19

## Figure 7.2 Example of Divide-and-Conquer

Conquer algorithm application

## The figure shows that the most typical case of divide-and-conquer

divide
algorithm is the instance of a problem which in this case the instance of
size n is divided into two main instances of size n/2. Generally, the
instance
tance of size n can be divided into b instances of size n/b, with a of
them needing to be solved. Here a and b are constants: a = q and b > 1,
assuming that the size n is a power of b to simplify our analysis, we get
the following recurrence for the runnin
running time T(n):

## T(n) = aT(n/b) + f(n)

Where f(n) is a function that accounts for the time spent on dividing the
problem into smaller ones and on combining their solutions. Obviously,
the order of growth of its solution t(n) depends on the values of the
constants a and b and the order of growth of the function f(n).
In conclusion, the Divide and conquer technique leads to a kind of
recurrence formula: T (n) 2T (n/2) + c n and
nd time O(n log n).
n)
[What
What is Divide-and-Conquer
Conquer Algorithm Design Technique?,
Technique Pages
2-3
3 of 28
28]
Importance of Divide
Divide-and-Conquer Algorithm Design
esign Technique
This approach allows more freedom in the choice of the sub-problem
sub
that is to be solved next, a feature that is important in some applications.
applications
1. Solving difficult problems
Divide and conquer algorithm design technique is an influential
tool for solving theoretically tough problems which entails
breaking problems into sub-problems
problems and solving it in trivial
cases. In reality, divide and conquer algorithm is considered
con
as
a paradigm in providing simple solution to difficult problems.

Divide-and-ConquerAlgorithm
ConquerAlgorithm Design Technique

* Property of STI
Page 4 of 19

## Design and Analysis of Algorithm

Importance of Divide-andConquer Algorithm
Page 4 of 28

2. Algorithm efficiency

## Importance of Divide-andConquer Algorithm

c Solving difficult problems
c Algorithm efficiency
c Parallelism
c Memory Access

## Divide and conquer often offers an ordinary way of creating

efficient algorithms. For example, if the work of splitting the
problem and combining the partial solutions is proportional to
the problem's size n, there are a bounded number p of subsub
problems of size ~ n/p at each stage, and the base cases
require O(1) (constant-bounded)
bounded) time, then the divide-anddivide
conquer algorithm will have O(n log n) complexity. This is used
for problems such as sorting and FFTs to reduce the complexity
2
from O(n ), although in general there may also be other
approaches to designing efficient algorithms.
3. Parallelism

## Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 4 of 28

## Divide and conquer algorithm design technique is tailored for

implementation in multi-processor
processor machines, especially sharedshared
memory systems where the communication of data between
processors does not need to be planned in advance, because
different sub-problems
problems can be executed on different processors.
4. Memory Access
Divide-and-conquer
conquer algorithms design technique is expected to
use memory caches efficiently. This is because once a subsub
problem is small enough, it can be solved within the cache
without accessing the slower main memory.
In addition, it is also an algorithm designed to make use of the
cache in this way that is referred to as the cache oblivious
obliviou
since it does not contain the cache size(s) as an explicit
parameter. In fact, it can be applied to different algorithms
algorithm such
as sorting, matrix multiplication.
[Importance
Importance of Divide
Divide-and-Conquer
Conquer Algorithm, Page 4 of 28]
28

Divide-and-Conquer in Merge
Sort
Page 5 of 28
Design and Analysis of Algorithm

Divide-and-Conquer
in Merge Sort

Merge sort is a type of sorting algorithm that combines two files ordered
into one ordered file on the same given key. It is a perfect example of a
successful application of the divide
divide-and-conquer
conquer technique.
Pseudo code of Merge Sort algorithm

c Merge sort
q

Merge Sort

## combines two files ordered into one ordered

file on the same given key

The figure below illustrates the Pseudo code for Merge sort algorithm:

## c Pseudocode of Merge Sort

Merge-Sort A[1..n]
1 if n =1, done.
2 Recursively sort A[1..[n/2] ] and A[[n/2]+1.. n ]
3 Merge the two sorted lists
Divide-and-Conquer Algorithm Design Technique

Figure 7.3
3 Pseudo code for Merge sort algorithm

* Property of STI
Page 5 of 28

The figure shows that Merge sort algorithm is consists of three main
steps, these are:
1. Divide the array into two sub-arrays
arrays each with n/2 items.
Divide-and-ConquerAlgorithm
ConquerAlgorithm Design Technique

* Property of STI
Page 5 of 19

## Design and Analysis of Algorithm

Divide-and-Conquer in Merge
Sort
Page 6 of 28

## 2. Conquer (solve) each sub-array

array by sorting it. Unless the array is
sufficiently small, use recursion to do this.
3. Combine the solutions to the sub-array
array by merging them into a
single sorted array.

## Design and Analysis of Algorithm

Divide-and-Conquer
in Merge Sort
c Example of Merge Sort:
q

29

12

14

22

27

15

27

15

17

24

c Solution:
1.

22

17

24

12 14 22

3.

## To understand Merge sort algorithm, lets examine a sample problem.

For instance, you have a one
one-dimensional
dimensional array that contains the
following values:

29 12 14

2.

29

15

17

24

27

29

12

14

22

15

17

24

## 1. Divide the array:

29
Divide-and-Conquer Algorithm Design Technique

27

Figure 7.4 One dimensional array list for Merge sort algorithm example

12

14

22

17

24

and

* Property of STI
Page 6 of 28

27

15

Figure 7.5
5 The illustration of the one dimensional array after dividing the array

## 2. Sort each sub-array:

12

14

22

29

24

27

and

15

17

Figure 7.6 The illustration of the sorted one dimensional array after dividing the array

## 3. Merge the sub-arrays:

12
Divide-and-Conquer in Merge
Sort
Page 7 of 28

14

15

17

22

24

27
2

29

Figure 7.7 The illustration of the sorted one dimensional array using Merge sort

## The illustration below is the simulation of the Merge sort algorithm:

Design and Analysis of Algorithm

Divide-and-Conquer
in Merge Sort
c Simulation of the Merge sort algorithm:

## Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 7 of 28

Divide-and-ConquerAlgorithm
ConquerAlgorithm Design Technique

* Property of STI
Page 6 of 19

## Figure 7.8 The simulation of Merge sort algorithm

Divide-and-Conquer in Merge
Sort
Page 8 of 28
Design and Analysis of Algorithm

Divide-and-Conquer
in Merge Sort
c Analysis of algorithm of Merge sort using the
Divide-and-Conquer approach

## Analysis of algorithm for Merge sort Applying Divide-and-Conquer

Divide
algorithm
The conquer step of merge
merge-sort consists of merging two sorted
sequences, each with n/2 elements and implemented by means of a
doubly linked list, takes at most bn steps, for some constant b. Likewise,
the basis case ((n < 2) will take at b most steps.
Therefore, if we let T(n) denote
enote the running time of merge sort we will
come up with the formula below:

## Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 8 of 28

## We can therefore analyze the running time of merge

merge-sort
sort by finding a
closed form solution to the above equation. That is, a solution that has
T(n)) only on the left-hand side.

Divide-and-ConquerAlgorithm
ConquerAlgorithm Design Technique

* Property of STI
Page 7 of 19

## Figure 7.9 Time complexity of Merge Sort algorithm

Therefore,
refore, the Divide
Divide-and-conquer algorithm is simplified to different
steps which indicates that:
for the ith
th merging iteration, the complexity of the problem is
O(n)
number of iterations is O(log n)
running time: O(n log n)
[Divide
Divide-and-Conquer in Merge Sort, Pages 5-8 of 28]]
Divide-and-Conquer in Merge
Sort
Page 9 of 28
Design and Analysis of Algorithm

Divide-and-Conquer
in Quick Sort

Application of Divide
Divide-and-Conquer Algorithm Design Technique in
Quick Sort
Quick sort is an exchange sort developed by C.A.R. Hoare in 1962. It is
a sorting algorithm that is quite similar with Merge sort
ort algorithm where
sorting is done by dividing the array into two partitions and then sorting
sor
each partition recursively
recursively.

c Quick sort
q

## an exchange sort developed by C.A.R. Hoare in

1962
sorting is done by dividing the array into two
partitions and then sorting each partition
recursively
a pivot key is placed in its correct position in
the array while rearranging other elements
widely dispersed across the list

## In Quick sort, however, the arrays is partitioned by placing a pivot key in

its correct position in the array while rearranging other elements widely
dispersed across the list. Specifically, it rearranges elements of a given
array
ray A[0n-1] to achieve its partition, a situation where all the elements
before some position s are smaller than or equal to A[s] and all the
elements after position s are greater to A[s]:
? ?? ?g ? ?? ? ? ? ? ??? ? ?? ? ? ?g ? ?? ? ? ?

## Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 9 of 28

## All are =A[s]

When tthe partition has been achieved, A[s] will be in its final position in
the sorted array, and we can continue sorting the two sub-arrays
sub
of the
elements preceding and following A[s] independently.

Divide-and-ConquerAlgorithm
ConquerAlgorithm Design Technique

* Property of STI
Page 8 of 19

## Design and Analysis of Algorithm

Divide-and-Conquer in Merge
Sort
Page 10 of 28

## Pseudo code of Quick Sort algorithm

The figure below illustrates the Pseudo code for Quick sort algorithm:

## Design and Analysis of Algorithm

Divide-and-Conquer
in Quick Sort
c Pseudo code for Quick sort algorithm

1
2
3
4

5
6
q
q

q
q

QuickSort (Array[ ])
If length (array)>1
Choose bound; //partition array into subArray1 and
subArray2
while there are elements left in array
1 include element either in subArray1 = {target = bound}
2 or in subArray2 = {target = bound};
QuickSort(subArray1);
QuickSort(subArray2);

## Select one element target from the input

Partition the input into part containing
elements not greater than target and part
containing all bigger elements.
Sort each part separately.
Concatenate these sorted parts.

## Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 10 of 28

Divide-and-Conquer in Merge
Sort
Page 11 of 28
After Pass 2

## Figure 7.10 Pseudo code for Quick sort algorithm

The figure shows that Quick sort algorithm is consists of different steps
as follows
follows:
1. Select one element target from the input
2. Partition the input into part containing elements not greater than
target and part containing all bigger elements.
3. Sort each part separately.
4. Concatenate these sorted parts.

## Design and Analysis of Algorithm

Divide-and-Conquer
in Quick Sort

## To understand Quick sort algorithm, lets examine a sample problem.

For instance, you have a one
one-dimensional
dimensional array that contains the
following values:

q

## Given the original unsorted list:

Solution:
1. Partition the array :

## Example of Quick Sort

16

23

14

28

13

11

21
2

26

Figure 7.11 One dimensional array list for Quick sort algorithm example

1. Partition the array so that all items smaller than the pivot item are to
the left of it and the items larger are to the right
right:
Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 11 of 28

Divide-and-Conquer in Merge
Sort
Page 12 of 28
After Pass 2

## Design and Analysis of Algorithm

Divide-and-Conquer
in Quick Sort
c Example of Quick sort algorithm
q

## Given the original unsorted list:

Solution:
2. Sort each sub-array:

Figure 7.12 The illustration of the one dimensional array after dividing the array

## 2. Sort each sub-array:

Figure 7.13 The illustration of the sorted one dimensional array after dividing the array

## 3. Merge the sub

sub-arrays:
Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 12 of 28

Divide-and-ConquerAlgorithm
ConquerAlgorithm Design Technique

* Property of STI
Page 9 of 19

## Design and Analysis of Algorithm

Divide-and-Conquer in Merge
Sort
Page 13 of 28
After Pass 2

11

13

14

16

21

23

26
2

28

Figure 7.13 The illustration of the sorted one dimensional array using Quick sort

## Design and Analysis of Algorithm

Divide-and-Conquer
in Quick Sort

q

## Given the original unsorted list:

Solution:
3. Merge sub-arrays:

## Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 13 of 28

Divide-and-Conquer in Merge
Sort
Page 14 of 28
After Pass 2

## Design and Analysis of Algorithm

Divide-and-Conquer
in Quick Sort

## Analysis of algorithm for Quick sort applying

pplying Divide-and-Conquer
Divide
algorithm

## Generally, the quick sort efficiency is O(nlog2n).

For the analysis of w
worst-case time complexity,, we will consider the
formula below:

## Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 14 of 28

Divide-and-Conquer in Merge
Sort
Page 15 of 28
Design and Analysis of Algorithm

## In the given formula, w

we are using the notation T(n)) because we are
presently determining the every
every-case
case complexity for the class of
instances that are already sorted in non
non-decreasing
decreasing order.
Since, T(0) = 0, we have the recurrence:

Divide-and-Conquer
in Quick Sort
c Analysis of algorithm of Quick sort using the
Divide-and-Conquer technique
q

## Divide-and-Conquer in Quick Sort, Pages 9-15

15 of 28]
28
[Divide

* Property of STI
Page 15 of 28

Divide-and-ConquerAlgorithm
ConquerAlgorithm Design Technique

* Property of STI
Page 10 of 19

## Design and Analysis of Algorithm

Divide-and-Conquer in Binary
Search
Page 16 of 28
Design and Analysis of Algorithm

Divide-and-Conquer
in Binary Search
c Binary search
q
q

## used to locate an element in an ordered array

starts by testing the data in the element at the
middle of the array to determine if the target
is in the first or second half of the list

## Application of Divide-and-Conquer Algorithm Design Technique in

Binary search
Binary search is a type of search algorithm that is used to locate an
element in an ordered array. It is advisable to use a binary search
whenever the list starts to become larger.
Binary search starts by testing the data in the element at the middle of
the array to determine if the target is in the first or second half of the list.
If it is in the first half, we do not need to test the second half. Same goes
with the second half. In other words, we eliminate half the list from
further consideration. We repeat this process until we find the target to
determine that it is not in the list.
To find the middle of the list, we need three variables to identify the
beginning
beginning, the middle, and the end of the list. We analyze two cases
here: the target that is in the list and the target that is not in the list.

## Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 16 of 28

The divide
divide-and-conquer steps of Binary search:
The steps of Binary search starts by testing the target. If target is equal
to the middle item, quit.
Otherwise:
1. DIVIDE the array into two sub-arrays
arrays about half as large. If
target is smaller than the middle item, choose the left subsub
array. If target is larger than the middle item, choose the
right sub-array.
2. DIVIDE (Solve) the sub-array
array by determining whether target
is in that sub-array. Unless the sub-array
array is sufficiently
small, use recursion to do this.
3. OBTAIN the solution to the array from the solution to subsub
array.

Divide-and-ConquerAlgorithm
ConquerAlgorithm Design Technique

* Property of STI
Page 11 of 19

## Design and Analysis of Algorithm

Divide-and-Conquer in Binary
Search
Page 17 of 28

## BinarySearch (val list <array>,

val end <index>,
val target <keyType>
val locn <index>)
1 first = 0
2 last = end
3 loop ( first <= last)
1 mid = (first + last)/2
2 if (target > list[mid])
Look in upper half
1 first = mid + 1
3 else if (target < list[mid])
Look in lower half
1 Last = mid 1
4 else
Found equal: force exit
1 first = last + 1
5 end if
4 end loop
5 locn = mid
6 if (target equal list [mid])
1 found = true
7 else
1 found = false
8 end if
9 return found
end BinarySearch

## Design and Analysis of Algorithm

Divide-and-Conquer
in Binary Search
c Pseudo code for Binary search algorithm

## Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 17 of 28

## Figure 7.15 Pseudo

do code for Binary search algorithm
Divide-and-Conquer in Binary
Search
Page 18 of 28
Design and Analysis of Algorithm

Divide-and-Conquer
in Binary Search
c Example:

## Example of Binary search algorithm

To understand Quick sort algorithm, lets examine a sample problem.
For instance, you have a one
one-dimensional
dimensional array that contains the
following values:

5 8

11 15 22 23 37 63 78
7 82 92

Figure 7.16 One dimensional array list for Binary search algorithm example

Solution:

The figure below illustrated the process of searching the 23 as the target
value in the given array list:

## Divide-and-Conquer Algorithm Design Technique

Firs
First

Mid

Last

Target:
23

11

* Property of STI
Page 18 of 28

5 8

11 15 22 23 37 63 78 82 92

23 > 22
Divide-and-ConquerAlgorithm
ConquerAlgorithm Design Technique

* Property of STI
Page 12 of 19

## Design and Analysis of Algorithm

5 8

5 8

First

Mid

Last

11

11 15 22 23 37 63 78 82 92
First

Mid

Last

23 < 63

11 15 22 23 37 63 78 82 92

23 = 23
Figure 7.17 Binary search algorithm example

In the
he illustration
illustration, the array length is 12 (0-11 indices).
indices
The binary
search process starts by getting the middle index through adding the first
and last index then dividing the sum by two. Hence, (0+11)/2 = 5. The
middle index (5) has an element of 22 that will be compared to the target
value which is 23. Since 23 is not equal to 22 then the search value is
not yet found. Afterwards, a comparison will be done ifi the target value
is greater than or less than the current value. If the target is greater than
the value, we will proceed to the upper bound
bound. Otherwise,
Otherwise we will
proceed to the lower bound of the given array. The process will be
repeated until it finds the target or not.
Divide-and-Conquer in Binary
Search
Page 19 of 28
Design and Analysis of Algorithm

Divide-and-Conquer
in Binary Search
c Analysis of algorithm of Binary search using
the Divide-and-Conquer approach

## Analysis of algorithm for Binary search using the Brute Force

The standard way to analyze the efficiency of binary search is to count
the number of times the search key is compared with the element of the
array. Moreover, for the sake of simplicity, we will count the so-called
so
three way comparisons. This assumes th
that
at after one comparison of K
with A[m], the algorithm can determine whether K is smaller, equal to, or
larger than A[m].

* Property of STI
Page 19 of 28

[Divide
Divide-and-Conquer in Binary search, Pages 16-19
19 of 28]
28

Divide-and-ConquerAlgorithm
ConquerAlgorithm Design Technique

* Property of STI
Page 13 of 19

## Design and Analysis of Algorithm

Divide-and-Conquer in Binary
Tree Traversals
Page 20 of 28
Design and Analysis of Algorithm

Divide-and-Conquer in
Binary Tree Traversals

Application of Divide
Divide-and-Conquer
Conquer Algorithm Design Technique in
Binary Tree Traversals
Binary Tree is a type of tree data structure in which no node can have
more than two subtrees. In other words, a node can have zero, one, or
two subtrees. These subtrees are designated as the left subtree and
right subtree.

c Binary Tree
q

binary tree:

Left
Subtree

## Divide-and-Conquer Algorithm Design Technique

Right
Subtree

* Property of STI
Page 20 of 28

Divide-and-Conquer in Binary
Tree Traversals
Page 21 of 28
Figure 7.18 Binary tree
Design and Analysis of Algorithm

Divide-and-Conquer in
Binary Tree Traversals
c Balance factor
q

## height difference between the left subtree and

the right subtree: B = H L HR

A
B

A
C

B=0
B = -1

A
B

B=1

## In the given figure, it shows that o

one
ne of the properties of
o the Binary tree
is its height. Given that we need to store N,, which refers to the number
of nodes in a binary tree, the maximum height: Hmax = N. But a tree with
maximum height is rare. It occurs when the entire tree is built in one
direction. The minimum height of a tree Hmin = [log2N] + 1.
1

* Property of STI
Page 21 of 28

The distance of a node from the root determines how efficiently it can be
located
located.. The children of any node in a tree can be accessed by
following only one branch path. In addition, it is easier to locate a node in
i
a shorter tree.
Balancing of tree is an important characteristic of a binary tree to
determine whether the tree is balanced factor. A balance factor is the
height difference between the left subtree and the right subtree. B = HL
HR.
Example:

B
HL = 1; HR = 1
B = H L HR
B=0

A
C

D
HL = 2; HR = 1
B = HL HR
B=1

C
D

HL = 1; HR = 2
B = HL HR
B = -1

## A binary tree is considered balanced if the height of its subtrees differs

by no more than one. Its balance factor is -1,
1, 0, and 1 considering that
each subtree is also balanced.

Divide-and-ConquerAlgorithm
ConquerAlgorithm Design Technique

* Property of STI
Page 14 of 19

## Design and Analysis of Algorithm

Divide-and-Conquer in Binary
Tree Traversals
Page 22 of 28
Design and Analysis of Algorithm

Divide-and-Conquer in
Binary Tree Traversals
c Algorithm for defining the height of a binary
tree

Algorithm for d
defining the height of a binary tree

Algorithm Height(T)
//Compute recursively the height of a binary tree
//Input: A binary tree T
//Output: The height of T
If T = 0 return -1
else return max[Height(TL), Height(TR) + 1
Figure 7.19 Pseudo code for determining the height of a binary tree

## We measure the problems instance size by the number of nodes n(T) in

a given tree T. Obviously, the number of comparisons made to compute
the maximum of two numbers and the number of addition A(n(T)) made
by the algorithm are the same. We have the following relation
relatio for
A(n(T)):

* Property of STI
Page 22 of 28

## A(n(T)) = A(n(TL)) + A(n(TR) + 1 for n(T) > 0, A(0)=)

Divide-and-Conquer in Binary
Tree Traversals
Page 23 of 28
Design and Analysis of Algorithm

Divide-and-Conquer in
Binary Tree Traversals
c Binary tree traversal
q

## requires that each node of the tree be

processed once and only once in a
predetermined sequence.

## c Approaches to the traversal sequence:

q

Depth-first traversal
the processing proceeds along a path from the
root through one child to the most distant
descendent of that first child before processing
a second child

the processing proceeds horizontally from the
root to all of its children, then to its childrens
children, and so forth until all nodes have been
processed

## Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 23 of 28

Divide-and-Conquer in Binary
Tree Traversals
Page 24 of 28
Design and Analysis of Algorithm

Divide-and-Conquer in
Binary Tree Traversals
c Types of Depth-first search Binary Tree
traversals :
q

## Binary Tree Traversal

A binary tree traversal requires that each node of the tree be processed
once and only once in a predetermined sequence. The two general
approaches to the trave
traversal
rsal sequence are depth first and breadth first.
Dept
Depth-first traversal is where the processing proceeds along a path from
the root through one child to the most distant descendent of that first
child before processing a second child. In other words, in the depth-first
depth
search traversal, all of the descendents of a child are processed before
going on to the next child.
Breadth-first traversal is where the processing proceeds horizontally
from the root to all of its children, then to its childrens children, and so
forth until all nodes have been processed. In other words, in the
breadth-first traversal, each level is completely processed before the
next level is started.
Types of Depth
Depth-first search Binary Tree traversals
There are th
three types of depth-first
first search binary tree traversal, the
preorder, inorder and postorder traversals.
In the preorder traversal,, the root is visited first, followed by the left
subtree and then the right subtree.
The figure below illustrates the preorder traversal:

Preorder traversal

Root

Right
Subtree

Left
Subtree

## //Pre-condition: Root is the entry node of a tree or subtree

//Post-condition: Each node has been processed in order
1 if (root is not equal to null)
1 Process (root)
2 preOrder (root -> leftSubtree)
3 preorder (root -> rightSubtree)
2 end if
3 return
Divide-and-Conquer Algorithm Design Technique

## Figure 7.20 Preorder traversal

* Property of STI
Page 24 of 28

Divide-and-ConquerAlgorithm
ConquerAlgorithm Design Technique

* Property of STI
Page 15 of 19

## Algorithm for preorder traversal:

//Pre-condition:
condition: Root is the entry node of a tree or subtree
//Post-condition:
condition: Each node has been processed in order
1. if (root is not equal to null)
1. Process (root)
2. preOrder (root -> leftSubtree)
3. preorder (root -> rightSubtree)
2. end if
3. return

Divide-and-Conquer in Binary
Tree Traversals
Page 25 of 28
Design and Analysis of Algorithm

In the inorder traversal,, the left subtree is visited first, followed by the
root and then the right subtree.
The figure below illustrates the inorder traversal:

Divide-and-Conquer in
Binary Tree Traversals
c Types of Depth-first search Binary Tree
traversals :
q

Inorder traversal
left subtree root right subtree

## //Pre-condition: Root is the entry node of a tree or subtree

//Post-condition: Each node has been processed in order
1 if (root is not equal to null)
1inOrder (root -> leftSubtree)
2 process (root)
3 inOrder(root -> rightSubtree)
2 end if
return
Divide-and-Conquer Algorithm Design Technique

## Figure 7.21 Inorder traversal

* Property of STI
Page 25 of 28

Divide-and-Conquer in Binary
Tree Traversals
Page 26 of 28
Design and Analysis of Algorithm

Divide-and-Conquer in
Binary Tree Traversals
c Types of Depth-first search Binary Tree
traversals :
q

## Algorithm for inorder traversal:

//Pre-condition:
condition: Root is the entry node of a tree or subtree
//Post-condition:
condition: Each node has been processed in order
1. if (root is not equal to null)
1. inOrder (root -> leftSubtree)
2. process (root)
3. inOrder(root -> rightSubtree)
2. end if
return

In the postorder traversal,, the left subtree is visited first, followed by the
right subtree and then the root.
The figure below illustrates the postorder traversal:

Postorder traversal
left subtree right subtree root

## //Pre-condition: Root is the entry node of a tree or subtree

//Post-condition: Each node has been processed in order
1 if (root is not equal to null)
1 postOrder (root -> leftSubtree)
2 postOrder(root -> rightSubtree)
3 process (root)
2 end if
return
Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 26 of 28

Divide-and-ConquerAlgorithm
ConquerAlgorithm Design Technique

## Figure 7.22 Postorder traversal

* Property of STI
Page 16 of 19

## Algorithm for postorder traversal

//Pre-condition:
condition: Root is the entry node of a tree or subtree
//Post-condition:
condition: Each node has been processed in order
1. if (root is not equal to null)
1. postOrder (root -> leftSubtree)
2. postOrder(root -> rightSubtree)
3. process (root)
4.
2. end if
return
Divide-and-Conquer in Binary
Tree Traversals
Page 27 of 28

## Sample application of Binary Tree traversals

Consider the given binary tree below:

## Design and Analysis of Algorithm

Divide-and-Conquer in
Binary Tree Traversals
c Sample application of Binary Tree Traversals:
q

## Consider the given binary tree below:

Preorder: A, B, C, D, E, F, G
Inorder: C, B, D, A, F, E, G
Postorder: C, D, B, F, G, E, A

q
q

## Figure 7.22 Sample binary tree to use depth-first traversals

* Property of STI
Page 27 of 28

## The values of preorder, inorder and postorder

ostorder traversals applied in the
given example are the following:
Preo
Preorder: A, B, C, D, E, F, G
Inorder: C, B, D, A, F, E, G
Postorder: C, D, B, F, G, E, A
Analysis of algorithm for Depth-First traversal algorithm using DivideDivide
and-Conquer
Conquer
The most important divide
divide-and-conquer algorithms for binary trees are
the three classic traversals: preorder, inorder and postorder
ostorder traversals.
All three traversals visit nodes of a binary tree recursively.
[Divide
[Divide-and-Conquer
Conquer in Binary Tree Traversals, Pages 20-27
20
of 28]
SEATWORK:
Draw a binary tree consists of ten nodes based on the given inorder
traversal: ABCEDF
This problem is solved by setting the root of the tree to the first node in
the preorder traversal
traversal, that is J, and then from the inorder traversal,
identifying its left subtrees as A B C E D F and the right subtree as G I
H. The process is repeated with each subtree until the complete tree is
developed.

Divide-and-ConquerAlgorithm
ConquerAlgorithm Design Technique

* Property of STI
Page 17 of 19

## Design and Analysis of Algorithm

SEATWORK
Page 28 of 28
Design and Analysis of Algorithm

SEATWORK

## c Draw a binary tree consists of ten nodes

based on the given:
q
q

## Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 28 of 28

[SEATWORK,
SEATWORK, Pages 28 of 28
28]
GENERALIZATION:
o

o
o

o
o
o
o

## Divide-and-Conquer is a general algorithm design technique that

solves a problems instance by dividing it into several smaller
instances (ideally of equal size), solving each of them
recursively, and then combining their solutions to get a solution.
Divide-and-Conquer
Conquer algorithm design technique is applied in
Merge sort, Quick sort, Binary search and the depth-first
depth
traversals.
Merge sort is a type of sorting algorithm that combines two files
ordered into one ordered file on the same given key. It is a
perfect
ect example of a successful application of the divide-anddivide
conquer technique.
Quick sort is an exchange sort developed by C.A.R. Hoare in
1962.
It is more similar to Merge Sort in that sort is
accomplished by dividing the array into two partitions and then
sorting each partition recursively.
Binary Tree is a type of tree data structure in which no node can
have more than two subtrees. In other words, a node can have
zero, one, or two subtrees.
Binary tree traversal requires that each node of the tree be
processed once and only once in a predetermined sequence.
Dept-first
first traversal is where the processing proceeds along a
path from the root through one child to the most distant
descendent of that
at first child before processing a second child.
first traversal is where the processing proceeds
horizontally from the root to all of its children, then to its
childrens children, and so forth until all nodes have been
processed.
The three depth-first traversals are preorder, inorder and
Postorder traversals.
In the preorder traversal, the root is visited first, followed by the
left subtree and then the right subtree.
In the inorder traversal, the left subtree is visited first, followed
by the root and then the right subtree
In the postorder traversal, the left subtree is visited first, followed
by the right subtree and then the root.

Divide-and-ConquerAlgorithm
ConquerAlgorithm Design Technique

* Property of STI
Page 18 of 19

## Design and Analysis of Algorithm

REFERENCES:
o www.csc.liv.ac.uk/~darek/COMP523/lecture4.ppt
pt
o www.csie.nuk.edu.tw/~cychen/96-1/AA_The%20
1/AA_The%20Divide-andConquer%20Algorithms.ppt
o www.cs.unc.edu/Courses/comp790090f08/Lecturenotes/Lecture
12.ppt
o http://www.cs.uml.edu/~bliu/404-f08/notes/divide
f08/notes/divide-andconquer.pdf
o http://fs.mis.kuas.edu.tw/~jwding/Algorithms/PPT/Ch2_Dividehttp://fs.mis.kuas.edu.tw/~jwding/Algorithms/PPT/Ch2_Divide
and-Conquer.pdf
o http://www.bambooweb.com/articles/d/i/Divide-and
andconquer_algorithm.html
o Anany Levitin,(2007), The design and analysis of algorithm (2nd
ed.), Pearson Education Inc.

Divide-and-ConquerAlgorithm
ConquerAlgorithm Design Technique

* Property of STI
Page 19 of 19