+ All Categories
Home > Documents > Algorithm

Algorithm

Date post: 13-Nov-2014
Category:
Upload: api-3723664
View: 15 times
Download: 3 times
Share this document with a friend
Description:
Author_ Khalid Nazim S A
Popular Tags:
40
Algorithm –History Muhammad ibn Musa Al-Khwarizmi http:// www -groups.dcs.st-andrews.ac.uk/~history/Mathematicians/Al- Khwarizmi.html Book on arithmetic: Hindu numeration, decimal numbers, use of zero, method for finding square root Latin translation (c.1120 CE): “Algoritmi de numero IndorumBook on algebra Hisab al-jabr w’al-muqabala
Transcript
Page 1: Algorithm

Algorithm –History

Muhammad ibn Musa Al-Khwarizmihttp:// www -groups.dcs.st-andrews.ac.uk/~history/Mathematicians/Al-Khwarizmi.html

Book on arithmetic:Hindu numeration, decimal numbers, use of zero, method for finding square rootLatin translation (c.1120 CE): “Algoritmi de numero Indorum”

Book on algebraHisab al-jabr w’al-muqabala

Page 2: Algorithm

The Problem-solving Process

Problem specification

Algorithm

Program

Executable (solution)

Analysis

Design

Implementation

Compilation

Page 3: Algorithm

Components of an Algorithm

1. Variables and values

2. Instructionsa. Sequences

b. Procedures

c. Selections

d. Repetitions

3. Also required: Documentation

Page 4: Algorithm

Algorithm basicsIt is a step by step procedure to solve a given problem in/with finite number of steps, where every step is labeled with a step number and no 2 steps are same.

The components of an algorithm comprises of simple English statements with mathematical expressions, that specify step by step proceduretowards the problem solution

Page 5: Algorithm

Algorithm basics

Properties of an algorithmIt should be definite- no ambiguityIt should be finite- definite number of stepsIt should be complete- must work

successfully for which it has been designed for.

It should be efficient

Page 6: Algorithm

Need for writing algorithm

Effective communication-understandability Effective analysis-easy to write programs Proper documentation- to identify logical

errors Easy and efficient coding Program debugging-detect & fix errors Program maintenance- maintenance of the

software becomes easier

Page 7: Algorithm

Algorithm -- Examples

1. Assembly instructions for a model

2. The rules of how to play a game

3. VCR instructions

4. Directions for driving from A to B

Page 8: Algorithm

Algorithmic Notations

Algorithms are identified by a algorithm name followed by the type of computationit performsEx: Algorithm Area_of_Circle( This algorithm computes the area of a circle given radius)Each step is identified by a number followed by a

description of what it doesEx: Step 3:[write the area of the circle]

print areaFinally the algorithm is terminated by using a stop/end.Step 4: [Stop]

finished

Provide the necessary

information as what the algorithm

performs, along with the inputs and

outputs

Page 9: Algorithm

Algorithm Example #1:

Algorithm Area_of_a CircleThis algorithm computes the area of a circle given radiusStep 1: [start]Step 2: [ input the value of radius]

read radiusStep 3: [ calculate the area of the circle]

area= 22/7*r*rStep 4: [ output the calculated area]

write areaStep 5: [finished]

stop

Page 10: Algorithm

Algorithm Example #2:Algorithm Area_of_a_RectangleThis algorithm computes the area of a rectangle given length and

breadthstep 1: [start]Step 2: [ input the value of length and breadth]

read length, breadth

Step 3: [ calculate the area of the rectangle]area= length * breadth

Step 4: [ output the calculated area]write area

Step 5: [finished] stop

Page 11: Algorithm

Exchange (Swap) the contents of 2 variables

a b

temp1 3

1

2

Page 12: Algorithm

Concept of Swapping““SWAP”- refers to exchange the values between 2 variables, if the 2 variables are taken as a & b with initial values a=2, b=3 then before swapping a=2,b=3, but after swapping a=3,b=2, indicating that the values are interchanged & this is achieved by using a temporary variable called “temp” to hold the value during the exchange process.Steps involved in the exchange process are1. temp=a, 2. a=b,3. b=temp.

Copy the value of a to temp

Copy the value of b to a

Copy the value of temp to b

Page 13: Algorithm

Algorithm Example # 3:Algorithm SwapThis algorithm exchanges the values of a with b and vice versastep 1: [start]Step 2: [ input the value of a and b]

