Professional Documents
Culture Documents
• In NDFA, for a particular input symbol, the machine can move to any
combination of the states in the machine
In other words, the exact state to which the machine moves cannot be
determined
Hence, it is called Non-deterministic Automaton
As it has finite number of states, the machine is called Non-deterministic Finite
Machine or Non-deterministic Finite Automaton
Non-deterministic Finite Automaton
• Formal Definition of an NDFA:
• An NDFA can be represented by a 5-tuple (Q, ∑, δ, q0, F) where:
Q is a finite set of states
∑ is a finite set of symbols called the alphabets
δ is the transition function where δ: Q × ∑ → 2Q
(Here the power set of Q (2Q) has been taken because in case of NDFA, from a state,
transition can occur to any combination of Q states)
q0 is the initial state from where any input is processed (q0 ∈ Q)
F is a set of final state/states of Q (F ⊆ Q)
Graphical Representation of an NDFA: (same as DFA)
• An NDFA is represented by digraphs called state diagram
The vertices represent the states
The arcs labelled with an input alphabet show the transitions
The initial state is denoted by an empty single incoming arc
The final state is indicated by double circles
• Example: Let a non-deterministic finite automaton be :
• Q = {a, b, c}
Present State Next State for Input 0 Next State for Input 1
• ∑ = {0, 1}
a a, b b
• q0 = {a}
• F = {c} b c a, c
c b, c c
• The transition function δ as shown in the Table
Graphical Representation of an NDFA: (same as DFA)
• Its graphical representation would be as follows −
DFA vs NDFA
Some key differences between DFA and NDFA:
DFA NDFA
The transition from a state is to a single particular The transition from a state can be to multiple next
next state for each input symbol states for each input symbol
Hence it is called deterministic Hence it is called non-deterministic
Empty string transitions are not seen in DFA NDFA permits empty string transitions
A string is accepted by a DFA, if it transits to a final A string is accepted by a NDFA, if at least one of all
state possible transitions ends in a final state
Applications: Acceptors, Classifiers and Transducers
• Acceptor (Recognizer)
An automaton that computes a Boolean function is called an acceptor
All the states of an acceptor is either accepting or rejecting the inputs given to it
• Classifier
A classifier has more than two final states and it gives a single output when it
terminates
• Transducer
An automaton that produces outputs based on current input and/or previous
state is called a transducer
Transducers can be of two types:
Mealy Machine − The output depends both on the current state and the current
input
Moore Machine − The output depends only on the current state
Acceptability by DFA and NDFA
• A string is accepted by a DFA/NDFA iff the DFA/NDFA starting at the initial
state ends in an accepting state (any of the final states) after reading the
string wholly:
Output:
Maximum possible value = 240, by taking items of weight 10 and 20 kg and 2/3 fraction of 30
kg. Hence total price will be 60 + 100 + (2/3)(120) = 240
Fractional Knapsack Problem
• A brute-force solution would be to try all possible subset with all different
fraction but that will be too much time taking
• An efficient solution may make use of Greedy approach
The basic idea of the greedy approach is to calculate the ratio value/weight for each
item and sort the item on basis of this ratio
Then take the item with the highest ratio and add them until we can’t add the next item
as a whole and at the end add the next item as much as we can
Which will always be the optimal solution to this problem
• As main time taking step is sorting, the whole problem can be solved in O(n log n)
Travelling Salesman and Hamiltonian Cycle Problems
• A Hamiltonian cycle (also tour, circuit) :
A cycle visiting each vertex exactly once
Graphs are said to be Hamiltonian if they contain a Hamiltonian cycle
• A Hamiltonian path
A path visiting each vertex exactly once
• The decision problems ask whether a Hamiltonian cycle or path exists in
a given graph
• The goal of Travelling salesman problem (TSP):
Find the minimum Hamiltonian cycle (Min-HC) i.e., a cycle that visits each city
once and exactly once and incurs the least length, time or cost, etc
• It has been proven to be NP-complete - There is no exact polynomial algorithm
for TSP until NP=P
Polynomial-Time Algorithms
• Are some problems solvable in polynomial time?
Of course: many algorithms we’ve studied provide polynomial-time solutions to
some problems
• Are all problems solvable in polynomial time?
No: Turing’s “Halting Problem” is not solvable by any computer, no matter how
much time is given
• Most problems that do not yield polynomial-time algorithms are either
optimization or decision problems
Optimization/Decision Problems
• An optimization problem tries to find an optimal solution
• A decision problem tries to answer a yes/no question
• Many problems will have decision and optimization versions
Example: Travelling salesman problem
optimization: find hamiltonian cycle of minimum weight
decision: is there a hamiltonian cycle of weight ≤ k
• Some problems are decidable, but intractable:
As they grow large, we are unable to solve them in reasonable time
Is there a polynomial-time algorithm that solves the problem?
The Class P
• P:
The class of decision problems that have polynomial-time deterministic algorithms
That is, they are solvable in O(p(n)), where p(n) is a polynomial on n
A deterministic algorithm is (essentially) one that always computes the correct
answer
• Why polynomial?
if not, very inefficient
Nice closure properties: the sum and composition of two polynomials are always
polynomials too
Sample Problems in P
• Fractional Knapsack
• MST
• Sorting
• Others?
The class NP
• NP:
The class of decision problems that are solvable in polynomial time on a
nondeterministic machine (or with a nondeterministic algorithm)
A determinstic computer is what we know
A nondeterministic computer is one that can “guess” the right answer or solution
Think of a nondeterministic computer as a parallel machine that can freely spawn an infinite
number of processes
• Thus NP can also be thought of as:
The class of problems whose solutions can be verified in polynomial time
Note that NP stands for “Nondeterministic Polynomial-time”
The class NP
• If problem A is reducible to problem B it is no harder than B
• Conversely, if A is known to be hard then reducing it to B shows that B is at least as hard:
B serves as an upper bound on the complexity of A, while A serves as a lower bound on the complexity of B
• A problem is said to be NP-complete if any problem in the class NP can be reduced to it and it is
therefore as hard as any problem in NP
• The class of NP complete problems includes
Satisfiability (for Boolean expressions)
Hamiltonian path problem
Travelling salesman problem
• An NP-complete problem “represents” the class NP in the sense that its solution in polynomial
time implies P=NP
• The strong evidence that P is not equal to NP for a wide range of models of computing includes
the fact that no NP-complete problem has yet been solved in polynomial time
• Complexity metrics are remarkably robust with respect to alternative models of computation,
but if unlimited (exponential) parallelism is permitted, then P=NP
Review: P and NP Summary
• P = set of problems that can be solved in polynomial time
Examples: Fractional Knapsack, …
• NP = set of problems for which a solution can be verified in
polynomial time
Examples: TSP, Hamiltonian Cycle, ...
• Clearly P ⊆ NP
• Open question: Does P = NP?
Most suspect not
An August 2010 claim of proof that P ≠ NP, by Vinay Deolalikar, researcher at
HP Labs, Palo Alto, has flaws
Review: NP-complete Problems
• A decision problem D is NP-complete iff
1. D ϵ NP
2. Every problem in NP is polynomial-time reducible to D