0% found this document useful (0 votes)
13 views50 pages

DAA Unit2

DAA Unit 2

Uploaded by

tusharmhans
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views50 pages

DAA Unit2

DAA Unit 2

Uploaded by

tusharmhans
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

MIT School of Computing

Department of Computer Science & Engineering

Third Year Engineering

18BTCS601-Design and Analysis of Algorithms

Class - T.Y.
PLD (SEM-I)

Unit - 2

Brute Force and Divide-and-Conquer


AY 2023-2024 SEM-I

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

Brute Force: Principle


● A Brute Force Algorithm is the straightforward
approach to a problem i.e., the first approach that
comes to our mind on seeing the problem.
● More technically it is just like iterating every
possibility available toPLD
solve that problem.

● Ex: Searching a String Pattern in Given Text


● Calculating Power of N
● Sorting an elements etc
when the problem size is limited it is useful.
The method is also used when the simplicity of
implementation is more important than speed.
3
MIT School of Computing
Department of Computer Science & Engineering

Brute force string matching algorithm and its analysis


What is String Matching:

Identify the index of String (m) in Given Text (P)


where m < p
PLD
Search for string ‘ NOT ‘ in the Text “ NOBODY NOTICED HIM’

4
MIT School of Computing
Department of Computer Science & Engineering

Brute force string matching algorithm

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

NAÏVE string matching algorithms and its analysis


● Finding all occurrences of a pattern in a given text(or body of
text).
● Naive algorithm is exact string matching(means finding one
or all exact occurrences of a pattern in a text) algorithm.
PLD
● The naive approach tests all the possible placement of
Pattern P [1…….m] relative to text T [1……n].
● We try shift s = 0, 1…….n-m, successively and for each shift s.
● Compare T [s+1…….s+m] to P [1……m].It returns all the valid
shifts found.

11
MIT School of Computing
Department of Computer Science & Engineering

NAÏVE string matching algorithms and its analysis

Example:

Input: txt[] = "THIS IS STRING MATCHING ALGORITHM"

pat[] = "STRING"
PLD

Output: Pattern found at position 10

12
MIT School of Computing
Department of Computer Science & Engineering

NAÏVE string matching algorithms and its analysis


Example :

Input:

Main String: “ABAAABCDBBABCDDEBCABC”


PLD
pattern: “ABC”

Output:

Pattern found at position: 4

Pattern found at position: 10

Pattern found at position: 18


13
MIT School of Computing
Department of Computer Science & Engineering

NAÏVE string matching algorithm


NAIVE-STRING-MATCHER (T, P)
1. n ← length [T]
2. m ← length [P]
3. for s ← 0 to n -m
4. do if P [1.....m] = TPLD[s + 1....s + m]
5. then print "Pattern occurs with shift"

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

NAÏVE string matching algorithm analysis

What is the best case?

The best case occurs when the first character of the


pattern is not present in text at all.
PLD

txt[] = "BBACCAADDEE";

pat[] = "HBB";

The number of comparisons in best case is O(n).

15
MIT School of Computing
Department of Computer Science & Engineering

NAÏVE string matching algorithm analysis

What is the worst case ?


The worst case of Naive Pattern Searching occurs in following scenarios.
1) When all characters of the text and pattern are same.
txt[] = "DDDDDDDDDDDD";
PLD
pat[] = "DDDDD";

2) Worst case also occurs when only the last character is different.
txt[] = "VVVVVVVVVVVVK";
pat[] = "VVVK";

The number of comparisons in the worst case is O(m*(n-m+1)).

16
MIT School of Computing
Department of Computer Science & Engineering

Rabin Karp algorithm and its analysis;

It is String matching algorithm.


It is another application of Hashing.
The Rabin-Karp string searching algorithm calculates a hash value for
the pattern, and for each M-character subsequence of text to be
PLD
compared.
If the hash values are unequal, the algorithm will calculate the hash value
for next M-character sequence.
If the hash values are equal, the algorithm will compare the pattern and
the M-character sequence.
In this way, there is only one comparison per text subsequence, and
character matching is only needed when hash values match.
17
MIT School of Computing
Department of Computer Science & Engineering