read a, bStep 3: [ perform the exchange operation]

temp=a, the value of a is put to temp, hence a is empty

a=b, the value of b is put to a, thus b is emptyb=temp, temp contains the value of a and is put to b

Step 4: [ output the exchanged values of a and b]write a & b

Step 5: [finished] stop

Page 14: Algorithm

Algorithm Example # 4:Algorithm Area_of_TriangleThis algorithm calculates the area of a triangle where 3 sides a, b,c, are givenstep 1: [start]Step 2: [ input the value of a ,b, c]

read a, b, cStep 3: [ calculate the value of S]

s= (a+b+c)/2.Step 4: [ compute the area of the triangle]

area=sx(s-a)x(s-b)x(s-c)Step 5: [ display the calculated area]

write areaStep 6: [finished]

stop

Page 15: Algorithm

Algorithm Example # 5:

Algorithm Simple_InterestThis algorithm calculates the simple interest given principle, rate and

time

step 1: [start]Step 2: [ input the value of p ,t, r]

read p, t, rStep 3: [ calculate the value of Simple interest]

S.I.= (p*t*r)/100.Step 4: [ display the calculated simple interest]

write S.I.Step 5: [finished]

stop

Page 16: Algorithm

Decision making

Decision making is an integral part of every one’s

life, in contrast to the computer languages have

”language constructs”, by which decisions are

made, 2-way decision making being the most

general

Example: given a number its type is determined

depending upon it being greater or lesser than 0.

For example in c programming language the

construct if..else provides a 2 way decision making facility

Page 17: Algorithm

Format of a if..else statement

If (logical expression){True block statements}Else{False block statements}

Page 18: Algorithm

Algorithm Example # 6:Algorithm odd_EvenThis algorithm determines whether the given number is odd or evenstep 1: [start]Step 2: [ input the value of n]

read nStep 3: [ determine whether the given number is odd or even]

if(n/2=0)write n as evenelsewrite n as oddend if

Step 4: [finished] stop

Page 19: Algorithm

Algorithm Example # 7:Algorithm Pos_Neg_ZeroThis algorithm checks whether the given number is positive, negative or zero

step 1: [start]Step 2: [ input the value of n]

read nStep 3: [ determine whether the given number is positive, negative, zero]

if(n>0)write n as positivegoto step 4elsewrite n as negativegoto step 4endifwrite n as zero

Step 4: [finished] stop

Page 20: Algorithm

Algorithm Example # 8:Algorithm biggest _of_ 2 numbersThis algorithm determines the largest or biggest among 2 numbers

step 1: [start]Step 2: [ input the value of a,b]

read a,b

Step 3: [ assume that a is largest]largest=a

Step 4: [ obtain the largest among b and largest]if (b> largest)largest=bend if

Step 5: [ display the largest]write largest

Step 6: [finished] stop

Page 21: Algorithm

Algorithm Example # 9:Algorithm biggest _of_ 3 numbersThis algorithm determines the largest or biggest among 3

numbers

step 1: [start]Step 2: [ input the value of a,b,c]

read a,b,c

Step 3: [ assume that a is largest]largest=a

Step 4: [ obtain the largest among a and b]if (b> largest)largest=bEnd if

Step 5:[ obtain the largest among a ,b,c]if (c> largest)largest=cEnd if

Step 5: [ display the largest]write largest

Step 7: [finished] stop

Using if..else statementstep 1: [start]Step 2: [ input the value of a, b, c]

read a, b, c

Step 3: [ compare and b]if (a>b) and (a>c)write a else if (b>a) and (b>c)write belsewrite c

End ifStep 4: [finished] stop

Page 22: Algorithm

Algorithm Example # 9:Algorithm roots_of_ a Quadratic equationThis algorithm determines the roots of a quadratic equationstep 1: [start]Step 2: [ input the coefficients of the quadratic equation]

read a,b,cStep 3: [ check whether the roots can be formed]

if(a=b=c=0)write roots cannot be formedexit

Step 3: [ find the 2 equal roots ]if (b2-4ac=0)thenroot 1=root 2= -b/(2xa)write (“equal roots”, root 1, root2)goto step 6end if

