MELJUN CORTES ALGORITHM Transform-And-Conquer Algorithm Design Technique

Embed Size (px)

Citation preview

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

    1/30

    Transform-and-Conquer Algorithm Design Technique

    Design and Analysis of Algorithm

    * Property of STI 

    Page 1 of 30

    Definition of Transform-and-Conquer algorithm

    design technique

    Application of Transform-and-Conquer algorithm

    design technique in the following algorithms: Presorting algorithm

    Gaussian Elimination

    Balanced search tree

    Problem reduction

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

    2/30

    Transform-and-Conquer Algorithm Design Technique

    Design and Analysis of Algorithm

    * Property of STI 

    Page 2 of 30

    an algorithm design technique in solving a

    problem by modifying instance to a simple one

    applied in the same or different problem

    involves three variations :

    1.   Instance simplification

    2.   Representation Change

    3.   Problem Reduction

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

    3/30

    Transform-and-Conquer Algorithm Design Technique

    Design and Analysis of Algorithm

    * Property of STI 

    Page 3 of 30

    Transform-and-Conquer is applied in:

    instance simplification through presorting and

    Gaussian elimination

    representation change through balanced search

    trees, heaps and Horner’s rule

    problem reduction through least common multiple

    and optimization

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

    4/30

    Transform-and-Conquer Algorithm Design Technique

    Design and Analysis of Algorithm

    * Property of STI 

    Page 4 of 30

    Presorting

    data are sorted to make computation easier

    solves instance of problem by transforming into

    another simplified instance of the same problem

    Many problems involving lists are easier to be solved

    when list is sorted.

    searching  computing the median

    checking if all elements are distinct (element

    uniqueness)

    sorting

    Geometric algorithms

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

    5/30

    Transform-and-Conquer Algorithm Design Technique

    Design and Analysis of Algorithm

    * Property of STI 

    Page 5 of 30

    If the array is sorted first, we can then apply the

    binary search which requires only [log 2n]+1comparison in a worst case.

    The total running time in the worst case will be:

    Presorting algorithm is also applied in different

    geometric algorithms engaging with sets of points.

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

    6/30

    Transform-and-Conquer Algorithm Design Technique

    Design and Analysis of Algorithm

    * Property of STI 

    Page 6 of 30

    named after Carl Friedrich Gauss

    The following equations illustrates the Gaussian

    Elimination algorithm:

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

    7/30

    Transform-and-Conquer Algorithm Design Technique

    Design and Analysis of Algorithm

    * Property of STI 

    Page 7 of 30

    The figure below is an illustration of the

    application of Gaussian elimination application:

    Solve the latter by substitutions starting with the last

    equation and moving up to the first one.

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

    8/30

    Transform-and-Conquer Algorithm Design Technique

    Design and Analysis of Algorithm

    * Property of STI 

    Page 8 of 30

    Matrix representation of the given equation:

    Ax B A’x B’

    where:

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

    9/30

    Transform-and-Conquer Algorithm Design Technique

    Design and Analysis of Algorithm

    * Property of STI 

    Page 9 of 30

    The figure below shows the steps in elementary

    operations:

    Exchanging two equations of the system

    Replacing an equation with its non-zero multiple

    Replacing an equation with a sum or difference of

    this equation and some multiple of another equation

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

    10/30

    Transform-and-Conquer Algorithm Design Technique

    Design and Analysis of Algorithm

    * Property of STI 

    Page 10 of 30

    Apply Gaussian elimination using the equation below:

    2x1 –

    x2 + x3 = 14x1 + x2 – x3 = 5

    x1 + x2 + x3 = 0

    Solution:

    We will start by creating the matrix. Subtract 2 *row 1. Afterwards, subtract ½ * row 1.

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

    11/30

    Transform-and-Conquer Algorithm Design Technique

    Design and Analysis of Algorithm

    * Property of STI 

    Page 11 of 30

    Then subtract ½ * row 2:

    Solution by backward substitutions:

    X

    3

    = -2)/2 = -1, x

    2

    = 3- -3) x

    3

    )/3 =0,

    and

    x1 = 1-x

    3

    - -1)x

    2

    )/2 = 1

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

    12/30

    Transform-and-Conquer Algorithm Design Technique

    Design and Analysis of Algorithm

    * Property of STI 

    Page 12 of 30

    Pseudo code:

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

     // Uses Gaussian elimination to matrix A of a

    system’s coefficients

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

    hand side values

     //input: Matrix A[i, n+ 1] and column vector b[1..n]

     //output: An equivalent upper-triangular matrix inplace of A with the corresponding right-hand side

    values in the (n+1) column

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

    13/30

    Transform-and-Conquer Algorithm Design Technique

    Design and Analysis of Algorithm

    * Property of STI 

    Page 13 of 30

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

     // Uses Gaussian elimination to matrix A of a

    system’s coefficients

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

    hand side values

     //input: Matrix A[i, n+ 1] and column vector b[1..n]

     //output: An equivalent upper-triangular matrix in

    place of A with the corresponding right-hand side

    values in the (n+1) column

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

    14/30

    Transform-and-Conquer Algorithm Design Technique

    Design and Analysis of Algorithm

    * Property of STI 

    Page 14 of 30

      Binary search tree is a type of binary tree that is

    composed of nodes that contains elements of a set

    of ordered items.

    All elements of the left subtree should be less than

    the root while all elements of the right subtree

    should be greater than or equal to the root.

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

    15/30

    Transform-and-Conquer Algorithm Design Technique

    Design and Analysis of Algorithm

    * Property of STI 

    Page 15 of 30

    AVL Tree

    Adelson-Velskii and Landis Tree

    the heights of the subtrees must differ by notmore than 1

    The first example of a tree is a search tree where

    each node is greater than its predecessor.

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

    16/30

    Transform-and-Conquer Algorithm Design Technique

    Design and Analysis of Algorithm

    * Property of STI 

    Page 16 of 30

    The second example of AVL tree is illustrated

    below.

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

    17/30

    Transform-and-Conquer Algorithm Design Technique

    Design and Analysis of Algorithm

    * Property of STI 

    Page 17 of 30

    Four cases of AVL tree:

    Left of left

    Right of right

    Left of right

    Right of left

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

    18/30

    Transform-and-Conquer Algorithm Design Technique

    Design and Analysis of Algorithm

    * Property of STI 

    Page 18 of 30

    Left of left - a subtree of a given tree that is left

    high node has also become the left high node

    Example: The figure below illustrates a sampleAVL tree before inserting 8.

    Out of

    balance

    at 20

     AFTER

    INSERTING 8

    BEFORE

    INSERTING 8

    LH

    LH

    LH

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

    19/30

    Transform-and-Conquer Algorithm Design Technique

    Design and Analysis of Algorithm

    * Property of STI 

    Page 19 of 30

    Right of right - a subtree that is right high has

    also become the right high

    Example: The figure below illustrates the right of right case.

     AFTER

    INSERTING 38

    BEFORE

    INSERTING 38

    RH

    RH

    RH

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

    20/30

    Transform-and-Conquer Algorithm Design Technique

    Design and Analysis of Algorithm

    * Property of STI 

    Page 20 of 30

    Right of Left - a subtree that is left high has

    become the right high

    Example: The figure below illustrates the right of left case.

     AFTER

    INSERTING 15

    BEFORE

    INSERTING 15RH

    LH

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

    21/30

    Transform-and-Conquer Algorithm Design Technique

    Design and Analysis of Algorithm

    * Property of STI 

    Page 21 of 30

    Left of Right - a subtree that is right high has

    become the left high

    Example: The figure below illustrates the left of right case.

     AFTER

    INSERTING 18

    BEFORE

    INSERTING 18

    LH

    RH

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

    22/30

    Transform-and-Conquer Algorithm Design Technique

    Design and Analysis of Algorithm

    * Property of STI 

    Page 22 of 30

    Types of rotations that can be applied to solve the

    unbalancing of trees.

    Single left rotation

    Single right rotation

    Double left-right rotation

    Double right-left rotation

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

    23/30

    Transform-and-Conquer Algorithm Design Technique

    Design and Analysis of Algorithm

    * Property of STI 

    Page 23 of 30

    Single left rotation - it solves the left of left case in

    an unbalanced tree by rotating the edge

    connecting the root and its left child in the binary

    tree

    Example: The figure below illustrates the singleleft rotation.

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

    24/30

    Transform-and-Conquer Algorithm Design Technique

    Design and Analysis of Algorithm

    * Property of STI 

    Page 24 of 30

    Single right rotation - it the same with the single

    left rotation but in opposite direction

    Example: The figure below illustrates the singleright rotation.

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

    25/30

    Transform-and-Conquer Algorithm Design Technique

    Design and Analysis of Algorithm

    * Property of STI 

    Page 25 of 30

    Double left-right rotation - solves the Right of Left

    case of unbalanced tree

    Example: The figure below illustrates the doubleleft-right rotation

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

    26/30

    Transform-and-Conquer Algorithm Design Technique

    Design and Analysis of Algorithm

    * Property of STI 

    Page 26 of 30

    Double right-left rotation - solves the Left of Right

    case of unbalanced tree

    Example: The figure below illustrates the doubleright-left rotation

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

    27/30

    Transform-and-Conquer Algorithm Design Technique

    Design and Analysis of Algorithm

    * Property of STI 

    Page 27 of 30

    Analysis of algorithm for Binary search using the

    Binary Search tree

    [log2n] ≤ h < 1.4405 log2 n + 2) –   3277

    The inequalities immediately imply that the

    operations of searching and insertion are Θ log n)

    in the worst case.

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

    28/30

    Transform-and-Conquer Algorithm Design Technique

    Design and Analysis of Algorithm

    * Property of STI 

    Page 28 of 30

    Problem reduction – process of transforming a

    given problem to another problem using a known

    algorithm

    Illustration of Problem reduction strategy:

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

    29/30

    Transform-and-Conquer Algorithm Design Technique

    Design and Analysis of Algorithm

    * Property of STI 

    Page 29 of 30

    Example for Problem Reduction

    Problem: Spacing to Center Text Problem

    Solution:• Given three numbers a, b, and c, where:

    o a is the width of a normal space

    o b is the width of an option-space

    o c is the amount we want to indent

    Find two more numbers

    • x is the number of normal spaces to be

    used

    • y is the number of option-spaces to beused

    So that ax + by is as close as possible to c.

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

    30/30

    Design and Analysis of Algorithm

    Any

    Questions?