Unit V
Unit V
In computer science, problems are divided into classes known as Complexity Classes. In
complexity theory, a Complexity Class is a set of issues with related complexity.
The common resources required by a solution are time and space, which refer to the amount
of time the algorithm takes to solve a problem and the corresponding memory usage.
• The time complexity of an algorithm is used to describe the number of steps required
to solve a problem, but it can also be used to describe how long it takes to verify the
answer.
• The space complexity of an algorithm describes how much memory is required for the
algorithm to operate.
• An algorithm having time complexity of the form O(nk) for input n and constant k is
called a polynomial time solution. These solutions scale well. On the other hand, time
complexity of the form O(nk) is exponential time.
P Class
The P in the P class stands for Polynomial Time. It is the collection of decision
problems(problems with a "yes" or "no" answer) that can be solved by a deterministic
machine (our computers) in polynomial time.
Features:
• P is often a class of computational problems that are solvable and tractable. Tractable
means that the problems can be solved in theory as well as in practice. But the
problems that can be solved in theory but not in practice are known as intractable.
Most of the coding problems that we solve fall in this category, like the below.
3. Merge Sort
NP Class
• The solutions of the NP class might be hard to find since they are being solved by a
non-deterministic machine, but the solutions are easy to verify.
NP-hard class
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 NP reduces to an NP-hard.
Features:
• It takes a long time to check them. This means if a solution for an NP-hard problem is
given then it takes a long time to check whether it is right or not.
• A problem A is in NP-hard if, for every problem L in NP, there exists a polynomial-time
reduction from L to A.
1. Halting problem.
3. No Hamiltonian cycle.
NP-complete class
A problem is NP-complete if it is both NP and NP-hard. NP-complete problems are the hard
problems in NP.
Features:
• If one could solve an NP-complete problem in polynomial time, then one could also
solve any NP problem in polynomial time.
1. Hamiltonian Cycle.
2. Satisfiability.
3. Vertex cover.
Asymptotic Notation:
• These notations provide a concise way to express the behavior of an algorithm's time
or space complexity as the input size approaches infinity.
• Asymptotic analysis allows for the comparison of algorithms' space and time
complexities by examining their performance characteristics as the input size varies.
Θ (g(n)) = {f(n): there exist positive constants c1, c2 and n0 such that 0 ≤ c1 * g(n) ≤
f(n) ≤ c2 * g(n) for all n ≥ n0}
The above expression can be described as if f(n) is theta of g(n), then the value f(n) is
always between c1 * g(n) and c2 * g(n) for large values of n (n ≥ n0). The definition of
theta also requires that f(n) must be non-negative for values of n greater than n0.
Big-O notation represents the upper bound of the running time of an algorithm. Therefore, it
gives the worst-case complexity of an algorithm. It is the most widely used notation for
Asymptotic analysis. It specifies the upper bound of a function. The maximum time required
by an algorithm or the worst-case time complexity. It returns the highest possible output
value(big-O) for a given input.
If f(n) describes the running time of an algorithm, f(n) is O(g(n)) if there exist a positive
constant C and n0 such that, 0 ≤ f(n) ≤ cg(n) for all n ≥ n0.
The execution time serves as an upper bound on the algorithm's time complexity.
O(g(n)) = { f(n): there exist positive constants c and n0 such that 0 ≤ f(n) ≤ cg(n) for all n ≥ n0 }
The Big-O notation is useful when we only have an upper bound on the time complexity of an
algorithm.
3. Omega Notation (Ω-Notation):
Omega notation represents the lower bound of the running time of an algorithm. Thus, it
provides the best case complexity of an algorithm.
The execution time serves as a lower bound on the algorithm's time complexity.
It is defined as the condition that allows an algorithm to complete statement execution in the
shortest amount of time.
Let g and f be the functions from the set of natural numbers to itself. The function f is said to
be Ω(g), if there is a constant c > 0 and a natural number n0 such that c*g(n) ≤ f(n) for all n ≥
n0
Ω(g(n)) = { f(n): there exist positive constants c and n0 such that 0 ≤ cg(n) ≤ f(n) for all n ≥ n0 }
Bin Packing Problem
Given n items of different weights and bins, each of capacity c, assign each item to a bin to
minimize the total number of bins used. It may be assumed that all items have weights smaller
than the bin capacity.
Example:
This problem is an NP-hard problem, and finding an exact minimum number of bins takes
exponential time. The following are approximate algorithms for this problem.
Online Algorithms
These algorithms are for Bin Packing problems where items arrive one at a time (in unknown
order), each must be put in a bin, before considering the next item.
1. Next Fit: When processing next item, check if it fits in the same bin as the last item.
Use a new bin only if it does not. Next Fit is a simple algorithm. It requires only O(n)
time and O(1) extra space to process n items.
2. First Fit: When processing the next item, scan the previous bins in order and place
the item in the first bin that fits. Start a new bin only if it does not fit in any of the
existing bins. First Fit can be implemented in O(n Log n) time using Self-Balancing
Binary Search Trees.
3. Best Fit: The idea is to places the next item in the tightest spot. That is, put it in the
bin so that the smallest empty space is left. Best Fit can also be implemented in O(n
Log n) time using Self-Balancing Binary Search Trees.
4. Worst Fit: The idea is to places the next item in the least tight spot to even out the
bins. That is, put it in the bin so that most empty space is left. Worst Fit can also be
implemented in O(n Log n) time using Self-Balancing Binary Search Trees.
Offline Algorithms
In the offline version, we have all items upfront. Unfortunately, the offline version is
also NP Complete, but we have a better approximate algorithm for it. First Fit
Decreasing uses at most (4M + 1)/3 bins if the optimal is M.
1. First Fit Decreasing: A trouble with online algorithms is that packing large
items is difficult, especially if they occur late in the sequence. We can circumvent
this by *sorting* the input sequence and placing the large items first. With
sorting, we get First Fit Decreasing and Best Fit Decreasing, as offline analogues
of online First Fit and Best Fit. First Fit Decreasing can also be implemented using
Self-Balancing Binary Search Trees in O(n Log n) time.