Adsa
Adsa
ADSA
Q Justify how KMP algorithm is more efficient than naïve
string-matching algorithm.
The Knuth-Morris-Pratt (KMP) algorithm is more efficient than the naïve
string-matching algorithm because it significantly reduces unnecessary
comparisons when searching for a pattern in a text. Here's a detailed
comparison between the two:
Naïve String-Matching
Criteria KMP Algorithm
Algorithm
ADSA 1
No preprocessing of the Preprocessing of pattern using
Preprocessing Step
pattern LPS array in O(m) time
Traffic Networks: The algorithm is used to find the shortest path between
points in a visitors' network. It can help reduce congestion and improve the
go with the flow of visitors in city areas.
ADSA 2
Naïve String Matching Algorithm
The naïve string matching algorithm is a simple and straightforward approach
for finding occurrences of a pattern within a text. It works by checking every
possible position in the text to see if the pattern matches the substring starting
at that position.
Algorithm Steps:
1. Start at the first position of the text.
2. Compare the pattern with the substring of the text at the current position.
4. Move the pattern by one position to the right in the text and repeat the
comparison until all positions have been checked.
Time Complexity:
Worst-case: O(m⋅n), where m is the length of the pattern and n is the
length of the text.
Example:
Given a text T = "ABAAABCD" and a pattern P = "ABC" , the algorithm compares P
with every substring of length 3 in T and reports a match at index 4 (0-based
index).
Advantages:
1. Simplicity: The algorithm is easy to understand and implement.
Disadvantages:
1. Inefficiency: In the worst case, the algorithm performs O(m⋅n)
comparisons. This happens when the text and pattern have many matching
ADSA 3
initial characters but no complete match (e.g., when searching for AAAAA in
AAAAAA ).
O(m*n)
3. Not Optimal for Large Texts: For large texts or patterns, the algorithm
becomes inefficient compared to more advanced algorithms like KMP or
Boyer-Moore, which reduce unnecessary comparisons.
Q Rabin-Karp String Matching Algorithm
The Rabin-Karp algorithm is a string searching algorithm that uses hashing to
find any one of a set of pattern strings in a text. It is particularly efficient for
searching multiple patterns simultaneously. The main idea is to compute a hash
value for the pattern and compare it to the hash values of substrings in the
text.
Algorithm Steps:
1. Hash Calculation: Calculate the hash value of the pattern and the hash
value of the first substring of the text that has the same length as the
pattern.
2. Sliding Window: Move through the text, updating the hash value for the
next substring by removing the leading character and adding the trailing
character. This is done in constant time using a rolling hash technique.
Time Complexity:
Average Case: O(n+m), where n is the length of the text and m is the
length of the pattern.
O(n+m)
ADSA 4
Worst Case: O(n⋅m) due to hash collisions leading to character
comparisons.
O(n⋅m)
Example:
Given a text T = "ABCDABCE" and a pattern P = "ABC" , the Rabin-Karp algorithm
computes the hash of P and compares it with the hash of substrings of T . If
the hash matches, it checks the characters to confirm the match.
Advantages:
1. Multiple Pattern Search: The algorithm is efficient for searching multiple
patterns at once, as it allows for simultaneous hash calculations for
different patterns.
Disadvantages:
1. Hash Collisions: The worst-case performance can degrade to O(n⋅m) due
to hash collisions, which necessitate direct comparisons.
P Class
ADSA 5
Definition: The class P (Polynomial time) consists of decision problems
(problems with a yes/no answer) that can be solved by a deterministic
Turing machine in polynomial time. This means there exists an algorithm
that can solve the problem in a time complexity of O(n^k) for some
constant k, where n is the size of the input.
Characteristics:
Matrix multiplication
NP Class
Definition: The class NP (Nondeterministic Polynomial time) consists of
decision problems for which a solution can be verified by a deterministic
Turing machine in polynomial time. Alternatively, a problem is in NP if a
hypothetical "nondeterministic" Turing machine can solve it in polynomial
time.
Characteristics:
ADSA 6
Hamiltonian path problem: Does there exist a path in a graph that
visits each vertex exactly once?
Q Optimization Problem
An optimization problem is a mathematical problem that seeks to find the best
solution from a set of feasible solutions, according to a specified criterion or
objective function. In essence, the goal is to maximize or minimize a particular
quantity while satisfying a set of constraints.
2. Decision Variables: Variables that affect the value of the objective function
and can be adjusted to find the optimal solution.
Breaks down problems into simpler subproblems and solves each one
only once, storing the solutions for future reference.
ADSA 7
Example: The Knapsack problem, where items must be selected to
maximize value without exceeding a weight limit.
4. Greedy Algorithms:
Makes a series of choices that look best at the moment, aiming for a
local optimum.
7. Simulated Annealing:
8. Constraint Programming:
ADSA 8
1. Resource Allocation:
Problem: Find the shortest possible route that visits a set of cities and
returns to the origin city.
3. Knapsack Problem:
5. Portfolio Optimization:
6. Job Scheduling:
The
0/1 Knapsack Problem is a classic optimization problem where the objective is
to select items to maximize total value without exceeding a given weight
capacity. Given a set of n items, each with a weight wᵢ and a value vᵢ, and a
knapsack with a maximum weight W, the goal is to maximize the sum of values
of selected items while ensuring their total weight does not exceed W.
ADSA 9