algorithm –history muhammad ibn musa al-khwarizmi http:// www...

21
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

Upload: clifford-carpenter

Post on 16-Dec-2015

217 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Algorithm –History Muhammad ibn Musa Al-Khwarizmi http:// www -groups.dcs.st-andrews.ac.uk/~history/Mathematicians/Al- Khwarizmi.html Book on arithmetic:

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 –History Muhammad ibn Musa Al-Khwarizmi http:// www -groups.dcs.st-andrews.ac.uk/~history/Mathematicians/Al- Khwarizmi.html Book on arithmetic:

The Problem-solving Process

Problem specification

Algorithm

Program

Executable (solution)

Analysis

Design

Implementation

Compilation

Page 3: Algorithm –History Muhammad ibn Musa Al-Khwarizmi http:// www -groups.dcs.st-andrews.ac.uk/~history/Mathematicians/Al- Khwarizmi.html Book on arithmetic:

Components of an Algorithm

1. Variables and values2. Instructions

a. Sequencesb. Proceduresc. Selectionsd. Repetitions

3. Also required: Documentation

Page 4: Algorithm –History Muhammad ibn Musa Al-Khwarizmi http:// www -groups.dcs.st-andrews.ac.uk/~history/Mathematicians/Al- Khwarizmi.html Book on arithmetic:

Algorithm basics

It 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 –History Muhammad ibn Musa Al-Khwarizmi http:// www -groups.dcs.st-andrews.ac.uk/~history/Mathematicians/Al- Khwarizmi.html Book on arithmetic:

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 –History Muhammad ibn Musa Al-Khwarizmi http:// www -groups.dcs.st-andrews.ac.uk/~history/Mathematicians/Al- Khwarizmi.html Book on arithmetic:

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 –History Muhammad ibn Musa Al-Khwarizmi http:// www -groups.dcs.st-andrews.ac.uk/~history/Mathematicians/Al- Khwarizmi.html Book on arithmetic:

Algorithm -- Examples1. Assembly instructions for a model2. The rules of how to play a game3. VCR instructions4. Directions for driving from A to B

Page 8: Algorithm –History Muhammad ibn Musa Al-Khwarizmi http:// www -groups.dcs.st-andrews.ac.uk/~history/Mathematicians/Al- Khwarizmi.html Book on arithmetic:

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 –History Muhammad ibn Musa Al-Khwarizmi http:// www -groups.dcs.st-andrews.ac.uk/~history/Mathematicians/Al- Khwarizmi.html Book on arithmetic:

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 –History Muhammad ibn Musa Al-Khwarizmi http:// www -groups.dcs.st-andrews.ac.uk/~history/Mathematicians/Al- Khwarizmi.html Book on arithmetic:

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 –History Muhammad ibn Musa Al-Khwarizmi http:// www -groups.dcs.st-andrews.ac.uk/~history/Mathematicians/Al- Khwarizmi.html Book on arithmetic:

Exchange (Swap) the contents of 2 variables

a b

temp1 3

2

Page 12: Algorithm –History Muhammad ibn Musa Al-Khwarizmi http:// www -groups.dcs.st-andrews.ac.uk/~history/Mathematicians/Al- Khwarizmi.html Book on arithmetic:

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 –History Muhammad ibn Musa Al-Khwarizmi http:// www -groups.dcs.st-andrews.ac.uk/~history/Mathematicians/Al- Khwarizmi.html Book on arithmetic:

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 empty

b=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 –History Muhammad ibn Musa Al-Khwarizmi http:// www -groups.dcs.st-andrews.ac.uk/~history/Mathematicians/Al- Khwarizmi.html Book on arithmetic:

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 –History Muhammad ibn Musa Al-Khwarizmi http:// www -groups.dcs.st-andrews.ac.uk/~history/Mathematicians/Al- Khwarizmi.html Book on arithmetic:

Algorithm Example # 5:Algorithm Simple InterestThis algorithm calculates the simple interest given principle,

rate and timestep 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 –History Muhammad ibn Musa Al-Khwarizmi http:// www -groups.dcs.st-andrews.ac.uk/~history/Mathematicians/Al- Khwarizmi.html Book on arithmetic:

Decision makingDecision 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 generalExample: 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 –History Muhammad ibn Musa Al-Khwarizmi http:// www -groups.dcs.st-andrews.ac.uk/~history/Mathematicians/Al- Khwarizmi.html Book on arithmetic:

Format of a if..else statementIf (logical expression){True block statements}Else{False block statements}

Page 18: Algorithm –History Muhammad ibn Musa Al-Khwarizmi http:// www -groups.dcs.st-andrews.ac.uk/~history/Mathematicians/Al- Khwarizmi.html Book on arithmetic:

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 –History Muhammad ibn Musa Al-Khwarizmi http:// www -groups.dcs.st-andrews.ac.uk/~history/Mathematicians/Al- Khwarizmi.html Book on arithmetic:

Algorithm Example # 7:

Algorithm Pos Neg ZeroThis algorithm checks whether the given number is positive, negative or zerostep 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 positivego to step 4elsewrite n as negativego to step 4endifwrite n as zero

Step 4: [finished] stop

Page 20: Algorithm –History Muhammad ibn Musa Al-Khwarizmi http:// www -groups.dcs.st-andrews.ac.uk/~history/Mathematicians/Al- Khwarizmi.html Book on arithmetic:

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 21: Algorithm –History Muhammad ibn Musa Al-Khwarizmi http:// www -groups.dcs.st-andrews.ac.uk/~history/Mathematicians/Al- Khwarizmi.html Book on arithmetic:

From Algorithms to Programs

Problem

C Program

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