Sei sulla pagina 1di 28

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

Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 1 of 28

Design and Analysis of Algorithm

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

c General Divide-and-Conquer Algorithm:

Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 2 of 28

Design and Analysis of Algorithm

What is Divide-andConquer Algorithm Design


Technique?
c Example:
q

Computing the sum of n numbers

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

Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 3 of 28

Design and Analysis of Algorithm

Importance of Divide-andConquer Algorithm


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

Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 4 of 28

Design and Analysis of Algorithm

Divide-and-Conquer
in Merge Sort
c Merge sort
q

combines two files ordered into one ordered


file on the same given key

c Pseudocode of Merge Sort

Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 5 of 28

Design and Analysis of Algorithm

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

Given the list below:


29

12

14

22

27

15

17

24

27

15

17

24

15

17

24

27

c Solution:
1.

Divide the array:


29 12 14

2.

Sort each sub-array:


12 14 22

3.

22

29

Merge the sub-arrays:

Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 6 of 28

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

Design and Analysis of Algorithm

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

c Merge Sort Time complexity

Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 8 of 28

Design and Analysis of Algorithm

Divide-and-Conquer
in Quick Sort
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

Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 9 of 28

Design and Analysis of Algorithm

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

q
q

q
q

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

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:
1. Partition the array :

Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 11 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:

Divide-and-Conquer Algorithm Design Technique

* Property of STI
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:
3. Merge sub-arrays:

Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 13 of 28

After Pass 2

Design and Analysis of Algorithm

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

Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 14 of 28

Design and Analysis of Algorithm

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

quick sort efficiency: O(nlog2n)

Analysis of Worst-Case Time Complexity

Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 15 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

Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 16 of 28

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

Design and Analysis of Algorithm

Divide-and-Conquer
in Binary Search
c Example:

Solution:

Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 18 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

Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 19 of 28

Design and Analysis of Algorithm

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

no node can have more than two subtrees.

c sample binary tree:

Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 20 of 28

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 = HL HR

A
B

A
C

B=0
B = -1

A
B

B=1

Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 21 of 28

Design and Analysis of Algorithm

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

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

Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 22 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

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

Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 23 of 28

Design and Analysis of Algorithm

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

Preorder traversal
root left subtree 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)
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

Design and Analysis of Algorithm

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

* Property of STI
Page 25 of 28

Design and Analysis of Algorithm

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

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

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

Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 27 of 28

Design and Analysis of Algorithm

SEATWORK

c Draw a binary tree consists of ten nodes


based on the given:
q
q

inorder traversal: ABCEDFJGIH


preorder traversal: JCBADEFIGH

Divide-and-Conquer Algorithm Design Technique

* Property of STI
Page 28 of 28