UNIT 5 - CS3401-Algorithms
UNIT 5 - CS3401-Algorithms
Given an array arr[] of size N and a number K, where K is smaller Given an integer n, the problem of deciding whether n is a prime
than the size of the array. is known as primality testing. Any number greater than one is
said to be prime if it is divisible by 1 or by itself.
Find the Kth smallest element in the given array. Given that all To iterate through all numbers from 2 to sqrt(n) and for every
array elements are distinct. number check if it divides n. If we find any number that divides,
we return false.
Steps to Solve:
Sort the input array in the increasing order If n is prime, then it returns true otherwise false.
(Randomized Quick Sort: The idea is to randomly pick a pivot
element.)
Return the element at the (K-1)th Index (0 – Based Indexing)
in the sorted array
Output: 7
Approximation algorithms are algorithms designed to solve problems Step 2 − Construct a minimum spanning tree of the graph with
that are not solvable in polynomial time for approximate solutions. the vertex chosen as the root using prim’s algorithm.
Analysis
The approximation algorithm of the travelling salesperson
problem is a 2-approximation algorithm if the triangle inequality
is satisfied.
The cost of the path would be the sum of all the costs in the
minimum spanning tree. Sum of all the costs in the minimum
spanning tree is 55.
CS3401-ALGORITHMS (Sem-IV-R2021) - UNIT-V: 3
Bin Packing Problem Best Fit (BF)
New item is placed in a bin where it fits the tightest. If it does not
Given n items of different weights and bins each of capacity c, the fit in any bin, then start a new bin.
objective is to determine the minimum number of bins needed to Numbers in bins are object indices.
accommodate all n objects. Best Fit
Min no. of bins >= Ceil ((Total Weight) / (Bin Capacity))
First bin contains {7,3}, Second bin {6,4} and third bin {5,5}
Applications
Loading of containers like trucks
Job scheduling
First bin contains {5,3}, Second bin {6,4}, third bin {7} and fourth Placing data on multiple disks
bin {5}
CS3401-ALGORITHMS (Sem-IV-R2021) - UNIT-V: 4
1) Differentiate Tractable and Intractable problems. 3) State the bin packing problem.
Tractable Problems Intractable Problems Bin Packing Problem (Minimize number of used Bins)
Problem that is solvable by a Problem that cannot be Given n items of different weights and bins each of capacity c,
polynomial-time algorithm solved by a polynomial-time the objective is to determine the minimum number of bins
algorithm needed to accommodate all n objects.
The upper bound is The lower bound is Min no. of bins >= Ceil ((Total Weight) / (Bin Capacity))
polynomial exponential Input: weight[] = {4, 8, 1, 4, 2, 1}
Example: Sorting a list, Example: Bin Capacity c = 10
Searching an ordered list, Travelling Salesman Problem, Output: 2
Searching an unordered list Knapsack Problem We need minimum 2 bins.
First bin contains {4, 4, 2} and second bin {8, 1, 1}
2) Write an algorithm to find the Kth smallest number. Applications: Loading of containers like trucks, Job scheduling,
Placing data on multiple disks
Output: 7
A problem is NP-hard if all problems in NP can be reduced to A problem is NP-complete if it is in NP and is NP-hard.
it in poly-time.
A problem X is NP-Complete (NPC) if X∈NP and every NP
A problem X is NP-Hard (NPH) if every NP problem reduces to problem reduces to X in polynomial time.
X in polynomial time.
NP-complete problems are the hard problems in NP.
An NP-hard problem is at least as hard as the hardest problem
in NP and it is a class of problems such that every problem in Any solution to NP-complete problems can be checked quickly.
NP reduces to NP-hard.
All the NP complete problems are NP hard but some NP hard
If a solution for an NP-hard problem is given, then it takes a problems are not known to be NP complete.
long time to check whether it is right or not.
Examples
If an NP hard problem can be solved in polynomial time, then Knapsack Problem
all the NP complete problems can also be solved in polynomial Hamiltonian Cycle Problem
time. Vertex Cover Problem
Example
Travelling Salesman Problem
Subset Sum Problem
Turing Halting problem