Algorithms and Pseudo code

Embed Size (px)

Citation preview

  • 8/9/2019 Algorithms and Pseudo code

    1/11

    An Assignment on

    Algorithms and Pseudo code

    Submitted to Submitted by

    Dr. Budhayash Gautam Anand

    Kumar Maurya

    13MTBI!""#

  • 8/9/2019 Algorithms and Pseudo code

    2/11

    $ontents

    1.Introduction2.Algorithm design techniques3.Pseudo codes%. Pseudo code conventions with an

    example&. References

  • 8/9/2019 Algorithms and Pseudo code

    3/11

    Algorithms and Pseudo code

    Introduction

     The word Algorithm! comes from the Persian author A"dullah

     #afar $uhammad i"n $usa Al%&howari'mi in ninth centur() who

    has given the de*nition of algorithm as follows+ An Algorithm is

    a set of rules for carr(ing out calculation either "( hand or on a

    machine. An Algorithm is a well de*ned computational

    procedure that ta&es input and produces output. An Algorithm

    is a *nite sequence of instructions or steps ,i.e. inputs- to

    achieve some particular output.

    An( Algorithm must satisf( the following criteria ,or Properties-

    • Input+ It generall( requires *nite no. of inputs

    • utput+ It must produce at least one output

    • /niqueness+ 0ach instruction should "e clear and

    unam"iguous

    • initeness+ It must terminate oer a *nite no. of steps.

    Algorithm design techni'ues

     There are "asicall( fundamental techniques which are used to

    design an algorithm e4cientl(+

    1. 5ivide%and%6onquer2. 7reed( method3. 5(namic Programming8. 9ac&trac&ing. 9ranch%and%9ound

    1. Di(ide ) con'uer techni'ue  is a top%down approach to

    solve a pro"lem. The algorithm which follows divide and

    conquer technique involves 3 steps+

    • 5ivide the original pro"lem into a set of su" pro"lems.

  • 8/9/2019 Algorithms and Pseudo code

    4/11

    • 6onquer ,or :olve- ever( su"%pro"lem individuall()

    recursive.

    • 6om"ine the solutions of these su" pro"lems to get the

    solution of original pro"lem.

    *. Greedy techni'ue  is used to solve an optimi'ation

    pro"lem. An ptimi'ation pro"lem is one in which) we are given

    a set of input values) which are required to "e either maximi'ed

    or minimi'ed ,&nown as o";ective function- w. r. t. some

    constraints or conditions. 7reed( algorithm alwa(s ma&es the

    choice ,greed( criteria- that loo&s "est at the moment) to

    optimi'e a given o";ective function. That is) it ma&es a locall(

    optimal choice in the hope that this choice will lead to a overall

    glo"all( optimal solution. The greed( algorithm does not alwa(s

    guaranteed the optimal solution "ut it generall( produces

    solutions that are ver( close in value to the optimal.

    3. Dynamic +rogramming technique is similar to divide and

    conquer approach. 9oth solve a pro"lem "( "rea&ing it down

    into a several su" pro"lems that can "e solved recursivel(. Thedierence "etween the two is that in d(namic programming

    approach) the results o"tained from solving smaller su"

    pro"lems are reused ,"( maintaining a ta"le of results- in the

    calculation of larger su" pro"lems. Thus d(namic programming

    is a 9ottom%up approach that "egins "( solving the smaller su"%

    pro"lems) saving these partial results) and then reusing them

    to solve larger su"%pro"lems until the solution to the original

    pro"lem is o"tained. Reusing the results of su"%pro"lems ,"(maintaining a ta"le of results- is the ma;or advantage of 

    d(namic programming "ecause it avoids the recomputations

    ,computing results twice or more- of the same pro"lem.

     Thus 5(namic programming approach ta&es much less time

    than na

  • 8/9/2019 Algorithms and Pseudo code

    5/11

    %. Bac,trac,ing  % The term "ac&trac&! was coined "(

    American mathematician 5.=. >ehmer in the 1?@s.

    9ac&trac&ing can "e applied onl( for pro"lems which admit theconcept of a partial candidate solution! and relativel( quic&

    test of whether it can possi"l( "e completed to a valid solution.

    9ac&trac& algorithms tr( each possi"ilit( until the( *nd the

    right one. It is a depth%*rst%search of the set of possi"le

    solutions. 5uring the search) if an alternative doesnt wor&) the

    search "ac&trac&s to the choice point) the place which

    presented dierent alternatives) and tries the next alternative.

    Bhen the alternatives are exhausted) the search returns to theprevious choice point and tr( the next alternative there. If there

    are no more choice points) the search fails.

    &. Branch-and-Bound B)B/ is a rather general optimi'ation

    technique that applies where the greed( method and d(namic

    programming fail. 9C9 design strateg( is ver( similar to

    "ac&trac&ing in that a state%spacetree is used to solve apro"lem. 9ranch and "ound is a s(stematic method for solving

    optimi'ation pro"lems. =owever) it is much slower. Indeed) it

    often leads to exponential time complexities in the worst case.

    n the other hand) if applied carefull() it can lead to algorithms

    that run reasona"l( fast on average. The general idea of 9C9 is

    a 9:%li&e search for the optimal solution) "ut not all nodes get

    expanded ,i.e.) their children generated-. Rather) a carefull(

    selected criterion determines which node to expand and when)and another criterion tells the algorithm when an optimal

    solution has "een found. 9ranch and 9ound ,9C9- is the most

    widel( used tool for solving large scale DP%hard com"inatorial

    optimi'ation pro"lems.

     The following ta"le summari'es these techniques with some

    common pro"lems that follows these techniques with their

  • 8/9/2019 Algorithms and Pseudo code

    6/11

    running time. 0ach technique has dierent running time ,Etime

    complexit(-.

  • 8/9/2019 Algorithms and Pseudo code

    7/11

    Design strategy Problem that 0ollos5ivide C 6onqer   • 9inar( :earch

    •$ultiplication of two n%"its num"ers

    • Fuic& :ort

    • =eap :ort

    • $erge :ort

    7reed( $ethod   • Gnapsac& ,fractional-

    Pro"lem $inim cost :panning

    tree Grus&alHs algorithm

    • PrimHs algorithm

    • :ingle source shortest

    path pro"lem 5i;i&straHs algorithm

    5(namic Programming   • All pair shortest path

    lo(ed algorithm

    6hain matrixmultiplication

    • >ongest common

    su"sequence ,>6:-

    • @J1 Gnapsac& Pro"lem

    •  Travelling salesman

    pro"lem ,T:P-9ac&trac&ing   • D% queenHs pro"lem

    • :um% of su"set

    9ranch C 9ound   • Assignment pro"lem

    •  Travelling salesman

    pro"lem ,T:P-

    Pseudo codes

  • 8/9/2019 Algorithms and Pseudo code

    8/11

    Pseudo%code ,derived from pseudo%code- is a compact and

    informal high level description of a computer programming

    algorithm that uses the structural conventions of some

    programming language. /nli&e actual computer language such

    as 6)6KK or #ALA) Pseudo%code t(picall( omits details that are

    not essential for understanding the algorithm) such as functions

    ,or su"routines-) varia"le declaration) semicolons) special

    words and so on. An( version of pseudo%code is accepta"le as

    long as its instructions are unam"iguous and is resem"les in

    form. Pseudo%code is independent of an( programming

    language. Pseudo %code cannot "e compiled nor executed) and

    not following an( s(ntax rules.

    low charts can "e thought of as a graphical alternative to

    pseudo%code. A Mowchart is a schematic representation of an

    algorithm) or the step%"(%step solution of a pro"lem) using

    some geometric *gures ,called Mowchart s(m"ols- connected

    "( Mow%lines for the purpose of designing or documenting a

    program.

     The purpose of using pseudo%code is that it ma( "e easier to

    read than conventional programming languages that ena"les

    ,or helps- the programmers to concentrate on the algorithms

    without worr(ing a"out all the s(ntactic details of a particular

    programming language. In fact) one can write a pseudocode for

    an( given pro"lem without even &nowing what programming

    language one will use for the *nal implementation.

    Pseudo code $on(entions

    1. 6omments "egin with JJ and continue until the end of the

    line.2. 9loc&s are indicated with matching "races Nand O.3. An identi*er "egins with a letter.8. Assignment of values to varia"les is done using the

    assignment statement varia"leQ expressionQS

    . There are 2 "oolean values true and false and relationaloperators ) )Q)Q etc are used.

  • 8/9/2019 Algorithms and Pseudo code

    9/11

    . 0lements of multidimensional arra(s are accessed using

    U and V.W. The looping statements availa"le are for) while and repeat

    until.

    X. A conditional statement has the following forms.If conditionQ then statementQif conditionQ then statement 1Q else statement 2Q

    ?. Input and output are done using read and write

    statements.1@. Algorithm is the one t(pe of procedure) which

    consists of a heading and a "od(.Algorithm Dame ,parameter listQ-

    2am+le4  To devise an algorithm that sorts a collection of 

    nQ1 elements of ar"itrar( t(pe.

    /sing the natural language li&e 0nglish rom those elements

    that are currentl( unsorted) *nd the smallest and place it next

    in the sorted list.

    Pseudo code representation %

    Algorithm :election:ort,a)n-

    JJ:ort the arra( aU1 + nV into non%decreasing order.

    N

    for i 1 to n do

    N

     ; iS

    for & i K1 to n do

    if ,aU&V aU;V- then ; &S

  • 8/9/2019 Algorithms and Pseudo code

    10/11

      t aUiVS aUiV aU;VS aU;V tS

    O

    O

  • 8/9/2019 Algorithms and Pseudo code

    11/11

    5e0erences

    1. An Introduction to 9ioinformatics Algorithms % Deil 6.

     #ones) Pavel A. Pev'ner2. Introduction to Algorithms) :econd 0dition % Thomas =.

    6ormen) 6harles 0. >eiserson) Ronald >. Rivest) 6liord

    :tein