Professional Documents
Culture Documents
(ESO207)
Lecture 2:
• Model of computation
• Efficient algorithm for F() mod .
1
RECAP OF THE 1ST LECTURE
2
Current-state-of-the-art computer
A processor (CPU)
speed = few GHz
(a few nanoseconds to execute an instruction)
4
Homework 1
Input: a number
: long long int (64 bit integer).
Output: F() mod
9
1 minute 48 4.5 ×10 Processor: 2.7 GHz
10
10 minutes 53 4.5 ×10
11
60 minutes 58 2.6 × 10
Here are the values obtained by executing the program
on a typical computer.
5
Inferences
from the Homework
• Inference for RFib algorithm
– Too slow
• Time taken to solve a problem may vary depending upon the algorithm used
A model of computation
Simple
7
word RAM :
a model of computation
8
word RAM :
a model of computation
Data
Processor
Program
RAM
9
How is an instruction executed?
1. Decoding instruction
2. fetching the operands
3. performing arithmetic/logical operation
4. storing the result back
into RAM
Processor
RAM
Each instruction takes a few cycles (clock ticks) to get executed.
10
word RAM model of computation:
Characteristics
• Word (can be a number, name) is the basic storage unit of RAM. Word is a
collection of few bytes.
• Each word is stored in binary format.
• RAM can be viewed as a huge array of words.
• Any arbitrary location of RAM can be accessed in the same time
irrespective of the location.
• Data as well as Program reside fully in RAM.
• Each arithmetic or logical operation (+,-,*,/,or, xor,…)
involving a constant number of words
takes a constant number of cycles (steps) by the CPU.
11
Efficiency of an algorithm
13
Iterative Algorithm for F() mod
Total number of instructions=
3
IFib(,) 4+3(-1)+1
if = return ;
else if =1 return 1; 4 instructions
else { 0; 1;
For(=2 to ) do n-1 iterations
{ temp ;
+ mod ; 3 instructions per iteration
temp;
}
}
return ; the final instruction
Let us calculate the number of instructions executed by IFib(,)
14
Recursive algorithm for F(n) mod m
RFib(,)
{ if =0 return 0;
else if =1 return 1;
else return((RFib(,) + RFib(,) ) mod )
}
Let G() denote the number of instructions executed by RFib(,)
• G(0) = 1;
• G(1) = 2;
• For >1
G() = G()+G() + 4
None of them works for entire range of long long int and int
Question: Can we compute F()mod quickly ?
16
How to compute F()mod quickly ?
17
A warm-up example
18
Compute
Problem: Given three integers , , and , compute .
𝑛
𝑥 =
{ 1 if 𝑛=0
𝑥 × 𝑥
𝑛 −1
otherwise
Power(, , )
If () return 1;
else {
Power(, , ); 4 instructions
excluding the
(× ) ; Recursive call
return ;
}
19
Compute
Problem: Given three integers , , and , compute .
𝑛
𝑥 =
{ 1 if 𝑛=0
𝑥 × 𝑥
𝑛 −1
otherwise
Power(, , )
Power(, , );
No. of instructions executed by Power(, , ) = ?4 𝑛
Power(, , );
Power(, , )
20
Compute
Problem: Given three integers , , and , compute .
{
𝑛
1 if 𝑛=0
𝑥 = 𝑥?𝑛/ if
2
× 𝑥 is 𝐞𝐯𝐞𝐧
𝑛 𝑛/2
× 𝑥 s 𝐨𝐝𝐝
𝑛/ 2 𝑛/2
𝑥 ?×if 𝑥 𝑛i
Power(, , )
If () return 1;
else {
Power(, , ); 5 instructions
(× ) ; excluding the
Recursive call
if () (× ) ;
return ;
}
21
Compute
Problem: Given three integers , , and , compute .
{
𝑛
1 if 𝑛=0
𝑥 = 𝑥?𝑛/ if
2
× 𝑥 is 𝐞𝐯𝐞𝐧
𝑛 𝑛/2
× 𝑥 s 𝐨𝐝𝐝
𝑛/ 2 𝑛/2
𝑥 ?×if 𝑥 𝑛i
Power(, , )
Power(, , )
Power(, , )
No. of instructions executed by Power(, , ) = 5 ?log 2 𝑛
Power(, , )
22
Efficient Algorithm for F()mod
23
Idea 1
Unfortunately no.
24
Idea 2
F() F()
1 1
F() 1
? 0 F()
𝑛?−1
F() 1
1 1
F() 1 0 0
25
A clever algorithm for F()mod
Clever-algo-Fib(, )
{ ; 4 instructions
mod ;
×;
6 instructions
return [1]; // the first element of vector stores F()mod
}
Question: How to compute efficiently ?
Answer :
26
A clever algorithm for F()mod
Let be a 2×2 matrix.
• If is even, = ×
• If is odd, = × ×
Question: How many instructions are required to multiply two 2×2 matrices ?
Answer: 12
Question: Number of instructions for computing ?
Answer : ?
16 + 1635
+3
Question: Number of instructions in New-algo-Fib(, )
Answer: 35 + 11
27
Three algorithms
Algorithm for F()mod No. of Instructions
RFib(,)
?
IterFib(,)
Clever_Algo_Fib(,) 35 + 11
28