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

download MELJUN CORTES ALGORITHM Divide-And-Conquer Algorithm Design Technique II

of 12

Transcript of MELJUN CORTES ALGORITHM Divide-And-Conquer Algorithm Design Technique II

  • 8/21/2019 MELJUN CORTES ALGORITHM Divide-And-Conquer Algorithm Design Technique II

    1/28

    Design and Analysis of Algorithm

    D iv i de -and -Conque r

    A l go r i t h m D esi gn Techn i q ue 

    c Definition of Divide-and-Conquer algorithm designtechnique

    c Importance of Divide-and-Conquer algorithm

    design techniquec Application of Divide-and-Conquer algorithm

    design technique in the following algorithms:

    q Merge sort

    q Quick sort

     

    Divide-and-Conquer Algorithm Design Technique * Property of STI 

    Page 1 of 28

    q  

    q Binary tree traversals

  • 8/21/2019 MELJUN CORTES ALGORITHM Divide-And-Conquer Algorithm Design Technique II

    2/28

    Design and Analysis of Algorithm

    Wh at i s D i v i d e - an d -  

    Co n q u er A l go r i t h m D esi gnTechn ique?

    c Divide-and-Conquer

    q solves a problem’s instance by dividing it into

    several smaller instances, solving each ofthem recursively, and then combining theirsolutions to get a solution to the originalinstance of the problem [LEV07].

     

    Divide-and-Conquer Algorithm Design Technique * Property of STI 

    Page 2 of 28

     

  • 8/21/2019 MELJUN CORTES ALGORITHM Divide-And-Conquer Algorithm Design Technique II

    3/28

    Design and Analysis of Algorithm

    Wh at i s D i v i d e - an d -  

    Co n q u er A l go r i t h m D esi gnTech n i q u e?

    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

  • 8/21/2019 MELJUN CORTES ALGORITHM Divide-And-Conquer Algorithm Design Technique II

    4/28

    Design and Analysis of Algorithm

    I m p o r t an ce o f D i v i d e- an d -  

    Co n q u er A l go r i t hm  

    c Solving difficult problems

    c Algorithm efficiency

    c Parallelism

    Divide-and-Conquer Algorithm Design Technique * Property of STI 

    Page 4 of 28

    c Memory Access

  • 8/21/2019 MELJUN CORTES ALGORITHM Divide-And-Conquer Algorithm Design Technique II

    5/28

    Design and Analysis of Algorithm

    D i v i d e - a n d - C o n q u e r

    i n M er ge So r t  

    c Merge sort

    q combines two files ordered into one orderedfile on the same given key

    c Pseudocodeof Merge Sort

    Divide-and-Conquer Algorithm Design Technique * Property of STI 

    Page 5 of 28

  • 8/21/2019 MELJUN CORTES ALGORITHM Divide-And-Conquer Algorithm Design Technique II

    6/28

    Design and Analysis of Algorithm

    D i v i d e - a n d - C o n q u e r

    i n M er ge So r t  

    c Example of Merge Sort:

    q Given the list below:

    c Solution:

    1. Divide the arra :

    29 12 14 22 27 15 17 24

    Divide-and-Conquer Algorithm Design Technique * Property of STI 

    Page 6 of 28

     

    2. Sort each sub-array:

    3. Merge the sub-arrays:

    29 12 14 22 27 15 17 24

    12 14 22 29 15 17 24 27

  • 8/21/2019 MELJUN CORTES ALGORITHM Divide-And-Conquer Algorithm Design Technique II

    7/28

    Design and Analysis of Algorithm

    D i v i d e - a n d - C o n q u e r

    i n M er ge So r t  

    c Simulation of the Merge sort algorithm:

    Divide-and-Conquer Algorithm Design Technique * Property of STI 

    Page 7 of 28

  • 8/21/2019 MELJUN CORTES ALGORITHM Divide-And-Conquer Algorithm Design Technique II

    8/28

    Design and Analysis of Algorithm

    D i v i d e - a n d - C o n q u e r

    i n M er ge So r t  

    c Analysis of algorithm of Merge sort using theDivide-and-Conquer approach

    Divide-and-Conquer Algorithm Design Technique * Property of STI 

    Page 8 of 28

    c Merge Sort Time complexity

  • 8/21/2019 MELJUN CORTES ALGORITHM Divide-And-Conquer Algorithm Design Technique II

    9/28

    Design and Analysis of Algorithm

    D i v i d e - a n d - C o n q u e r

    i n Q u i ck So r t  

    c Quick sort

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

    1962q sorting is done by dividing the array into two

    partitions and then sorting each partitionrecursively

    q a pivot key is placed in its correct position in

    Divide-and-Conquer Algorithm Design Technique * Property of STI 

    Page 9 of 28

     

    widely dispersed across the list

  • 8/21/2019 MELJUN CORTES ALGORITHM Divide-And-Conquer Algorithm Design Technique II

    10/28

    Design and Analysis of Algorithm

    D i v i d e - a n d - C o n q u e r

    i n Q u i ck So r t  

    c Pseudo code for Quick sort algorithm

    Divide-and-Conquer Algorithm Design Technique * Property of STI 

    Page 10 of 28

    q Select one element target  from the input

    q Partition the input into part containingelements not greater than target and partcontaining all bigger elements.

    q Sort each part separately.

    q Concatenate these sorted parts.

  • 8/21/2019 MELJUN CORTES ALGORITHM Divide-And-Conquer Algorithm Design Technique II

    11/28

    Design and Analysis of Algorithm

    D i v i d e - a n d - C o n q u e r

    i n Q u i ck So r t  

    c Example of Quick sort algorithm

    q Given the original unsorted list:

    q Solution:

    After Pass 2 

    Divide-and-Conquer Algorithm Design Technique * Property of STI 

    Page 11 of 28

    1. Partition the array :

  • 8/21/2019 MELJUN CORTES ALGORITHM Divide-And-Conquer Algorithm Design Technique II

    12/28

    Design and Analysis of Algorithm

    D i v i d e - a n d - C o n q u e r

    i n Q u i ck So r t  

    c Example of Quick sort algorithm

    q Given the original unsorted list:

    q Solution:

     

    After Pass 2 

    Divide-and-Conquer Algorithm Design Technique * Property of STI 

    Page 12 of 28

    . -

  • 8/21/2019 MELJUN CORTES ALGORITHM Divide-And-Conquer Algorithm Design Technique II

    13/28

    Design and Analysis of Algorithm

    D i v i d e - a n d - C o n q u e r

    i n Q u i ck So r t  

    c Example of Quick sort algorithm

    q Given the original unsorted list:

    q Solution:

    After Pass 2 

    Divide-and-Conquer Algorithm Design Technique * Property of STI 

    Page 13 of 28

    3. Merge sub-arrays:

  • 8/21/2019 MELJUN CORTES ALGORITHM Divide-And-Conquer Algorithm Design Technique II

    14/28

    Design and Analysis of Algorithm

    D i v i d e - a n d - C o n q u e r

    i n Q u i ck So r t  

    c Simulation of the Quick sort algorithm:

    After Pass 2 

    Divide-and-Conquer Algorithm Design Technique * Property of STI 

    Page 14 of 28

  • 8/21/2019 MELJUN CORTES ALGORITHM Divide-And-Conquer Algorithm Design Technique II

    15/28

    Design and Analysis of Algorithm

    D i v i d e - a n d -C o n q u e r  

    i n Q u i ck So r t  

    c Analysis of algorithm of Quick sort using theDivide-and-Conquer technique

    q quick sort efficiency:O (nlog2n)

    q Analysis of Worst-Case Time Complexity

    Divide-and-Conquer Algorithm Design Technique * Property of STI 

    Page 15 of 28

  • 8/21/2019 MELJUN CORTES ALGORITHM Divide-And-Conquer Algorithm Design Technique II

    16/28

    Design and Analysis of Algorithm

    D i v i d e - a n d - C o n q u e r

    i n B i n ar y Sear ch 

    c Binary search

    q used to locate an element in an ordered array

    q starts by testing the data in the element at themiddle of the array to determine if the targetis in the first or second half of the list

    Divide-and-Conquer Algorithm Design Technique * Property of STI 

    Page 16 of 28

  • 8/21/2019 MELJUN CORTES ALGORITHM Divide-And-Conquer Algorithm Design Technique II

    17/28

    Design and Analysis of Algorithm

    D i v i d e - a n d - C o n q u e r

    i n B i n ar y Sear ch 

    c Pseudo code for Binary search algorithm

    Divide-and-Conquer Algorithm Design Technique * Property of STI 

    Page 17 of 28

  • 8/21/2019 MELJUN CORTES ALGORITHM Divide-And-Conquer Algorithm Design Technique II

    18/28

    Design and Analysis of Algorithm

    D i v i d e - a n d -C o n q u e r  

    i n B i n ar y Sear ch 

    c Example:

    q Solution:

    Divide-and-Conquer Algorithm Design Technique * Property of STI 

    Page 18 of 28

  • 8/21/2019 MELJUN CORTES ALGORITHM Divide-And-Conquer Algorithm Design Technique II

    19/28

    Design and Analysis of Algorithm

    D i v i d e - a n d -C o n q u e r  

    i n B i n ar y Sear ch 

    c Analysis of algorithm of Binary search usingthe Divide-and-Conquer approach

    Divide-and-Conquer Algorithm Design Technique * Property of STI 

    Page 19 of 28

  • 8/21/2019 MELJUN CORTES ALGORITHM Divide-And-Conquer Algorithm Design Technique II

    20/28

    Design and Analysis of Algorithm

    D i v i d e- an d - Co n q u e r i n

    B i n ar y Tr ee Tr aver sa l s 

    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

  • 8/21/2019 MELJUN CORTES ALGORITHM Divide-And-Conquer Algorithm Design Technique II

    21/28

    Design and Analysis of Algorithm

    D i v i d e- an d - Co n q u e r i n

    B i n ar y Tr ee Tr aver sa l s 

    c Balance factor

    q height difference between the left subtreeand

    the right subtree:B = HL – HR

    A A

    Divide-and-Conquer Algorithm Design Technique * Property of STI 

    Page 21 of 28

    B C

    B = 0

    B C

    DB = -1

    A

    B C

    D

    B = 1

  • 8/21/2019 MELJUN CORTES ALGORITHM Divide-And-Conquer Algorithm Design Technique II

    22/28

    Design and Analysis of Algorithm

    D i v i d e- an d - Co n q u e r i n

    B i n ar y Tr ee Tr aver sa l s 

    c Algorithm for defining the height of a binarytree

    Divide-and-Conquer Algorithm Design Technique * Property of STI 

    Page 22 of 28

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

  • 8/21/2019 MELJUN CORTES ALGORITHM Divide-And-Conquer Algorithm Design Technique II

    23/28

    Design and Analysis of Algorithm

    D i v i d e- an d - Co n q u e r i n

    B i n ar y Tr ee Tr aver sa l s 

    c Binary tree traversal

    q requires that each node of the tree beprocessed once and only once in apredetermined sequence.

    c Approaches to the traversal sequence:

     

    Divide-and-Conquer Algorithm Design Technique * Property of STI 

    Page 23 of 28

    q

    ep rs raversa• the processing proceeds along a path from theroot through one child to the most distantdescendent of that first child before processinga second child

    q Breadth-first traversal

    • the processing proceeds horizontally from theroot to all of its children, then to its children’schildren, and so forth until all nodes have beenprocessed

  • 8/21/2019 MELJUN CORTES ALGORITHM Divide-And-Conquer Algorithm Design Technique II

    24/28

    Design and Analysis of Algorithm

    D i v i d e- an d - Co n q u e r i n

    B i n ar y Tr ee Tr aver sa l s 

    c Types of Depth-first search Binary Treetraversals :

    q Preorder traversal• root – left subtree – right subtree

    Divide-and-Conquer Algorithm Design Technique * Property of STI 

    Page 24 of 28

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

  • 8/21/2019 MELJUN CORTES ALGORITHM Divide-And-Conquer Algorithm Design Technique II

    25/28

    Design and Analysis of Algorithm

    D i v i d e- an d - Co n q u e r i n

    B i n ar y Tr ee Tr aver sa l s 

    c Types of Depth-first search Binary Treetraversals :

    q Inordertraversal• left subtree– root – right subtree

    Divide-and-Conquer Algorithm Design Technique * Property of STI 

    Page 25 of 28

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

  • 8/21/2019 MELJUN CORTES ALGORITHM Divide-And-Conquer Algorithm Design Technique II

    26/28

    Design and Analysis of Algorithm

    D i v i d e- an d - Co n q u e r i n

    B i n ar y Tr ee Tr aver sa l s 

    c Types of Depth-first search Binary Treetraversals :

    q

    Postordertraversal• left subtree– right subtree – root

    Divide-and-Conquer Algorithm Design Technique * Property of STI 

    Page 26 of 28

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

  • 8/21/2019 MELJUN CORTES ALGORITHM Divide-And-Conquer Algorithm Design Technique II

    27/28

    Design and Analysis of Algorithm

    D i v i d e- an d - Co n q u e r i n

    B i n ar y Tr ee Tr aver sa l s 

    c Sample application of Binary Tree Traversals:

    q Consider thegivenbinarytreebelow:

    Divide-and-Conquer Algorithm Design Technique * Property of STI 

    Page 27 of 28

    q Preorder: A, B, C, D, E, F, G 

    q Inorder: C, B, D, A, F, E, G 

    q Postorder: C, D, B, F, G, E, A

  • 8/21/2019 MELJUN CORTES ALGORITHM Divide-And-Conquer Algorithm Design Technique II

    28/28

    Design and Analysis of Algorithm

    SEATWORK 

    c Draw a binary tree consists of ten nodes

    based on the given:q inorder traversal:ABCEDFJGIH

    q preorder traversal: JCBADEFIGH