Disjoint Set, String Matching, NP Problem
Disjoint Set, String Matching, NP Problem
Two sets are called disjoint sets if they don’t have any element in common, the
intersection of sets is a null set.
1. Find
2. Union
1. Find:
Can be implemented by recursively traversing the parent array until we hit a node
that is the parent of itself.
Time complexity: This approach is inefficient and can take O(n) time in worst
case.
2. Union:
It takes two elements as input and finds the representatives of their sets using
the Find operation, and finally puts either one of the trees (representing the set)
under the root node of the other tree.
Time complexity: This approach is inefficient and could lead to tree of length O(n)
in worst case.
Design and Analysis of Algorithm
Naive Algorithm: It slides the pattern over text one by one and check
for a match. If a match is found, then slides by 1 again to check for
subsequent matches.
Pros: • The algorithm ensures that the characters of the text are
never compared more than once, which makes it very efficient. • No
backtracking is needed, so it's more efficient than the naive
approach.
NP Complete Problem
NP Problem:
Design and Analysis of Algorithm
The NP problems set of problems whose solutions are hard to find but easy to
verify and are solved by Non-Deterministic Machine in polynomial time.
NP-Hard Problem:
A Problem X is NP-Hard if there is an NP-Complete problem Y, such that Y is
reducible to X in polynomial time. NP-Hard problems are as hard as NP-Complete
problems. NP-Hard Problem need not be in NP class.
A lot of times takes the particular problem solve and reducing different
problems.
example :
1. Hamiltonian cycle .
2. optimization problem .
3. Shortest path
NP-Complete Problem:
NP-hard NP-Complete
Not all NP-hard problems are NP- All NP-complete problems are NP-
complete. hard
Approximation Algorithm
Design and Analysis of Algorithm
Performance Ratios
The approximate ratio is represented using ρ(n) where n is the input size of the
algorithm, C is the near-optimal solution obtained by the algorithm, C* is the
optimal solution for the problem. The algorithm has an approximate ratio of ρ(n)
if and only if −
max{CC∗,C∗C}≤ρ(n)