36 algorithm types

Upload: gunji-venkata-srinivasa-babu

Post on 02-Jun-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 36 Algorithm Types

    1/22

    Types of Algorithms

  • 8/11/2019 36 Algorithm Types

    2/22

    2

    Algorithm classification

    Algorithms that use a similar problem-solving approachcan be grouped together

    Well talk about a classification scheme for algorithms

    This classification scheme is neither exhaustive nordisjoint

    The purpose is not to be able to classify an algorithm as

    one type or another, but to highlight the various ays in

    hich a problem can be attacked

  • 8/11/2019 36 Algorithm Types

    3/22

    !

    A short list of categories

    Algorithm types e ill consider include" #imple recursive algorithms

    $acktracking algorithms

    %ivide and con&uer algorithms

    %ynamic programming algorithms

    'reedy algorithms

    $ranch and bound algorithms

    $rute force algorithms (andomi)ed algorithms

  • 8/11/2019 36 Algorithm Types

    4/22

    *

    #imple recursive algorithms +

    A simple recursive algorithm" #olves the base cases directly

    (ecurs ith a simpler subproblem

    %oes some extra ork to convert the solution to the simpler

    subproblem into a solution to the given problem

    + call these simple because several of the other

    algorithm types are inherently recursive

  • 8/11/2019 36 Algorithm Types

    5/22

    .

    /xample recursive algorithms

    To count the number of elements in a list" +f the list is empty, return )ero0 otherise,

    #tep past the first element, and count the remaining elements

    in the list

    Add one to the result

    To test if a value occurs in a list" +f the list is empty, return false0 otherise,

    +f the first thing in the list is the given value, return true0otherise

    #tep past the first element, and test hether the value occurs in

    the remainder of the list

  • 8/11/2019 36 Algorithm Types

    6/22

    1

    $acktracking algorithms

    $acktracking algorithmsare based on a depth-firstrecursive search

    A backtracking algorithm"

    Tests to see if a solution has been found, and if so, returns it0otherise

    or each choice that can be made at this point, 3ake that choice

    (ecur

    +f the recursion returns a solution, return it

    +f no choices remain, return failure

  • 8/11/2019 36 Algorithm Types

    7/224

    /xample backtracking algorithm

    To color a map ith no more than four colors" color56ountry n7"

    +f all countries have been colored 5n 8 number of countries7 return

    success0 otherise,

    or each color c of four colors, +f country n is not adjacent to a country that has been

    colored c 6olor country n ith color c

    recursively color country n9: +f successful, return success

    +f loop exits, return failure

  • 8/11/2019 36 Algorithm Types

    8/22;

    %ivide and 6on&uer

    A divide and con&uer algorithmconsists of to parts" %ivide the problem into smaller subproblems of the same

    type, and solve these subproblems recursively

    6ombine the solutions to the subproblems into a solution to

    the original problem

    Traditionally, an algorithm is only called divide and

    con&uer if it contains at least to recursive calls

  • 8/11/2019 36 Algorithm Types

    9/22