22 views

Uploaded by MELJUN CORTES, MBA,MPA

MELJUN CORTES ALGORITHM Divide-And-Conquer Algorithm Design Technique 1

- Quad Axis Separation Framework for Bounding-Volume Hierarchies Construction
- Amazon
- Data Structures Assignment
- 11111
- An Engineering Approach to Evolutionary Art (Paper)
- DSA Question Bank
- Merge Sort Algorithm using Haskell
- mergesort
- machine learning chapter 35
- Classification of Algorithms
- Alv Tree Docking
- greedy algo
- Merge Sort
- Binary Search Tree_3
- Expression Tree in Data Structure PDF
- 0B_Bf56Yp8VAQODI1ZS1mRzloelk.pdf
- Tutorial 9
- Data Structure Questions 121031045347 Phpapp02
- Sample Questions
- 234-Trees.pptx

You are on page 1of 19

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

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

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.

7. Ask the students to share their research about binary search

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.

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

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]

* Property of STI

Page 1 of 28

Divide-and-ConquerAlgorithm

ConquerAlgorithm Design Technique

* Property of STI

Page 2 of 19

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].

Technique?

c Divide-and-Conquer

q

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].

* Property of STI

Page 2 of 28

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

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:

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.

* Property of STI

Page 3 of 28

Divide-and-Conquer

Conquer algorithm

in typical case:

Divide-and-ConquerAlgorithm

ConquerAlgorithm Design Technique

* Property of STI

Page 3 of 19

Conquer algorithm application

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):

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

Importance of Divide-andConquer Algorithm

Page 4 of 28

2. Algorithm efficiency

c Solving difficult problems

c Algorithm efficiency

c Parallelism

c Memory Access

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

* Property of STI

Page 4 of 28

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

file on the same given key

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

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

Divide-and-Conquer in Merge

Sort

Page 6 of 28

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.

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.

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

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

12

14

22

29

24

27

and

15

17

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

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

Design and Analysis of Algorithm

Divide-and-Conquer

in Merge Sort

c Simulation of the Merge sort algorithm:

* Property of STI

Page 7 of 28

Divide-and-ConquerAlgorithm

ConquerAlgorithm Design Technique

* Property of STI

Page 6 of 19

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

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:

* Property of STI

Page 8 of 28

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

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

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

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 ? ?? ? ? ?

* Property of STI

Page 9 of 28

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

Divide-and-Conquer in Merge

Sort

Page 10 of 28

The figure below illustrates the Pseudo code for Quick sort 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);

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.

* Property of STI

Page 10 of 28

Divide-and-Conquer in Merge

Sort

Page 11 of 28

After Pass 2

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.

Divide-and-Conquer

in Quick Sort

For instance, you have a one

one-dimensional

dimensional array that contains the

following values:

q

Solution:

1. Partition the array :

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

Divide-and-Conquer

in Quick Sort

c Example of Quick sort algorithm

q

Solution:

2. Sort each sub-array:

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

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

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

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

Divide-and-Conquer

in Quick Sort

q

Solution:

3. Merge sub-arrays:

* Property of STI

Page 13 of 28

Divide-and-Conquer in Merge

Sort

Page 14 of 28

After Pass 2

Divide-and-Conquer

in Quick Sort

pplying Divide-and-Conquer

Divide

algorithm

For the analysis of w

worst-case time complexity,, we will consider the

formula below:

* Property of STI

Page 14 of 28

Divide-and-Conquer in Merge

Sort

Page 15 of 28

Design and Analysis of Algorithm

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

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

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

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

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.

* 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

Divide-and-Conquer in Binary

Search

Page 17 of 28

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

Divide-and-Conquer

in Binary Search

c Pseudo code for Binary search algorithm

* Property of STI

Page 17 of 28

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:

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:

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

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

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

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

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

the right subtree: B = H L HR

A

B

A

C

B=0

B = -1

A

B

B=1

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

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

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

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

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

processed once and only once in a

predetermined 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

Breadth-first traversal

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

* 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

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

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

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

//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

* Property of STI

Page 24 of 28

Divide-and-ConquerAlgorithm

ConquerAlgorithm Design Technique

* Property of STI

Page 15 of 19

//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

//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

* 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

//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

//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

* Property of STI

Page 16 of 19

//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

Consider the given binary tree below:

