Data Structures and Algorithms: (CS210/ESO207/ESO211)
Data Structures and Algorithms: (CS210/ESO207/ESO211)
(CS210/ESO207/ESO211)
Lecture 13
Ram model of computation further refinement
Algorithm paradigms
Algorithm paradigm of Divide and Conquer
1
Word RAM model of computation
Further refinements
2
word RAM : a model of computation
3
RAM
Processor
Data
Program
Execution of a instruction
(fetching the operands, arithmetic/logical operation, storing the result back into RAM)
4
RAM
Processor
word RAM model of computation:
Characteristics
Word is the basic storage unit of RAM. Word is a collection of few bytes.
Each input item (number, name) 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 steps by the CPU.
5
Each arithmetic or logical operation (+,-,*,/,or, xor,) involving O( log n) bits
take a constant number of steps by the CPU, where n is the number of bits of
input instance.
Justification for the extension
Question: How many bits are needed to access an input item from RAM ?
Answer: At least log n. (k bits can be used to create at most
different addresses)
Current-state-of-the-art computers:
RAM of size 4GB
Hence 32 bits to address any item in RAM.
Support for 64-bit arithmetic
Ability to perform arithmetic/logical operations on any two 64-bit numbers.
6
Algorithm Paradigms
7
Algorithm Paradigm
Motivation:
Many problems whose algorithms are based on a common approach.
A need of a systematic study of the characteristics of such widely used
approaches.
Algorithm Paradigms:
Divide and Conquer
Greedy Strategy
Dynamic Programming
Local Search
8
Divide and Conquer paradigm for
Algorithm Design
9
Divide and Conquer paradigm
An Overview
A problem in this paradigm is solved in the following way.
1. First Divide the problem instance into two or more instances
of the same problem. Solve each smaller instances
recursively (base case suitably defined).
2. Combine the solutions of the smaller instances to get the
solution of the original instance.
10
This is usually the main nontrivial step
in the design of an algorithm using
divide and conquer strategy
Example 1
11
Sorting
A Homework given in 5
th
Lecture
Merging two sorted arrays:
Given two sorted arrays A and B storing n elements each, Design an O(n) time
algorithm to output a sorted array C containing all elements of A and B.
Example: If A={1,5,17,19} B={4,7,9,13}, then output is
C={1,4,5,7,9,13,17,19}.
12
B A
Merging two sorted arrays A and B
13
1 5
13 9 7
17 19
4
C
5 7 9 13 1 4
17 19
Pesudo-code for Merging two sorted arrays
Merge(A[0..n-1],B[0..m-1])
{
i 0; j 0; k 0;
While(i<n and j<m)
{ If(A[i]< B[j]) { C[k] A[i]; k++; i++ }
Else { C[k] B[j]; k++; j++ }
}
While(i<n) { C[k] A[i]; k++; i++ }
While(j<m) { C[k] B[j]; k++; j++ }
return C;
}
14
Time Complexity =
O(n+m)
Correctness : An exercise
Divide and Conquer based sorting algorithm
MSort(A,i,j)
{ If (i< j)
{ mid(i+j)/2;
MSort(A,i,mid);
MSort(A,mid+1,j);
Create a temporary array C[0..,j+i-1]
C Merge(A[i,mid], A[mid+1,j]);
Copy C[0..,j+i-1] to A[i..j]
}
15
Divide step
Combine/conquer step
This is Merge Sort
algorithm
Analysis of Merge Sort
Time complexity:
If n = 1,
T(n) = c for some constant c
If n > 1,
T(n) = cn + 2 T(n/2)
= cn + cn +
T(n/
)
= cn + cn + cn +
T(n/
)
= cn + . (Log n terms) .+ cn
= O(n log n)
16
Analysis of Merge Sort
Correctness:
Question: What is to be proved ?
Answer: MSort(A,i,j) sorts the subarray A[i..j]
Question: How to prove ?
Answer:
By induction on the length (j-i+1) of the subarray.
Use correctness of the algorithm Merge.
17
Example 2
18
Faster algorithm for multiplying two
integers
Addition is faster than multiplication
Given: any two -bit numbers X and Y
Question: how many bit-operations are required to compute X+Y ?
Answer: O()
Question: how many bit-operations are required to compute X*
?
Answer: O() [left shift the number X by places, (do it carefully)]
Question: how many bit-operations are required to compute X*Y ?
Answer: O(
2
) (why ??)
19
Can we compute
X*Y faster ??
Pursuing Divide and Conquer approach
Question: how to express X*Y in terms of multiplication/addition of
{A,B,C,D} ?
Hint: First Express X and Y in terms of {A,B,C,D}.
X = A*
/
+ B and Y = C*
/
+ D
Hence
X*Y = (A*C)* ?? + (A*D + B*C)* ?? + B*D
20
X
Y
MSB LSB
n-1 n-2......n/2.............................2 1 0
A B
C D
/
4 multiplications
Pursuing Divide and Conquer approach
X*Y = (A*C)* ?? + (A*D + B*C)* ?? + B*D
Let T(n) : time complexity of multiplying X and Y using the above equation.
T(n) = cn + 4 T(n/2) for some constant c
= cn + 2cn +
T(n/
)
= cn + 2cn + 4cn +
T(n/
)
= cn + 2cn + 4cn + 8cn + +
= cn + 2cn + 4cn + 8cn + +
21
X
Y
MSB LSB
n-1 n-2......n/2.............................2 1 0
A B
C D
/
O(
) time algo
Pursuing Divide and Conquer approach
X*Y = (A*C)* ?? + (A*D + B*C)* ?? + B*D
Observation: A*D + B*C = (A+B)*(C+D) A*C B*D
Question: How many multiplications do we need now to compute X*Y ?
Answer: 3 multiplications :
A*C
B*D
(A+B)*(C+D) .
22
X
Y
MSB LSB
n-1 n-2......n/2.............................2 1 0
A B
C D
/
Pursuing Divide and Conquer approach
X*Y = (A*C)* ?? + ((A+B)*(C+D) - A*C B*D)* ?? + B*D
Let T(n) : time complexity of the new algo for multiplying two n-bit numbers
T(n) = cn + 3 T(n/2) for some constant c
= cn + 3/2cn +
T(n/
)
= cn + 3/2cn + (9/4)cn + +
= O(
) = O(
.
)
23
X
Y
MSB LSB
n-1 n-2......n/2.............................2 1 0
A B
C D
/
Conclusion
Theorem: There is a divide and conquer based algorithm for
multiplying any two n-bit numbers in O(
.
) time (bit operations).
Note:
The fastest algorithm for this problem runs in a little bit more than O( log )
time. One such algorithm was designed in 2008 by Dey, Kurur, Saha, and
Saptharishi (CSE, IIT Kanpur).
24