Computer Science Algorithm
Computer Science Algorithm
Items can only be uniquely sorted if the sorting criterion chosen always defines a total order; e.g.
student numbers are expected to be unique, but if we sort exams by name and two students
happen to have the same name, the order in which their exams get sorted is left undefined. In
such cases, merge sort will always arrive at one of the possible valid orderings, but which one is
left unspecifiedhence it is nondeterministic.
Decision problem: A question with a yes or no answer.
Polynomial Time Algorithm. (P class)
An algorithm is said to be solvable in polynomial time if the number of steps required to
complete the algorithm for a given input is
for some nonnegative integer , where is the
complexity of the input. Polynomial-time algorithms are said to be "fast." Most familiar
mathematical operations such as addition, subtraction, multiplication, and division, as well as
computing square roots, powers, and logarithms, can be performed in polynomial time.
A decision problem that can be solved in polynomial time (i.e O (n k) ). That is, given an instance
of the problem, the answer yes or no can be decided in polynomial time.
Example: Given a graph connected G, can its vertices be colored using two colors so that no
edge is monochromatic. Algorithm: start with an arbitrary vertex, color it red and all of its
neighbors blue and continue. Stop when you run our of vertices or you are forced to make an
edge have both of its endpoints be the same color.
Nondeterministic Polynomial Time Algorithm (NP) : A decision problem where instances of
the problem for which the answer is yes have proofs that can be verified in polynomial time. This
means that if someone gives us an instance of the problem and a certificate (sometimes called a
witness) to the answer being yes, we can check that it is correct in polynomial time.
Example: Integer factorization is NP. This is the problem that given integers n and m, is there an
integer f with 1 < f < m such that f divides n (f is a small factor of n)? This is a decision problem
because the answers are yes or no. If someone hands us an instance of the problem (so they hand
us integers n and m) and an integer f with 1 < f < m and claim that f is a factor of n (the
certificate) we can check the answer in polynomial time by performing the division n / f.
NP complete
Generally this is a class of problems that are so difficult that even the best solutions cannot
consistently determine their solutions in an efficient way. Specifically, NP Complete problems
can only possibly be solved in polynomial time using a nondeterministic Turing machine (Turing
machine is a computer, capable of making guesses and checking them in polynomial time).
Problems such as weather prediction, how to fill a box with odd-sized objects, and the Traveling
Salesman problem fall into the realm of NP Complete problems. Because of the problem with
finding the best solution, programs are often developed to find a usually reasonable solution.
The NP-Complete problems are an interesting class of problems whose status is unknown. No
polynomial-time algorithm has been discovered for an NP-Complete problem
Reduction or transformation
A problem P can be reduced to another problem Q if any instance of P can be rephrased to an
instance of Q, the solution to which provides a solution to the instance of P
This rephrasing is called a transformation.
If P reduces in polynomial time to Q, P is no harder to solve than Q.
NP Hard