DAA Unit2
DAA Unit2
Class - T.Y.
PLD (SEM-I)
Unit - 2
1
MIT School of Computing
Department of Computer Science & Engineering
Unit-II Syllabus
Brute Force: Principle, Brute force string
matching algorithm and its analysis, NAÏVE
string matching algorithms and its analysis, Rabin
PLD
Karp algorithm and its analysis;
Divide-and-Conquer Methodology: Principle,
algorithms: Binary Search and its analysis, Merge
sort and its analysis, Quick sort and its analysis,
Stassen’s matrix multiplication
2
MIT School of Computing
Department of Computer Science & Engineering
4
MIT School of Computing
Department of Computer Science & Engineering
PLD
5
MIT School of Computing
Department of Computer Science & Engineering
PLD
6
MIT School of Computing
Department of Computer Science & Engineering
PLD
7
MIT School of Computing
Department of Computer Science & Engineering
PLD
8
MIT School of Computing
Department of Computer Science & Engineering
PLD
9
MIT School of Computing
Department of Computer Science & Engineering
PLD
10
MIT School of Computing
Department of Computer Science & Engineering
11
MIT School of Computing
Department of Computer Science & Engineering
Example:
pat[] = "STRING"
PLD
12
MIT School of Computing
Department of Computer Science & Engineering
Input:
Output:
Analysis: This for loop from 3 to 5 executes for n-m + 1(we need at least
m characters at the end) times and in iteration we are doing m
comparisons. So the total complexity is O (n-m+1).
14
MIT School of Computing
Department of Computer Science & Engineering
txt[] = "BBACCAADDEE";
pat[] = "HBB";
15
MIT School of Computing
Department of Computer Science & Engineering
2) Worst case also occurs when only the last character is different.
txt[] = "VVVVVVVVVVVVK";
pat[] = "VVVK";
16
MIT School of Computing
Department of Computer Science & Engineering
RABIN-KARP-MATCHER (T, P, d, q)
1. n ← length [T]
2. m ← length [P]
3. h ← d mod q
4. p ← 0
PLD
5. t0 ← 0
6. for i ← 1 to m
7. do p ← (dp + P[i]) mod q
8. t0 ← (dt0+T [i]) mod q
9. for s ← 0 to n-m
10. do if p = ts
11. then if P [1.....m] = T [s+1.....s + m]
12. then "Pattern occurs with shift" s
13. If s < n-m
14. then ts+1 ← (d (ts-T [s+1]h)+T [s+m+1])mod q 18
MIT School of Computing
Department of Computer Science & Engineering
PLD
19
MIT School of Computing
Department of Computer Science & Engineering
PLD
20
MIT School of Computing
Department of Computer Science & Engineering
PLD
21
MIT School of Computing
Department of Computer Science & Engineering
PLD
22
MIT School of Computing
Department of Computer Science & Engineering
PLD
23
MIT School of Computing
Department of Computer Science & Engineering
PLD
24
MIT School of Computing
Department of Computer Science & Engineering
PLD
25
MIT School of Computing
Department of Computer Science & Engineering
Complexity:
The running time of RABIN-KARP-MATCHER in the worst case
scenario O ((n-m+1) m
But it has a good average case running
PLD time
26
MIT School of Computing
Department of Computer Science & Engineering
Complexity:
The running time of RABIN-KARP-MATCHER in the worst case
scenario O ((n-m+1) m
But it has a good average case running
PLD time
27
MIT School of Computing
Department of Computer Science & Engineering
28
MIT School of Computing
Department of Computer Science & Engineering
Technique
PLD
29
MIT School of Computing
Department of Computer Science & Engineering
Algorithm
PLD
30
MIT School of Computing
Department of Computer Science & Engineering
Flowchart
PLD
31
MIT School of Computing
Department of Computer Science & Engineering
Complexity Analysis
PLD
32
MIT School of Computing
Department of Computer Science & Engineering
Let n be the total number of elements in the list under search and there exist an
integer k such that:-
•For successful search:-
•If , then the binary search algorithm requires at least one comparison and
at most k comparisons.
•For unsuccessful search:-
•If , then the binary search algorithm requires k comparisons.
•If PLDalgorithm requires either k-1 or k number
, then the binary search
of comparisons
33
MIT School of Computing
Department of Computer Science & Engineering
34
MIT School of Computing
Department of Computer Science & Engineering
PLD
35
MIT School of Computing
Department of Computer Science & Engineering
PLD
36
MIT School of Computing
Department of Computer Science & Engineering
37
MIT School of Computing
Department of Computer Science & Engineering
PLD
38
MIT School of Computing
Department of Computer Science & Engineering
PLD
39
MIT School of Computing
Department of Computer Science & Engineering
PLD
40
MIT School of Computing
Department of Computer Science & Engineering
PLD
41
MIT School of Computing
Department of Computer Science & Engineering
PLD
42
MIT School of Computing
Department of Computer Science & Engineering
43
MIT School of Computing
Department of Computer Science & Engineering
Quicksort I
■ To sort a[left...right]:
1. if left < right: PLD
Partitioning II
Partitioning
■ To partition a[left...right]:
1. Set pivot = a[left], l = left + 1, r = right;
2. while l < r, do
PLD
2.1. while l < right & a[l] < pivot , set l = l + 1
2.2. while r > left & a[r] >= pivot , set r = r - 1
2.3. if l < r, swap a[l] and a[r]
3. Set a[left] = a[r], a[r] = pivot
4. Terminate
46
MIT School of Computing
Department of Computer Science & Engineering
Example of partitioning
■ choose pivot: 436924312189356
■ search: 436924312189356
■ swap: 433924312189656
PLD
■ search: 433924312189656
■ swap: 433124312989656
■ search: 433124312989656
■ swap: 433122314989656
■ search: 4 3 3 1 2 2 3 1 4 9 8 9 6 5 6 (left > right)
■ swap with pivot: 1 3 3 1 2 2 3 4 4 9 8 9 6 5 6
47
MIT School of Computing
Department of Computer Science & Engineering
48
MIT School of Computing
Department of Computer Science & Engineering
Strassen’s Algorithm
Strassen's Matrix Multiplication is the divide and conquer approach to
solve the matrix multiplication problems. The usual matrix multiplication
method multiplies each row with each column to achieve the product
by this approach is O(n3), since it takes
matrix. The time complexity takenPLD
two loops to multiply. Strassen’s method was introduced to reduce the
time complexity from O(n3) to O(nlog 7).
49
MIT School of Computing
Department of Computer Science & Engineering
Strassen’s Algorithm
Multiply 2 x 2 Matrices:
| r s | | a b| |e g| Where:
=
| t u| | c d| | f h| p1 = (b + d)(f + g)
PLD
p2= (c + d)e
p3= a(g – h)
r = p 1 + p 4 – p5 + p 7
p4= d(f – e)
s = p3 + p5
p5= (a – b)h
t = p2 + p5
p6= (c – d)(e + g)
u = p 1 + p 3 – p2 + p 7
p7= (b – d)(f + h)
50