Experimental Analysis of Algorithm

Embed Size (px)

Citation preview

  • 8/3/2019 Experimental Analysis of Algorithm

    1/18

    SHANKARA INSTITUTE OF TECHNOLOGYSHANKARA INSTITUTE OF TECHNOLOGY

    (SITE(SITE -- 2009)2009)

    Experimental Analysis ofExperimental Analysis ofAlgorithmAlgorithm

    Deepak Menghani (3Deepak Menghani (3rdrd Year E.C.)Year E.C.)Gopesh Sharma (3Gopesh Sharma (3rdrd Year E.C.)Year E.C.)

    Poornima College of Engineering,Poornima College of Engineering,Jaipur (Raj.)Jaipur (Raj.)

  • 8/3/2019 Experimental Analysis of Algorithm

    2/18

    14 January 201214 January 2012 22

    Contents:Contents:

    Entire PPT is divided into 13 modules:Entire PPT is divided into 13 modules:

    IntroductionIntroduction 3030 -- 35 sec35 sec

    Definition of an AlgorithmDefinition of an Algorithm 11 -- 1.2 min1.2 min

    Criteria that an Algo must SatisfyCriteria that an Algo must Satisfy 11 -- 1.4 min1.4 min

    Structure of a Standard AlgoStructure of a Standard Algo 11 -- 1.1 min1.1 min

    Rules of Writing an Algo ~Rules of Writing an Algo ~ 1 min1 min Various Symbol MeaningVarious Symbol Meaning 3030 -- 35 sec35 sec

    Different Methods of Writing AlgoDifferent Methods of Writing Algo 11 -- 1.2 min1.2 min

    Examples of AlgorithmExamples of Algorithm 11 -- 1.5 min1.5 min

    Space & Time Complexity of an Algo ~Space & Time Complexity of an Algo ~ 2 min2 min

    Needs of Writing an AlgoNeeds of Writing an Algo 4545 -- 55 sec55 sec

    AlgoAlgo--TranslaterTranslater 33 -- 5 min5 min

    Simulation & ConclusionSimulation & Conclusion

    ReferencesReferences

    Questions ~ 2 minQuestions ~ 2 min

    __________________________________________________________________________________________________

    Total ~ 15 minTotal ~ 15 min

  • 8/3/2019 Experimental Analysis of Algorithm

    3/18

    14 January 201214 January 2012 33

    Experimental Analysis ofExperimental Analysis of

    AlgorithmAlgorithm

    EAAEAA stands forstands forExperimental Analysis ofExperimental Analysis ofAlgorithm.Algorithm. It is composed of 3 terms:It is composed of 3 terms:

    a > EXPERIMENTAL;a > EXPERIMENTAL;b > ANALYSIS; &b > ANALYSIS; &c> ALGORITHM.c> ALGORITHM.

    EXPERIMENTALEXPERIMENTAL means which is based onmeans which is based onexperiments or whose output is obtained by doingexperiments or whose output is obtained by doingexperiments.experiments. ANALYSISANALYSIS refers to detailedrefers to detailedexamination of elements or structure &examination of elements or structure &ALGORITHMALGORITHM refers to steprefers to step--wise procedure of doingwise procedure of doinga particular activity. EAA measures the efficiency ofa particular activity. EAA measures the efficiency of

    an algorithm in terms of time & space complexityan algorithm in terms of time & space complexitythat should be minimum. Commonly there are 2that should be minimum. Commonly there are 2tools which help us to document program logic:tools which help us to document program logic:Flowcharts and PseudocodeFlowcharts and Pseudocode..

  • 8/3/2019 Experimental Analysis of Algorithm

    4/18

    14 January 201214 January 2012 44

    Definition of anDefinition of an

    AlgorithmAlgorithm According toAccording to Brent Daviduck of the ComputerBrent Daviduck of the Computer

    Systems Technology program at Red DeerSystems Technology program at Red DeerCollege in Alberta, CanadaCollege in Alberta, Canada, an algorithm is a, an algorithm is asequence of instructionssequence of instructions that are a fundamentalthat are a fundamentalpart of computing.part of computing.

    Another definition taken fromAnother definition taken from Wikipedia, the freeWikipedia, the freeencyclopediaencyclopedia is "anis "an algorithm is a computeralgorithm is a computerprogram that calculates somethingprogram that calculates something..

    Thus a standard definition of an algorithm can beThus a standard definition of an algorithm can bestated as: An algorithm is astated as: An algorithm is a stepstep--wise methodwise method //procedureprocedure toto perform a specific desired taskperform a specific desired task ..

    The time & spaces it uses are 2 major measuresThe time & spaces it uses are 2 major measuresof its efficiency. The complexity of an algorithm isof its efficiency. The complexity of an algorithm isthe function which gives the running time or spacethe function which gives the running time or spacein terms of the input size.in terms of the input size.

  • 8/3/2019 Experimental Analysis of Algorithm

    5/18

    14 January 201214 January 2012 55

    Criteria that anCriteria that an

    algo must satisfyalgo must satisfy

    InputInput-- Zero or more quantities are externallyZero or more quantities are externallysuppliedsupplied..

    OutputOutput-- At least one quantity is produced atAt least one quantity is produced at

    outputoutput.. EffectivenessEffectiveness -- All the statements of algorithmAll the statements of algorithm

    are clear andare clear and terminateterminate only after a finite no.only after a finite no.of stepsof steps..

    Lowspace & timecomplexityLowspace & timecomplexity-- WhenWhenimplemented in real life, they should occupyimplemented in real life, they should occupyoptimal memory space & must save timeoptimal memory space & must save time..

    DatatypeDatatype--An algorithm cannot containAn algorithm cannot containdatatype of variables to be used into itdatatype of variables to be used into it..

    For e.g.: we cannot write Read int a,b. HereFor e.g.: we cannot write Read int a,b. Heredatatype int is explicitly mentioned.datatype int is explicitly mentioned.

  • 8/3/2019 Experimental Analysis of Algorithm

    6/18

    14 January 201214 January 2012 66

    Structure of a StandardStructure of a Standard

    AlgorithmAlgorithm

    Structure refers toStructure refers to formatting / design / layoutformatting / design / layout of setof setof statements that we must follow, while writing anof statements that we must follow, while writing analgorithm on paper. It is justalgorithm on paper. It is just similar to syntax /similar to syntax /GrammarGrammarof a Language. The syntax of every languageof a Language. The syntax of every languageis different.is different.

    For e.g.:For e.g.:If we are writing a program of printing a message onIf we are writing a program of printing a message onoutput screen, the syntax will be different for c, c++ &output screen, the syntax will be different for c, c++ &java.java.

    InIn c,c, we can write:we can write: printf(printf(\\n PROGRAMMING IN C ISn PROGRAMMING IN C ISFUN!);FUN!);

    InIn c++,c++, we can write:we can write: cout

  • 8/3/2019 Experimental Analysis of Algorithm

    7/18

    14 January 201214 January 2012 77

    Rules of Writing a StandardRules of Writing a Standardalgoalgo

    First step is alwaysFirst step is always startstart.. StopStop is used to terminate algorithm.is used to terminate algorithm.

    Statements areStatements are not casenot case--sensitivesensitive.. e.g.e.g.: Read: Reada & b is similar to READ A & B.a & b is similar to READ A & B.

    ReadRead is a function which is used to read value ofis a function which is used to read value ofvariables from user.variables from user. e.g.e.g.:: READ A,B,C.READ A,B,C.

    WriteWrite is a function which is used to write messageis a function which is used to write messageor print value of a variable.or print value of a variable. e.g.e.g. WRITE: WRITINGWRITE: WRITING

    AN ALGO IS FUN! or WRITE C.AN ALGO IS FUN! or WRITE C.

    ISIS THENTHEN is used to check conditions.is used to check conditions.e.g:e.g: IS a > b, THEN: SET max := a, ELSE: SETIS a > b, THEN: SET max := a, ELSE: SETmax := b.max := b.

    RepeatRepeat is a function which is used to define a setis a function which is used to define a setof statements in a loop body.of statements in a loop body. e.g.e.g.: REPEAT steps: REPEAT steps33--4, WHILE: SUM =/= 0.4, WHILE: SUM =/= 0.

  • 8/3/2019 Experimental Analysis of Algorithm

    8/18

    14 January 201214 January 2012 88

    Symbol MeaningSymbol Meaning

    In Standard algorithm, meaning of various symbols is:In Standard algorithm, meaning of various symbols is:

    ==== EqualsEquals

    =/==/= Not EqualsNot Equals

    Greater thanGreater than = Greater than or equals toGreater than or equals to

    In practice, the computer is presented not with a true/falseIn practice, the computer is presented not with a true/falsestatement, but with a question having a "Yes" or "No"statement, but with a question having a "Yes" or "No"answer, for example if A = 10, B = 20, K = 5, and SALESanswer, for example if A = 10, B = 20, K = 5, and SALES= 10000, then:= 10000, then:

    Condition (Question) "Answer"Condition (Question) "Answer"Is A == B? NoIs A == B? NoIs B > A? YesIs B > A? YesIs K = $5000.00? Yes

  • 8/3/2019 Experimental Analysis of Algorithm

    9/18

    14 January 201214 January 2012 99

    Different Methods of WritingDifferent Methods of WritingAlgoAlgo

    METHOD 1:~METHOD 1:~STEP1STEP1:: STARTSTARTSTEP2STEP2:: READREAD MESSAGEMESSAGESTEP3STEP3:: WRITEWRITEMESSAGEMESSAGESTEP4STEP4:: STOPSTOP

    METHOD 2:~METHOD 2:~STEP0STEP0:: STARTSTARTSTEP1STEP1:: GETGETMESSAGEMESSAGESTEP2STEP2:: PRINTPRINTMESSAGEMESSAGESTEP3STEP3:: RETURNRETURN

    METHOD 3:~METHOD 3:~STEP1STEP1 ##StartStartprocessing.processing.STEP2STEP2##ReadReadaa messagemessage from user.from user.STEP3STEP3 ##SaveSave this messagethis message in a temporary variable.in a temporary variable.STEP4STEP4 # R# Retrieve this messageetrieve this message from temporary variable.from temporary variable.STEP5STEP5# P# Print this messagerint this message on output screen.on output screen.STEP6STEP6##Terminate processTerminate process && returnreturn..

    And so onAnd so on

  • 8/3/2019 Experimental Analysis of Algorithm

    10/18

    14 January 201214 January 2012 1010

    Examples of AlgorithmExamples of Algorithm

    Let us Now writeLet us Now write algoalgo of some specific programs using standard rulesof some specific programs using standard rulesstated above:stated above:Example 1:Example 1: If we are desired to write a program in c programmingIf we are desired to write a program in c programming

    language whose output is simply addition of 2 variables, then we simplylanguage whose output is simply addition of 2 variables, then we simplywrite:write:

    mainmain ()() /*/*User defined functionUser defined function*/*/{{intint a,b,ca,b,c; /*; /*Declaration of variablesDeclaration of variables */*/

    printfprintf((\\n ENTER values of a & b: );n ENTER values of a & b: );/*/*Function to print on dos screenFunction to print on dos screen*/*/

    scanfscanf(%(%d%d,&a,&bd%d,&a,&b););/*/* Function to read input from dos screenFunction to read input from dos screen */*/

    c = a + b /*c = a + b /* Statement to assign value of (a + b) into (c)Statement to assign value of (a + b) into (c) */*/printfprintf((\\n ADDITION of a & b is: %n ADDITION of a & b is: %d,cd,c););}}

    Now, let us writeNow, let us write algoalgo. of above program. of above programSTEP1:STEP1: STARTSTART

    STEP2:STEP2: READREAD A& B [Step corresponding to A& B [Step corresponding toscanfscanf()()]]STEP3:STEP3: SETSETC := A + B [Step corresponding toC := A + B [Step corresponding to c= a + bc= a + b]]STEP4:STEP4: WRITEWRITEC [Step corresponding toC [Step corresponding to printfprintf()()]]STEP5:STEP5: STOPSTOP

  • 8/3/2019 Experimental Analysis of Algorithm

    11/18

    14 January 201214 January 2012 1111

    Example 2Example 2:: Program to find largest out of 2 numbers.Program to find largest out of 2 numbers.main()main()

    {{intint a,b;a,b;printfprintf((\\n ENTER value of a & b: );n ENTER value of a & b: );scanfscanf(%d%d,&a,&b);(%d%d,&a,&b);ifif(a > b)(a > b)printfprintf((\\nn a is greater than ba is greater than b););else ifelse if(b > a)(b > a)printfprintf((\\nn b is greater than ab is greater than a););

    elseelseprintfprintf((\\nn a & b are equala & b are equal););}}Corresponding algorithm can be written as:Corresponding algorithm can be written as:

    STEP1STEP1:: STARTSTARTSTEP2STEP2:: READREAD A& B [Step corresponding to A& B [Step corresponding toscanf()scanf()]]STEP3STEP3:: ISA > BISA > B, THEN: WRITE: A IS GREATER B,, THEN: WRITE: A IS GREATER B,

    ELSEIF: B > AELSEIF: B > A,, THENTHEN:: WRITEWRITE: B IS GREATER THAN: B IS GREATER THANA,A,ELSE:ELSE: WRITEWRITE: A & B ARE EQUAL: A & B ARE EQUAL[Step corresponding to[Step corresponding to ifif elseelse]]

    STEP4STEP4:: STOPSTOP

  • 8/3/2019 Experimental Analysis of Algorithm

    12/18

    14 January 201214 January 2012 1212

    Space & Time Complexity of anSpace & Time Complexity of an

    AlgorithmAlgorithm

    TIME COMPEXITYTIME COMPEXITY:: This measure is used to estimateThis measure is used to estimaterunning time ofrunning time of algorithmalgorithm in terms of size of inputin terms of size of inputdata.data.

    SPACE COMPLEXITYSPACE COMPLEXITY:: This measure is used toThis measure is used todefinedefine extra space consumedextra space consumed by the algorithmby the algorithmexcept input data.except input data.Space & Time Complexity are important parameters of anSpace & Time Complexity are important parameters of an

    algorithm in deciding its efficiency. There are 3 popularalgorithm in deciding its efficiency. There are 3 popularnotations to calculate Space & Time Complexity:notations to calculate Space & Time Complexity:

    Big Oh NotationBig Oh Notation (O)(O) It measures theIt measures the upper boundupper bound ofofa function.a function.

    Omega NotationOmega Notation (Omega)(Omega) -- It measures theIt measures the lowerlowerboundbound of a function.of a function. Theta NotationTheta Notation (Theta)(Theta) It measuresIt measures in betweenin between

    boundbound of a function i.e. between upper & lower bound ofof a function i.e. between upper & lower bound ofa function.a function.

  • 8/3/2019 Experimental Analysis of Algorithm

    13/18

    14 January 201214 January 2012 1313

    Needs of Writing AlgorithmNeeds of Writing Algorithm

    As already discussed, an algorithm refers to that set ofAs already discussed, an algorithm refers to that set ofstatements / instructions, which are designed to perform astatements / instructions, which are designed to perform aspecific task. They do not depend on the programming languagespecific task. They do not depend on the programming languagein which the code is to be implemented. An algo cannot containin which the code is to be implemented. An algo cannot containdatatype of variables.datatype of variables.For e.g.:For e.g.: The statementsThe statements READ INT A,B or WRITEREAD INT A,B or WRITE INT AINT A areareinvalid statements of algo as they explicitly specify the datatypeinvalid statements of algo as they explicitly specify the datatype

    of variables used into it. Before writing a code in someof variables used into it. Before writing a code in someprogramming language,programming language, writing an algorithm is a betterwriting an algorithm is a betterpracticepractice because:because:

    In algo,In algo, syntax never matterssyntax never matters, thus we can write it in any way, thus we can write it in any waywe want;we want;

    It givesIt gives information about all the variablesinformation about all the variables to be used in ourto be used in oursource codesource code;;

    Datatype of all variables can be judgedDatatype of all variables can be judged by analyzing the logicby analyzing the logicused in algorithm;used in algorithm;

    Used for the analyses of logicUsed for the analyses of logic before writing its code onbefore writing its code oncomputer;computer;

    Everyone can understand algorithm soEveryone can understand algorithm so codecode--modificationmodificationbecomes an easy task; etc.becomes an easy task; etc.

  • 8/3/2019 Experimental Analysis of Algorithm

    14/18

    14 January 201214 January 2012 1414

    ALGOALGO--TRANSLATERTRANSLATER

    AlgorithmAlgorithm --> Source Code> Source CodeAs a practical approach to validate ourAs a practical approach to validate ourproposed design & to apply rules ofproposed design & to apply rules ofstandard algorithms as created , we arestandard algorithms as created , we arecurrently working on software module thatcurrently working on software module that

    can decode/convert an algorithm orcan decode/convert an algorithm orflowchart into source code of c and c++.flowchart into source code of c and c++.The algorithm / flowchart should be writtenThe algorithm / flowchart should be writtenin standard form following rules statedin standard form following rules statedabove. User will write algorithm andabove. User will write algorithm andinterpreter will interpret line by line. It willinterpreter will interpret line by line. It will

    point out any logical or syntactical error &point out any logical or syntactical error &will generate the corresponding sourcewill generate the corresponding sourcecode in a file whose name is stated by user.code in a file whose name is stated by user.A module of application can beA module of application can bedemonstrated asdemonstrated as

  • 8/3/2019 Experimental Analysis of Algorithm

    15/18

    14 January 201214 January 2012 1515

    For e.g.:For e.g.:

    If user write an algo of adding 2 integers in algodecoder IDE:If user write an algo of adding 2 integers in algodecoder IDE:

    Step1Step1: Start: StartStep2Step2: Read: Read a,ba,b

    Step3Step3: Set: Set c := a + bc := a + bStep4Step4: Write: Write cc..Step5Step5: Stop: Stop

    then corresponding code generated will be:then corresponding code generated will be:main()main(){{int a,b,c;int a,b,c;scanf(%d%d,&a,&b);scanf(%d%d,&a,&b);c = a + b;c = a + b;printf(%d,c);printf(%d,c);}}in c programming language.in c programming language.void main()void main(){{int a,b,c;int a,b,c;cin>>endl>>a>>b;cin>>endl>>a>>b;c = a + b;c = a + b;cout

  • 8/3/2019 Experimental Analysis of Algorithm

    16/18

    14 January 201214 January 2012 1616

    Simulation & ConclusionsSimulation & Conclusions

    --> We described about algorithm, their importance> We described about algorithm, their importancein our real life, created rules & decided criteriain our real life, created rules & decided criteriafor writing a standard algorithm.for writing a standard algorithm.

    --> An algorithm is the base for computing and it> An algorithm is the base for computing and it

    does not depend on programming language indoes not depend on programming language inwhich the code is to be written, operating systemwhich the code is to be written, operating systemin which we are working, environment variables,in which we are working, environment variables,temporary variables, etc. It is just a skeleton of atemporary variables, etc. It is just a skeleton of aprogram / software module.program / software module.

    --> An algorithm cannot contain datatype of> An algorithm cannot contain datatype ofvariables to be used into it.variables to be used into it.

  • 8/3/2019 Experimental Analysis of Algorithm

    17/18

    14 January 201214 January 2012 1717

    ReferencesReferences

    WikipediaWikipedia, the free encyclopedia., the free encyclopedia.

    BrentDaviduckBrentDaviduckof the ComputerSystemsof the ComputerSystemsTechnology program atRedDeer College inTechnology program atRedDeer College inAlberta, Canada.( http://cst.rdc.ab.ca/)Alberta, Canada.( http://cst.rdc.ab.ca/)

    Schaum'sOutline ofTheoryandProblemsSchaum'sOutline ofTheoryandProblems

    ofEssentialComputerMathematics, byDr.ofEssentialComputerMathematics, byDr.SeymourLipshutzSeymourLipshutz, McGraw, McGraw--Hill, Inc., ISBN:Hill, Inc., ISBN:00--0707--037990037990--4 Chapter 54 Chapter 5 AlgorithmsAlgorithms

    Boolos & Jeffrey 1974, 1999, p. 19.Boolos & Jeffrey 1974, 1999, p. 19. whatis.techtarget.com/definition/0,,sid9_gci21whatis.techtarget.com/definition/0,,sid9_gci21

    1545,00.html1545,00.html

    www.nist.gov/dads/HTML/algorithmwww.nist.gov/dads/HTML/algorithm www.wisegeek.com/whatwww.wisegeek.com/what--isis--anan--algorithm.htmalgorithm.htm

  • 8/3/2019 Experimental Analysis of Algorithm

    18/18