Divide-and-Conquer in

Binary Tree Traversals

c Sample application of Binary Tree Traversals:

q

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

* Property of STI

Page 27 of 28

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

ABCEDFJGIH and preorder traversal: JCBADEFIGH.

Answer:

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

SEATWORK

Page 28 of 28

Design and Analysis of Algorithm

SEATWORK

based on the given:

q

q

preorder traversal: JCBADEFIGH

* Property of STI

Page 28 of 28

[SEATWORK,

SEATWORK, Pages 28 of 28

28]

GENERALIZATION:

o

o

o

o

o

o

o

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.

Breadth-first

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

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

- Quad Axis Separation Framework for Bounding-Volume Hierarchies ConstructionUploaded byJournal of Computing
- AmazonUploaded bymysticmind.ssk9028
- Data Structures AssignmentUploaded byapi-26041279
- 11111Uploaded byParag Amritkar
- An Engineering Approach to Evolutionary Art (Paper)Uploaded byGarrison LeMasters
- DSA Question BankUploaded byanurekhar
- Merge Sort Algorithm using HaskellUploaded bypi194043
- mergesortUploaded byxcygon
- machine learning chapter 35Uploaded bysurvinderpal
- Classification of AlgorithmsUploaded byRohit Chaudhary
- Alv Tree DockingUploaded byRicky Das
- greedy algoUploaded byRahul Srinivasan
- Merge SortUploaded bysqiks
- Binary Search Tree_3Uploaded byakg299
- Expression Tree in Data Structure PDFUploaded byAmanda
- 0B_Bf56Yp8VAQODI1ZS1mRzloelk.pdfUploaded byYourfriend 2113
- Tutorial 9Uploaded byadchy7
- Data Structure Questions 121031045347 Phpapp02Uploaded bySanthosh Mamidala
- Sample QuestionsUploaded byMBANZABUGABO
- 234-Trees.pptxUploaded bymln35
- Learning Outcome 3 - Ac 2 (Analytical Methods)Uploaded byChan Siew Chong
- pdc.pdfUploaded byfff
- 4_rulesUploaded byPushkal KS Vaidya
- 2-3TreesUploaded byHuy Hóm Hỉnh
- iwpec09.pdfUploaded byhimself
- Quiz QuestionsUploaded byHimanshu Aggarwal
- TextUploaded byBetox94zx
- 1)6-sorting.docUploaded byKarteek Sirisetti
- m3-search(1)Uploaded byapi-3705912
- lec2Uploaded byraw.junk

