cs3401-algorithm-unit5
cs3401-algorithm-unit5
UNIT V
deterministic polynomial time then NP hard problem can be solved in polynomial time.
To solve the class of NP complete problems in deterministic polynomial time the
reduction technique is used,
6. How NP-Hard problems are different from NP-Complete? (R)
These are the problems that are even harder than the NP-complete problems. Note
that NP-hard problems do not have to be in NP, and they do not have to be decision
problems.
The precise definition here is that a problem X is NP-hard, if there is an NP- complete
problem Y, such that Y is reducible to X in polynomial time.
But since any NP-complete problem can be reduced to any other NP-complete
problem in polynomial time, all NP-complete problems can be reduced to any NP-hard
problem in polynomial time. Then, if there is a solution to one NP-hard problem in
polynomial time, there is a solution to all NP problems in polynomial time.
Randomized quick sort is designed to decrease the chances of the algorithm being executed
in the worst case time complexity of O(n2). The worst case time complexity of quick sort arises
PREPARED BY D.SUJATHA AP/CSE pg. 2 VERIFIED BY HOD APPROVED BY DEAN
PREPARED BY D.SUJATHA, AP/CSE SRRCET/II/IV/CS3401-ALGORITHM
when the input given is an already sorted list, leading to n(n – 1) comparisons. There are two ways
to randomize the quicksort −
Randomly shuffling the inputs: Randomization is done on the input list so that the sorted input is
jumbled again which reduces the time complexity. However, this is not usually performed in the
randomized quick sort.
Randomly choosing the pivot element: Making the pivot element a random variable is commonly used
method in the randomized quick sort. Here, even if the input is sorted, the pivot is chosen randomly
so the worst case time complexity is avoided.
PART –B
Randomized quick sort is designed to decrease the chances of the algorithm being executed
in the worst case time complexity of O(n2). The worst case time complexity of quick sort arises
when the input given is an already sorted list, leading to n(n – 1) comparisons. There are two ways
to randomize the quicksort −
Randomly shuffling the inputs: Randomization is done on the input list so that the sorted input is
jumbled again which reduces the time complexity. However, this is not usually performed in the
randomized quick sort.
Randomly choosing the pivot element: Making the pivot element a random variable is commonly used
method in the randomized quick sort. Here, even if the input is sorted, the pivot is chosen randomly
so the worst case time complexity is avoided.
i) NP Hardness
The NP hard is a class of problems in computational complexity that is as hard as the hardest
problem in NP. If an NP hard problem can be solved in polynomial time then all the NP
complete problems can also be solved in polynomial time. For example the sum of subset
problem, travelling salesman problem are NP hard.
i) Problem reduction
i) Problem Reduction:
Primality testing is the process of determining whether a given positive integer is prime or
composite.
There are several algorithms for primality testing, each with different trade-offs in terms of time
complexity and accuracy.
Some well-known primality tests include:
o Trial Division: The simplest method, but inefficient for large numbers.
o Fermat’s Little Theorem: Based on modular arithmetic. If (a^{p-1} \equiv 1 \pmod{p}) for
a prime (p), then (p) is likely prime.
o Miller-Rabin Test: A probabilistic algorithm that provides a high probability of correctly
identifying prime numbers.
o AKS Primality Test: A deterministic algorithm with polynomial time complexity, but not
practical for very large numbers.
In practice, probabilistic tests like Miller-Rabin are often used due to their efficiency.
Randomized sorting algorithms use randomness to improve their performance or guarantee certain
properties.
One example is the Bogosort (also known as “stupid sort” or “random sort”), which randomly
shuffles the input until it happens to be sorted. It has terrible average-case time complexity.
Another example is Randomized QuickSort, where the pivot element is chosen randomly. This
improves the worst-case behavior of QuickSort.
Randomized algorithms are interesting because they demonstrate that randomness can sometimes be
harnessed to achieve better results than deterministic algorithms.
In the above examples, lower bound for first example is “ceil(4 + 8 + 1 + 4 + 2 + 1)/10” = 2 and lower
bound in second example is “ceil(9 + 8 + 2 + 2 + 5 + 4)/10” = 3.
PREPARED BY D.SUJATHA AP/CSE pg. 6 VERIFIED BY HOD APPROVED BY DEAN
PREPARED BY D.SUJATHA, AP/CSE SRRCET/II/IV/CS3401-ALGORITHM
This problem is a NP Hard problem and finding an exact minimum number of bins takes exponential
time. Following are approximate algorithms for this problem.
Applications
1. Loading of containers like trucks.
2. Placing data on multiple disks.
3. Job scheduling.
4. Packing advertisements in fixed length radio/TV station breaks.
5. Storing a large collection of music onto tapes/CD’s, etc.
Review Questions.
1. Suggest an approximation algorithm for travelling salesperson problem. Assume that the
cost function satisfies the triangle inequality. (MAY 2015) (R)
2. Implement an algorithm for Knapsack problem using NP-Hard approach. (MAY 2015) (R)
3. Discuss the approximation algorithm for NP hard problem? (NOV/DEC 2016) (R)
4. What is class NP? Discuss about any five problems for which no polynomial time for TSP
problem. (APR/MAY 2018)
5. Elaborate on the nearest-neighbor algorithm and multifragment- heuristic algorithm for TSP
problem. (APR/MAY 2018)
6. Discuss the approximation algorithm for NP-hard problem. (13)(NOV/DEC 2018)
7. Write an algorithm to solve the travelling salesman problem and prove that it is a 2 time
approximation algorithm.(13)(APR/MAY 2019)