Computational Complexity
Introduction
It is already discussed that decidable problems have algorithms. For solving a decidable problem, there may be more than one algorithm. The algorithms may differ in time taken and/or memory required to execute and produce the result, for the same input. This is known as computational complexity. It concerns with the question ‘which is the efficient algorithm for solving a decision problem.’ While discussing computational complexity, some underlying particulars such as hardware, software, and data structure are generally ignored. Computer science deals with the theoretical foundations of information, computation, and practical techniques for implementation and application. In this chapter, we shall discuss the different types of complexities of an algorithm, polynomial time algorithm, classes of P and NP, and different examples related with these.
12.1 Types of Computational Complexity
Mainly, two types of complexities are measured for an algorithm. These are
- Time complexity
- Space complexity
The measurement of complexity is done for the worst cases of an algorithm.
12.1.1 Time Complexity
The time complexity of a program for a given input is the number of elementary instructions (the number of basic steps required) that this program executes. This number is computed based on the length (n) of the input. If T(n) is the time complexity for an algorithm ‘A’ for a problem ‘P’, then the number of steps required to produce the output from an input of length n is less than or equal to n. (Here it is assumed that each elementary instruction takes unit time and one processor instruction.)
12.1.2 Space Complexity
The space complexity of a program for a given input is the number of elementary objects that this program needs to store (i.e., memory consumed) during its execution. This number is computed based on the length (n) of the input. If S(n) is the space complexity for an algorithm ‘A’ for a problem ‘P’, then the number of memory locations required to produce the output from an input of length n is less than or equal to n.
12.2 Different Notations for Time Complexity
Take four functions:
- f 1 (n) = 2n
- f 2 (n) = n 2
- f 3 (n) = 2n 2
- f 4 (n) = 2 n + 3
Now, in the following table, we are calculating the values of the four functions for different values of n.
Among these, consider f 2 (n) and f 3 (n). Both of them are directly proportional to one another. For n = 0, both of them give the same results but for n > 0, f 2 (n) < f 3 (n). If we consider f 1 (n) and f 2 (n), we shall notice that for n = 0 and n = 2 both the functions give the same results but for n > 2, f 1 (n) < f 2 (n).
It can be proved that the growth rate of any exponential function is greater than that of any polynomial.
From this discussion, we can draw different growth rate notations. There are mainly five well-known notations for growth rates of functions. These are
- Big oh notation, denoted as ‘O’
- Big omega notation, denoted as Ω
- Theta notation, denoted as Θ
- Little-oh notation, denoted as ‘o’
- Little omega notation, denoted as ‘ω’
12.2.1 Big Oh Notation
Let f(n) and g(n) be two functions of positive natural or real numbers. f(n) is said to be O(g(n)) if there exists a positive constant C and a value k such that f(n) ≤ Cg(n) for all n ≥ k. Big oh represents the worst case time complexity of an algorithm.
12.2.1.1 Rules Related to Big Oh
Let there be two functions of n, namely F 1 (n) and F 2 , of order O(f(n)) and O(g(n)), respectively.
- F 1 (n) + F 2 (n) = max (O(f(n)), O(g(n))).
- F 1 (n) * F 2 (n) = O (f(n)* g(n)).
- C* F 1 (n) = O(f(n)), where C is a constant.
Example 12.1
Find the order of the following functions in big oh notation.
- f(n) = K, where K is a constant
- f(n) = 3n – 2
- f(n) = 6n 3 + 5n + 1
Solution:
-
Let K = 10. So, f(n) = 10 ≤ 11*1
Consider C = 11 and g(n) = 1. Here, k = 0.
Thus, f(n) = O(1).
-
f(n) = 3n – 2 ≤ 4n for n ≥ 0
Consider C = 4 k = 0 and g(n) = n.
Thus, f(n) = O(n).
-
Consider g(n) as n 3 . We can write the following here: f(n) ≤ 12n 3 for n ≥ 1.
Thus, f(n) = O(g(n)), i.e., O(n 3 ) for C = 12 and k = 1.
Example 12.2
Check whether f(n) = 6n 3 + 5n + 1 be O(n 4 )
Solution: Consider C = 2 and g(n) = n 4 . The following table is constructed to find the value k.
| |
f(n)
|
Cg(n)
| | --- | --- | --- | |
n = 0
|
1
|
0
| |
n = 1
|
12
|
2
| |
n = 2
|
59
|
32
| |
n = 3
|
192
|
162
| |
n = 4
|
405
|
512
| |
n = 5
|
776
|
1250
|
The table shows that f(n) is becoming less after n = 4. Thus, f(n) = O(n 4 ) for C = 2 and k = 4.
Example 12.3
Can n 3 be O(f(n)) where f(n) = 6n 3 + 5n + 1?
Solution: If we take C = 1 and k = 1, then n 3 ≤ 6n 3 + 5n + 1.
Thus, n 3 is O(f(n)) for C = 1 and k = 1.
12.2.2 Big Omega (Ω) Notation
Let f(n) and g(n) be two functions of positive natural numbers. f(n) is said to be Ω(g(n)) if there exists a positive constant C and a value k such that f(n) ≥ Cg(n) for all n ≥ k. Big omega represents the asymptotic lower bound of a given function. It is opposite to the big oh notation.
Example 12.4
Find the order of the following functions in the big omega notation.
- f(n) = K, where K is a constant
- f(n) = 3n – 2
- f(n) = 6n 3 + 5n + 1
Solution:
-
Let K = 10. So, f(n) = 10 ≥ 9* 1
Consider C = 9, g(n) = 1 Here, k = 0.
Thus, f(n) = Ω(1).
-
f(n) = 3n – 2 ≥ 2n for n ≥ 2
Consider C = 2 k = 2 and g(n) = n.
Thus, f(n) = Ω(n).
-
f(n) = 6n 3 + 5n + 1 ≥ 6n 3 for n ≥ 1
Consider C = 6 k = 1 and g(n) = n 3 .
Thus, f(n) = Ω (n 3 ).
Example 12.5
Check whether f(n) = 6n 3 – 3n 2 – 5n + 1 be Ω(n 2 ).
Solution: f(n) is Ω(n 2 ) if there exists a positive number C and k (≤ n) such that
Rename 6n 3 – (3 + C)n 2 – 5n + 1 as T(n).
Lesser the value of (3 + C) makes better the probability of the previous inequality becoming true. Let us take C = 1.
The following table is constructed to find the value k.
| |
T(n)
| | --- | --- | |
n = 0
|
1
| |
n = 1
|
− 2
| |
n = 2
|
23
| |
n = 3
|
112
|
The previous table shows that the inequalities are true from n = 2 onwards. Thus, f(n) ≥ C g(n) for C = 1 and k = 2 (n ≥ k), where g(n) = n 2 . So f(n) = Ω(n 2 ).
12.2.3 Theta Notation (Θ)
Let f(n) and g(n) be two functions of positive natural numbers (both real and integer).
f(n) is called Θ(g(n)) if there exist two positive constants C 1 and C 2 and a value k such that C 2 g(n) ≤ f(n) ≤ C 1 g(n) for all n ≥ k.
If we break the notation for a function to be in Θ, we get f(n) ≤ C 1 g(n) and f(n) ≥ C 2 g(n). In other words, it can be said that for two functions f(n) and g(n), f(n) = Θ(g(n)) if f(n) = O(g(n)) and f(n) = Ω(g(n)).
12.2.4 Little-oh Notation (o)
Let f(n) and g(n) be two functions of positive natural numbers (both real and integer). f(n) is called o(g(n)) if there exist a natural number k and a constant C > 0 such that f(n) < Cg(n) for all n ≥ k ≥ 1.
12.2.5 Little Omega Notation (ω)
Let f(n) and g(n) be two functions of positive natural numbers (both real and integer). f(n) is called ω(g(n)) if there exist a natural number k and a constant C > 0 such that f(n) > Cg(n) for all n ≥ k.
12.3 Problems and Its Classification
A problem is a question to be answered. A problem consists of a parameter. What is the value of X + Y, is the element X available in list[ ], how can an array be sorted in minimum time, etc. Problems are classified into different types such as the following.
- Decision problem
- Optimization problem
- Search problem
- Functional problem
Among these, we shall discuss here mainly two classes of problems—decision and optimization problem.
Decision Problem: Decision problem answers in ‘yes’ or ‘no’. Depending on the situation, the answer may be 1 or 0, true or false, success or failure.
- Given two numbers X and Y, ‘whether Y is a factor of X?’ is a decision problem.
- Given a number N, ‘is N a prime number?’ is a decision problem.
- Given a list of N items, to sort the list of items in ascending order is not a decision problem.
Optimization Problem: Optimization problem is described as a problem of finding the best solution from all available feasible solutions. In other words, it can be said to find the optimal solution from all available feasible solutions. Optimal solution means the use of resources in the most effective and efficient manner, and yielding the highest possible returns under the circumstances. Optimization problems can be either a maximization or minimization problem.
- Shortest path algorithm: This is the problem of finding a path between two vertices (or nodes) in a graph in such a way that the sum of the weights (cost) of its constituent edges is minimized.
- Minimum spanning tree: Given the connected graph G with positive edge weights (cost), the minimum spanning tree is to find a minimum weight (cost) set of edges that connects all the vertices of the graph.
12.4 Different Types of Time Complexity
Already we have learnt about the different notations of time complexity and different types of problems. For solving these problems, we need to construct algorithms. For those algorithms, times complexities need to be calculated which are needed to analyse the algorithms and find the suitable one. So, there exists a close relation between design analysis of algorithm and computational complexity theory. The former is related to the analysis of the resources (time and/or space) utilized by a particular algorithm to solve a problem and the later is related to a more general question about all possible algorithms that could be used to solve the same problem. There are different types of time complexity for different algorithms. In this section, we shall discuss the different types of time complexities.
12.4.1 Constant Time Complexity
An algorithm whose operation does not depend on the size of the input is called constant time complexity algorithm. Regardless of the size of the input, the algorithm takes the same amount of time for all cases. The complexity is denoted as O(1).
Example 12.6
Find the time complexity for accessing the ith element of an array.
Solution: The operation of the algorithm does not depend on the size of the algorithm. Let the algorithm be
Algorithm:
Procedure: Accessing [A, location]
Input: A[ ], i // An array of size n and location
Local b as integer
Begin:
b = A[i]
return b
End
If the size of the array is m, then also the time remains the same.
The algorithm for accessing the highest or lowest element of a sorted list is also in constant time complexity.
12.4.2 Logarithmic Time Complexity
An algorithm which takes logarithmic time based on the input to perform its operation is called a logarithmic time complexity algorithm. For logarithmic time complexity algorithm, the base is usually 2 because computers use the binary number system. For this reason, log 2 n is usually written as log n. Logarithmic time complexity is denoted as O(log n).
Example 12.7
Find the time complexity of binary search.
Solution: Binary search is performed on sorted array. The algorithm compares the key element input by the user with the middle element of the array. If the key is matched with the middle element, return the index of the middle element. If the key is less than the middle element, apply the same algorithm for the left half of the array. If the key is greater than the middle element, apply the same algorithm for the right half of the array. The algorithm for the binary search is
int binary _ search(int A[ ], int key, int min, int max)
{
int mid = [(min + max)/2];
if (A[mid] > key)
return binary _ search(A, key, min, mid-1);
else if (A[mid] < key)
return binary _ search(A, key, mid + 1, max);
else
// key has been found
return mid;
}
Calculation of the mid-element takes a unit time. If the key does not match with the mid-element, the search is limited to n/2 space. Hence, the time taken by the algorithm is
Adding all these equations, we get
Let 2 i = n. i = log 2 n
T(n) = T(1) + log 2 n.
If there is only one element in the array, it takes no time to search. Thus, T(n) = log 2 n.
So, the complexity of the algorithm is O(log 2 n).
12.4.3 Linear Time Complexity
An algorithm is said to be linear if its running time increases linearly with the size of the input. Linear time complexity is denoted as O(n).
Example 12.8
Find the time complexity of linear search.
Solution: Linear search is the simplest method of the searching technique. In this technique, the item to be searched is searched sequentially from the first item of the list until it is found. If the item is found, the location of the item in the list is returned. Following is the algorithm for linear search.
Procedure: LinearSearch(List[ ], target)
Inputs: List[ ] - A list of numbers
Local: i integers
Begin:
int location = -1;
For i = 0 to List.Size;
If (List[i] == target)
location = i;
return location;
End If
Next i
Return -1
End
Start calculating the complexity of the algorithm. The assignment of the value of ‘i’ to ‘location’ is of constant time, i.e., O(1). Returning the location also takes O(1). These two statements are executed once if the condition within the ‘if’ statement holds well. ‘If’ condition checking takes O(1). The statement ‘if’ takes maximum time if the condition is true [till the time is O(1) as O(1) + O(1) + O(1) = O(1)]. This ‘if’ statement is surrounded by a ‘for’ loop. The ‘if’ statement is executed as many times as the ‘For’ loop iterates. For the worst case (if the target element is the last element of list[ ] or if it does not exist), the loop iterates N times, where N is the size of list[ ].
Thus, the time taken by this algorithm is (worst case):
So, the complexity is O(N). Here N is a polynomial of N of degree 1.
12.4.4 Quasilinear Time Complexity
Before discussing quasilinear time complexity, we need the knowledge of linearithmic algorithm. The running time of the algorithms with linearithmic time complexity increases faster than the linear algorithm and slower than the logarithmic algorithm. Linearithmic time complexity is denoted as O(n log n).
Quasilinear time complexity was first proposed by Schnorr and Gurevich and Shelah in their research paper ‘Satisfiability is quasilinear complete in NQL’ in 1978. An algorithm is said to be in quasilinear time complexity if its running time is of O(n (log n) k ) where k ≥ 1. Quasilinear algorithm runs faster than the polynomial algorithms of degree more than 1. Linearithmic algorithms are one type of quasilinear algorithm with k = 1.
Example 12.9
Find the time complexity of quicksort.
Solution: Quicksort is one type of divide and conquer type sorting algorithm proposed by T. Hoare. In this algorithm, a pivot element is chosen and the remaining elements of the list are divided into two halves based on the condition whether the elements are greater or smaller than the pivot element. The same quicksort algorithm is run again separately for two halves.
Process:
- Take an element, called pivot element, from the list.
- Rearrange the list in such a way that all lesser elements than the pivot come before the pivot, while all greater elements than the pivot come after it (equal values can go in either direction). This partitioning makes the pivot to be placed in its final position. This is called the partition operation.
- Recursively, sort the sub-list of lesser elements and the sub-list of greater elements.
A list of size zero or one never needs to be sorted. This is the base case of the recursion.
Procedure: QuickSort (A[ ], left, right)
{
if (left < right)
select PIVOT s.t left ≤ PIVOT ≤ right
PIVOT = PARTITION(A[ ], left, right, PIVOT)
QuickSort(A[ ], left, PIVOT-1)
QuickSort(A[ ], PIVOT + 1, right)
}
Function: PARTITION(A[ ], left, right, PIVOT)
{
Value = A[PIVOT]
swap A[PIVOT] and A[right] // Move pivot to end
Index = left
for i from left to right - 1 // left ≤ i < right
if A[i] < pivot
swap A[i] and A[Index]
Index = Index + 1
swap A[Index] and A[right] // Move pivot to its final place
return Index
}
12.4.5 Average Case Time Complexity
Let the pivot element be the kth smallest element of the array. Then, there are (k – 1) elements to the left of the position of k and (n – k) elements are at the right of the position of k. Thus, for sorting the left half of the array needs T(k – 1), and for sorting the right half of the array needs T(n – k). To place the pivot element, the number of comparisons is (n + 1). For an array of n element, the time complexity for average case quicksort is
Replacing n by (n – 1), we get
Subtracting equation (3) from (2) , we get
Note:
Rearranging equation (4) , we get
Hence, the time complexity is O(n log n).
12.4.6 Polynomial Time Complexity
In mathematics, a polynomial is an expression of finite length constructed by the operations of addition, subtraction, multiplication, and non-negative integer exponents from variables and constants. As an example, f(x) = x 4 + 2x 3 – 3x + 4 is a polynomial of degree 4.
An algorithm is said to be in polynomial time complexity if its execution time is upper bounded by a polynomial expression in the size of the input for the algorithm. The complexity is denoted as O(n k ), where k is a constant.
Let us discuss some algorithms to make the idea clear.
Example 12.10
Find the time complexity of bubble sort.
Solution: Consider the example of bubble sort to sort a list of n items.
Here, an array called list is taken as input. The array is sorted by bubble sort() using the following algorithm.
Procedure: BubbleSort(List[ ])
Inputs: List[ ] - A list of numbers
Locals: i, j - integers
Begin:
For i = 0 to List.Size-1
For j = i + 1 to List.Size-1
If List[i] > List[j], Then
Swap List[i] and List[j]
End If
Next j
Next i
End
Start calculating the complexity of the algorithm from the ‘swap’ operation of two elements. Regardless of size of the list, ‘swap’ always takes the same amount of time, i.e., O(1). The ‘swap’ operation depends on the ‘if statement’ condition. If the condition is false, it does not execute swap. In the worst case (if condition holds true), swap executes. Again, the ‘if’ statement is of O(1). Thus, if the condition is true, it takes maximum time which is still O(1).
The ‘if’ statement is surrounded by two ‘for’ loops. The ‘if’ statement is executed as many times as the inner ‘for’ loop iterates, and the inner ‘for’ loop executes as many times as the outer ‘for’ loop iterates. For each iteration of the outer loop, the inner loop executes N – 1 times, where N is the size of list[ ]. The outer loop iterates N – 1 times. So, the complexity for this algorithm is:
So, the complexity is O(N 2 ). Here,
is a polynomial of N of degree 2.
Example 12.11
Find the time complexity of matrix multiplication.
Solution: Matrix multiplication is a two-dimensional array. Matrix multiplication can be done if the number of rows of the first matrix is equal to the number of columns of the second matrix. Matrix multiplication is done by the following algorithm.
Procedure: MatrixMul(MAT1[][], MAT2[][])
Inputs: MAT1[][], MAT2[][]
Locals: i, j, k – integers
Mult[][] = 0 -integers
If No. of row of MAT1 = No. of column of MAT2
For i = 0 to No. of row of MAT1
For j = 0 to No. of column of MAT2
Mult[i][j] = 0
For k = 0 to No. of row of MAT1
Mult[i][j]= Mult[i][j] + MAT1[i][k] * MAT2[k][j]
Next k
Return Mult[i][j]
Next j
Next i
End If
Start calculating the complexity of the algorithm from ‘mult’. ‘Mult’ consists of addition and multiplication. Let the time taken for addition and multiplication be t 1 and t 2 , respectively. The total time for ‘mult’ operation is t 1 +t 2 , and thus constant, i.e., O(1). This operation is surrounded by three ‘for’ loops of i, j, and k. So, the complexity for this algorithm is:
So, the complexity is O(N 3 ). N 3 is a polynomial of N of degree 3.
12.4.7 Super Polynomial Time Complexity
This is sometimes called exponential time complexity. An algorithm is called super polynomial time complexity if it runs in 2 n steps for an input of size n. The execution time of such algorithms increases rapidly as n grows.
A funny story on exponential growth is the suitable place to mention in this context. An Indian Brahmin Sissa wanted to teach his king a good lesson. The king told this Brahmin proudly that he can pay any amount of paddy grain as he has a large grain container. The Brahmin challenged the king to play chess. The king was defeated. In return, the Brahmin requested the king to pay him such amount of grains so that he can place 1 packet of grain (about 20 kg) on the first box, 2 on the second, 4 on the third, 8 on the fourth, and so on. So, the last box would contain 2 63 packets of grains. (A chess board contains 64 boxes.) The total amount of grains payable to the Brahmin is 2 64 – 1 = 1, 84, 467, 44, 07, 370, 95, 51, 616 packets. India requires several years to produce this amount of paddy grains!
This is called exponential grows. The problems with exponential grows are hard to compute by a computer.
Consider the following problems.
There are 15 different-sized tennis balls. The job of the computer is to arrange the balls such that no two sets of arrangement are equal.
The numbers of such different arrangements are 15! = 1307674368000.
Let there be a computer which can print 10 6 such arrangements in 1 second. This computer will take about 2.5 years to print all such arrangements!
Example 12.12
Find the time complexity of the tower of the Hanoi problem.
Solution: Tower of Hanoi is a mathematical puzzle invented by E. Lucas in 1883. This consists of three rods and a number of different-sized disks with a hole in the middle so that the disks can be arranged by sliding them on the rods. At the initial stage, the disks are placed in ascending order with the smallest radius on the top on the leftmost rod.
The objective of the puzzle is to move the entire stack of disks from the leftmost to the rightmost rod, obeying the following conditions:
- At a time, only one disk can be moved.
- In each move, the upper disk from a rod is slided out and placed on the top of the other disks that may already be present on the another rod.
- A larger radius disk is not allowed to be placed on the top of a smaller disk.
To solve this problem, three rods are named as ‘source’, ‘auxiliary’, and ‘destination’. There are n disks to be shifted from one rod and placed on another rod, which abide by the rules given previously. If the number of disks is 1, it can be easily moved from one rod to another. It can be considered as the base case. For n disks, this can be implemented by the following process.
- Move the top n – 1 disks from source to auxiliary.
- Move the bottom disk from source to destination.
- Move n – 1 disks from auxiliary to destination.
The algorithm to implement this is given as follows.
Tower of Hanoi (n, source, auxiliary, destination)
{
If n = 1 move disk from source to destination; ( base case )
Else,
{
Tower of Hanoi(top n - 1, source, destination, auxiliary);
Move the nth disk from source to destination;
Tower of Hanoi(n - 1, auxiliary, source, destination);
}
}
Moving operation takes a unit time. And the recursive functions take T(n – 1) time each. Thus, the time required for running the else part of the algorithm one time is T(n) = 2T(n – 1) + 1 . The subsequent operation will continue up to T(2). The total time for the algorithm is
Multiplying equation (2) , (3) , (4) , ….. (n – 1) th by 2 1 , 2 2 , . . . . . 2 n – 1 on both sides, we get
Adding the equations, we get
Thus, the complexity of the algorithm is O(2 n )
12.5 The Classes P
An algorithm is known as polynomial time complexity problem if the number of steps (or time) required to complete the algorithm is a polynomial function of n (where n is some non-negative integer) for a problem of size n. In other words, for a problem of size n, the time complexity of the algorithm to solve the problem is a function of the polynomial of n.
In general, polynomial class contains all of the problems which are solved using computers easily. The problems those we solve using computers do not require too many computations, but hardly O(n 3 ) or O(n 4 ) time. Truly speaking, most of the important algorithms we have learnt are somewhere in the range of O(log n) to O(n 3 ). Thus, we shall state that the time complexity of practical computation resides within polynomial time bounds. These classes of problems are written as the P class problem.
Definition 1: The class of polynomial solvable problems , P, contains all sets in which the membership may be decided by an algorithm whose running time is bounded by a polynomial.
Definition 2: P is defined as the set of all decision problems for which an algorithm exists which can be carried out by a deterministic Turing machine in polynomial time.
12.6 Non-polynomial Time Complexity
Non-deterministic polynomial time is abbreviated as NP time. For these types of problems if we are able to certify a solution anyhow then it becomes easy to prove its correctness using the polynomial time algorithm. For this reason, NP class problems are called polynomial time verifiable. In NP problem, it is allowed to make a guess about the answer, and then to verify the correctness of the answer done in polynomial time. Let us consider the case of the vertex cover problem. A vertex cover of a graph G is the set of vertices C such that each edge of G is incident to at least one vertex of C. If a set of vertices is chosen, it can be easily checked in polynomial time whether the set of vertices is a vertex cover of G or not. To find the minimum, such vertex cover is called minimum vertex cover problem. This problem is in class NP.
NP is the set of all decision problems that can be solved in polynomial time on a non-deterministic Turing machine. Already, we have discussed about deterministic and non-deterministic Turing machines. The difference between a deterministic and non-deterministic Turing machine is that the deterministic one operates like a conventional computer, performing each instruction in sequence, forming a computational path, whereas a non-deterministic Turing machine can form a ‘branch off’ where each branch can execute a different statement in parallel, forming a computational tree. The time required to solve the NP problem using any currently known algorithm increases very quickly as the size of the problem grows.
Definition: NP is defined as the set of all decision problems for which an algorithm exists which can be carried out by a non-deterministic Turing machine in polynomial time.
Consider some examples.
Example 12.13
Prime Factorization: A number N can be decomposed into prime numbers where N ≥ 2 such that N = p 1 × p 2 × . . . . . . p k , where p i are k prime numbers. But there are some numbers which have only two prime factors. ‘Prime factorization’ is finding which prime numbers multiply together to make the original number.
Consider a number N, which has to be broken into two factors. The process is
- Guess two numbers A and B.
- Multiply B times A, and check whether the result matches with N.
The checking can be performed in polynomial time.
The algorithm seems to be easy, but not really. In 2009, several researchers factored a 232-digit number, utilizing several hundreds of machines. It took more than 2 years. These types of large numbers left for prime factorization are called RSA number. There are huge amounts of cash prizes for finding the factors of these large numbers! Prime factorization of such large numbers is used for RSA public key cryptography.
Example 12.14
Traveling Salesman Problem: The problem is to find the minimum distance covered by a salesman who wishes to travel around a given set of cities, travelling each city exactly once and returning to the beginning.
Let us consider that an area of travelling consists of 4 cities as given in the following. The cost (distance) to travel from one city to another is given in the following matrix. We are assuming that the cost of ith node to jth node is the same as the cost of jth to ith. [They may be different (if the road is one way).]
Consider a tour starts and ends on node 1. From node 1, the person has to follow the path going to any of 2, 3, or 4 and returning to 1. In general, we can say that the tour consists of an edge
(V is the set of all vertices) and a path from k to 1. According to the condition of the problem, the salesman can travel each city exactly once. Thus, the path from k to 1 must go through all the vertices in V – {1, k}, and this path must be the shortest. Let the length of the shortest path starting from node i and covering all the nodes exactly once and ending on node I be l(i, S). If i = 1, then the function becomes
c 1k is the cost of node 1 to node k.
In general, it can be written as
where i ∉ S.
If n = 4, i = 1, then S = {2, 3, 4}.
So, l(1, {2, 3, 4}) = min{[c 12 + l(2, {3, 4})], [c 13 + l(3, {2, 4})], [c 14 + l(4, {2, 3})]}
If the element of the set S is 3, there are 3 distinct choices for i. If | S | is 2, there are 2 distinct choices for i. In general, for each value of | S | there are n – 1 distinct choices for i.
Not including 1 and I, the number of distinct sets of size k that can be constructed is n–2 C k . The number of steps required for TSP of node n is
We know that (1 + x) n = n C 0 x 0 + n C 1 x 1 + n C 2 x 2 + ....... + n C n x n . If we put x = 1, it becomes 2 n . Therefore, the simplification of the previous formula is (n – 1)2 n – 2 .
The complexity of the problem is O(n2 n ), and it is an NP problem.
Example 12.15
Vertex Cover Problem: It is one type of optimization problem in computer science. Given a graph G, the aim is to find the minimum vertex cover C ⊆ G.
Given a graph G = {V, E} and an integer k ≤ V, the problem is to find V′, subset of at most k vertices, such that each edge of G is incident to at least one vertex in V′.
The algorithm can be constructed by assuming a set of k vertices C for a given graph G. We have to check whether there exists a vertex cover of ≤ k vertices.
The checking can be done by the following steps.
- Check whether C contains at most k number of vertices.
- Check whether C is a vertex cover of G.
Input G = {V, E}, C= {V ′ }, where | V ′ | ≤ k
for i = 1 to | E |
check the edges ends on V ′ i
remove them from {E}.
End
Thus, this checking operation is performed in O(| V′ |+| E |) i.e. in polynomial time.
A graph with n number of vertices can have 2 n number of such vertex combination. For each of the combinations, the same checking algorithm is performed. Among these, the set of vertices with minimum number is the minimum vertex cover of G. So, a minimum vertex cover algorithm is carried out by a non-deterministic Turing machine in polynomial time.
12.7 Polynomial Time Reducibility
In a deterministic Turing machine for a single state and single input, there is only one move. Thus, a problem fed to a deterministic Turing machine is either solved or not. Polynomial-time reduction is a reduction which is computable by a deterministic Turing machine in polynomial time. This is called polynomial time many-to-one reduction. A reduction which converts instances of one decision problem into instances of a second decision problem is called many to one reduction proposed by Emil Post (1944). Already we have learnt about Turing reduction where a problem A is reduced to a problem B, to solve A, assuming B is already known. Many-to-one reductions are stronger than the Turing reduction.
The polynomial time reducibility concept is used in the standard definitions of NP complete.
If P 1 and P 2 are two decision problems, then a problem P 1 is called polynomial time reducible to P 2 if there is a polynomial time algorithm which transforms an instance of P 1 to an instance of P 2 .
Theorem 1: If there is a polynomial time reduction from problem P 1 to problem P 2 and if problem P 2 is in P, then P 1 also is in P.
Proof: A Problem P 1 is polynomial time reducible to P 2 if there is a polynomial time algorithm which converts an instance of P 1 to an instance of P 2 . Let the size of the input of P 1 be n. According to the condition of polynomial time reducibility, the instance of P 2 from the instance of P 1 can be obtained in polynomial time. Let the time complexity of this conversion be O(n i ). According to the big oh notation, f(n) is said to be O(g(n)) if there exists a positive constant C and a value k such that f(n) ≤ Cg(n) for all n ≥ k. Thus, the size of the converted instance of P 2 is less than or equal to Cn i , where C is a constant. The problem P 2 is in P. Let the complexity of solving a problem in P 2 be O(m k ) where m is the size of the problem instance of P 2 . Thus, the instance of P 2 of size Cn i can be solved in complexity O((Cn i ) k ). The total conversion time from the input in P 1 to the solution by P 2 (in other words the solution of problem P 1 ) takes O(n i + (Cn i ) k ) = O(n i + C k n ik ) = O(n ik ). It is in polynomial time. It is proved that P 1 is in P.
12.8 Deterministic and Non-deterministic Algorithm
An algorithm is called a deterministic algorithm if, for a function, the output generated for a given input is the same for all trials. The behaviour of this type of algorithm is predictable as the underlying machine follows the same sequence of states for a given input to produce output. A mathematical function is deterministic. Hence, the state is known at every step of the algorithm.
An algorithm is called a non-deterministic algorithm if, for a function, the output generated for a given input is different for different trails. An algorithm that solves a problem in non-deterministic polynomial time can run in polynomial time or exponential time depending on the choices it makes during execution. An algorithm is non-deterministic if the underlying machine follows a different sequence of states for a given input. Some examples of non-deterministic algorithm are spanning tree construction algorithm, searching algorithm, merge sort algorithm, vertex set covering problems, etc. To know more about these problems, follow some books on algorithm.
12.8.1 Tractable and Intractable Problem
A problem is called a tractable problem if there is a polynomial time algorithm to solve it for all instances. There exists a deterministic Turing machine to solve these problems. In other words, it can be said that a problem is called a tractable problem, when there exists a deterministic Turing machine M such that
- M runs in polynomial time on all input
- for x ∈ L, M gives output 1
- for x ∉ L, M gives output 0.
A tractable problem may be of constant [O(1)], logarithmic [O(log n) or O(n log n)], linear [O(n)], or polynomial [O(n k ), where k is a finite number]. The upper bound of a tractable problem is O(polynomial).
A problem is called an intractable problem if the optimal algorithm for the problem cannot solve all instances of the problem in polynomial time. These types of problems can be solved in theory, but in practice they take too long for their solutions to be useful. Intractable problems are sometimes called computationally intractable, computationally complex, or computationally hard. An intractable problem may be exponential [O(k n )], factorial [O(n!)], or super exponential [O(n n )].
constant | O(1) |
logarithmic | O(log n) |
linear | O(n) |
n-log-n | O(n × log n) |
quadratic | O(n 2 ) |
cubic | O(n 3 ) |
exponential | O(k n ), e.g. O(2 n ) |
factorail | O(n!) |
super-exponential | e.g. O(n n ) |
12.9 P = NP?—The Million Dollar Question
It is discussed that if a problem can be solved by a deterministic Turing machine in polynomial time, it is called P class problem. If a problem can be solved by a deterministic Turing machine in exponential time, it is called E class problem. The time complexity of E class problems are of O(2 n ) or O(n n ). If n = 1, 2, 3 . . . . , then E class problem becomes P class problem. Thus, it can be said that P ⊂ E. On the other hand, if a problem can be solved by a non-deterministic Turing machine in polynomial time, it is called NP class problem. In the chapter ‘Variation of Turing machine’, the conversion process of a non-deterministic Turing machine to a deterministic Turing machine is discussed. There, we have to construct a computational tree consisting of the branches for accepting a string by a non-deterministic Turing machine. Then the tree is traversed by BFS. Traversing this BFS tree means visiting all possible computations of the NTM in the order of increasing length. It stops when it finds an accepting one. Therefore, the length of an accepting computation of the DTM is, in general, exponential in the length of the shortest accepting computation of the NTM. This means that all problems in class NP belong to class E, and they can be solved by a DTM in an exponential time.
It is mentioned that P ⊂ E. Now, NP also belongs to class E. Now, can it be told that P = NP or P ≠ NP? It is one of the unsolved problems in computer science. NP class problems are polynomial time verifiable. Whether S is a solution or not for an NP problem PROB can be verified easily (in polynomial time). But whether the problem PROB can be solved as easy as verification is the basis for P = NP? As an example, consider the subset sum problem. The problem is as follows: is there a non-empty subset from a set of integer numbers whose sum is zero? Let a set of integer numbers be {1, 2, –3, 4, –5, 7, 1}. Does a subset of this set produce 0? The answer is affirmative as a subset {1, –3, –5, 7} produces 0. Thus, the answer is easily verified but does there exist an algorithm to find the answer (construction of the subset) in polynomial time? Till now no such algorithm is found, though there exists an algorithm which can find the answer in O(2 n ) complexity, and thus NP.
The P = NP problem was first introduced by S. Cook in his research paper ‘The complexity of theorem proving procedures’ in 1971 at the third annual ACM symposium on theory of computing. The Clay mathematical institute has declared a one million dollar prize for solving this question.
Scott Aaronson of the MIT believes that ‘There is value for creative leaps in the world because P ≠ NP. If P = NP, then everyone who could appreciate a symphony would be Mozart; everyone who could follow a step-by-step argument would be Gauss.’
Still, research is on to find whether P = NP or P ≠ NP. You can also try. If you succeed, then one million dollar is yours! No need for further study to get a job!
12.10 SAT and CSAT Problem
12.10.1 Satisfiability Problem (SAT)
This problem plays an important role in NP problem. So, the discussion of this problem is very important before discussing topics such as NP complete, Cook’s theorem, etc.
In mathematics a formula is called satisfiable if it is possible to find an interpretation that makes the formula to return true.
12.10.2 Circuit Satisfiability Problem (CSAT)
A circuit consisting of AND, OR, and NOT GATE (because these three are called basic GATE) can be represented by a Boolean function. Each element of the circuit has a constant number of Boolean input and output. Boolean values are defined on the set of {0, 1} where ‘0’ means FALSE and ‘1’ means TRUE.
Symbolically, OR is represented as ∨, AND is represented as ∧, and NOT is represented as ¬. The truth tables representing these three gates can be found in Chapter 1 .
Given a Boolean function consisting of AND, OR, and NOT gate with single output. Is there an assignment of values to the circuit’s inputs in such a way as to make the function evaluate to TRUE ? The C-SAT problem is sometimes called as Boolean satisfiability problem.
Before discussing the details, we have to know some basic terms.
Literal: A literal is either a variable or negative of a variable. X 1 and ¬X 1 are literal. X 1 ∨ ¬X 1 always evaluate to true.
Disjunction: Disjunction is defined as a logical formula having one or more literals separated only by ORs. (X 1 ∨ ¬X 2 ∨ X 3 ) is a disjunction but (X 1 ∨ ¬X 2 ∧ X 3 ) is not. Disjunction is sometimes referred to as clause.
Conjunction: A logical conjunction is defined as an AND operation on two or more logical values. (X 1 ∨ ¬X 2 ) ∧ (X 2 ∨ X 3 ) is a logical conjunction.
Conjunctive Normal Form (CNF): A Boolean formula is known to be in conjunctive normal form if it is a conjunction of disjunction (clauses). A formula in CNF can contain only AND, OR, and NOT operations. A NOT operation can only be used as a part of literal. Like ¬X 2 , but not ¬(X 2 ∨ X 3 ).
The examples in (a), (b), and (c) are in CNF
a) (X 1 ∨ ¬ X 2 ) ∧ (X 2 ∨ X 3 )
b) (X 1 ∨ ¬ X 2 ∨ X 3 ) ∧ (X 2 ∨ ¬ X 1 )
c) X 1 ∧ (¬ X 2 ∨ X 3 ) ∧ X 3
The following three examples in (d), (e), and (f) are not in CNF
d) ¬ (X 1 ∨ ¬ X 2 ) [NOT problem]
e) ¬ X 2 ∨ (X 1 ∧ X 3 ) (AND problem)
f ) X 1 ∧ (X 2 ∨ (X 1 ∧ X 3 )) (OR problem)
These three formulas can be converted to equivalent CNF as in (g), (h), and (i).
g) ¬ X 1 ∧ X 2
h) (¬ X 2 ∨ X 1 ) ∧ (¬ X 2 ∨ X 3 )
i) X 1 ∧ (X 2 ∨ X 1 ) ∧ (X 2 ∨ X 3 )
CNF is used in SAT problems for detecting conflict and to remember partial assignments that do not work.
(In the context-free grammar chapter, we learnt about the Chomsky normal form. Do not mess this CNF with the conjunctive normal form.)
2 SAT: A SAT problem is called 2 SAT if the number of literals in each disjunction (clause) is exactly 2.
3 SAT: A SAT problem is called 3 SAT if the number of literals in each disjunction (clause) is exactly 3.
SAT is in NP because any assignment of Boolean values to Boolean variables that are claimed to satisfy the given logical expression can be verified in polynomial time by a deterministic Turing machine. (Can be proved)
Prove that 2 SAT problem is in P
Proof: Let φ be an instance of 2 SAT. Generate a graph G(φ) by the following rules.
- Vertices of the graph G are the variables and their negations. If the number of variables is n, then the number of nodes is 2n.
- If there is a clause (¬α ∨ β) or (β ∨ ¬α) in φ, then an edge α → β is added in G.
- If there is an edge from α → β in G, then an edge ¬β → ¬α is added in G.
- If there is a clause (α ∨ β) in φ, then two edges ¬α → β and ¬β → α are added in G.
Let us take an example φ = (x 1 ∨ x 2 ) ∧ (x 1 ∨ ¬x 3 ) ∧ (¬x 1 ∨ x 2 ) ∧ (x 2 ∨ x 3 ).
- There are three variables x 1 , x 2 , and x 3 in φ. Thus, the graph contains 6 nodes namely x 1 , ¬x 1 , x 2 , ¬x 2 , x 3 , and ¬x 3 .
- (x 1 ∨¬x 3 ) is in the form (β ∨ ¬α). Thus, an edge x 3 → x 1 is added to G. According to the rule (c), an edge (¬x 1 → ¬x 3 ) is also added to G.
- By the same rule, x 1 → x 2 and (¬x 2 → ¬x 1 ) are also added to G.
- (x 1 ∨ x 2 ) is in the form (α ∨ β). Thus, two edges (¬x 1 → x 2 ) and (¬x 3 → x 1 ) are added to the graph.
- By the same rule, (¬x 2 → x 3 ) and (¬x 3 → x 2 ) are added to the graph for (x 2 ∨ x 3 ).
φ is unsatisfiable if and only if there is a variable x such that there are edges from x to ¬x and ¬x to x in G.
Suppose such x exists. x may have any value between 0 and 1. Consider x = 1 and an edge x to ¬x. (Consider x as α and ¬x as β.) It means there is a clause (¬x ∨ ¬x) or (¬x ∨ ¬x) (both are the same) in φ. The clause (¬x ∨ ¬x) returns 0 if x = 1. This makes the total φ as 0 [because the sub-clauses are attached with ∧] which means unsatisfiable.
The same thing is true for an edge ¬x to x in G.
For proving the ‘only if’ part, we have to take the help of contradiction. Consider φ is unsatisfiable.
- If there is a path from α to ¬α for a node α, then α must be assigned to false.
- If there is no path from α to ¬α for a node α, then those nodes which are reachable from α must be assigned to false. [According to the rule that if there is an edge α → β, then there are clauses (¬α ∨ β) or (β ∨ ¬α) in φ.]
- Repeat this for all the nodes.
Now consider the previous graph. There is no edge from x 3 to ¬x 3 .
If x 3 is true, then x 1 must be false, because this was added for the sub-clause (x 1 ∨ ¬x 3 ) in φ. There is an edge x 1 → x 2 for the clause (¬x 1 ∨ x 2 ) in φ. If x 1 is true, then x 2 is false, which means x 3 must be false and ¬x 3 must be true.
There is an edge (¬x 2 → x 3 ) for the clause (x 2 ∨x 3 ) in φ. If x 2 is false, then x 3 must be false.
Here, we are getting the contradiction for x 3 and ¬x 3 .
It proves that step (2) cannot exist if φ is unsatisfiable, which justifies that φ is unsatisfiable if and only if there is a variable x such that there are edges from x to ¬x and ¬x to x in G.
For a 2 SAT problem, if there are n variables, then the existence of an edge from x to ¬x and ¬x to x can be found within 2n steps. This signifies that 2 SAT is in P.
Prove that 3 SAT problem is in NP
Proof: A SAT problem φ is called 3 SAT if the number of literals in each clause is exactly 3. Let F be a CNF SAT problem. We have to convert F to a 3 CNF SAT problem F′ such that if F is satisfiable, then F′ is also satisfiable. Let the problem F contain clauses C 1 , C 2 ….C k . Here, three cases of C may occur.
- C i contains exactly 3 literals.
- C i contains less than 3 literals.
- C i contains more than 3 literals.
We have to concentrate on cases (2) and (3).
Solution for Case (2) : If C i contains less than 3 literals, then the number of literals may be 2 or 1. If it contains 1 literal, say L 1 , then replace this by (L 1 ∨ L 1 ∨ L 1 ).
If it contains 2 literals, say (L 1 ∨ L 2 ), then replace it by (L 1 ∨ L 2 ∨ L 1 ).
Solution for Case (3) : If C i contains more than 3 literals, then consider new literals Y j , where j = 1 to k – 3. If C i is in the form (x 1 ∨ x 2 ∨. . . . . ∨x k ), where k ≥4, replace it by
The previous equation is true for any value of x k and y j .
Let C i contain 4 literals (x 1 ∨ x 2 ∨ x 3 ∨ x 4 ). According to the previous equation, it is converted as
(x 1 ∨ x 2 ∨ y 1 ) ∧ (x 3 ∨ x 4 ∨ ¬y 1 )
As (x 1 ∨ x 2 ∨ x 3 ∨ x 4 ) = 1, any of the literals is 1 to make the clause 1. Let x 1 and x 2 be 1 but x 3 and x 4 be 0. If y 1 is 0, then the total clause will be 1. If x 3 and x 4 are 1 but x 1 and x 2 are 0, then y 1 is 1 to make the clause evaluate to 1. From the discussion, it is proved that there exists assignment of literals to make the clause evaluate to 1. Using the rules, F′ is converted to 3 CNF form.
It is also proved that if there is an assignment of literals to make F = 1, then there also exist literals to make F′ = 1.
Now, calculate the time complexity of 3 SAT problem. Let a SAT problem F contain 2 clauses where each clause contains 4 literals. F′ contains 2 × 2 = 4 clauses. If the number of clauses is 3 but the number of literals in each clause is 4, then F′ contains 2 × 2 × 2 = 2 3 clauses. If the number of literals in each clause is 5, then F′ contains 3 × 3 × 3 = 3 3 clauses. For l literals with k clauses each, F′ contains (1 – 2) k clauses, where l ≥ 4 and k ≥ 1. It means the time increases exponentially to convert a SAT problem to 3 SAT. But the time to check whether a SAT is in 3-SAT or not can be checked in polynomial time. This means 3-SAT is in NP.
12.11 NP Complete
The concept of NP complete was first introduced by Stephen Cook by the circuit satisfiability problem. Then, Hopcroft and Ullman make it familiar in their book of automata theory. Already we have gained knowledge about the NP problem. These types of problems can be verified in polynomial time (easily) but cannot be solved easily by any algorithm known till date.
The NP complete problem ρ, written as NPC, is the problem which belongs to NP (i.e., can be verified easily), and any NP problem can be converted to ρ by a transformation of the inputs in polynomial time.
NP Complete: A decision problem ρ is in NP complete if
- It is in a class of NP.
- A known NP complete problem can be reducible to ρ in polynomial time.
To prove that a problem is NP complete, first prove that it is in NP, and then reduce some known NP complete problem to it.
The Boolean satisfiability problem (CSAT), traveling salesman problem, subset sum problem, Hamiltonian path problem, vertex cover problem, graph colouring problem, knapsack problem, and N-puzzle problem are some examples of NP complete problem. All these problems can be reducible to the CSAT problem, and thus CSAT is NP complete. TSP can be reducible to the Hamiltonian path problem which is reducible to the vertex cover problem.
Theorem 2: Let P 1 and P 2 be two decision problems in NP, and P 1 is NP-complete. Problem P 2 is NP complete if there is a polynomial time reduction for P 1 to P 2 .
Proof: Let L be a language which belongs to an NP complete problem. P 1 is an NP complete problem. According to the definition of the NP complete problem, every problem can be reducible to P 1 in polynomial time. So, L can be reduced to P 1 in polynomial time. Let w be a string of L of size n. The string w can be converted to an instance of P 1 , say w′, in polynomial time, say ρ(n), where ρ is a polynomial.
Let P 1 be reducible to P 2 in polynomial time. Suppose this conversion takes ρ′(n) time. Hence, the time taken to transform w to an instance of P 2 (via an instance of P 1 ) is ρ(n) + ρ′(ρ(n)). The addition of two polynomials is a polynomial. Thus, L is reducible to P 2 in polynomial time.
- P 2 is in NP
- An NP problem L is reducible to P 2 in polynomial time.
So, we can decide that P 2 is NP complete.
Theorem 3: If any NP complete problem is solvable in P, P = NP proves.
Proof: Let ρ be an NP complete problem which is solvable in polynomial time. Let ρ′ be another NP complete problem. According to the definition of NP completeness, ρ′can be reduced to ρ in polynomial time. According to theorem 1, as ρ is in P, ρ′ is also in P. A problem is NP complete means it is in NP. Thus, it is proved that NP = P.
12.11.1 Cook–Levin Theorem
In complexity theory, the Cook–Levin theory is sometimes known as the Cook theorem. The theorem states that SAT is an NP complete problem .
Proof: To prove any problem as NP complete, we need to follow two steps.
- The problem is in NP
- Every other language in NP is polynomial time reducible to that problem.
For SAT, the first one is proved in the case of the 3 SAT problem. This section contains the proof of the section (b).
Idea: Consider N to be a non-deterministic TM that decides A ∈ NP. The reduction process takes a string w ∈ A, and generates a Boolean formula F such that N accepts w ⇔ F is satisfiable (the symbol ⇔ signifies denotes). In particular, we choose the Boolean formula F such that its satisfying assignment corresponds to the accepting computation for N to accept string w ∈ A.
The following section describes the process to make the idea come true.
Let N be the non-deterministic Turing machine which decides an NP problem A. Let n k be the running time of the Turing machine N on an input of length n. Here, k is some constant.
Define a table of dimension n k × n k for N on input w. Each row of the table stores a configuration in the branch of computation of N on w. The table is as follows.
All configuration start and end with an end marker #. Configurations are represented row wise, where the first row represents the starting configuration and the subsequent rows follow its previous row. The table is declared in accepting configuration or, in other words, an accepting computation is achieved if any row of the table is in accepting configuration.
It means deciding whether the Turing machine N accepts w is the same as to decide whether an accepting table for N on w exists. So, our task is now simplified to find a formula F that can check if such an accepting table exists.
The formula F is a combination of some variables. Consider a variable X i, j , s for a cell label (i, j) in the table (i is the number of rows and j is the number of columns) and s is a symbol ∈ Q ∪ Γ ∪ #. Let us represent this notation as C. The variable X i,j,s = 1 if the cell label (i, j) contains symbol s.
For making the formula F to be satisfiable, it must be assured that
- Each cell of the table contains only one symbol.
- The table has accepting configuration.
- Each row of the table is correct.
For the previously mentioned three cases, consider three sub-formula f cell , f accept , and f correct , respectively. The formula F is satisfiable if the three formulas are satisfiable. Thus, we can write
F = f cell ∧ f accept ∧ f correct
A cell contains only one symbol if it contains at least one symbol and at most one symbol. Let f cell 1 and f cell 2 be two sub-functions for the two conditions.
- f cell 1 \= ∨ xeC x ij
- f cell 2 \=∧ s, t ∈ C,s ≠ t ((-x i,j,s )∨(-x i,j,t ))
Thus, f cell = f cell1 ∧ f cell2
For f accept , i.e., to ensure that the table has accepting configuration, the notation is
f accept = ∨ i,j x i,j,q accept
Ensuring each row of the table is correct means to ensure that the first row of the table is correct and the subsequent rows are correct. The following sub-formula is used to ensure that the starting row is correct.
A window of size 2 × 3 known as legal window is used to ensure that the remaining rows are correct.
(i, j) | (i, j + 1) | (i, j + 2) |
(i + 1, j) | (i + 1, j + 1) | (i + 1, j + 2) |
A window of cell (i, j) refers to a group of 2 × 3 cells having label as mentioned previously. A legal window is a window which does not violate the actions of the transitional functions of the Turing machine N, assuming the configuration of each row follows legally from the configuration of its upper row.
Let us concentrate more on the legal window.
-
The following window is legal for the transitional function δ(q 1 , b) → (q 2 , a, L).
-
The following window is legal for the transitional function δ(q 1 , b) → (q 2 , a, R).
-
The following window is legal for the transitional function δ(q 1 , a) → (q 2 , b, R).
-
The following window is legal for the transitional function δ(q 1 , b) → (q 2 , a, L).
-
The following window is legal for the transitional function δ(q 1 , b) → (q 2 , a, L).
-
The following window is also legal.
Based on the concept of the legal window, the sub-formula f move is constructed as
f move = ∧ i ≥ 1, j ≤ n k – 2(window i, j correct)
where the notation ‘windows i,j correct’ expressed as ∨ a 1 , a 2 , . . . . a 6 is a legal window.
From the previous discussions, we can now construct F as
Here, F is satisfiable means its satisfying assignment represents an n k X n k accepting table. This implies that N has an accepting computation on input w, i.e., N accepts w.
In the reverse, if N accepts w, then there must be an accepting computation, and F has a satisfying assignment, i.e., F is satisfiable.
This means for any w, we can construct a Boolean formula F such that N accepts w, which implies F is satisfiable. The construction of the Boolean formula F gives a reduction from deciding a language in NP to deciding whether a formula is in SAT.
Let w be of length n. So, the sub-formula f start can be constructed in O(n k ) time.
The sub-formula (∧ i,j (f cell 1 ∧ f cell 2 )), f accept , f move ) can be construct in O(n 2k ) time.
So, F can be constructed in polynomial time. Thus, any language in NP is polynomial time reducible to SAT and SAT is in NP, which implies SAT is NP complete.
Example 12.16
Prove that 3 SAT is NP complete.
Solution:
- 3 SAT is clearly in NP, given an instance of a circuit C and a ‘satisfying’ setting of Boolean variables. A simulation of the circuit can easily check in polynomial time whether the output is 1 or not.
- A SAT problem φ is called 3 SAT if the number of literals in each clause is exactly 3. It is already shown how a SAT formula with clauses less than or more than 3 literals can be converted to a formula with clauses each having exactly 3 literals. This conversion can be made in polynomial time. Hence, SAT can be reduced to 3 SAT in polynomial time. So 3 SAT is NP complete.
Example 12.17
Prove that the vertex cover problem is NP complete.
Solution:
- That the vertex cover problem is in NP is already proved.
- Reduce 3-SAT to vertex cover.
Reduction:
- For each variable of the Boolean expression in 3 SAT, create two vertices v i and ¬v i , and connect them by edges. For N such variables, there are 2N vertices and N edges. Let the graph be G 1
- To cover G 1 , at least n vertices must be taken, one for each pair.
- For each clause of the Boolean expression in 3 SAT, create 3 vertices and connect them by edges. If a Boolean expression contains C clauses, then the number of vertices will be 3C. Let the graph be G 2 .
- To cover G 2 , at least 2C vertices must be taken. (To cover each triangle, at least 2 vertices are required.)
- Finally, the vertices of G 1 and G 2 which share the same literal are connected to produce a graph G with N + 3C vertices.
Example: Let the 3 SAT Boolean formula be
F = (U ∨ ¬W ∨ ¬X) ∧ (¬U ∨ V ∨ ¬X)
There are 4 variables U, V, W, and X. So, there are 8 vertices and 4 edges.
Construction of G 1
Construction of G 2
Construction of G
If is it proved that the graph G has a vertex cover of size N + 2C, if and only if the expression is satisfiable, then the reduction is correct.
Proof: Already it is shown that, any cover of G must have at least N + 2C vertices. To prove that the reduction is correct, it must be shown that:
-
Every satisfying truth assignment gives a cover.
Take a Boolean formula in 3 SAT with N literals. Taking N vertices, at least one of the three edges constructing a clause is covered. Taking the other two vertices (total 2C) will cover the other two edges.
-
Every vertex cover gives a satisfying truth assignment.
Every vertex cover must contain N vertices from G 1 and 2C vertices from G 2 . Let the vertices from G 1 define the truth assignment. To give the cover, at least one of the three edges of the triangle constructed from a clause is incident on the vertex. By adding the other two vertices (total 2C) to the cover, all the edges of G 2 are associated with it, which forms a cover of N + 2C vertices.
From here, it is proved that every 3 SAT defines a cover and every cover gives the TRUE value for a Boolean expression in 3 SAT. This conversion is possible in polynomial time.
As the reduction is possible in polynomial time, we can conclude that the vertex cover problem is NP complete.
Co-NP: The complement of every NP language is Co-NP.
The complement of language in P is also in P. All P are also in NP. The complement of the NP-complete problem is called the Co-NP complete problem, which is a subset of the Co-NP problem.
The relation of NP, P, and Co-NP is given in the following.
If it is proved that P = NP, then P = NP = Co-NP. NP = Co-NP if there exists some NP complete problem whose complement is in NP. NP = Co-NP? is another unsolvable problem in computer science.
12.12 NP Hard
In complexity theory, the NP hard problems are known informally ‘at least as hard as the hardest problems in NP’. A problem P is called NP hard if there is an NP complete problem P 1 , which is Turing reducible to P in polynomial time. In this context, we need to know about another machine called the Oracle machine. The Oracle machine is an abstract machine which can be thought of as a Turing machine with a black box. This abstract machine is used to decide certain decision problems in single operation. An Oracle machine can perform all operations performed by a Turing machine, as well as it can answer some questions in the form ‘A is a set of natural numbers. If x ∈A?’. We can say in other words that a problem P is called NP hard if there is an NP complete problem P 1 where P 1 can be solved in polynomial time by an Oracle machine with an oracle for P.
12.12.1 Properties of NP Hard Problems
- P is used to solve P 1 , so P is as hard as P 1 .
- P does not have to be in NP; for this reason, it does not have to be a decision problem.
- If for any NP-hard problem there is a polynomial time algorithm, then all problems in NP can be solved in polynomial time. From here, we can prove P = NP.
- P = NP does not resolve that NP-hard problems can be solved in polynomial time.
From the discussions of NP-hard problems, we can construct a diagram showing the relations between P, NP, NP complete, and NP hard.
Example:
- Minimum vertex cover problem.
- Given a program and its input, will it run forever?—Halting problem.
- Maximum clique problem.
The following are the set of NP complete problems. Here, the arrow direction from A → B signifies that problem A is reducible to problem B.
Following are the descriptions of the given problems.
- Clique problem: Let a graph G = (V, E), where V is the set of vertices and E is the set of edges. A clique is described as a complete sub-graph of G, that is, any two vertices in the sub-graph are joined by an edge. The size of a clique is denoted by the number of vertices that exist in the subgraph. The clique problem is ‘given a graph G, is there a clique of size n?’
- Vertex cover problem: Let a graph G = (V, E), where V is the set of vertices and E is the set of edges. A vertex cover of the graph is a set of vertices such that each edge of the graph is incident to at least one vertex of the set. The vertex cover problem is ‘given a graph G, does G has a vertex cover of size n?’
- Hamiltonian cycle problem: Let G be an undirected graph of V vertices. The Hamiltonian path is a path which visits every vertices of G exactly once. The Hamiltonian cycle is a Hamiltonian path which contains a cycle. The Hamiltonian cycle problem is ‘given a graph G, does G have a Hamiltonian cycle?’
- Subset sum problem: Given a set of integer Z and another integer n, the problem is whether there is a non-empty subset S, sum of whose elements is n?
12.13 Space Complexity
Space complexity was mentioned in Section 12.1 . To execute an algorithm, memory space is necessary. Although elementary, the Turing machine input tape cell is one kind of a resource. Space complexity of a program is the number of elementary objects that it requires to store during execution. This is computed with respect to the size of the input data. Space complexity shares many features of time complexity. In this section, the Turing machine is considered as a computing model and space means the tape space consumed during the execution of the algorithm.
Definition: Let M be a deterministic Turing machine designed for algorithm A and that halts on all inputs. The space complexity of M is defined as a function f: N → N, where f(n) is the maximum number of tape cells scanned by M on any input of length n.
Let M be a non-deterministic Turing machine designed for algorithm A and where all branches halt on all inputs. Here, space complexity f(n) is the maximum number of tape cells that M scans on any branch during the computation phase on any input of length n.
For an algorithm A and input I, space complexity SPACE(A, I) is denoted as the number of cells (maximum for NTM) used during the computation of A(I) (input I applied on A), when A(I) must halt. SPACE(A, I) = O(f(n)) if | I | = n.
Polynomial Space or PSPACE: The complexity of the decision problems which can be solved by a deterministic Turing machine and a polynomial amount of space are called polynomial space complexity problem or PSPACE problem.
The class PSPACE contains all classes of SPACE(f(n)), where f(n) is a polynomial of finite degree.
Non-polynomial Space or NSPACE: The space complexity for an algorithm performed by a non-deterministic Turing machine is denoted as NSPACE. NSPACE(f(n)) is the set of decision problems that can be solved by a non-deterministic Turing machine with the use of O(f(n)) amount of space. Here, f(n) is the maximum number of tape cells that NTM scans on any branch during the computation phase on any input of length n and must halt.
From the previous discussions, it is clear that PSPACE ⊆ NSPACE.
Relation Between Time and Space
What We Have Learned So Far
- Algorithms may differ in time taken and/or memory required to execute and produce result, for the same input.
- Mainly, two types of complexities are measured for an algorithm:- time complexity and space complexity.
- The time complexity of a program for a given input is the number of elementary instructions (the number of basic steps required) that this program executes.
- The space complexity of a program for a given input is the number of elementary objects that this program needs to store (i.e., memory consumed) during its execution.
- The growth rate of any exponential function is greater than that of any polynomial.
- The well-known notations for the growth rate of functions are big oh notation (O), big omega notation (Ω), theta notation (Θ), little-oh notation (o), and little omega (ω).
- The problems are mainly classified as decision problem, optimization problem, search problem, and functional problem.
- An algorithm is known as a polynomial time complexity problem if the number of steps (or time) required to complete the algorithm is a polynomial function of n (where n is some non-negative integer) for a problem of size n.
- NP is defined as the set of all decision problems for which an algorithm exists which can be carried out by a non-deterministic Turing machine in polynomial time.
- Polynomial-time reduction is a reduction which is computable by a deterministic Turing machine in polynomial time.
- If there is a polynomial time reduction from problem P 1 to problem P 2 and if problem P 2 is in P, then P 1 is also in P.
-
A decision problem ρ is NP complete if
- it is in a class of NP.
- Every NP problem can be reducible to ρ in polynomial time.
- If any NP complete problem is solvable in P, P = NP proves.
Solved Problems
-
Find the complexity of the following algorithm.
int powerA(int x, int n)
{
if (n==0)
return 1;
if (n==1)
return x;
else
return x * powerA(x, n – 1);
}
Solution: If n = 1, then the result is x. Returning x takes unit time. If n > 1, then the algorithm enters into the else part. This part contains a multiplication and a same algorithm repetition up to n – 1 time. So, we can write T(n) = T(n – 1) + 2 if n >1.
So, we can write
Adding all these, we get
T(n) = (n – 1) * 2 + 1 = 2n – 1
So, the complexity is O(n). Here, 2n – 1 is a polynomial of n of degree 1.
-
Write the divide and conquer algorithm for merge sort and find its complexity. [Sri Venkateswara University 2008]
Solution: In merge sort, the n element array is divided into two halves each with n/2 elements (for n = even), or one with n/2 and the other with n/2 + 1 elements. The two halves are sorted and merged. The algorithm for performing merge sort is as follows:
<table><colgroup><col> <col></colgroup><tbody><tr><td><p><span>mergeSort(int min, int max)</span></p><p><span>{</span></p><p><span>if(min < max)</span></p><p><span>{</span></p><p><span>m = (min + max)/2</span></p><p><span>mergeSort(min, m)</span></p><p><span>mergeSort(m + 1, max)</span></p><p><span>Merge(min, m, max)</span></p><p><span>}</span></p><p><span>}</span></p></td><td><p><span>Merge(min, m, max)</span></p><p><span>{</span></p><p><span>i = min, j = min + 1, k = 0</span></p><p><span>while((i</span> ≤ <span>min) && j</span> ≤ <span>max)</span></p><p><span>{</span></p><p><span>if(A[i] < A[j])</span></p><p><span>{</span></p><p><span>temp[k++] = A[i]</span></p><p><span>i++</span></p><p><span>}</span></p><p><span>elseif(A[i] > A[j])</span></p><p><span>{</span></p><p><span>temp[k++] = A[j]</span></p><p><span>j++</span></p><p><span>}</span></p><p><span>else</span></p><p><span>{</span></p><p><span>temp[k++] = A[i]</span></p><p><span>temp[k++] = A[j]</span></p><p><span>}</span></p><p><span>}</span></p><p><span>while(i</span> ≤ <span>m)</span></p><p><span>{</span></p><p><span>temp[k++] = A[i++]</span></p><p><span>}</span></p><p><span>while(j</span> ≤ <span>q)</span></p><p><span>{</span></p><p><span>temp[k++] = A[j++]</span></p><p><span>}</span></p><p><span>for(i = 0, i < n, i++)</span></p><p><span>{</span></p><p><span>A[i] = temp[i]</span></p><p><span>}</span></p><p><span>}</span></p></td></tr></tbody></table>
The algorithm Merge(min, m, max) is performed in O(n). The algorithm mergeSort(int min, int max) of n element becomes half when it is called recursively. Thus, the whole algorithm takes
T(1) = 1 (time required to sort a list of one element).
Thus, the complexity of merge sort is O(n log 2 n).
-
Prove that the k clique problem is NP complete.
Solution:
Definition: Given a graph G = {V, E} and an integer k, check whether there exists a sub-graph C of G which contains k number of vertices.
(The max clique problem is finding a clique with largest number of vertices. It is an NP hard problem.)
To prove that clique is NP complete, we need to prove:
-
Clique is in NP
- Check whether C contains k number of vertices. This can be done in O(| C |).
- Check whether C is a complete sub-graph. This can be done in k C 2 steps.
Thus, checking whether C is a clique of G or not can be performed in polynomial time.
A graph with n number of vertices can have 2 n number of such vertex combination. For each of the combinations, the same checking algorithm is performed. Among these, the clique with k vertices is the k-clique of G. So, a k-clique algorithm is carried out by a non-deterministic Turing machine in polynomial time. Hence, it is in NP.
-
Reduce 3-SAT to k-clique
Reduction:
-
For each clause of a Boolean assignment, create distinct vertices for each literal. If a Boolean assignment contains C clauses, then the number of vertices is 3C.
-
Join the vertices of the clauses in such a way that
– Two vertices of the same clause cannot be joined.
– Two vertices whose literals are the negation of the other cannot be joined.
Example: Consider F = (X 1 ∨ X 2 ∨ Y 1 ) ∧ (X 3 ∨ ¬Y 1 ∨ ¬Y 2 ) ∧ (X 4 ∨ X 5 ∨ ¬Y 2 )
The graph is
If it can be proved that G has a k-clique if and only if F is satisfiable, then the reduction is correct.
-
If F is satisfiable, G has k-clique: If the 3-SAT instance F is TRUE, then each clause is TRUE, which means every clause has a TRUE literal. By selecting a corresponding vertex to a TRUE literal in each clause, a clique in G of size k is constructed, where k is the number of clauses of the 3-SAT instance. Because, if there is a missing edge, that would mean that our truth assignment effectively set something to be true and false at the same time (Y 1 and ¬Y 1 )!
-
If G-has k-clique, F is satisfiable: Assume that there is a clique of size k in G where k is the number of clauses in F. It is to be proved that there must be a truth assignment that satisfies the given Boolean formula. Clique is a complete graph. Let us assume that the truth assignment induced by the labels of the vertices satisfies F. It signifies that every pair of vertices in the clique has an edge. But the vertices labelling the literals may be set to both true and false. Already it is mentioned that every trio of vertices corresponding to a clause of F have no edges between those vertices, which signifies that there must be a vertex from every clause of F in the clique. This shows that the clauses of F are satisfied as well as F is satisfied.
This reduction is possible in polynomial time. Hence, clique is NP complete.
-
-
Multiple Choice Questions
-
Worst case time complexity is denoted by the notation.
- Big oh notation
- Big omega notation
- Theta notation
- Little omega notation
-
Best case time complexity is denoted by the notation.
- Big oh notation
- Big omega notation
- Theta notation
- Little omega notation
-
If f(n) = O(g(n)) and f(n) = Ω(g(n)), then which is true?
- f(n) = ω(g(n))
- f(n) = o(g(n))
- f(n) = Θ(g(n))
- f(n) = θ(g(n))
-
The problem which results in ‘yes’ or ‘no’ is
- Decision problem
- Optimization problem
- Search problem
- Functional problem
-
Which type of problem is the shortest path algorithm?
- Decision problem
- Optimization problem
- Search problem
- Functional problem
-
Which is true for the P class problem?
- The number of steps (or time) required to complete the algorithm is a polynomial function of n.
- It is computable by a deterministic Turing machine in polynomial time.
- It contains all sets in which the membership may be decided by an algorithm whose running time is bounded by a polynomial.
- All of these
Answers:
1. a
2. b
3. c
4. a
5. b
6. d
GATE Questions
-
For problems X and Y, Y is NP complete and X reduces to Y in polynomial time. Which of the following is true?
- If X can be solved in polynomial time, then so can Y.
- X is NP complete
- X is NP hard
- X is in NP, but not necessarily NP complete.
-
Which of the problems is not NP hard?
- Hamiltonian circuit problem
- The 0/1 knapsack problem
- Finding bi-connected components of a graph
- The graph colouring problem
-
Ram and Shyam have been asked to show a certain problem ∏ is NP complete. Ram shows a polynomial time reduction from 3-SAT problem to ∏, and Shyam shows a polynomial time reduction from ∏ to 3 SAT. Which of the following can be inferred from these reductions?
- ∏ is NP hard but not NP complete
- ∏ is NP, but is not NP complete
- ∏ is NP complete
- ∏ is neither NP hard nor NP
-
No body knows yet if P = NP. Consider the language L defined as follows.
Which of the following statements is true?
- L is recursive
- L is recursively enumerable but not recursive
- L is not recursively enumerable
- Whether L is recursive or not will be known after we find out if P = NP
-
Consider two languages L 1 and L 2 , each on the alphabet Σ. Let f: Σ → Σ be a polynomial time computable bijection such that (∀x)[x ∈ L 1 iff f(x) ∈ L 2 ]. Further, let f –1 be also polynomial time computable. Which of the following cannot be true?
- L 1 ∈ P and L 2 is finite
- L 1 ∈ NP and L 2 ∈ P
- L 1 is undecidable and L 2 is decidable
- L 1 is recursively enumerable and L 2 is recursive
-
The problems 3-SAT and 2-SAT are
- Both in P
- Both NP complete
- NP complete and P
- Undecidable and NP complete, respectively
-
Consider the following two problems on undirected graphs:
- Given G(V, E), does G have an independent set of size V – 4?
- Given G(V, E), does G have an independent set of size 5?
Which one of the following is TRUE?
- (i) is in P and (ii) is NP complete
- (i) is NP complete and (ii) is in P
- Both (i) and (ii) are NP complete
- Both (i) and (ii) are in P
-
Let S be an NP-complete problem and Q and R be two other problems not known to be in NP. Q is polynomial time reducible to S and S is polynomial time reducible to R. Which one of the following statements is true?
- R is NP complete
- R is NP hard
- Q is NP complete
- Q is NP hard
-
Let SHAM 3 be the problem of finding a Hamiltonian cycle in a graph G = (V, E) with V divisible by 3 and DHAM 3 be the problem of determining if a Hamiltonian cycle exists in such graphs. Which one of the following is true?
- Both DHAM 3 and SHAM 3 are NP hard
- SHAM 3 is NP hard, but DHAM 3 is not
- DHAM 3 is NP hard, but SHAM 3 is not
- Neither DHAM 3 nor SHAM 3 is NP hard
-
Let ∏ A be a problem that belongs to the class NP. Then which one of the following is true?
- There is no polynomial time algorithm for ∏ A
- If ∏ A can be solved deterministically in polynomial time, then P = NP
- If ∏ A is NP hard, then it is NP complete.
- ∏ A may be undecidable.
-
Assuming P ≠ NP, which of the following is true?
- NP complete = NP
- NP complete ∩ P = Ø
- NP hard = NP
- P = NP complete
-
The recurrence relation capturing the optimal execution time of the tower of Hanoi problem with n discs is
- T(n) = 2T(n – 2) + 2
- T(n) = 2T(n –1) + n
- T(n) = 2T(n/2) + 1
- T(n) = 2T(n – 1) + 1
-
A list of n strings, each of length n, is sorted into lexicographic order using the merge-sort algorithm. The worst case running time of this computation is
- O(n log n)
- O(n 2 log n)
- (n 2 + log n)
- O(n 2 )
Answers:
- b
- c
- c
- a
- b
- c
- c
- c
- a
- a
- b
- d
- a
Hints:
4. There are two options (0 + 1)* or φ. Both are regular hence both are recursive.
Exercise
-
Determine O(n), Ω(n), and θ(n) of the following
- f(n) = 3n 2 + 2log n + 1
- f(n) = 5n 3 – 3n 2 + 5
- f(n) = 5n 3 – 3n 2 – 5
- f(n) = 3n – 3log n + 2
-
Find the O or the following recurrence relations
-
-
where n is a power of b
-
-
-
-
Find the time complexity of the following algorithms
-
bool IsFirstElementNull(String[ ] strings)
{
if(strings[0] == null)
{
return true;
}
return false;
}
-
for (int i = 0; i < N; i++)
for (int j = i + 1; j < N; j++)
if (A[i] > A[j])
swap(A[i], A[j])
-
-
Given a sorted array A, determine whether it contains two elements with the difference D. Find the time complexity of the algorithm.
-
Find the time complexity of heap sort operation.
-
Find the time complexity of the Lagrange interpolation polynomial.
-
Prove that the following problem is in NP.
Given integers n and k, is there a factor f with 1 < f < k and f dividing n?
-
Prove that the subset sum problem is NP complete.
-
A set of vertices inside a graph G is an independent set if there are no edges between any two of these vertices. Prove that for a given graph G and an integer k, ‘is G′ ⊆ G an independent set of size k?’ an NP complete problem. (Reduce it to 3 CNF or vertex cover.)
-
Prove that the vertex cover is NP complete by reducing the independent set problem to it.
-
Prove that the graph colouring problem is NP complete.