- MELJUN CORTES Facebook Page Accounts ALL VIDEOS as FB.com/CORTES2525Uploaded byMELJUN CORTES, MBA,MPA
- MELJUN CORTES Facebook Page Account as FB.com/CORTES2525Uploaded byMELJUN CORTES, MBA,MPA
- MELJUN CORTES YOUTUBE Accounts Part 1Uploaded byMELJUN CORTES, MBA,MPA
- MELJUN CORTES YOUTUBE Accounts Part 2Uploaded byMELJUN CORTES, MBA,MPA
- MELJUN CORTES YOUTUBE Accounts Part 1Uploaded byMELJUN CORTES, MBA,MPA
- MELJUN CORTES SOUND CLOUD AccountsUploaded byMELJUN CORTES, MBA,MPA
- MELJUN CORTES - TCU OSAS Strategic Planning 2019Uploaded byMELJUN CORTES, MBA,MPA
- MELJUN CORTES - TCU OSAS Operational Planning 2019Uploaded byMELJUN CORTES, MBA,MPA
- MELJUN CORTES - TCU OSAS Tactical Planning 2019Uploaded byMELJUN CORTES, MBA,MPA
- MELJUN CORTES - DATA SCIENCE 4Uploaded byMELJUN CORTES, MBA,MPA
- MELJUN CORTES - DATA SCIENCE 3Uploaded byMELJUN CORTES, MBA,MPA
- MELJUN CORTES - DATA SCIENCE 2Uploaded byMELJUN CORTES, MBA,MPA
- MELJUN CORTES - DATA SCIENCE 1Uploaded byMELJUN CORTES, MBA,MPA
- MELJUN CORTES OJT 2019 ManualUploaded byMELJUN CORTES, MBA,MPA
- MELJUN CORTES Diploma Masteral MPA 2000Uploaded byMELJUN CORTES, MBA,MPA
- MELJUN CORTES 3rd Metro South Research Congress March 2017Uploaded byMELJUN CORTES, MBA,MPA
- MELJUN CORTES 4th Metro South Research Congress March 2017Uploaded byMELJUN CORTES, MBA,MPA
- MELJUN CORTES 6th TCU University Research Festival August 2017Uploaded byMELJUN CORTES, MBA,MPA
- MELJUN TCU Grades Transmittal CBM 2nd Semester 2018 2019 (June 6 2019)Uploaded byMELJUN CORTES, MBA,MPA
- MELJUN CORTES IPCR June 2019 (Faculty Assistant of OSAS)Uploaded byMELJUN CORTES, MBA,MPA
- TCU Examine Masterlist Admission Report Batch 1 (April 7, 2019)Uploaded byMELJUN CORTES, MBA,MPA
- TCU Examine Masterlist Admission Report Batch 2 (April 14, 2019)Uploaded byMELJUN CORTES, MBA,MPA
- TCU Examine Masterlist Admission Report Batch 3 (April 28, 2019)Uploaded byMELJUN CORTES, MBA,MPA
- MELJUN CORTES - MACHINE LEARNING Part 14Uploaded byMELJUN CORTES, MBA,MPA
- MELJUN CORTES - MACHINE LEARNING Part 13Uploaded byMELJUN CORTES, MBA,MPA
- MELJUN CORTES - MACHINE LEARNING Part 12Uploaded byMELJUN CORTES, MBA,MPA
- MELJUN CORTES - MACHINE LEARNING Part 11Uploaded byMELJUN CORTES, MBA,MPA
- MELJUN CORTES - MACHINE LEARNING Part 10Uploaded byMELJUN CORTES, MBA,MPA
- MELJUN CORTES - MACHINE LEARNING Part 9Uploaded byMELJUN CORTES, MBA,MPA
- MELJUN CORTES - MACHINE LEARNING Part 7Uploaded byMELJUN CORTES, MBA,MPA

- Control Philosophy AC.docxUploaded bywqs_19
- Using_SQLUploaded bychris75726
- slang, jargon and argotUploaded byAfif Kurniawan
- 2151458517750516 (1)Uploaded bybeatrice
- Slalom MathUploaded byGloersen
- Mq90cdr Qsg EnUploaded byHiperion Meister
- 29f2f95b-6b41-44ae-a20c-213d7ac6eb8cUploaded byLytiana Williams
- Accelo BriefUploaded bySilvio Firmino
- on the origin of ours species.pdfUploaded byHenry Choquepata
- Electro-optical synergy (ELOS) technology for combined acne scars.pdfUploaded byAryantii Anty
- 1990 Laubscher Geomechanics Classification SystemUploaded bybuthheadgac
- MEMORANDUM OF COOPERATION updated.docUploaded byZrijana Ruza
- Public SuffixUploaded byBen Degler
- 3020 TDSUploaded byyasafy
- An Investigatory ProjectUploaded byEsther Lois Santiago Alvarade
- Transformasi Nitrogen IIUploaded byrizcha
- Rock Climbing AbseilingUploaded bysamnewsy12
- Moisture-Associated Skin Damage (MASD) -- Tear CollectionUploaded byIchigo Rukia
- ReviewUploaded bySheilla Hermanto
- Opportunity Evaluation - DoormintUploaded byamitcms
- 1TESCOUploaded byCF Wong
- B_Com(Gen_ & Hons_)1st Semester to 6th Semester W_e_f_ the Session 2013-14Uploaded byGorishsharma
- Rethinking the Equity Risk Premium 2011 - CHAPTER 4Uploaded byAndrei Leu
- Kim-2015-Color_Research_&_Application.pdfUploaded byVeljko Džikić
- Asha VoIP v1 0 Implementation Specifications v1 0 EnUploaded byKrishna Kanth
- Excitation SystemUploaded byAnand Swami
- Rate Transition Blocks ExplainedUploaded byjhon
- Millennium Pipeline Company, L.L.C. Eastern System Upgrade, Docket No. CP16-486-000, Expert ReportUploaded by2rQiTPh4Kn
- hp5225_uputstvoUploaded byКатарина Крстић
- LABS ProposalUploaded byXandae Mempin