Sie sind auf Seite 1von 23

Algorithms

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 of value 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 specifies in general terms the desired input/output relation. The algorithm describes a specific computational procedure for achieving that desired I/O relation.

Pictorial Representation

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 step input (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

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 we an choose the one that best suits out particular circumstances. It is also a science that tells us how to design a new algorithm External factors: Storage requirement, Speed

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

Problem Solving

Algorithm Development Algorithm


Algorithm Design (informal problem statement) Formalize Problem Repeat Devise Algo Specify Input Specify Output Analyze correctness Specify what processing required Analyze Efficiency Write steps to accomplish that Refine Until Algorithm Good Enough Return Algorithm

Important Points

Non-ambiguity

Range of inputs
The same algorithm can be represented in different ways Several algorithms for solving the same problem Most algorithms are intended to be implemented as computer programs.

Expressing algorithms

There is a need to specify /express sequence of steps comprising an Algorithm. The only requirement is that the specification must provide a precise description of the computational procedure to be followed. Algorithms can be expressed in many kinds of notation: Natural languages Pseudocode Flowcharts Programming Languages Natural language expressions of algorithms tend to be ambiguous, and are rarely used for formal complex or technical algorithms.

How to specify algorithms?

Pseudocode, flowcharts are more structured ways to express algorithms that avoid many of the ambiguities common in natural language statements, while remaining independent of a particular implementation language. Programming languages are primarily intended for expressing algorithms in a form that can be executed by a computer. Typically algorithms are described as pseudo code because it gives the liberty of expressing in a manner that 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

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 <- Each line of an algorithm may be numbered for future reference Use of Control Structures such as Decision Structures ( if statement) & Iterative Structures ( do-while/for/while loop) is allowed. However the exact syntax of a particular programming language should be avoided

Phrases for writing algorithm


Phrases such as mentioned below are used to link sequences of steps if required:if then
_________ _________

else
_________ _________ _________

Phrases for writing algorithm


Repeat
_________ _________ _________ _________

until

Phrases for writing algorithm


while
_____ _____ _____

do
_____ _____ _____

While

Representations of Algorithms

High-level description Prose to describe an algorithm, ignoring the implementation details. Formal description Detailed Lowest level description

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 larger than the largest item so far, make a note of it. The last noted item is the largest in the list when the process is complete.
Formal description Input: A non-empty list of numbers L. Output: The largest number in the list L. largest L0 for each item in the list L1, do if the item > largest, then largest the item return largest

Flowchart

A flowchart is a common type of diagram, that represents an algorithm, showing the steps as boxes of various kinds, and their order by connecting these with arrows. This diagrammatic representation can give a step-bystep solution to a given problem. Data is represented in these boxes, and arrows connecting them represent flow / direction of flow of data. Flowcharts are used in analyzing, designing, documenting or managing a process or program in various fields.

Flowchart Symbols
Start/Stop (End) Symbol Read/Write (Input/Output) Symbol The Operation (Process) Symbol Lines and Arrow Connector, New page Decision

Examples

Replying to a friends letter


Algorithm Read friends letter Decide what to write Write letter in reply
START Read friends letter Decide what to write

Write letter in reply

END

Das könnte Ihnen auch gefallen