Professional Documents
Culture Documents
(CSE102)
Lecture 3:
• Efficiency of Algorithms
1
Let us open a desktop/laptop
A processor (CPU)
speed = few GHz
(a few nanoseconds to execute an instruction)
2
A simplifying assumption
(for the rest of the lecture)
3
4
What is an algorithm ?
Definition:
A finite sequence of well defined instructions
required to solve a given computational problem.
5
6
Subset-sum problem
𝒏/𝟐
The fastest existing algorithm till date : 𝟐 instructions
7
Revisiting Fibonacci numbers
Fibonacci numbers
𝑭(0) = 0;
𝑭(1) = 1;
𝑭(𝑛) = 𝑭(𝑛 − 1) + 𝑭(𝑛 − 2) for all 𝑛 > 1;
8
Iterative Algorithm for 𝑭(𝒏)
IFib(n)
if n=0 return 0;
else if n=1 return 1;
else {
a 0; b 1;
For (i=2 to n) do
{ temp b;
b a+b;
a temp;
}
}
return b;
9
Recursive algorithm for 𝑭(𝒏)
Rfib(n)
{ if n=0 return 0;
else if n=1 return 1;
else return (Rfib(n-1) + Rfib(n-2))
}
10
Exercise
Input: A number n
n is a 64-bit integer
1 minute
10 minutes
60 minutes
11
Exercise
Input: A number n
n is a 64-bit integer
• Time taken to solve a problem may vary depending upon the algorithm used
14
Current-state-of-the-art Computer
A model of computation
Simple
15
word RAM :
a model of computation
17
word RAM :
a model of computation
Data
Processor
Program
RAM
18
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.
19
word RAM model of computation:
Characteristics
• Word is the basic storage unit of RAM. Word is a collection of few bytes.
22
Iterative Algorithm for F(𝑛) mod 𝑚
Total number of instructions=
4+3(𝑛-1)+1 ≈ 3𝑛
IFib(𝑛,𝑚)
if 𝑛 = 0 return 0;
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(𝑛,𝑚)
23
Recursive algorithm for F(n) mod m
RFib(𝑛,𝑚)
{ if 𝑛 =0 return 0;
else if 𝑛 =1 return 1;
else return((RFib(𝑛 − 1,𝑚) + RFib(𝑛 − 2,𝑚) ) mod 𝑚)
}
Let G(𝑛) denote the number of instructions executed by RFib(𝑛,𝑚)
• G(0) = 1;
• G(1) = 2;
• For 𝑛 >1
G(𝑛) = G(𝑛 − 1 )+G(𝑛 − 2) + 4
24
Algorithms for F(𝑛)mod 𝑚
𝑛−2
• # instructions by Recursive algorithm RFib(𝑛): > 2 2
(exponential in 𝑛)
• # instructions by Iterative algorithm IFib(𝑛): 3𝑛
( linear in 𝑛)
25
How to compute F(𝑛)mod 𝑚 quickly ?
26
Idea 1
Unfortunately no.
27
Idea 2
F(𝑛) F(𝑛 − 1)
1 1
F(𝑛 − 1) 1
? 0 F(𝑛 − 2)
𝑛?− 1
F(𝑛) 1
1 1
F(𝑛 − 1) 1 0 0
28
A clever algorithm for F(𝑛)mod 𝑚
Clever-algo-Fib(𝑛, 𝑚)
1 1
{ A ; 4 instructions
1 0
B A𝑛−1 mod 𝑚;
C B × 10 ; 6 instructions
29
A clever algorithm for F(𝑛)mod 𝑚
Let A be a 2×2 matrix.
• If 𝑛 is even, 𝑨𝑛 = 𝑨𝑛/2 × 𝑨𝑛/2
• If 𝑛 is odd, 𝑨𝑛 = 𝑨𝑛/2 × 𝑨𝑛/2 × A
Question: How many instructions are required to multiply two 2×2 matrices ?
Answer: 12
𝑛−1
1 1
Question: Number of instructions for computing mod 𝑚 ?
1 0
+3 𝐥𝐨𝐠 2 (𝑛 − 1)
Answer : 16 +?1635
30
Three algorithms
No. of Instructions
31
Three algorithms
Algorithm for F(𝑛)mod 𝑚 No. Instructions in RAM model
IterFib(𝑛,𝑚) 3𝑛
Clever_Algo_Fib(𝑛,𝑚) 35 𝐥𝐨𝐠 2 (𝑛 − 1) + 11
32
Three algorithms
Algorithm for F(𝑛)mod 𝑚 No. Instructions in RAM model
Clever_Algo_Fib(𝑛,𝑚) 35 𝐥𝐨𝐠 2 (𝑛 − 1) + 11
Find it yourselves ☺
33
Lesson 1 ?
?
Dependence on input
34
Lesson 2 ?
IterFib(𝑛,𝑚) 3𝑛
Clever_Algo_Fib(𝑛,𝑚) 35 𝐥𝐨𝐠 2 (𝑛 − 1) + 11
100 1000
Answer: Clever_Algo_Fib would still be the fastest algorithm ...for large value of 𝑛.
35