Chapter Two-Computational Complexity
Chapter Two-Computational Complexity
Computational Complexity
Overview
Complexity theory (from elementary Algorithm Analysis) is concerned with
analyzing a program (or its efficiency),
figuring out whether the program is correct,
figuring out whether one program is better than the other and
representing the program a quantified way.
From programming perspective, there are two main complexities:
Time complexity
determines how fast your code runs,
determines how much time it will take (depends on the number of steps).
is hardware-independent
Space Complexity:
the total space taken by the algorithm with respect to the input size.
Space complexity includes both Auxiliary space and space used by input.
o Auxiliary Space is the extra space or temporary space used by an algorithm.
Example of Complexity in Time (execution) and Space (memory) factors:
Example-1: More Complex
i = 1; 1s
while( i <= 10 ) 11s
{
a = 5; 10s
result = i * a; 10s
printf(“\n” /d”, result); 10s
i++; 10s
}
Here, we assume each variable is equal to 2 Bytes. In the above example we use three variables (i,
a, result) which is 6 Bytes.
Execution Time: 52s
Memory (Space): 6 Bytes
The field of computational complexity theory attempts to determine the amount of resources
(computational complexity) solving a given problem will require and explain why some problems
are intractable or undecidable.
Computational problems belong to complexity classes that define broadly the resources (e.g.
time, space/memory, energy, circuit depth) it takes to compute (solve) them with various abstract
machines. For example, the complexity class P for classical machines, and BQP for quantum
machines.
Computability theory: The branch of theory of computation that studies which problems are
computationally solvable using different model. In computer science, the computational complexity,
or simply complexity of an algorithm is the amount of resources required for running it.
Complexity Theory: is concerned with the study of the intrinsic complexity of computational tasks.
Its final goal includes the determination of the complexity of any well-defined task. Computer
science is devoted to complexity theory. Computer scientists use complexity theory to relate and
group problems together. In some cases, if one problem can be solved, it opens up a way to solve
other problems in the complexity class as well.
Unsolvable Problem:
An unsolvable problem is one for which no algorithm can ever be written to find the
solution.
Undecidable Problem: is one for which no algorithm can ever be written that will always give a
correct true/false decision for every input value.
Undecidable problems are a subcategory of unsolvable problems that include only problems
that should have a yes/no answer (such as: does my code have a bug?).
Turing Machine
Turing formulated a model of algorithm or computation that is widely accepted. The Church- Turing
thesis states that any algorithmic procedure that can be carried out by human beings/computer can be
carried out by a Turing machine. It has been universally accepted by computer scientists that the
Turing machine provides an ideal theoretical model of a computer.
For formalizing computability, Turing assumed that, while computing, a person writes symbols on a
one-dimensional tape which is divided into cells. One scans the cells one at a time and usually
performs one of the three simple operations, namely
i) writing a new symbol in the cell being currently scanned,
ii) moving to the cell left of the present cell and
iii) moving to the cell light of the present cell. With these observations in mind, Turing
proposed his 'computing machine.'
The Model
Formally, a TM (TM) is expressed as a 7-tuple (Q, T, B, ∑, δ, q0, F) where:
Q is a finite set of states
T is the tape alphabet (symbols which can be written on Tape)
B is blank symbol (every cell is filled with B except input alphabet initially)
∑ is the input alphabet (symbols which are part of input alphabet)
δ is a transition function which maps Q × T → Q × T × {L, R}. Depending on its present
state and present tape alphabet (pointed by head pointer), it will move to new state, change
the tape symbol (may or may not) and move head pointer to either left or right.
q0 is the initial state
F is the set of final states. If any state of F is reached, input string is accepted.
Illustration
Let us see how this TM works for 0011. Initially head points to 0 which is underlined and state is
q0 as:
The move will be δ(q0, 0) = (q1, X, R). It means, it will go to state q1, replace 0 by X and head
will move to right as:
The move will be δ(q1, 0) = (q1, 0, R) which means it will remain in same state and without
changing any symbol, it will move to right as:
The move will be δ(q1, 1) = (q2, Y, L) which means it will move to q2 state and changing 1 to Y,
it will move to left as:
Working on it in the same way, the machine will reach state q3 and head will point to B as shown:
The halting problem is a decision problem about properties of computer programs on a fixed Turing-
complete model of computation, i.e., all programs that can be written in some given programming
language that is general enough to be equivalent to a Turing machine. The problem is to determine,
given a program and an input to the program, whether the program will eventually halt when run
with that input. In this abstract framework, there are no resource limitations on the amount of
memory or time required for the program's execution; it can take arbitrarily long and use an arbitrary
amount of storage space before halting. The question is simply whether the given program will ever
halt on a particular input.
While deciding whether these programs halt is simple, more complex programs prove problematic.
One approach to the problem might be to run the program for some number of steps and check if it
halts. But if the program does not halt, it is unknown whether the program will eventually halt or run
forever. Turing proved no algorithm exists that always correctly decides whether, for a given
arbitrary program and input, the program halts when run with that input. The essence of Turing's
proof is that any such algorithm can be made to contradict itself and therefore cannot be correct.
The answer is no we cannot design a generalized algorithm which can appropriately say that given a
program will ever halt or not?
The only way is to run the program and check whether it halts or not. We can refrain the halting
problem question in such a way also: Given a program written in some programming
language(c/c++/java) will it ever get into an infinite loop (loop never stops) or will it always
terminate(halt)?
This is an undecidable problem because we cannot have an algorithm which will tell us whether a
given program will halt or not in a generalized way i.e by having specific program/algorithm. In
general we can’t always know that’s why we can’t have a general algorithm. The best possible way
is to run the program and see whether it halts or not. In this way for many programs we can see that
it will sometimes loop and always halt.
Proof by Contradiction –
Problem statement: Can we design a machine which if given a program can find out if that
program will always halt or not halt on a particular input?
Solution: Let us assume that we can design that kind of machine called as HM(P, I) where HM is
the machine/program, P is the program and I is the input. On taking input the both arguments the
machine HM will tell that the program P either halts or not.
If we can design such a program this allows us to write another program we call this program
CM(X) where X is any program(taken as argument) and according to the definition of the program
CM(X) shown in the figure.
In the program CM(X) we call the function HM(X), which we have already defined and to HM() we
pass the arguments (X, X), according to the definition of HM() it can take two arguments i.e one is
program and another is the input. Now in the second program we pass X as a program and X as input
to the function HM().We know that the program HM() gives two output either “Halt” or “Not Halt”.
But in case second program, when HM(X, X) will halt loop body tells to go in loop and when it
doesn’t halt that means loop, it is asked to return.
It is impossible for the outer function to halt if its code (inner body) is in loop and also it is
impossible for the outer non halting function to halt even after its inner code is halting. So both
conditions result in non halting for the CM machine/program even we had assumed in the beginning
that it would halt. So this is the contradiction and we can say that our assumption was wrong and this
problem, i.e., halting problem is undecidable.
Hence, we proved that the halting problem is undecidable.
Example 2: A single tape TM M has two states q0 and q1, of which q0 is the starting state. The
tape alphabet of M is {0, 1, B} and its input alphabet is {0, 1}. The symbol B is the blank symbol
used to indicate end of an input string. The transition function of M is described in the following
table.
The table is interpreted as illustrated below. The entry (q1, 1, R) in row q0 and column 1 signifies
that if M is in state q0 and reads 1 on the current tape square, then it writes 1 on the same tape
square, moves its tape head one position to the right and transitions to state q1. Which of the
following statements is true about M?
Using δ(q0, 1) = (q1, 1, R), it will move to state q1 and head will move to right as:
Using δ(q1, B) = (q0, B, L), it will move to state q0 and head will move to left as:
It will run in the same way again and again and not halt.
Option D says M halts on all string ending with 1, but it is not halting for 1. So, option D is
incorrect.
Let us see whether machine halts on string ‘0’. Initially state will be q0, head will point to 1 as:
Using δ(q0, 0) = (q1, 1, R), it will move to state q1 and head will move to right as:
Using δ(q1,B)=(q0,B,L), it will move to state q0 and head will move to left as:
It will run in the same way again and again and not halt.
Option C says M halts on all string ending with 0, but it is not halting for 0. So, option C is
incorrect.
Option B says that TM does not halt for any string (00 + 1)*. But NULL string is a part of
(00 + 1)* and TM will halt for NULL string. For NULL string, tape will be,
Using δ(q0, B) = halt, TM will halt. As TM is halting for NULL, this option is also incorrect.
So, option (A) is correct.
Input : 0 0 c 0 0 0 // 2 + 3
Output : 0 0 0 0 0 // 5
Input : 0 0 0 0 c 0 0 0 // 4 + 3
Output : 0 0 0 0 0 0 0 // 7
Approach used
Convert a 0 in the first number in to X and then traverse entire input and convert the first blank
encountered into 0. Then move towards left ignoring all 0’s and “c”. Come the position just next to
X, and repeat the same procedure until the time we get a “c” instead of X on returning. Convert the c
into blank and addition is completed.
Steps –
Step-1: Convert 0 into X and go to step 2. If symbol is “c” then convert it into blank(B), move right
and go to step 6.
Step-2: Keep ignoring 0’s and move towards right. Ignore “c”, move right and go to step 3.
Step-3: Keep ignoring 0’s and move towards right. Convert a blank (B) into 0, move left and go to
step 4.
Step-4: Keep ignoring 0’s and move towards left. Ignore “c”, move left and go to step 3.
Step-5: Keep ignoring 0’s and move towards left. Ignore an X, move right and go to step 1.
Step-6: End.
Steps:
Step-1. If 0 found convert 0 into X and go right then convert all 0’s into 0’s and go right.
Step-2. Then convert C into C and go right then convert all X into X and go right.
Step-3. Then convert 0 into X and go left then convert all X into X and go left.
Step-4. Then convert C into C and go left then convert all 0’s into 0’s and go left then
convert all X into X and go right and repeat the whole process.
Step-5. Otherwise if C found convert C into C and go right then convert all X into B and go
right then convert 0 into 0 and go left and then stop the machine.
Here, q0 shows the initial state and q1, q2, q3, q4, q5are the transition states and q6 shows the final
state.
And X, 0, C are the variables used for subtraction and R, L shows right and left.
Example:
Steps:
Step-1. First ignore 0’s, C and go to right & then if B found convert it into C and go to left.
Step-2. Then ignore 0’s and go left & then convert C into C and go right.
Step-3. Then convert all X into X and go right if 0 found convert it into X and go to left
otherwise if C found convert it into B and go to right and stop the machine.
Step-4. If then X found convert it into X and go left then C into C and left then Y into Y and
left.
Step-5. Then if B found convert it into B and right then if Y into 0 and right or if C into C
and right and go to step 3 and repeat the process otherwise if 0 found after 4th step then
convert it into Y and right then Y into Y and right then C into C and right then 0 into 0 or X
into X and right then C into C and right then 0 into 0 and right then B into 0 and left then 0
into 0 and left then C into C and left then 0 into 0 or X into X and left then C into C and left.
Step-6. Then repeat the 5th step.
Solution: The language L = {wwR | w ∈ {0, 1}} represents a language where only 2 characters, i.e.,
0 and 1 are employed. The first part of language can be any string of 0 and 1. The second part is the
reverse of the first part. Combining both parts out string will be formed. Any such string which fall
in this category will be accepted by this language. The beginning and end of string is marked by the
B sign.
For example, if first part w = 1 1 0 0 1 then second part wR = 1 0 0 1 1. It is clearly visible that wR is
the reverse of w, so the string 1 1 0 0 1 1 0 0 1 1 is a part of given language.
Examples –
Input : 0 0 1 1 1 1 0 0
Output : Accepted
Input : 1 0 1 0 0 1 0 1
Output : Accepted
Basic Representation –
Example – Lets understand it with the help of an example. Lets string 1 0 1 1 0 1, so w = 1 0 1 and
string is of form (ww).
The first thing that we do is to find the midpoint. For this, we convert 1 in the beginning into Y and
move right till the end of the string. Here we convert 1 into y.
Now our string would look like Y 0 1 1 0 Y. Now move left till, find a X or Y. When we do so,
convert the 0 or 1 right of it to X or Y respectively and then do the same on the right end. Now our
string would look like Y X 1 1 X Y. Thereafter, convert these 1’s also and finally it would look like
Y X Y Y X Y.
At this point, you have achieved the first objective which was to find the midpoint. Now convert all
X and Y on the left of midpoint into 0 and 1 respectively, so string becomes 1 0 1 Y X Y. Now,
convert the 1 into Y and move right till, find Y in the beginning of the right part of the string and
convert this Y into a blank (denoted by B). Now string looks like Y 0 1 B X Y.
Similarly, apply this on 0 and x followed by 1 and Y. After this string looks like Y X Y B B B. Now
that you have no 0 and 1 and all X and Y on the right part of the string are converted into blanks so
our string will be accepted.
Input : 1 1 0 0 1 1 0 0
Output : Accepted
Input : 1 0 1 1 1 0 1
Output : Not accepted
Step-1:
If the symbol is 0 replace it by X and move right
If the symbol is 1 replace it by Y and move right,
Go to state Q1 and step 2
———————————————
If the symbol is X replace it by X and move left or
If the symbol is Y replace it by Y and move left,
Go to state Q4 and step 5
Step-2:
If the symbol is 0 replace it by 0 and move right, remain on the same state
If the symbol is 1 replace it by 1 and move right, remain on the same state
———————————————
Step-3:
If symbol is 0 replace it by X and move left, or
If symbol is 1 replace it by Y and move left,
Go to state Q3 and step 4
Step-4:
If the symbol is 0 replace it by 0 and move left, remain on the same state
If the symbol is 1 replace it by 1 and move left, remain on the same state
———————————————
If the symbol is X replace it by X and move right or
If the symbol is Y replace it by Y and move right,
Go to state Q0 and step 1
Step-5:
If symbol is X replace it by X and move left or
If symbol is Y replace it by Y and move left
Go to state Q4 and step 6
Step-6:
If symbol is X replace it by 0 and move left, remain on same state
If symbol is Y replace it by 1 and move left, remain on same state
–––––––––—–––––––––—
If symbol is $ replace it by $ and move right
Go to state Q4 and step 7
Step-7:
If symbol is 0 replace it by X and move right, go to state Q6 and step 8
If symbol is 1 replace it by Y and move right, go to state Q7 and step 9
–––––––––—–––––––––—
If symbol is B replace it by B and move left, STRING ACCEPTED, GO TO FINAL STATE
Q9
Step-8:
If symbol is 0 replace it by 0 and move right, remain on same state
If symbol is 1 replace it by 1 and move right, remain on same state
If symbol is B replace it by B and move right, remain on same state
Step-9:
Step-10:
If symbol is 0 replace it by 0 and move left, remain on same state
If symbol is 1 replace it by 1 and move left, remain on same state
If symbol is B replace it by B and move left, remain on same state
–—––––––––––––––––––
If symbol is Y replace it by Y and move right or
If symbol is X replace it by X and move right
Go to state Q5 and step 7
In this the input tape is of finite length In this the input tape is of finite length from left but is of
from both left and right side. infinite length from right.
Exercise: Use the template in the table above to compare PDAs and TMs