Professional Documents
Culture Documents
LIST OF PRACTICALS
Practical 1.
This automaton consists of states and transitions. The State is represented by circles, and
the Transitions is represented by arrows.
Automata is the kind of machine which takes some string as input and this input goes through a
finite number of states and may enter in the final state.
There are the basic terminologies that are important and frequently used in automata:
Symbols:
Symbols are an entity or individual objects, which can be any letter, alphabet or any picture.
Example:
1, a, b, #
Alphabets:
Examples:
∑ = {a, b}
∑ = {A, B, C, D}
∑ = {0, 1, 2}
∑ = {0, 1, ....., 5]
∑ = {#, β, Δ}
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
String:
Example 1:
If ∑ = {a, b}, various string that can be generated from ∑ are {ab, aa, aaa, bb, bbb, ba, aba.....}.
Example 2:
w = 010
Number of Sting |w| = 3
Language:
Example: 1
L1 = {Set of string of length 2}
Example: 2
L2 = {Set of all strings starts with 'a'}
Transition Diagram
A transition diagram or state transition diagram is a directed graph which can be constructed as
follows:
Transition Table
The transition table is basically a tabular representation of the transition function. It takes two
arguments (a state and a symbol) and returns a state (the "next state").
Practical 2.
Formal Definition of FA
A finite automaton is a collection of 5-tuple (Q, ∑, δ, q0, F), where:
Input tape: It is a linear tape having some number of cells. Each input symbol is placed in each
cell.
Finite control: The finite control decides the next state on receiving particular input from input
tape. The tape reader reads the cells one by one from left to right, and at a time only one input
symbol is read.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
Types of Automata:
There are two types of finite automata:
1. DFA
DFA refers to deterministic finite automata. Deterministic refers to the uniqueness of the
computation. In the DFA, the machine goes to one state only for a particular input character.
DFA does not accept the null move.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
2. NFA
NFA stands for non-deterministic finite automata. It is used to transmit any number of states for
a particular input. It can accept the null move.
1. In DFA, the input to the automata can be any string. Now, put a pointer to the start state q and
read the input string w from left to right and move the pointer according to the transition
function, δ. We can read one symbol at a time. If the next symbol of string w is a and the pointer
is on state p, move the pointer to δ(p, a). When the end of the input string w is encountered, then
the pointer is on some state F.
2. The string w is said to be accepted by the DFA if r ∈ F that means the input string w is
processed successfully and the automata reached its final state. The string is said to be rejected
by DFA if r ∉ F.
Example 1:
DFA with ∑ = {0, 1} accepts all strings starting with 1.
Solution:
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
The finite automata can be represented using a transition graph. In the above diagram, the
machine initially is in start state q0 then on receiving input 1 the machine changes its state to q1.
From q0 on receiving 0, the machine changes its state to q2, which is the dead state. From q1 on
receiving input 0, 1 the machine changes its state to q1, which is the final state. The possible
input strings that can be generated are 10, 11, 110, 101, 111......., that means all string starts with
1.
Example 2:
NFA with ∑ = {0, 1} accepts all strings starting with 1.
Solution:
The NFA can be represented using a transition graph. In the above diagram, the machine initially
is in start state q0 then on receiving input 1 the machine changes its state to q1. From q1 on
receiving input 0, 1 the machine changes its state to q1. The possible input string that can be
generated is 10, 11, 110, 101, 111......, that means all string starts with 1.
o DFA refers to deterministic finite automata. Deterministic refers to the uniqueness of the
computation. The finite automata are called deterministic finite automata if the machine is read an
input string one symbol at a time.
o In DFA, there is only one path for specific input from the current state to the next state.
o DFA does not accept the null move, i.e., the DFA cannot change state without any input
character.
o DFA can contain multiple final states. It is used in Lexical Analysis in Compiler.
In the following diagram, we can see that from state q0 for input a, there is only one path which
is going to q1. Similarly, from q0, there is only one path for input b going to q2.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
δ: Q x ∑→Q
Skip Ad
Example 1:
Solution:
Transition Diagram:
Transition Table:
→q0 q0 q1
q1 q2 q1
*q2 q2 q2
Example 2:
DFA with ∑ = {0, 1} accepts all starting with 0.
Solution:
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
Explanation:
o In the above diagram, we can see that on given 0 as input to DFA in state q0 the DFA changes
state to q1 and always go to final state q1 on starting input 0. It can accept 00, 01, 000, 001....etc.
It can't accept any string which starts with 1, because it will never go to final state on a string
starting with 1.
Example 3:
DFA with ∑ = {0, 1} accepts all ending with 0.
Solution:
Explanation:
In the above diagram, we can see that on given 0 as input to DFA in state q0, the DFA changes
state to q1. It can accept any string which ends with 0 like 00, 10, 110, 100....etc. It can't accept
any string which ends with 1, because it will never go to the final state q1 on 1 input, so the
string ending with 1, will not be accepted or will be rejected.
o NFA stands for non-deterministic finite automata. It is easy to construct an NFA than DFA for a
given regular language.
o The finite automata are called NFA when there exist many paths for specific input from the
current state to the next state.
o Every NFA is not DFA, but each NFA can be translated into DFA.
o NFA is defined in the same way as DFA but with the following two exceptions, it contains
multiple next states, and it contains ε transition.
In the following image, we can see that from state q0 for input a, there are two next states q1 and
q2, similarly, from q0 for input b, the next states are q0 and q1. Thus it is not fixed or determined
that with a particular input where to go next. Hence this FA is called non-deterministic finite
automata.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
δ: Q x ∑ →2Q
where,
Example 1:
Solution:
Transition diagram:
Transition Table:
→q0 q0, q1 q1
q1 q2 q0
*q2 q2 q1, q2
In the above diagram, we can see that when the current state is q0, on input 0, the next state will
be q0 or q1, and on 1 input the next state will be q1. When the current state is q1, on input 0 the
next state will be q2 and on 1 input, the next state will be q0. When the current state is q2, on 0
input the next state is q2, and on 1 input the next state will be q1 or q2.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
Example 2:
NFA with ∑ = {0, 1} accepts all strings with 01.
Solution:
Transition Table:
→q0 q1 ε
q1 ε q2
*q2 q2 q2
Example 3:
NFA with ∑ = {0, 1} and accept all string of length atleast 2.
Solution:
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
Transition Table:
→q0 q1 q1
q1 q2 q2
*q2 ε ε
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
Practical 3.
Let, M = (Q, ∑, δ, q0, F) is an NFA which accepts the language L(M). There should be
equivalent DFA denoted by M' = (Q', ∑', q0', δ', F') such that L(M) = L(M').
Step 2: Add q0 of NFA to Q'. Then find the transitions from this start state.HTML Tutorial
Step 3: In Q', find the possible set of states for each input symbol. If this set of states is not in Q',
then add it to Q'.
Step 4: In DFA, the final state will be all the states which contain F(final states of NFA)
Example 1:
Convert the given NFA to DFA.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
Solution: For the given transition diagram we will first construct the transition table.
State 0 1
→q0 q0 q1
q1 {q1, q2} q1
δ'([q0], 0) = [q0]
δ'([q0], 1) = [q1]
δ'([q2], 0) = [q2]
δ'([q2], 1) = [q1, q2]
The state [q1, q2] is the final state as well because it contains a final state q2. The transition table
for the constructed DFA will be:
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
State 0 1
Example 2:
Convert the given NFA to DFA.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
Solution: For the given transition diagram we will first construct the transition table.
State 0 1
δ'([q1], 0) = ϕ
δ'([q1], 1) = [q0, q1]
Similarly,
As in the given NFA, q1 is a final state, then in DFA wherever, q1 exists that state becomes a
final state. Hence in the DFA, final states are [q1] and [q0, q1]. Therefore set of final states F =
{[q1], [q0, q1]}.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
State 0 1
Suppose
A = [q0]
B = [q1]
C = [q0, q1]
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
Practical 4.
To Study Mealy & Moore Machine.
Finite automata may have outputs corresponding to each transition. There are two types of finite
state machines that generate output −
Mealy Machine
Moore machine
Mealy Machine
A Mealy Machine is an FSM whose output depends on the present state as well as the present
input.
It can be described by a 6 tuple (Q, ∑, O, δ, X, q0) where −
Q is a finite set of states.
∑ is a finite set of symbols called the input alphabet.
O is a finite set of symbols called the output alphabet.
δ is the input transition function where δ: Q × ∑ → Q
X is the output transition function where X: Q × ∑ → O
q0 is the initial state from where any input is processed (q0 ∈ Q).
Next state
Present state
input = 0 input = 1
→a b x1 c x1
b b x2 d x3
c d x3 c x1
d d x3 d x2
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
Moore Machine
Moore machine is an FSM whose outputs depend on only the present state.
A Moore machine can be described by a 6 tuple (Q, ∑, O, δ, X, q0) where −
Q is a finite set of states.
∑ is a finite set of symbols called the input alphabet.
O is a finite set of symbols called the output alphabet.
δ is the input transition function where δ: Q × ∑ → Q
X is the output transition function where X: Q → O
q0 is the initial state from where any input is processed (q0 ∈ Q).
The state table of a Moore Machine is shown below −
Next State
Present state
Output
Input = 0 Input = 1
→a b c x2
b b d x1
c c d x2
d d d x3
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
Output depends both upon the present state and the Output depends only upon the present state.
present input
Generally, it has fewer states than Moore Machine. Generally, it has more states than Mealy Machine.
The value of the output function is a function of the The value of the output function is a function of the
transitions and the changes, when the input logic current state and the changes at the clock edges,
on the present state is done. whenever state changes occur.
Mealy machines react faster to inputs. They In Moore machines, more logic is required to decode
generally react in the same clock cycle. the outputs resulting in more circuit delays. They
generally react one clock cycle later.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
Example
Let us consider the following Moore machine –
Next State
Present State Output
a=0 a=1
→a d b 1
b a d 0
c c c 0
d b a 1
Step 1 & 2 −
Next State
→a d b
b a d
c c c
d b a
Step 3 −
Next State
=> a d 1 b 0
b a 1 d 1
c c 0 c 0
d b 0 a 1
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
Example
Let us consider the following Mealy Machine –
Next State
→a d 0 b 1
b a 1 d 0
c c 1 c 0
d b 0 a 1
Here, states ‘a’ and ‘d’ give only 1 and 0 outputs respectively, so we retain states ‘a’ and ‘d’.
But states ‘b’ and ‘c’ produce different outputs (1 and 0). So, we divide b into b0,
b1 and c into c0, c1.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
Next State
Present State Output
a=0 a=1
→a d b1 1
b0 a d 0
b1 a d 1
c0 c1 C0 0
c1 c1 C0 1
d b0 a 0
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
Practical 5.
To Study the Conversion of DFA to Regular Expression.
In order to find out a regular expression of a Finite Automaton, we use Arden’s Theorem along
with the properties of regular expressions.
Statement −
Let P and Q be two regular expressions.
If P does not contain null string, then R = Q + RP has a unique solution that is R = QP*
Proof −
R = Q + (Q + RP)P [After putting the value R = Q + RP]
= Q + QP + RPP
When we put the value of R recursively again and again, we get the following equation −
R = Q + QP + QP2 + QP3…..
R = Q (ε + P + P2 + P3 + …. )
R = QP* [As P* represents (ε + P + P2 + P3 + ….) ]
Hence, proved.
Method
Step 1 − Create equations as the following form for all the states of the DFA having n states
with initial state q1.
q1 = q1R11 + q2R21 + … + qnRn1 + ε
q2 = q1R12 + q2R22 + … + qnRn2
…………………………
…………………………
…………………………
…………………………
qn = q1R1n + q2R2n + … + qnRnn
Rij represents the set of labels of edges from qi to qj, if no such edge exists, then Rij = ∅
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
Step 2 − Solve these equations to get the equation for the final state in terms of Rij
Problem
Construct a regular expression corresponding to the automata given below −
Solution −
Here the initial state and final state is q1.
The equations for the three states q1, q2, and q3 are as follows −
q1 = q1a + q3a + ε (ε move is because q1 is the initial state0
q2 = q1b + q2b + q3b
q3 = q2a
Now, we will solve these three equations −
q2 = q1b + q2b + q3b
= q1b + q2b + (q2a)b (Substituting value of q3)
= q1b + q2(b + ab)
= q1b (b + ab)* (Applying Arden’s Theorem)
q1 = q1a + q3a + ε
= q1a + q2aa + ε (Substituting value of q3)
= q1a + q1b(b + ab*)aa + ε (Substituting value of q2)
= q1(a + b(b + ab)*aa) + ε
= ε (a+ b(b + ab)*aa)*
= (a + b(b + ab)*aa)*
Hence, the regular expression is (a + b(b + ab)*aa)*.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
Problem
Construct a regular expression corresponding to the automata given below −
Solution −
Here the initial state is q1 and the final state is q2
Now we write down the equations −
q1 = q10 + ε
q2 = q11 + q20
q3 = q21 + q30 + q31
Now, we will solve these three equations −
q1 = ε0* [As, εR = R]
So, q1 = 0*
q2 = 0*1 + q20
So, q2 = 0*1(0)* [By Arden’s theorem]
Hence, the regular expression is 0*10*.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
Practical 6.
We have to follow the various steps to minimize the DFA. These are as follows:
Step 1: Remove all the states that are unreachable from the initial state via any set of the
transition of DFA.
Step 3: Now split the transition table into two tables T1 and T2. T1 contains all final states, and
T2 contains non-final states.
1. δ (q, a) = p
2. δ (r, a) = p
That means, find the two states which have the same value of a and b and remove one of them.
Step 5: Repeat step 3 until we find no similar rows available in the transition table T1.
Step 7: Now combine the reduced T1 and T2 tables. The combined transition table is the
transition table of minimized DFA.
Example:
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
Solution:
Step 1: In the given DFA, q2 and q4 are the unreachable states so remove them.
Step 2: Draw the transition table for the rest of the states.
State 0 1
→q0 q1 q3
q1 q0 q3
*q3 q5 q5
*q5 q5 q5
Step 3: Now divide rows of transition table into two sets as:
1. One set contains those rows, which start from non-final states:
State 0 1
q0 q1 q3
q1 q0 q3
2. Another set contains those rows, which starts from final states.
State 0 1
q3 q5 q5
q5 q5 q5
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
Step 5: In set 2, row 1 and row 2 are similar since q3 and q5 transit to the same state on 0 and 1.
So skip q5 and then replace q5 by q3 in the rest.
State 0 1
q3 q3 q3
State 0 1
→q0 q1 q3
q1 q0 q3
*q3 q3 q3
Practical 7.
o Pushdown automata is a way to implement a CFG in the same way we design DFA for a regular
grammar. A DFA can remember a finite amount of information, but a PDA can remember an
infinite amount of information.
o Pushdown automata is simply an NFA augmented with an "external stack memory". The addition
of stack is used to provide a last-in-first-out memory management capability to Pushdown
automata. Pushdown automata can store an unbounded amount of information on the stack. It can
access a limited amount of information on the stack. A PDA can push an element onto the top of
the stack and pop off an element from the top of the stack. To read an element into the stack, the
top elements must be popped off and are lost.
o A PDA is more powerful than FA. Any language which can be acceptable by FA can also be
acceptable by PDA. PDA also accepts a class of language which even cannot be accepted by FA.
Thus PDA is much more superior to FA.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
PDA Components:
Input tape: The input tape is divided in many cells or symbols. The input head is read-only and
may only move from left to right, one symbol at a time.
Finite control: The finite control has some pointer which points the current symbol which is to
be read.
Stack: The stack is a structure in which we can push and remove the items from one end only. It
has an infinite size. In PDA, the stack is used to store the items temporarily.
Γ: a stack symbol which can be pushed and popped from the stack
δ: mapping function which is used for moving from current state to next state.
Turnstile Notation:
⊢ sign describes the turnstile notation and represents one move.
For example,
(p, b, T) ⊢ (q, w, α)
In the above example, while taking a transition from state p to q, the input symbol 'b' is
consumed, and the top of the stack 'T' is represented by a new string α.
Example 1:
Design a PDA for accepting a language {anb2n | n>=1}.
Solution: In this language, n number of a's should be followed by 2n number of b's. Hence, we
will apply a very simple logic, and that is if we read single 'a', we will push two a's onto the
stack. As soon as we read 'b' then for every single 'b' only one 'a' should get popped from the
stack.
Now when we read b, we will change the state from q0 to q1 and start popping corresponding 'a'.
Hence,
δ(q0, b, a) = (q1, ε)
Thus this process of popping 'b' will be repeated unless all the symbols are read. Note that
popping action occurs in state q1 only.
δ(q1, b, a) = (q1, ε)
After reading all b's, all the corresponding a's should get popped. Hence when we read ε as input
symbol then there should be nothing in the stack. Hence the move will be:
δ(q1, ε, Z) = (q2, ε)
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
Where
PDA = ({q0, q1, q2}, {a, b}, {a, Z}, δ, q0, Z, {q2})
Now we will simulate this PDA for the input string "aaabbbbbb".
Example 2:
Design a PDA for accepting a language {0n1m0n | m, n>=1}.
Solution: In this PDA, n number of 0's are followed by any number of 1's followed n number of
0's. Hence the logic for design of such PDA will be as follows:
Push all 0's onto the stack on encountering first 0's. Then if we read 1, just do nothing. Then read
0, and on each read of 0, pop one 0 from the stack.
For instance:
Now we will simulate this PDA for the input string "0011100".
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
Practical 8.
The mapping function shows the mapping from states of finite automata and input symbol on the
tape to the next states, external symbols and the direction for moving the tape head. This is
known as a triple or a program for turing machine.
(q0, a) → (q1, A, R)
That means in q0 state, if we read symbol 'a' then it will go to state q1, replaced a by X and move
ahead right(R stands for right).
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
Example:
Construct TM for the language L ={0n1n} where n>=1.
Solution:
We have already solved this problem by PDA. In PDA, we have a stack to remember the
previous symbol. The main advantage of the Turing machine is we have a tape head which can
be moved forward or backward, and the input tape can be scanned.
The simple logic which we will apply is read out each '0' mark it by A and then move ahead
along with the input tape and find out 1 convert it to B. Now, repeat this process for all a's and
b's.
Now we will see how this turing machine work for 0011.
Now, we will see how this turing machine will works for 0011. Initially, state is q0 and head
points to 0 as:
The move will be δ(q0, 0) = δ(q1, A, R) which means it will go to state q1, replaced 0 by A and
head will move to the right as:
The move will be δ(q1, 0) = δ(q1, 0, R) which means it will not change any symbol, remain in
the same state and move to the right as:
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
The move will be δ(q1, 1) = δ(q2, B, L) which means it will go to state q2, replaced 1 by B and
head will move to left as:
Now move will be δ(q2, 0) = δ(q2, 0, L) which means it will not change any symbol, remain in
the same state and move to left as:
The move will be δ(q2, A) = δ(q0, A, R), it means will go to state q0, replaced A by A and head
will move to the right as:
The move will be δ(q0, 0) = δ(q1, A, R) which means it will go to state q1, replaced 0 by A, and
head will move to right as:
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
The move will be δ(q1, B) = δ(q1, B, R) which means it will not change any symbol, remain in
the same state and move to right as:
The move will be δ(q1, 1) = δ(q2, B, L) which means it will go to state q2, replaced 1 by B and
head will move to left as:
The move δ(q2, B) = (q2, B, L) which means it will not change any symbol, remain in the same
state and move to left as:
Now immediately before B is A that means all the 0?s are market by A. So we will move right to
ensure that no 1 is present. The move will be δ(q2, A) = (q0, A, R) which means it will go to
state q0, will not change any symbol, and move to right as:
The move δ(q0, B) = (q3, B, R) which means it will go to state q3, will not change any symbol,
and move to right as:
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
The move δ(q3, B) = (q3, B, R) which means it will not change any symbol, remain in the same
state and move to right as:
The move δ(q3, Δ) = (q4, Δ, R) which means it will go to state q4 which is the HALT state and
HALT state is always an accept state for any TM.
1. The input tape is having an infinite number of cells, each cell containing one input symbol and
thus the input string can be placed on tape. The empty tape is filled by blank characters.
2. The finite control and the tape head which is responsible for reading the current input symbol.
The tape head can move to left to right.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
4. Finite set of symbols called external symbols which are used in building the logic of turing
machine.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
Practical 9.
To Study P & NP Problems
In Computer Science, many problems are solved where the objective is to maximize or
minimize some values, whereas in other problems we try to find whether there is a solution or
not. Hence, the problems can be categorized as follows −
Optimization Problem
Optimization problems are those for which the objective is to maximize or minimize some
values. For example,
Finding the minimum number of colors needed to color a given graph.
Finding the shortest path between two vertices in a graph.
Decision Problem
There are many problems for which the answer is a Yes or a No. These types of problems are
known as decision problems. For example,
Whether a given graph can be colored by only 4-colors.
Finding Hamiltonian cycle in a graph is not a decision problem, whereas checking a
graph is Hamiltonian or not is a decision problem.
What is Language?
Every decision problem can have only two answers, yes or no. Hence, a decision problem may
belong to a language if it provides an answer ‘yes’ for a specific input. A language is the totality
of inputs for which the answer is Yes. Most of the algorithms discussed in the previous chapters
are polynomial time algorithms.
For input size n, if worst-case time complexity of an algorithm is O(nk), where k is a constant,
the algorithm is a polynomial time algorithm.
Algorithms such as Matrix Chain Multiplication, Single Source Shortest Path, All Pair Shortest
Path, Minimum Spanning Tree, etc. run in polynomial time. However there are many problems,
such as traveling salesperson, optimal graph coloring, Hamiltonian cycles, finding the longest
path in a graph, and satisfying a Boolean formula, for which no polynomial time algorithms is
known. These problems belong to an interesting class of problems, called the NP-
Complete problems, whose status is unknown.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
P-Class
The class P consists of those problems that are solvable in polynomial time, i.e. these problems
can be solved in time O(nk) in worst-case, where k is constant.
These problems are called tractable, while others are called intractable or super polynomial.
Formally, an algorithm is polynomial time algorithm, if there exists a polynomial p(n) such that
the algorithm can solve any instance of size n in a time O(p(n)).
Problem requiring Ω(n50) time to solve are essentially intractable for large n. Most known
polynomial time algorithm run in time O(nk) for fairly low value of k.
The advantages in considering the class of polynomial-time algorithms is that all
reasonable deterministic single processor model of computation can be simulated on each
other with at most a polynomial slow-d
NP-Class
The class NP consists of those problems that are verifiable in polynomial time. NP is the class
of decision problems for which it is easy to check the correctness of a claimed answer, with the
aid of a little extra information. Hence, we aren’t asking for a way to find a solution, but only to
verify that an alleged solution really is correct.
Every problem in this class can be solved in exponential time using exhaustive search.
P versus NP
Every decision problem that is solvable by a deterministic polynomial time algorithm is also
solvable by a polynomial time non-deterministic algorithm.
All problems in P can be solved with polynomial time algorithms, whereas all problems in NP -
P are intractable.
It is not known whether P = NP. However, many problems are known in NP with the property
that if they belong to P, then it can be proved that P = NP.
If P ≠ NP, there are problems in NP that are neither in P nor in NP-Complete.
The problem belongs to class P if it’s easy to find a solution for the problem. The problem
belongs to NP, if it’s easy to check a solution that may have been very tedious to find.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE
A problem is in the class NPC if it is in NP and is as hard as any problem in NP. A problem
is NP-hard if all problems in NP are polynomial time reducible to it, even though it may not be
in NP itself.
If a polynomial time algorithm exists for any of these problems, all problems in NP would be
polynomial time solvable. These problems are called NP-complete. The phenomenon of NP-
completeness is important for both theoretical and practical reasons.
Definition of NP-Completeness
A language B is NP-complete if it satisfies two conditions
B is in NP
Every A in NP is polynomial time reducible to B.
If a language satisfies the second property, but not necessarily the first one, the language B is
known as NP-Hard. Informally, a search problem B is NP-Hard if there exists some NP-
Complete problem A that Turing reduces to B.
The problem in NP-Hard cannot be solved in polynomial time, until P = NP. If a problem is
proved to be NPC, there is no need to waste time on trying to find an efficient algorithm for it.
Instead, we can focus on design approximation algorithm.
NP-Complete Problems
Following are some NP-Complete problems, for which no polynomial time algorithm is known.
NP-Hard Problems
The following problems are NP-Hard
TSP is NP-Complete
The traveling salesman problem consists of a salesman and a set of cities. The salesman has to
visit each one of the cities starting from a certain one and returning to the same city. The
challenge of the problem is that the traveling salesman wants to minimize the total length of the
trip
Proof
To prove TSP is NP-Complete, first we have to prove that TSP belongs to NP. In TSP, we find
a tour and check that the tour contains each vertex once. Then the total cost of the edges of the
tour is calculated. Finally, we check if the cost is minimum. This can be completed in
polynomial time. Thus TSP belongs to NP.
Secondly, we have to prove that TSP is NP-hard. To prove this, one way is to show
that Hamiltonian cycle ≤p TSP (as we know that the Hamiltonian cycle problem is
NPcomplete).
Assume G = (V, E) to be an instance of Hamiltonian cycle.
Hence, an instance of TSP is constructed. We create the complete graph G' = (V, E'), where
E’={(i,j):i,j∈V andi≠j
Thus, the cost function is defined as follows −
t(i,j)= 0 if(i,j)∈E
1 otherwise
Now, suppose that a Hamiltonian cycle h exists in G. It is clear that the cost of each edge
in h is 0 in G' as each edge belongs to E. Therefore, h has a cost of 0 in G'. Thus, if graph G has
a Hamiltonian cycle, then graph G' has a tour of 0 cost.
Conversely, we assume that G' has a tour h' of cost at most 0. The cost of edges
in E' are 0 and 1 by definition. Hence, each edge must have a cost of 0 as the cost of h' is 0. We
therefore conclude that h' contains only edges in E.
We have thus proven that G has a Hamiltonian cycle, if and only if G' has a tour of cost at
most 0. TSP is NP-complete.