0% found this document useful (0 votes)
48 views

UNIT 5 - CS3401-Algorithms

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

UNIT 5 - CS3401-Algorithms

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Randomized Algorithms

1) Randomized Quick Sort


Quick Sort is a sorting algorithm based on the Divide and Conquer
algorithm. Quicksort chooses a pivot element and sorts the input
list around that pivot element.

It is to be noted that the worst case time complexity of the quick


sort will always remain O(n2) but randomizations decrease the
occurrences of that worst case.

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. 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. Even if the input is sorted, the pivot is chosen
randomly so the worst case time complexity is avoided.

The time complexity of Randomized Quick Sort is O(nlogn)

CS3401-ALGORITHMS (Sem-IV-R2021) - UNIT-V: 1


2) Finding Kth Smallest Number 3) Primality Testing

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

Input: arr[] = {7, 10, 4, 3, 20, 15}, K = 3

Sorted Array arr[]={3,4,7,10,15,20}


If K=3 that return the element at the (K-1)th Index

Output: 7

CS3401-ALGORITHMS (Sem-IV-R2021) - UNIT-V: 2


Approximation Algorithms: Step 1 − Choose vertex 1 of the given graph randomly as the
TSP – Travelling Salesman Problem starting and ending point.

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.

Approximation algorithms are algorithms used to find approximate


solutions to optimization problems.

Step 3 − Once the spanning tree is constructed, pre-order


traversal is performed on the minimum spanning tree

Analysis
The approximation algorithm of the travelling salesperson
problem is a 2-approximation algorithm if the triangle inequality
is satisfied.

To prove this, we need to show that the approximate cost of the


problem is double the optimal cost.
The preorder traversal of the tree is found to be:
1→2→5→6→3→4
Adding the root node at the end of the traced path
1→2→5→6→3→4→1

Step 4 − The pre-order solution obtained is the Hamiltonian path


of the travelling salesman.

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

Example: Let L=10, n=6 and (l1,l2,l3,l4,l5,l6)=(5,6,3,7,5,4).


First bin contains {5,5}, Second bin {6,4} and third bin {3,7}
Numbers in bins are object indices.
Optimal Packing First Fit Decreasing (FFD)
First order the items by size, from largest to smallest {7,6,5,5,4,3},
then run the First Fit Algorithm. (Six objects are considered in the
order 4,2,1,5,6,3)

We need minimum 3 bins. Best Fit Decreasing (BFD)


First bin contains {5,5}, Second bin {6,4} and third bin {3,7} First order the items by size, from largest to smallest {7,6,5,5,4,3},
then run the Best Fit Algorithm. (Six objects are considered in the
First Fit (FF) order 4,2,1,5,6,3)
Scan the bins in order and place the new item in the first bin that Numbers in bins are object indices.
is large enough to hold it. A new bin is created only when an item
does not fit in the previous bins.

Numbers in bins are object indices.

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

4) Write short notes on NP algorithms.

A problem is in NP if its solution can be verified in poly-time.

NP: Non-deterministic Polynomial


The set of programs that can be run in Polynomial time on a
Nondeterministic machine is called the NP algorithms.

Input: arr[] = {7, 10, 4, 3, 20, 15}, K = 3


NP: the class of decision problem which can be solved by a non-
deterministic polynomial algorithm.
Sorted Array arr[]={3,4,7,10,15,20} NP Algorithm Types: NP Hardness and NP Completeness
If K=3 that return the element at the (K-1)th Index

Output: 7

CS3401-ALGORITHMS (Sem-IV-R2021) - UNIT-V: 5


5) Write short notes on NP Hardness. 6) Write short notes on NP Completeness.

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

CS3401-ALGORITHMS (Sem-IV-R2021) - UNIT-V: 6


7) Write short notes on Problem Reduction. 9) Write short notes on Primality Testing.

To solve an instance of problem A: Given an integer n, the problem of deciding whether n is a


 Transform the instance of problem A into an instance of prime is known as primality testing. Any number greater than
problem B one is said to be prime if it is divisible by 1 or by itself.
 Solve the instance of problem B
To iterate through all numbers from 2 to sqrt(n) and for every
 Transform the solution of problem B into the solution of number check if it divides n. If we find any number that
problem A divides, we return false.
We say that problem A reduces to problem B
If n is prime, then it returns true otherwise false.
Reduction is a way of transforming one problem into another
problem in such a way that the solution of the second problem
can be used to solve the first problem.

8) State the 3-CNF Problem.


10) Write short notes on Randomized Algorithm.
3-CNF Satisfiability (3CNF SAT)
3-CNF (Conjunctive Normal Form) is a Boolean formula that is An algorithm that uses random numbers to decide what to do
an AND of clauses, each of which is an OR of exactly 3 distinct next anywhere in its logic is called a Randomized Algorithm.
literals.
For example, in Randomized Quick Sort, we use a random
Such as (X+Y+Z) (X+Y+Z) (X+Y+Z) number to pick the next pivot (or we randomly shuffle the
You can define as (XvYvZ) ᶺ (XvYvZ) ᶺ (XvYvZ) array).
v =OR operator
^ =AND operator There are two ways to randomize:
 Randomly shuffling the inputs
3-CNF is said to be satisfiable if it has satisfying assignment.
 Randomly choosing the pivot element

CS3401-ALGORITHMS (Sem-IV-R2021) - UNIT-V: 7


11) Write short notes on Randomized Sorting. 13) Write short notes on Approximation Algorithm.

There are two ways to randomize the quicksort: Approximation Algorithms:


TSP – Travelling Salesman Problem
Randomly shuffling the inputs: Randomization is done on
the input list so that the sorted input is jumbled again Approximation algorithms are algorithms designed to solve
which reduces the time complexity. This is not usually problems that are not solvable in polynomial time for
approximate solutions.
performed in the randomized quick sort.
Approximation algorithms are algorithms used to find
Randomly choosing the pivot element: Making the pivot approximate solutions to optimization problems.
element a random variable is commonly used method in
the randomized quick sort. Even if the input is sorted, the
pivot is chosen randomly so the worst case time complexity
is avoided.

14) What is polynomial time algorithms?

A polynomial-time algorithm is one whose running time grows


as a polynomial function of the size of its input.

A polynomial-time algorithm is an algorithm whose execution


time is either given by a polynomial on the size of the input
12) What are the applications of Randomized Algorithms?
or can be bounded by such a polynomial.
Applications of Randomized Algorithms
Problem that is solvable by a polynomial-time algorithm is
 Primality Testing
called "Tractable Problems".
 Randomized Quick Sort
 Finding Kth smallest element

CS3401-ALGORITHMS (Sem-IV-R2021) - UNIT-V: 8


15) Represent polynomial time algorithm using Venn
Diagram.

Venn Diagram Representation

A problem is in NP (Non-deterministic Polynomial) if its


solution can be verified in poly-time.

A problem is NP-hard if all problems in NP can be reduced to


it in poly-time.

A problem is NP-complete if it is in NP and is NP-hard.

16) The bin packing is NP-hard. Prove it.

To see this, consider the partition problem.


Let {a1,a2,.....an} be an instance of the partition problem.

Define an instance of the bin packing problem as li=ai and


L=∑ai/2.

Clearly, the minimum number of bins needed is two if and


only if there is a partition for {a1,a2,.....an}

CS3401-ALGORITHMS (Sem-IV-R2021) - UNIT-V: 9

You might also like