3. Intro uc n
d tio
• An algorithm, named after the ninth century Muslim
scholar Abu Jafar Muhammad Ibn Musu Al-
Khowarizmi, is defined as follows:
• An algorithm is a finite step-by-step procedure to
achieve a required result.
• An algorithm is a sequence of computational steps
that transform the input into the output.
• An algorithm is a sequence of operations performed
on data that have to be organized in data structures.
4. Algorithmic Notations
• variable:=expression
• if condition then statements {elsif condition then statements}
[else statements]
• case switch of {sub case: statements}
• while condition do statements
• repeat statements until condition
• for variable:=initial value [step step] to final value do statements
• proc procedure name (parameters) [returns type] statements
• procedure name (arguments)
• return expression
• read variable
• write expression
6. Programming notation
General purpose algorithmic notations
Pascal, C, C++, Java
• Declarations:
int i, j = 3; double sum = 0; real x;
int a[n] // array of n integers
int b[1:n] # array of n integers5
double c[n,n] = ([n] ([n] 1.0))
7. Programming notation (2)
• Sequential statements
(look: Java)
for [quantifier1, quantifier2 , ….] {
(list of statements)
}
quantifiers:
i=0 to N i=1 to N by 2
i=0 to n-1 st i!=x #every value except x
8. Programming notation (3)
• Concurrent statements
– co statement1; #N parallel processes
// ...
// statementN;
oc
– process foo { # a process in the ...
(list of statements) # ... background
}
process bar [i=1 to n] # n processes
9. Programming notation (4)
• Functions and Procedures
• int addone(int v) { # an integer fnct
return (v+1);
}
• main () { # a “void" procedure
int n, sum;
read(n);
for [i = 1 to n]
sum = sum + addone(i);
write (“the final value is”, sum);
}
10. Specification notation (1)
• Specifying atomic transactions
process_state_1 #state of computation
<S1; … Sn;> #list of statements
process_state_2 #state of computation
the execution is atomic: the other processes see
either state_1 or state_2
• Example: x = 0; y = 10;
< x = x+1; y = y + 1; >
For other processes:
either x==0 & y==10 or x==1 & y==11
11. Specification notation (2)
• Specification of await
< await (B) S; >
– B delay condition
– S sequence of statements
• B is guaranteed to be true when execution of S
begins
• S is guaranteed to terminate
• No internal state of S is visible to other processes
• Example: <await ( s>0 ) s = s - 1; >