0% found this document useful (0 votes)
5 views

Lecture 4

Uploaded by

Hassan ali Sayed
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lecture 4

Uploaded by

Hassan ali Sayed
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

1

Lecture 4. Dated:10/05/24

RAM Model, Space and Time Complexity

Dr. Shamshad Lakho


2

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.
3

The RAM Model


• Random Access Machine (not R.A. Memory)
• An idealized notion/concept of how the
computer works
• Each "simple" operation (+, -, =, if) takes
exactly 1 step.
• Each memory access takes exactly 1 step
• Loops and method calls are not simple
operations, but depend upon the size of the
data and the contents of the method.
• Measure the run time of an algorithm by
counting the number of steps.
4

Random Access Machine


• A Random Access Machine (RAM) consists of:
• a fixed program . . .
• an unbounded memory input tape
• a read-only input tape
• a write-only output tape
Program
• Each memory register can hold an arbitrary

...
integer (*). Memory
• Each tape cell can hold a single symbol from a output tape
finite alphabet s. . . .

Instruction set:
x  y, x  y {+, -, *, div, mod} z
goto label
if y {<, , =,  ,> , } z goto label
x  input, output  y
halt
5

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
6

Space Complexity (Cont !!!)


• Fixed part: The size required to store
certain data/variables, that is independent
of the size of the problem:

• Such as int a (2 bytes, float b (4 bytes)


etc

• Variable part: Space needed by variables,


whose size is dependent on the size of the
problem:
• Dynamic array a[]
7

Space Complexity (cont’d)


• S(P) = c + S(instance characteristics)Formula to
calculate the space complexity of algorithm
• c = constant
• Example:
void float sum (float* a, int n)
N=2 bytes
{ A=4
float s = 0; S=4
for(int i = 0; i<n; i++) { i=2
Total=12 bytes
s+ = a[i]; N=4=>4*4=16bytes or 4 spaces
}
return s;
}
Constant Space:
one for n, one for a [passed by reference!], one for s, one
for I , constant space=c=4
8

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

• number of primitive operations performed


• Primitive operation
• unit of operation that can be identified in the pseudo-code
9

Steps To determine Time Complexity


Step-1. Determine how you will measure input size. Ex:
• N items in a list
• N x M table (with N rows and M columns)
• Two numbers of length N
Step-2. Choose the type of operation (or perhaps two
operations)
• Comparisons
• Swaps
• Copies
• Additions

Note: Normally we don't count operations in input/output.


10

Steps To determine Time Complexity


(Cont !!!)
Step-3. Decide whether you wish to count operations in
the
• Best case? - the fewest possible operations
• Worst case? - the most possible operations
• Average case? This is harder as it is not always clear
what is meant by an "average case". Normally
calculating this case requires some higher
mathematics such as probability theory.
Step-4. For the algorithm and the chosen case (best,
worst, average), express the count as a function of the
input size of the problem.
11

Steps To determine Time Complexity


(Cont !!!)
5. Given the formula that you have determined,
decide the complexity class of the algorithm.
Q. What is the complexity class of an algorithm?
A. a complexity class is a set of problems of
related resource-based complexity, such as P, NP
classes
Q. Is there really much difference between
3n
5n + 20
and 6n -3
A. Yes but when n is large?
12

Describe the Polynomial, NP, and


hard problems
13

Primitive Operations in an
algorithm
• Assign a value to a variable (i.e. a=5)
• Call a method (i.e. method())
• Arithmetic operation (i.e. a*b, a-b*c)
• Comparing two numbers ( i.e. a<=b, a>b
&&a>c)
• Indexing into an array (i.e. a[0]=5)
• Following an object reference (i.e. Test
obj)
• Returning from a method (i.e. return I )
14

Assignment Statement
• The running time of a an assignment is
considered as constant
• Examples:
• A=5;
• A[5]=7
• C=a+b;

Note:- Running time of


assignments can be
considered as 1 or C
(Refer to constant values)
15

for Loops
• The running time of a for loop is at most the
running time of the statements inside the for
loop (including tests) times the number of
iterations.
Example Let
For(i=0;i<=n-1;i++) 1. running time of
A[i]=0; basic operations is
constant C
2. and loop is
Note: iterated n times
(number of basic then
steps in loop body) * Total Running time
(number of will be
iterations) n*c
16

Nested for Loops


• The total running time of a statement
inside a group of nested for loops is the
running time of the statement multiplied
by the product of the sizes
Let of all the for
loops. 1. running time of
• Example basic operations is
for(i=0;i<=n-1;i++) constant C
for(j=0;j<=m-1;j++) 2. Running time of
outer loop (i.e. i)
K++;
is n
3. And running time
of Inner loop (i.e.
j) is m
4. Total Running time
17

Consecutives for Loops


• The total running time of consecutive
loops is the sum of running of all
consecutives loops Let
• Example 1. running time of
for(i=0;i<=n-1;i++) basic operations is
K++; constant C
2. Running time of
for(j=0;j<=m-1;j++)
loop (i.e. i) is n
K++; 3. And running time
of loop (i.e. j) is
m
4. Total Running time
will be
n*c + m * c

You might also like