Flat M2
Flat M2
– 2M
A derivation tree (also called a parse tree) is a graphical representation of the derivation of a
string using a context-free grammar (CFG). It visually represents how the start symbol of a CFG
derives a particular string by applying production rules step by step. The root of the tree is the
start symbol, and each internal node represents a non-terminal. The children of a node
represent the symbols produced by a production applied to that non-terminal.
Example:
Grammar:
S → aSb | ε
Explanation:
● The derivation tree shows the hierarchy and order of rule applications clearly.
Leftmost Derivation:
S
⇒ aSb
⇒ aaSbb
⇒ aaεbb
⇒ aabb
Rightmost Derivation:
S
⇒ aSb
⇒ aabSb
⇒ aabb
Explanation:
Both derivations generate the same string, but they apply rules in different orders. In leftmost,
we expand from left to right, and in rightmost, we expand from right to left.
Example Grammar:
E → E + E | E * E | id
Consider string: id + id * id
Second Parse Tree (interpret multiplication first):
Conclusion:
Since the same string has multiple parse trees, the grammar is ambiguous.
4. Given CFG for palindromes: S → aSa | bSb | a | b, find PDA that accepts L(G). – 2M
The given CFG generates odd-length palindromes over the alphabet {a, b}. The idea behind
the PDA is to push the first half of the input onto the stack and then pop and match with the
second half.
CFG:
S → aSa | bSb | a | b
● Length 1: a, b
PDA Design:
● Σ = {a, b}
● Γ = {a, b, Z}
● q0 = Start state
Transition Function δ:
1. Initialize stack:
δ(q0, ε, ε) = (q1, Z)
Explanation:
This PDA accepts all palindromes of odd length over {a, b} by simulating symmetric matching.
A parse tree (also known as a derivation tree) is a tree structure that shows how a string is
derived from a CFG. It reflects the syntactic structure and rule application order.
Key Properties:
● Root is always the start symbol.
Example Grammar:
S → aSb | ε
Parse Tree:
less
CopyEdit
S
/ | \
a S b
/ \
a S b
|
ε
Explanation:
Components of a PDA:
● Σ: Input alphabet
● Γ: Stack alphabet
● δ: Transition function
Example Language:
PDA Idea:
Transitions:
1. δ(q0, a, Z) → (q0, aZ)
Explanation:
A Deterministic Pushdown Automaton (DPDA) is a PDA in which for every state, input
symbol, and stack symbol, there is at most one possible transition. It’s more restrictive than
a non-deterministic PDA (NPDA).
Key Points:
L = { aⁿbⁿ | n ≥ 0 }
DPDA Design:
Transitions:
Explanation:
Context-Free Grammars (CFGs) have wide applications in computer science, compilers, and
language processing.
Key Applications:
○ CFGs define the syntax rules of programming languages (e.g., how expressions,
loops, functions are structured).
Leftmost Derivation:
Rightmost Derivation:
Example Grammar:
S → aSb | ε
String: aabb
Leftmost Derivation:
S
⇒ aSb
⇒ aaSbb
⇒ aaεbb
⇒ aabb
Rightmost Derivation:
S
⇒ aSb
⇒ aabSb
⇒ aabb
Key Differences:
Feature Leftmost Derivation Rightmost Derivation
Tree Building Builds parse tree left-first Builds parse tree right-first
10. Write closure properties of CFL. – 2M
Context-Free Languages (CFLs) are not closed under all operations, but they are closed
under several important ones.
● Union:
If L₁ and L₂ are CFLs, then L₁ ∪ L₂ is also a CFL.
● Concatenation:
If L₁ and L₂ are CFLs, then L₁L₂ is a CFL.
● Kleene star:
If L is a CFL, then L* is a CFL.
● Substitution:
If each symbol is replaced with a CFL, the result is a CFL.
● Complementation:
¬L may not be a CFL.
Decision properties refer to problems for which we can always determine an answer (yes or no).
Some problems involving CFLs are decidable, others are undecidable.
● Emptiness:
Is L = ∅? → Decidable
● Finiteness:
Is L finite? → Decidable
● Membership:
Is w ∈ L? → Decidable using CYK or PDA simulation
● Equivalence:
Are two CFGs equivalent? → Undecidable
● Universality:
Does CFG generate all strings? → Undecidable
● Inclusion:
Is L₁ ⊆ L₂ for two CFLs? → Undecidable
12. Define CNF with suitable example. – 2M
Chomsky Normal Form (CNF) is a way of rewriting a CFG such that all productions follow strict
forms:
Example:
Original CFG:
S → aAB
A → a
B→b
Convert to CNF:
● S → XA
● A → a
● B → b
● X → a
Why CNF is useful:
Chomsky Normal Form (CNF) is a special form of a Context-Free Grammar (CFG) that
imposes strict constraints on the structure of production rules. These constraints simplify parsing
and formal analysis, making CNF an important tool in compiler construction, formal language
theory, and parsing algorithms like CYK (Cocke-Younger-Kasami).
Characteristics of CNF:
1. Production Rules in CNF: A CFG is said to be in Chomsky Normal Form if all its
production rules satisfy the following conditions:
○ Additionally, a single rule S → ε is allowed, but only if S is the start symbol, and ε
is part of the language.
2. Therefore, the production rules of a CNF grammar can only fall into three categories:
○ A → a (non-terminal to a terminal).
○ Simplicity for Parsing: The structure of CNF allows for easier parsing and
checking whether a string belongs to the language defined by the grammar.
Parsing algorithms, like CYK, work efficiently with CNF as every rule ensures
uniformity in the form of the production.
○ Formal Analysis and Proofs: CNF provides a simplified structure for theoretical
analysis, such as proving properties of languages or automata.
○ Algorithm Design: CNF simplifies the process of converting grammars into
automata or working with parsing algorithms that require consistent production
rule structures.
Greibach Normal Form (GNF) is a form of context-free grammar where every production rule
has the following format:
GNF ensures that every production starts with a terminal symbol, making it especially useful for
top-down parsing.
Example Grammar:
Original CFG:
S → aAB | bA
A → a
B→b
Convert to GNF:
1. Start with the production S → aAB. This already follows GNF because it begins with a
terminal (a).
● S → aAB | bA
● A → a
● B → b
● It guarantees that each production starts with a terminal, simplifying top-down parsing.
NP-Hard and NP-Complete are both classes of problems in computational complexity theory,
but they are distinct in their definitions and properties.
NP-Hard:
● NP-Hard problems are not necessarily in NP, and they may not have a known solution
that can be verified in polynomial time.
NP-Complete:
○ It is in NP.
● NP-Complete problems are the hardest problems in NP, meaning that if one
NP-Complete problem is solved in polynomial time, all NP problems can be solved in
polynomial time.
Example:
● NP-Hard Example:
The Halting Problem is NP-Hard but not in NP because it cannot be verified in
polynomial time and isn’t solvable by any known algorithm.
● NP-Complete Example:
The Traveling Salesman Problem (TSP) is NP-Complete because:
Key Differences:
Feature NP-Hard NP-Complete
Solution verification? May not be verifiable in polynomial time Verifiable in polynomial time
A Turing Machine (TM) is a theoretical model of computation that defines an abstract machine
capable of solving any problem that can be algorithmically solved.
● Transition Function: Defines the machine’s operation based on the current state and
symbol it reads.
● Accept State: A state that indicates the machine has finished processing.
Example Language:
TM Design for L:
2. It reads input symbol 0 or 1 and moves to the next state accordingly.
3. It will count 1’s by transitioning between states, and in state q1, it ensures the machine is
processing in pairs.
4. If the machine finishes reading the input and the number of 1’s is even, it accepts the
string.
Explanation:
The Turing machine above accepts strings with an even number of 1’s by tracking the count in
states q0 and q1. If it ends with an even number of 1’s, it transitions to the accept state.
Greibach Normal Form (GNF) is a normal form for Context-Free Grammars (CFGs) where
each production rule is constrained to the following form:
● A → aα, where:
○ A is a non-terminal.
○ a is a terminal symbol.
This form ensures that every production starts with a terminal symbol followed by zero or more
non-terminals, making it particularly useful for top-down parsing, such as recursive descent
parsers.
Characteristics of GNF:
● Every production has the form A → aα, where a is a terminal and α is a sequence of
non-terminals (which can be empty).
● This structure allows for direct prediction of the next symbol in the input string (hence,
predictive parsing).
Advantages of GNF:
● Simplifies top-down parsing because it allows for unambiguous prediction of what the
next input symbol will be.
● Facilitates the construction of recursive descent parsers, as the production rules lead
directly to terminal symbols.
Example Grammar:
Consider the following CFG that is not in GNF:
less
CopyEdit
S → AB | a
A → a
B → b
2. The production A → a is in GNF, but B → b is not part of the right-hand side of a
non-terminal to terminal rule (i.e., doesn't follow the A → aα format).
1. Eliminate ε-productions (productions of the form A → ε) unless the start symbol
generates ε.
○ Replace any unit production with equivalent rules. In this case, there are no unit
productions to eliminate.
○ Left recursion occurs when a non-terminal can eventually produce itself as the
first symbol in its derivation, like A → Aα. This doesn't happen in this grammar,
so we skip this step as well.
○ We need to ensure all rules have the form A → aα, where a is a terminal. If not,
we break down the non-terminal into a terminal-first structure.
● S → AB: This does not start with a terminal, so we need to transform it.
● We need to break down the production S → AB. We can introduce a new non-terminal X
to represent A → a and B → b and modify S:
○ S → aB (since A → a)
less
CopyEdit
S → aB | a
A → a
B → b
1. Start with the original grammar and identify the productions that do not follow GNF.
2. Introduce new non-terminals where necessary to ensure that productions begin with a
terminal.
3. Modify the rules by replacing non-terminal starts with terminal-first sequences, ensuring
each rule is of the form A → aα.
4. Repeat the process until all productions are in GNF, ensuring that each rule is simplified
to start with a terminal symbol, followed by zero or more non-terminals.
Key Points:
● GNF simplifies top-down parsing by ensuring that each production starts with a terminal
symbol.
● The conversion process involves eliminating any productions that do not start with a
terminal and introducing new non-terminals if necessary to maintain the structure of
GNF.
PART-B
1. Explain rightmost derivation and leftmost derivation with a suitable example. 4M
Leftmost Derivation:
In leftmost derivation, we start with the start symbol and at each step, we expand the
leftmost non-terminal first. The goal is to replace the leftmost non-terminal with the
corresponding production rule until the entire string is derived.
Steps for Leftmost Derivation:
1. Start with the start symbol.
2. Replace the leftmost non-terminal using one of its production rules.
3. Continue expanding the leftmost non-terminal until no non-terminal remains.
Example of Leftmost Derivation:
Consider the following grammar:
css
Copy code
S → ABC
A→a
B→b
C→c
Let’s derive the string w = abc using leftmost derivation:
• Step 1: Start with S.
nginx
Copy code
S → ABC
• Step 2: Expand the leftmost non-terminal A to a (using A → a).
nginx
Copy code
S → aBC
• Step 3: Now, the leftmost non-terminal is B. Expand B to b (using B → b).
nginx
Copy code
S → abC
• Step 4: Finally, expand C to c (using C → c).
nginx
Copy code
S → abc
Now, S → abc is the final derived string.
Rightmost Derivation:
In rightmost derivation, we start with the start symbol and at each step, we expand the
rightmost non-terminal first. The goal is to replace the rightmost non-terminal with the
corresponding production rule until the entire string is derived.
Steps for Rightmost Derivation:
1. Start with the start symbol.
2. Replace the rightmost non-terminal using one of its production rules.
3. Continue expanding the rightmost non-terminal until no non-terminal remains.
Example of Rightmost Derivation:
Let’s derive the string w = abc again using rightmost derivation for the same grammar:
css
Copy code
S → ABC
A→a
B→b
C→c
• Step 1: Start with S.
nginx
Copy code
S → ABC
• Step 2: Expand the rightmost non-terminal C to c (using C → c).
nginx
Copy code
S → ABc
• Step 3: Now, expand the rightmost non-terminal B to b (using B → b).
nginx
Copy code
S → Abc
• Step 4: Finally, expand A to a (using A → a).
nginx
Copy code
S → abc
Now, S → abc is the final derived string.
________________________________________
Summary of Key Differences:
• In leftmost derivation, you always replace the leftmost non-terminal first.
• In rightmost derivation, you always replace the rightmost non-terminal first.
• For the same grammar, a string can have both leftmost and rightmost derivations,
but the order in which the non-terminals are replaced will differ.
________________________________________
Example to Illustrate Both:
Let’s use a simple example with the grammar:
css
Copy code
S → ABC
A→a
B→b
C→c
For string abc, we get the following derivations:
• Leftmost Derivation:
css
Copy code
S → ABC
→ aBC (using A → a)
→ abC (using B → b)
→ abc (using C → c)
• Rightmost Derivation:
css
Copy code
S → ABC
→ ABc (using C → c)
→ Abc (using B → b)
→ abc (using A → a)
Both derivations result in abc, but the order of non-terminal replacements differs.
3. Construct the CFG representing the set of palindrome over (0+1)*. 4M
4. Explain about pumping lemma algorithm. 4M
5. Write about closure properties of context free language. 4M
6. Enumerate normal forms for context free language. 4M
7. Convert the following context free language to CNF: S → ABC, A → Aa / ε, B → bB
/ ε, C → cC / ε. 4M
8. Convert the following CFG into GNF: S → AB, A → a, B → CA, C → AB / b. 4M
9. Construct a PDA for accepting a language {L = a^n b^n | n ≥ 1}. 4M
10. Construct PDA for the given CFG: S → 0BB, B → 0S | 1S | 0. Test whether 01044 is
accepted by this PDA. 8M
11. Convert the following CFG into Chomsky’s Normal Form (CNF): S → A B A | B a A
| A, A → B a | S | ε, B → B a | b | C a, C → C a, D → D a D | a. 8M
13. Differentiate between Chomsky’s Normal Form (CNF) and GNF (Greibach Normal
Form) with suitable examples. 4M
14. Explain decidability and undecidability with examples. 4M
15. Explain Post correspondence problem with an example. 4M
16. Discuss about Modified Post correspondence problem with an example. 4M
17. Explain about the Decision Properties and Closure Properties of CFL. 4M
18. Construct a Turing Machine (TM) that accepts the language L = {0^n 1^n | n > 1}.
4M
19. Short notes on: i) P ii) NP iii) NP Hard iv) NP Complete with example. 8M
Introduction to P, NP, NP-Hard, and NP-Complete
In the theory of computational complexity within Automata Theory, P, NP, NP-Hard, and
NP-Complete are classes used to categorize decision problems based on their
computational complexity. These classifications help determine the difficulty of problems
and the resources needed to solve them.
Understanding the relationships between these complexity classes is crucial for
determining which problems are solvable efficiently (in polynomial time) and which are
likely intractable (requiring exponential time).
Let's define each of these terms one by one and explore them with examples.
________________________________________
i) P (Polynomial Time)
P is the class of decision problems (problems with a "yes" or "no" answer) that can be
solved by a deterministic Turing machine in polynomial time. In other words, a problem is
in P if there exists an algorithm to solve it that runs in time proportional to a polynomial
function of the input size.
Key Points:
• P problems are considered efficiently solvable.
• The solution to these problems can be computed in time O(nk)O(n^k)O(nk), where
nnn is the size of the input and kkk is some constant.
• Examples include problems like sorting a list, searching an element in an array,
and matrix multiplication.
Example: Sorting Problem
Given a list of numbers, sorting them in non-decreasing order is a problem in P. The
popular Merge Sort algorithm solves this in O(nlogn)O(n \log n)O(nlogn), which is
polynomial time.
________________________________________
ii) NP (Non-deterministic Polynomial Time)
NP is the class of decision problems for which a non-deterministic Turing machine can
solve the problem in polynomial time, or equivalently, for which a solution can be verified
in polynomial time given a potential solution.
Key Points:
• A problem is in NP if, once a solution is guessed (non-deterministically), it can be
verified in polynomial time.
• NP problems may or may not have efficient solutions, but any proposed solution
can be checked in polynomial time.
• NP includes both problems that are solvable in polynomial time (like those in P)
and those that are not known to be solvable efficiently.
Example: Hamiltonian Path Problem
Given a graph, the Hamiltonian Path Problem asks if there exists a path that visits each
vertex exactly once. Verifying a proposed solution (i.e., checking if a given path is a
Hamiltonian path) can be done in polynomial time, but finding such a path (if one exists)
is not known to be solvable in polynomial time.
________________________________________
iii) NP-Hard
NP-Hard is a class of problems that are, informally, at least as hard as the hardest
problems in NP. A problem is considered NP-Hard if every problem in NP can be reduced
to it in polynomial time. In other words, if we can solve an NP-Hard problem efficiently,
we can solve all NP problems efficiently.
Key Points:
• NP-Hard problems are not necessarily in NP because they may not be decision
problems (they may not have a yes/no answer).
• Solving an NP-Hard problem would give a solution to all NP problems, but finding
such a solution may be difficult or impossible.
• NP-Hard does not require polynomial-time verification.
Example: Traveling Salesman Problem (TSP)
In the Traveling Salesman Problem, you are given a list of cities and must determine the
shortest possible route that visits each city exactly once and returns to the starting city.
This problem is NP-Hard because it is computationally very difficult, and no
polynomial-time solution is known, though it can be verified in polynomial time once a
route is proposed.
________________________________________
iv) NP-Complete
NP-Complete is the class of problems that are both in NP and NP-Hard. These problems
are the hardest problems in NP, and they are believed to be intractable (i.e., no
polynomial-time solution exists unless P=NPP = NPP=NP).
A problem is considered NP-Complete if:
1. It is in NP (its solution can be verified in polynomial time).
2. Every other problem in NP can be reduced to it in polynomial time.
If any NP-Complete problem can be solved in polynomial time, then all problems in NP
can be solved in polynomial time (i.e., P=NPP = NPP=NP).
Key Points:
• NP-Complete problems are both hard and verifiable in polynomial time.
• If we find a polynomial-time solution to one NP-Complete problem, it would imply
that P=NPP = NPP=NP.
• NP-Complete problems are important because they represent the "hardest"
problems in NP.
Example: Boolean Satisfiability Problem (SAT)
The SAT Problem asks whether there exists an assignment of truth values to variables
that satisfies a given Boolean formula. It is NP-Complete because:
• It is in NP (a solution can be verified in polynomial time).
• Any problem in NP can be reduced to SAT in polynomial time.
________________________________________
Summary of Differences and Relationships
Class Description Example
P Problems solvable in polynomial time. Sorting, matrix multiplication, shortest
path
NP Problems verifiable in polynomial time, but not necessarily solvable in polynomial
time. Hamiltonian Path, Boolean satisfiability (SAT)
NP-Hard Problems that are at least as hard as the hardest problems in NP.
Traveling Salesman Problem (TSP), Halting problem
NP-CompleteProblems that are both in NP and NP-Hard. SAT, Knapsack problem,
Vertex Cover problem
________________________________________
Conclusion
In summary, P is the class of problems solvable in polynomial time, NP is the class of
problems whose solutions can be verified in polynomial time, NP-Hard includes the
hardest problems in NP (or even harder), and NP-Complete represents the hardest
problems in NP that are both in NP and NP-Hard. Understanding these complexity
classes is crucial in the field of computational theory and automata, as it helps determine
the tractability of algorithms and problems in computer science.
Unit-4
31.
https://www.naukri.com/code360/library/cfg-to-pda-conversion
Unit-5
40. short notes on:
i) P ii) NP iii) NP Hard iv) NP Complete with example
41. Illustrate the process of Recursive languages and Recursively enumerable
Languages with suitable examples?
Ans:
Recursively Enumerable Languages
In simple words, a "language" is a collection of strings, like words in a dictionary. A
recursively enumerable language is a language where we can create a computer program
(or a Turing machine) that can systematically list out all the strings that belong to the
language.
Consider a machine that can generate all the possible sentences in the English language,
one by one. This machine wouldn't necessarily know which sentences are not in the
English language, but it could list out all the valid sentences. This is the idea of a
recursively enumerable language. It can enumerate all the strings that are part of the
language.
.
Recursive Languages: A Subset of RE Languages
Another important subset of RE languages is recursive language. In a recursive
language, the Turing machine not only accepts strings belonging to the language but
also always halts for strings that are not in the language
As an example, Consider the language L={anbncn|n≥0}L={anbncn|n≥0}. This language
consists of strings where the number of a's, b's, and c's are equal