Step 4:[ find the 2 distinct roots ]if (b2-4ac=0)thenroot 1=-b+ b2-4ac/(2xa)root 2= -b-b2-4ac/(2xa)write (“distinct roots”, root 1, root2)goto step 6end if

Step 4:[ find the 2 complex roots ]if (b2-4ac<0)thenr1=-b/(2xa) r2=|b2-4ac|/(2xa)write (“complex roots”, root 1=r1+ir2, root2=r1-ir2)end if

Step 6: [finished] stop

@ a=1,b=-4,c=4,

solve the Q.E.

Page 23: Algorithm

Divide and conquer Strategy

When a given problem is large it is always natural to divide it into smaller

independent sub problems and then to solve these sub problems independently

and to merge the results of all these sub problems together to get the final

solution.

A central program that “puts together”, the smaller sub programs are referred to

as the main programs or main functions & this strategy is commonly referred to

as the divide and conquer strategy.

The subprograms are referred differently in the programming languages for

example in PASCAL they are referred as PROCEDURES, in C as FUNCTIONS

and in FORTRAN as SUBPROGRAMS etc.

Page 24: Algorithm

From Algorithms to Programs

Problem

C Program

Algorithm: A sequence of instructions describing how to do a task (or process)

Page 25: Algorithm

Flow ChartFlowcharts use standard symbols to represent a type of operation or process to be performed. The use of standardized symbols provides a common language for people to visualize problems and also makes flowcharts easier to read and understand.

Types of Flowcharts

There are four basic types of flowcharts: Basic, Process, Deployment, and Opportunity.

Basic flowcharts quickly identify all the major steps in a process. They are used to orient a team with the major steps by just giving a broad overview of the process.

Process flowcharts examine the process in great detail. They provide a comprehensive listing of all the major and sub-steps in a process.

Deployment flowcharts are similar to Process flowcharts in that they are very detailed but also indicate the people who are involved in the process. This could be very useful when the process involves cooperation between functional areas.

Opportunity flowcharts highlight decision step and check point. They are used for very complicated processes because they highlight specific opportunities for improvement.

Page 26: Algorithm

Flow Chart - SymbolsOvals are used to represent starting and ending points to the flowchart process

Rectangles are used to describe an action taken or a task completed.

Diamonds contain questions requiring a "Yes" or "No" decision.

Data Input/Output uses a skewed rectangle to represent a point in the process where data is entered or retrieved.

Rectangles with dividers are used to describe a function or a

pre defined process

Rectangles with triangle edges are used to define looping functions

Circle – used as connector

Flow lines

Page 27: Algorithm

Terminators

Oval Shape One line in or out Used to indicate the beginning

and end of a process

Start

Stop

Page 28: Algorithm

Data Boxes

Parallelogram shaped One line in, One line out Used to indicate the input or output of data from the system..

Input

Output

Page 29: Algorithm

Process Boxes

Rectangle shaped One line in, One line out Used to indicate a process step

Process

Page 30: Algorithm

Decision Boxes

Diamond shaped One line in, two lines out Must contain a binary

question (Yes/No, True/False, 0/1, etc)

Used to branch a program dependent upon a condition being met

Yes/No Question

Page 31: Algorithm

Subprocess Box

Rectangle shaped, with bars on the sides

One line in, One line out Used to include a

predefined process

Subprocess

Page 32: Algorithm

Simple Flowchart

This chart indicates a typical example of Input, Process, Output

Page 33: Algorithm

Three basic structures

All flowcharts are composed of three basic

control structures… Sequence Decision Repetition

Page 34: Algorithm

Sequence

Events happen one after the other, with no branching

Flow is directly from top to bottom

Page 35: Algorithm

Decision Uses a decision box to

branch to one of two options

Flow is downwards Lines rejoin the flow at

lines, NOT boxes.

Page 36: Algorithm

Repetition

Used to repeat a process, or

wait until a condition is met

before proceeding. Flow is upwards. Lines join at lines, NOT

boxes.

Page 37: Algorithm

Bad design…

What is wrong with this chart? The box has two lines in,

one line out… Lines must join other lines,

never boxes!

Page 38: Algorithm

Bad design…

What is wrong with this

flowchart? Never use curved lines. Always straight Always vertical or

horizontal.

Page 39: Algorithm

Bad design…

What is wrong with this

flowchart? Never use curved lines. Always straight Always vertical or horizontal.

Page 40: Algorithm

Recommended