Algorithm Analysis Important Topics
Algorithm Analysis Important Topics
Topics
For more notes visit
https://rtpnotes.vercel.app
Algorithm-Analysis-Module-5-Important-Topics
1. Randomized quick sort
What is a randomized Algorithm?
Advantages of randomized algorithms?
Types of Randomized algorithm
Deterministic Quicksort Algorithm
Algorithm QuickSort(A[],low,high)
Worst case
Randomized Quicksort Algorithm
Algorithm randQuickSort(A[],low,high)
Time complexity
Graph Coloring
Vertex Coloring
Four Color theorem
Edge Coloring
Face Coloring
Graph coloring Approximation Algorithm
Algorithm
Time complexity
Applications of graph coloring
Algorithm QuickSort(A[],low,high)
Worst case
Algorithm randQuickSort(A[],low,high)
3. Partition A[low..high] into two subarrays. The subarray has all the elements of A that are
less than x and the second subarray has all those that are greater than x. Now the index of
x be pos
4. randQuickSort(A,low,pos-1)
5. randQuickSort(A,pos+1,high)
Time complexity
Tractable Problems
Intractable Problems
Complexity classes
Class P
What is Class P?
n = size of input
k = constant
Example
Path Problem: Given directed graph G, determine where a directed path exists from s
to t
Class NP
A Hamiltonian path in a directed graph G is a directed path that goes through each
node exactly once
The HAMPATH problem is to test whether a graph a graph contains a hamiltonian path
connecting 2 specified nodes
There is no polynomial solution
HAMPATH Problem have a feature called polynomial verifiability
Algorithm
Inputs G,s,t,P
P: Is the path P1,P2,P3 .... Pm
m number of nodes in G
s and t are the two vertices
This is hamiltonian path verifier algorithm, this algorithm complexity is in polynomial time
For checking if Hamiltonian path is existing, a polynomial time algorithm doesnt exist
But, for verification, polynomial time algorithm exists
A clique in an undirected graph is a subgraph where every two nodes are connected by an
edge
Inside the circle, you can see, every two nodes are connected, which forms a Clique
Input <G,k,V>
More Examples
Circuit-SAT problem
3CNF-SAT Problem
Vertex Cover Problem
Independence set problem
Travelling salesman problem
3-coloring problem
Class NP Hard
Class NP Complete
Given a Boolean circuit C, is there an assignment to the variables that causes the citcuits
to output 1?
Consider the below
Other examples
Clique Problem
Problem is to determine whether the graph contains clique of size k (Yes/No)
Vertex Cover problem
GIven a graph G(V,E)
Vertices V
Subset of V is V'
assume V' size = k
Here the question is whether we can use k nodes to cover all edges in a graph or
not? (Yes/No)
3-CNF SAT
3. Approximation Algorithm
Approximate solution
A feasible solution with value close to the value of optimal solution is called an
approximate solution.
Approximation Algorithms:
An algorithm that returns near optimal solution is called Approximation Algorithm
Approximation algorithms have two main properties
They run in polynomial time
They produce solutions close to optimal solutions
Approximation algorithms are useful to give approximate solutions to NP complete
optimization problems
Its also useful to give fast approximations to problems that run in polynomial time
Approximation Ratio/Approximation Factor
For a given problem assume C is the result obtained by the algorithm and C* is the
optimal result
The approximation ratio of an algorithm is the ratio between the result obtained by the
algorithm and the optimial result
For maximization problem 0 < C <= C *
Approximation Ratio = C*/ C
For minimization problem 0 <= C * <= C
Approximation Ratio = C / C *
Approximation ratio of an approximation algorithm is never less than 1
Given n items of different weights and bins each of capcity c, assign each item to a bin
such that the number of total used bins is minimized. It may be assumed that all items
have weights smaller than bin capacity
The lower bound on minimum number of bins required can be given as
Min no of bins >= Ceil( (Total Weight) / (Bin Capacity) )
Applications
Online Algorithm
n items and corresponding weights are not initially available, will be available in
realtime
These are divided into
Next Fit Algorithm
First Fit Algorithm
Best Fit Algorithm
Worst Fit Algorithm
Offline Algorithm
n items and corresponding weights will be initially available
First Fit Decreasing Algorithm
Best Fit Decreasing Algorithm
Example
Apply Next Fit Bin packing approximation algorithms on the following items with bin
capacity = 10. Assuming the sizes of items be {5,7,5,2,4,2,5,1,6}
Minimum number of bins >= Ceil(Total Weight / Bin Capacity)
= Ceil(37/10) = 4
We got the minimum number of bits, now lets start
We have the following array
{5,7,5,2,4,2,5,1,6}
Lets take each one by one
First we have 5
Total capacity of the bin = 10, So it can be fitted in a bin
Next value is 7
We cant place in first bin because 5 + 7 = 12 which is bigger than max capacity
10
Next value is 5,
Our current Bin is 7
7 + 5 = 12, not possible, since the max capacity is 10
Next value is 2
Current bin in 5
5 + 2 = 7, less than 10, so possible
Next value is 4
current bin is 5+2
5+2+4 = 11 not possible
Next value 2
Current bin is 4
4+2 = 6, possible
Next value 5
New bin
Next value 1
Next value 6
Definition
If the current item is fit in the same bin as the last item. then insert it in the same bin
Otherwise use the new bin
Time complexity
Best case time complexity = θ(n)
Average case = θ(n)
Worst case = θ(n)
Example
Apply First Fit Bin packing approximation algorithms on the following items with bin
capacity = 10. Assuming the sizes of items be {5,7,5,2,4,2,5,1,6}
Considering the array {5,7,5,2,4,2,5,1,6}, taking each element one by one
Taking 5
Putting in first bin
Next, 7
Check 5, 5+7 = 12, not possible
new bin
Next 5
We check the first bin for space, instead of the current bin
5 + 5 = 10, possible
Next 2
First bin space?
5 +5 + 2 = 12, no
2nd bin? = 7 + 2 = 9, yes
No of bins required = 5
Definition
Scan the previous bins in order and find the first bin that it fits
It such bin exits, place the item in that bin, otherwise use a new bin
Time complexity
Best case time complexity = θ(nlogn)
Average case time complexity = θ(n 2
)
Algorithm
Apply Best Fit Bin packing approximation algorithms on the following items with bin
capacity = 10. Assuming the sizes of items be {5,7,5,2,4,2,5,1,6}
Starting with 5
Next 7
Next 5
Check both bins, and checks which is the best fitting of both
Repeating steps
Definition
Scan the previous bins and find a bin that is having the minimum remaining capacity that
can accommodate this item
If such bin exists, place the item in that bin
Otherwise use a new bin
Time complexity
Best case time complexity = θ(nlogn)
Average case = θ(n 2
)
Algorithm
Apply Worst Fit Bin packing approximation algorithms on the following items with bin
capacity = 10. Assuming the sizes of items be {5,7,5,2,4,2,5,1,6}
Starting with 5
Next 7
Next 5
Take both bins,choose one with the larger balance space
Repeat the steps
For 1
Definition
Scan the previous bins and find a bin that has the maximum remaining capacity that can
accomodate this item
If such bin exists, place the item in that bin
Otherwise use a new bin
Time complexity
Best case time complexity = θ(nlogn)
Average case = θ(n 2
)
Definition
Example
Apply First Fit Bin packing approximation algorithms on the following items with bin
capacity = 10. Assuming the sizes of items be {5,7,5,2,4,2,5,1,6}
Arrange the items in the decreasing order of their size
{7,6,5,5,5,4,2,2,1}
Do the First fit as usual
Definition
Sort the items in the descending order of their size
Apply Best fit algorithm
Time complexity
Best case time complexity = θ(nlogn)
Average case = θ(n 2
)
Example
Apply Best Fit Bin packing approximation algorithms on the following items with bin
capacity = 10. Assuming the sizes of items be {5,7,5,2,4,2,5,1,6}
Arrange the items in the decreasing order of their size
{7,6,5,5,5,4,2,2,1}
Graph Coloring
Vertex Coloring
Assignment of colors to vertices in a graph such that no two adjacent vertices share the
same color
A Graph is 0-colorable if no of verticies is empty (V = Φ)
A Graph is 1-colorable if there are no edges( E = Φ )
2 colorable graph
3 Colorable Graph
For every Planar graph, the chromatic number is less than or equal to 4
A Graph is k colorable if it has k colors
Chromatic number: It is the minimum number of colors with which a graph can be
colored.
Edge Coloring
Given a graph G=(V,E), assign a color to each edges so that no two adjacent edges share
the same color
Face Coloring
For a planar graph, assign a color to each face/region so that no two faces that shares
boundary have the same color
Graph coloring Approximation Algorithm
We have 4 colors
Vertex 1
Here none of the vertices are colored, so we can place our first color
Vertex 2
First color Red
Its not possible because there is an adjacent red in 1
Second color Green
No issues, we can place
Vertex 3
FIrst color red
We can place Red in 3, because neighbours are green
Vertex 4
First color red
Not possible, 1, and 3 has red which are adjacent
2nd color green
Possible
Algorithm
Algorithm Approximate_Graph_Coloring(G,n)
Time complexity
3
O(n )