Algorithm Lecture1

download Algorithm Lecture1

of 23

Transcript of Algorithm Lecture1

  • 8/1/2019 Algorithm Lecture1

    1/23

    Algorithms

  • 8/1/2019 Algorithm Lecture1

    2/23

    Algorithm

    Basic idea or logic behind computer programs. Step by step procedure for performing a task in a finite amount of

    time. Procedure of manipulating data. A set of finite unambiguous instructions that take input and give

    desired output Set of rule for carrying out some calculations either manually or

    usually by a machine. Any well defined computational procedure that takes some value

    or a set of values as input and produces some value or a set ofvalue a output

    Sequence of computations that transform input to output Algorithms can be viewed as a tool for solving a well specified

    computational problem. The statement of the problem specifiesin general terms the desired input/output relation. The algorithmdescribes a specific computational procedure for achieving that

    desired I/O relation.

  • 8/1/2019 Algorithm Lecture1

    3/23

    Pictorial Representation

  • 8/1/2019 Algorithm Lecture1

    4/23

    Features of an Algorithm

    An algorithm has the five important features:finiteness

    terminates after a finite number of steps

    definiteness

    Precise/complete definition of each stepinput (zero or more)

    Valid inputs are clearly defined

    output (one or more) Can be proved to produce the correct output given a valid

    input

    effectiveness Steps are sufficiently simple and basic

  • 8/1/2019 Algorithm Lecture1

    5/23

    What is Algorithmics?

    Study of Algorithms A problem may have many algorithms as solutions Priorities+ Limitations require that we choose one amongst

    them It may happen that none of the available algorithms satisfy the

    requirement thus we have to devise a new algorithm Algorithmics is a science that lets us evaluate the effect of

    various external factors on the available algorithm so that wean choose the one that best suits out particularcircumstances. It is also a science that tells us how to design

    a new algorithm External factors: Storage requirement, Speed

  • 8/1/2019 Algorithm Lecture1

    6/23

    PARAMETERS FOR SELECTION

    OF AN ALGORITHM

    Type of Available Computing Equipment

    Nature of Problem

    Speed of Execution Storage Requirement

    Programming Effort

    Good choice can save both money and time,and can successfully solve the problem

  • 8/1/2019 Algorithm Lecture1

    7/23

    Problem Solving

  • 8/1/2019 Algorithm Lecture1

    8/23

    Algorithm Development Algorithm

    Algorithm Design (informal problem statement)Formalize ProblemRepeat

    Devise Algo

    Analyze correctness Analyze Efficiency Refine

    Until Algorithm Good EnoughReturn Algorithm

    Specify Input

    Specify Output

    Specify what processing required

    Write steps to accomplish that

  • 8/1/2019 Algorithm Lecture1

    9/23

    Important Points

    Non-ambiguity

    Range of inputs

    The same algorithm can be represented indifferent ways

    Several algorithms for solving the same problem

    Most algorithms are intended to be implementedas computer programs.

  • 8/1/2019 Algorithm Lecture1

    10/23

    Expressing algorithms

    There is a need to specify /express sequence of stepscomprising an Algorithm.

    The only requirement is that the specification must provide aprecise description of the computational procedure to befollowed.

    Algorithms can be expressed in many kinds of notation:

    Natural languages

    Pseudocode

    Flowcharts

    Programming Languages

    Natural language expressions of algorithms tend to beambiguous, and are rarely used for formal complex ortechnical algorithms.

  • 8/1/2019 Algorithm Lecture1

    11/23

    How to specify algorithms?

    Pseudocode, flowcharts are more structured ways toexpress algorithms that avoid many of the ambiguitiescommon in natural language statements, whileremaining independent of a particular implementationlanguage.

    Programming languages are primarily intended forexpressing algorithms in a form that can be executed bya computer.

    Typically algorithms are described as pseudo code

    because it gives the liberty of expressing in a mannerthat is most clear & concise. May come across phrases of English at places. Since we are not confining ourselves to programming

    language therefore essential parts of the algorithm will

    not be obscured by unimportant programming details

  • 8/1/2019 Algorithm Lecture1

    12/23

    Psuedo Code Conventions

    No need of well structured language Use of English for simplicity & clarity Use of mathematical language & symbols where appropriate Indentation indicates block structures.

    Delimiters are not used. No variable declaration All variables used are implicitly local unless specified

    otherwise. Return statement used to mark end of algorithm to return a

    value if required. Assignment operator used is = or

  • 8/1/2019 Algorithm Lecture1

    13/23

    Phrases for writing algorithm

    Phrases such as mentioned below are used tolink sequences of steps if required:-

    ifthen__________________

    else___________________________

  • 8/1/2019 Algorithm Lecture1

    14/23

    Phrases for writing algorithm

    Repeat

    _________

    _________

    _________

    _________

    until

  • 8/1/2019 Algorithm Lecture1

    15/23

    Phrases for writing algorithm

    while _______________

    do__________

    _____While

  • 8/1/2019 Algorithm Lecture1

    16/23

    Representations of Algorithms

    High-level description

    Prose to describe an algorithm, ignoring theimplementation details.

    Formal description

    Detailed

    Lowest level description

  • 8/1/2019 Algorithm Lecture1

    17/23

    Example

    Algorithm to find the largest number in an (unsorted) list of numbers.

    High-level description (somewhat ambiguous)Assume the first item is largest.Look at each of the remaining items in the list and if it is largerthan the largest item so far, make a note of it.

    The last noted item is the largest in the list when the process iscomplete.

    Formal descriptionInput: A non-empty list of numbers L.

    Output: The largestnumber in the list L.largest L0for eachitemin the list L

    1, do

    if the item> largest, thenlargest the item

    returnlargest

  • 8/1/2019 Algorithm Lecture1

    18/23

    Flowchart

    A flowchart is a common type of diagram, thatrepresents an algorithm, showing the steps as boxes ofvarious kinds, and their order by connecting these witharrows.

    This diagrammatic representation can give a step-by-step solution to a given problem.

    Data is represented in these boxes, and arrowsconnecting them represent flow / direction of flow ofdata.

    Flowcharts are used in analyzing, designing,documenting or managing a process or program invarious fields.

  • 8/1/2019 Algorithm Lecture1

    19/23

    Flowchart Symbols

    Start/Stop (End) Symbol

    Read/Write (Input/Output) Symbol

    The Operation (Process) Symbol

    Lines and Arrow

    Connector, New page

    Decision

  • 8/1/2019 Algorithm Lecture1

    20/23

    Examples

  • 8/1/2019 Algorithm Lecture1

    21/23

    Replying to a friends letter

    Algorithm

    Read friends letter

    Decide what to write

    Write letter in reply

    START

    END

    Decide what towrite

    Readfriends

    letter

    Write letterin reply

  • 8/1/2019 Algorithm Lecture1

    22/23

  • 8/1/2019 Algorithm Lecture1

    23/23