Cs3401 Algorithm Unit5

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

SRRCET/CSE/IV/CS3401-ALGORITHM

UNIT V

NP-COMPLETE AND APPROXIMATION ALGORITHM


Tractable and intractable problems: Polynomial time algorithms – Venn diagram
representation - NP- algorithms - NP-hardness and NP-completeness – Bin Packing problem -
Problem reduction: TSP – 3-CNF problem. Approximation Algorithms: TSP - Randomized
Algorithms: concept and application - primality testing - randomized quick sort - Finding k th
smallest number

1. Define tractable and intractable problem.


Tractable problems are the class of problems that can be solved within reasonable time and
space. For example- sorting a list, multiplication of integers.
Intractable problems are the class of problems that can be solved within polynomial time. For
example-list of all permutations of ‘n ‘ numbers, tower of Hanoi. This has lead to two classes
of solving problems-P and NP class problem.
2. Define NP hard and NP completeness.
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.
NP Completeness: A Paradigm Dis called NP –Complete if
i) It belongs to class NP
ii) Every problem in NP can also be solved in polynomial time
For Example: Finding hamitonian path is NP Complete.
3. What are NP- hard and NP-complete problems? (R)
The problems whose solutions have computing times are bounded by polynomials of
small degree.
4. List example of NP hard problem. (R)
NP hard graph problem
Example: sum of subset problem, travelling salesman problem are NP hard.
5. An NP-hard problem can be solved in deterministic polynomial
time. How?(NOV/DEC 2012)
PREPARED BY D.SUJATHA AP/CSE pg. 1 VERIFIED BY HOD APPROVED BY
DEAN
SRRCET/CSE/IV/CS3401-ALGORITHM

If the class of NP complete problem under the NP hard problems is solved in


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.

7. Define P and NP problems. (APR/MAY 2017) (R)


P- Polynomial time solving . Problems which can be solved in polynomial time,
which take time like O(n), O(n2), O(n3). Eg: finding maximum element in an array or to
check whether a string is palindrome or not. There are many problems which can be
solved in polynomial time.
NP- Non deterministic Polynomial time solving. Problem which can't be solved in
polynomial time like TSP( travelling salesman problem)
8. What do you meant by primality testing?
A primality test is an algorithm for determining whether an input number is prime or not.
The application of primality testing is cryptography
9. What is Kth smallest number?
An array of elements and a value of k is given where k is smaller than the size of the
array, we need to find the k’th smallest element in the given array. It is given that all array
elements are distinct.
Step:1 sort the input array in ascending order.
Step:2 Return the (k-1) index in the sorted array.
10. Define randomized quick sort algorithm.

PREPARED BY D.SUJATHA AP/CSE pg. 2 VERIFIED BY HOD APPROVED BY


DEAN
SRRCET/CSE/IV/CS3401-ALGORITHM

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.

11. Define Bin packing Problem.


Bin packing is a technique of assigning n items of different weights in to the bins of capacity c
such that the number of bins is minimized. It may be assumed that all items have weights
smaller than bin capacity.

12. Define Travelling salesman problem


The Travelling Salesman Problem (also known as the Travelling Salesperson Problem or TSP)
is an NP-hard graph computational problem where the salesman must visit all cities (denoted
using vertices in a graph) given in a set just once. The distances (denoted using edges in the
graph) between all these cities are known. We are requested to find the shortest possible route
in which the salesman visits all the cities and returns to the origin city.

13. What is Approximation algorithm?


Approximation algorithm are algorithms used to find approximate solutions to optimization
problem. Approximation algorithms are often associated with NP hard problems because it is
very difficult to get an efficient polynomial time exact algorithm for solving NP-hard problem.

PART –B

1. Elaborate NP –complete problem and NP hard problem with an example


The NP hard is a class of problems in computational complexity that is as hard as the hardest

PREPARED BY D.SUJATHA AP/CSE pg. 3 VERIFIED BY HOD APPROVED BY


DEAN
SRRCET/CSE/IV/CS3401-ALGORITHM

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.
NP Completeness: A Paradigm Dis called NP –Complete if
iii) It belongs to class NP
iv) Every problem in NP can also be solved in polynomial time
For Example: Finding hamitonian path is NP Complete.
(Explain sum of subset problem, TSP problem)

2. Outline the Randomized quick sort algorithm with suitable example.


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.

3. Write short notes on the following

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.

ii) NP- Completeness


NP Completeness: A Paradigm Dis called NP –Complete if
 It belongs to class NP
 Every problem in NP can also be solved in polynomial time

PREPARED BY D.SUJATHA AP/CSE pg. 4 VERIFIED BY HOD APPROVED BY


DEAN
SRRCET/CSE/IV/CS3401-ALGORITHM

For Example: Finding hamitonian path is NP Complete

4, write short notes on the following:

i) Problem reduction

ii) Primality testing.

iii) Randomized Sorting.

i) Problem Reduction:

 Problem reduction is a fundamental concept in computer science and computational complexity


theory. It involves transforming one problem into another problem in such a way that if we can
solve the second problem efficiently, we can also solve the first problem efficiently.
 The most common type of problem reduction is polynomial-time reduction (often denoted as
“reduces in polynomial time” or “≤p”). If problem A reduces to problem B in polynomial time, we
write it as (A \leq_p B).
 For example, if we can transform an instance of the Traveling Salesman Problem (TSP) into an
instance of the Hamiltonian Cycle Problem, and vice versa, using a polynomial-time algorithm,
then we say that TSP reduces to Hamiltonian Cycle Problem.

ii) Primality Testing:

 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.

PREPARED BY D.SUJATHA AP/CSE pg. 5 VERIFIED BY HOD APPROVED BY


DEAN
SRRCET/CSE/IV/CS3401-ALGORITHM

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.

iii) Randomized Sorting:

 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.

5. Explain briefly bin packing problem .


Given n items of different weights and bins each of capacity c, assign each item to a bin such that number
of total used bins is minimized. It may be assumed that all items have weights smaller than bin capacity.
Example:
Input: weight[] = {4, 8, 1, 4, 2, 1}
Bin Capacity c = 10
Output: 2
We need minimum 2 bins to accommodate all items
First bin contains {4, 4, 2} and second bin {8, 1, 1}

Input: weight[] = {9, 8, 2, 2, 5, 4}


Bin Capacity c = 10
Output: 4
We need minimum 4 bins to accommodate all items.

Input: weight[] = {2, 5, 4, 7, 1, 3, 8};


Bin Capacity c = 10
Output: 3

PREPARED BY D.SUJATHA AP/CSE pg. 6 VERIFIED BY HOD APPROVED BY


DEAN
SRRCET/CSE/IV/CS3401-ALGORITHM

Lower Bound
We can always find a lower bound on minimum number of bins required. The lower bound can be given
as :

Min no. of bins >= Ceil ((Total Weight) / (Bin Capacity))

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.
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)

PREPARED BY D.SUJATHA AP/CSE pg. 7 VERIFIED BY HOD APPROVED BY


DEAN
SRRCET/CSE/IV/CS3401-ALGORITHM

PREPARED BY D.SUJATHA AP/CSE pg. 8 VERIFIED BY HOD APPROVED BY


DEAN

You might also like