MSIT-104 Data Structure and Algorithms

You might also like

Download as pps, pdf, or txt
Download as pps, pdf, or txt
You are on page 1of 20

Theory of Computer Science - Algorithms

Theory of Computer Science - Algorithms

Algorithm is a sequence of elementary operations that


give solution to any problem from a specific class of
problems in a finite number of steps.

elementary operations - one command precisely defined


all elementary operations should be precisely defined:
x<y precise definition
x<<y without additional comment it is not precise definition
any problem from a specific class of problems - not a specific, one
problem but specific class of problems
finite numbers of steps - algorithm must end and give a result
Theory of Computer Science - Algorithms

Features of a good algorithms

finiteness - executed algorithm must have the end


specificness - all operations and their order should be precisely
defined
efficiency - algorithm must give a result in a shortest way
generality - algorithm must give solution to any problem
from a specific class of problems
correctness - must give a correct solution
Theory of Computer Science - Algorithms

Basic way in creating algorithms:

1. problem definition
2. problem analysis
3. preparation of rules
4. create concise procedure for solving problem
Theory of Computer Science - Algorithms

Ways of describing algorithms:

• Recursive - mathematical function


• Natural language
• Flow chart – block diagram
• Formal languages
Theory of Computer Science - Algorithms

Recursive function - example: factorial n

Problem is defined by itself:

n! = n * (n-1)! if n = 1 then n!=1 and 0!=1

4!=4 * (4-1)! = 4 * 3!
3!= 3 * 2!
2!= 2 * 1!= 2 * 1
Theory of Computer Science - Algorithms

Ways of describing algorithms:

Recursive - mathematical function


n! = n * (n-1)!

advantage: high precision


disadvantage: can describe only few kinds of problems
Theory of Computer Science - Algorithms

Natural language - example

Give an algorithm of finding the value:


y = Max{ xi }, where 1 ≤ i ≤ n

1. i ← 1, go to 2
2. y ← xi, go to 3
3. whether i = n ? If yes – the end, if no – go to 4
4. i ← i + 1, go to 5
5. whether xi > y ? If yes – go to 2, if no– go to 3
Theory of Computer Science - Algorithms

Natural language – example: y = Max{ xi }, where 1 ≤ i ≤ n

sequence x: 3 4 2 5
quantity of x elements: n = 4
index of processing element: i=?
present maximum: y=?

1. i ← 1, go to 2
2. y ← xi, go to 3
3. whether i = n ? If yes – the end, if no – go to 4
4. i ← i + 1, go to 5
5. whether xi > y ? If yes – go to 2, if no– go to 3
Theory of Computer Science - Algorithms

Ways of describing algorithms:

Natural language

advantage: easy to understand, simplicity, no need to use


special conventions, wide vocabulary
disadvantage: lack of precision, possibility of
misunderstanding
Theory of Computer Science - Algorithms

Flow chart -block diagram


computational transaction, process

decision - testing condition

begining and the end of algorithm

input/output operations

connecting element, direction


Theory of Computer Science - Algorithms

Flow chart– example: y = Max{ xi }, where 1 ≤ i ≤ n


Theory of Computer Science - Algorithms

Ways of describing algorithms:

Flow chart formal way of describing algorithm

advantage: precise, can be used at every level of software


development, flexible
disadvantage: technical difficulty of representing large
algorithms
Theory of Computer Science - Algorithms

Formal languages – example: y = Max{ xi }, where 1 ≤ i ≤ n

The same algorithm presenting with Pascal


function Max( x : array of integer ) : integer;
var
i, y : integer;
begin
y := x[ 1 ];
for i := 2 to n do
if x[ i ] > y then
y := x[ i ];
Max := y;
end;
Theory of Computer Science - Algorithms

Formal languages – example: y = Max{ xi }, where 1 ≤ i ≤ n

The same algorithm presenting with C


int Max( int *x )
{
int i;
int y = x[0];
for( i = 1; i < n; i++ )
if( x[i] > y )
y = x[i];
return y;
}
Theory of Computer Science - Algorithms

Ways of describing algorithms:

Formal languages – programming languages

advantage: precision, executive


disadvantage: you have to know the language – difficulty of
learning the language
Theory of Computer Science - Algorithms

Sequence algorithm
The previous algorithm was a sequence algorithm.
instructions are executed one after one
n + 1 – amount of algorithm’s operations
Oi – operation number i
t( x ) – moment of time when is executed operation x

This is always true that: moment of time, when is the end of


previous operation, is in the some time or earlier, then the
beginning of next operation.
Theory of Computer Science - Algorithms

Parallel algorithm
At least 2 operations can be executed in the some time
n + 1 – amount of algorithm’s operations
Oi – operation number i
t( x ) – moment of time when is executed operation x

There is moment of time, when the end of previous operation,


is later, then the beginning of next operation.
Theory of Computer Science - Algorithms

Way of comparing algorithms:

1. Time complexity How much time is required


to execute algorithm (usually we don’t measure
time but steps)

2. Space/memory complexity How much memory


space is required, to execute the algorithm
Theory of Computer Science - Algorithms

Conclusions
• In most cases the increase in space complexity, causes the
decrease in time complexity and vice versa.
• Processed data, have implication on execution time and
needed memory.
• Pessimistic and average efficiency, have implication on
algorithm’s application.
• In parallel algorithms, amount of processors, have
implication on time execution. The more processors,
the shorter time. But there is limited amount of
processors, for each algorithm, where if we use more
processors above this limit, it doesn’t increase the time.

You might also like