You are on page 1of 28

Data Structures and Algorithms

(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)

Internal memory (RAM)


size = a few GB (Stores few million bytes/words)
speed = a few GHz(a few nanoseconds to read a byte/word)

External Memory (Hard Disk Drive)


size = a few tera bytes
speed : seek time = miliseconds
transfer rate= a billion bytes per second
3
Motivation

• for Efficient algorithms


– Subset sum problem
– Sorting

• for Efficient Data Structures


– Telephone Directory
– Range Minima

4
Homework 1

Write a C program for the following problem:

Input: a number
: long long int (64 bit integer).
Output: F() mod

Time Taken Largest for Rfib Largest for IFib

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 

• Inference for Ifib algorithm :


– Faster than Rfib
– But not a solution for the problem 

• Efficiency of an algorithm does matter

• Time taken to solve a problem may vary depending upon the algorithm used

How to analyse the efficiency


of an algorithm ?
6
Current-state-of-the-art Computer

A model of computation

Simple

Close to real world computer

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

Question: How to measure time taken by an algorithm ?

• Number of instructions taken in word RAM model.

Variation in the time of various instructions


What about the influence of
Architecture : 32 versus 64
so many other parameters ?
Code
We shall judge the influence, if optimization
any, of these due to Compiler
parameters through Multitasking
experiments. due to Operating system

Who knows, these factors might have


little or negligible impact on most of algorithms. 
12
Homework 1 from Lecture 1

Computing F() mod

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

Observation 1: G()>F() for all ;


G()> !!!
15
Algorithms for F()mod

• # instructions by Recursive algorithm RFib():


(exponential in )
• # instructions by Iterative algorithm IFib():
( linear in )

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 ?

… need some better insight …

17
A warm-up example

How good are your programming skills ?

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

Question: Can we express F() as for some constant ?

Unfortunately no.

24
Idea 2

F() F()
1 1

F() 1
? 0 F()

Unfolding the RHS of


this equation, we get …

𝑛?−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 :

Inspiration from Algorithm for

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

• Which algorithm is the best ?


• What is the exact base of the exponent in the running time of RFib ?
• Are we justified in ignoring the influence of so many other parameters ?
(Variation in the time of instructions/Architecture/Code optimization/…)
• How close to reality is the RAM model of computation ?
Find out yourself !

28

You might also like