Rabin Karp algorithm

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

Rabin Karp algorithm

PLD

19
MIT School of Computing
Department of Computer Science & Engineering

Rabin Karp algorithm

PLD

20
MIT School of Computing
Department of Computer Science & Engineering

Rabin Karp algorithm

PLD

21
MIT School of Computing
Department of Computer Science & Engineering

Rabin Karp algorithm

PLD

22
MIT School of Computing
Department of Computer Science & Engineering

Rabin Karp algorithm

PLD

23
MIT School of Computing
Department of Computer Science & Engineering

Rabin Karp algorithm

PLD

24
MIT School of Computing
Department of Computer Science & Engineering

Rabin Karp algorithm

PLD

25
MIT School of Computing
Department of Computer Science & Engineering

Rabin Karp algorithm

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

Rabin Karp algorithm

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

Binary Search Algorithm

is a searching algorithm used in a sorted array by repeatedly

dividing the search interval in half. The idea of binary search is

to use the information that the


PLDarray is sorted and reduce the

time complexity to O(log N).

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

DIVIDE AND CONQUER


• Divide and Conquer Algorithm involves breaking a larger
problem into smaller subproblems, solving them
independently, and then combining their solutions to solve
the original problem.
PLD

• The basic idea is to recursively divide the problem into


smaller subproblems until they become simple enough to
be solved directly. Once the solutions to the subproblems
are obtained, they are then combined to produce the overall
solution.

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

• Merge Sort Algorithm:


• Divide: If S has at leas two elements (nothing needs to be done if S has zero
or one elements), remove all the elements from S and put them into two
sequences, S1 and S2, each containing about half of the elements of S. (i.e. S1
contains the first ⎡n/2⎤ elements and S2 contains the remaining ⎣n/2⎦
elements.
• Recur: Recursive sort sequences S1 and S2.
• Conquer: Put back the elements into S by merging the sorted sequences S1
PLD
and S2 into a unique sorted sequence.
• Merge Sort Tree:
• Take a binary tree T
• Each node of T represents a recursive call of the merge sort algorithm.
• We associate with each node v of T a the set of input passed to the invocation
v represents.
• The external nodes are associated with individual elements of S, upon which
no recursion is called.

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

Quicksort I: Basic idea


■ Pick some number p from the array
■ Move all numbers less than p to the beginning of the array
■ Move all numbers greater than (or equal to) p to the end of the
array

PLD p
Quicksort the numbers less than
■ Quicksort the numbers greater than or equal to p

numbers p numbers greater than


less than p or equal to p

43
MIT School of Computing
Department of Computer Science & Engineering

Quicksort I

■ To sort a[left...right]:
1. if left < right: PLD

1.1. Partition a[left...right] such that:


all a[left...p-1] are less than a[p], and
all a[p+1...right] are >= a[p]
1.2. Quicksort a[left...p-1]
1.3. Quicksort a[p+1...right]
2. Terminate
44
MIT School of Computing
Department of Computer Science & Engineering

Partitioning II

■ Choose an array value (say, the first) to use as the


pivot
■ Starting from the left end, find the first element
that is greater than or equal to the pivot
■ Searching backward from the right end, find the
first element that is less than the pivot
■ Interchange (swap) these two elements
■ Repeat, searching from where we left off, until
done
45
MIT School of Computing
Department of Computer Science & Engineering

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

Typical case for quicksort


■ If the array is sorted to begin with, Quicksort is
terrible: O(n2)
■ It is possible to construct
PLD other bad cases

■ However, Quicksort is usually O(n log2n)


■ The constants are so good that Quicksort is
generally the fastest algorithm known
■ Most real-world sorting is done by Quicksort

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

You might also like