Analysis of Algorithm: By: Waqas Haider Khan Bangyal
Analysis of Algorithm: By: Waqas Haider Khan Bangyal
Analysis of Algorithm: By: Waqas Haider Khan Bangyal
By:
Waqas Haider khan Bangyal
P, NP, NP-Complete and NP-Hard?
Polynomial time
Problems in class P can be solved with algorithms that
run in polynomial time.
Say you have an algorithm that finds the smallest
integer in an array. One way to do this is by iterating
over all the integers of the array and keeping track of
the smallest number you've seen up to that point.
Every time you look at an element, you compare it to
the current minimum, and if it's smaller, you update
the minimum.
Polynomial time
How long does this take? Let's say there are n
elements in the array. For every element the algorithm
has to perform a constant number of operations.
Therefore we can say that the algorithm runs in O(n)
time, or that the runtime is a linear function of how
many elements are in the array.* So this algorithm
runs in linear time.
You can also have algorithms that run in quadratic
time (O(n^2)), exponential time(O(2^n)), or
even logarithmic time (O(log n)).
Polynomial time
Binary search (on a balanced tree) runs in logarithmic
time because the height of the binary search tree is a
logarithmic function of the number of elements in the
tree.