Lecture 4
Lecture 4
Lecture 4. Dated:10/05/24
Recap
• Time and space tradeoff factors should be focus
during designing of algorithms,
...
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
• size of an array
• polynomial degree
• # of elements in a matrix
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;
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