Professional Documents
Culture Documents
1
Recap
Time and space tradeoff factors should be focus during designing of algorithms,
Performance of algorithm should be measure rather than performance of its implemented program.
Switching between CPU and IO circuit should be in mind before writing the instructions of an algorithm.
2
The RAM Model
3
Random Access Machine
A Random Access Machine (RAM) consists of: . . .
– a fixed program input tape
– an unbounded memory
– a read-only input tape Program
...
– a write-only output tape
Memory
Each memory register can hold an arbitrary integer (*). output tape
Each tape cell can hold a single symbol from a finite . . .
alphabet s.
Instruction set:
x y, x y {+, -, *, div, mod} z
goto label
if y {<, , =, ,> , } z goto label
x input, output y
4 halt
Space Complexity
The amount of memory required by an algorithm to run to
completion
– The term memory leaks refer to the amount of memory
required is larger than the memory available on a given
system
5
Space Complexity (Cont !!!)
6
Space Complexity (cont’d)
S(P) = c + S(instance characteristics)
– c = constant
Example:
float sum (float* a, int n)
{
float s = 0;
for(int i = 0; i<n; i++) {
s+ = a[i];
}
return s;
}
Constant Space:
one for n, one for a [passed by reference!], one for s, one for I , constant space=c=4
7
Running Time of Algorithms
Running time
– depends on input size n
size of an array
polynomial degree
# of elements in a matrix
# of bits in the binary representation of the input
vertices and edges in a graph
8
Steps To determine Time Complexity
10
Steps To determine Time Complexity (Cont !!!)
5. Given the formula that you have determined, decide the complexity class of
the algorithm.
12
Assignment Statement
RAM model is used to measure the run time of an algorithm by counting the number of steps.
Space complexity of an algorithm refer to the amount of memory required to run.
Time complexity of an algorithm refer to its running time, which depends on input size.
Primitive operations refer to the unit of operation that can be identified in the pseudo-code
22
In Next Lecturer
In next lecture, we will talk about the types of algorithm complexity and how to find running time of different pseudo
codes.
23