Category: Subjects

Minimization of DFA | Minimize DFA | Examples

Minimization of DFA-

 

The process of reducing a given DFA to its minimal form is called as minimization of DFA.

 

  • It contains the minimum number of states.
  • The DFA in its minimal form is called as a Minimal DFA.

 

How To Minimize DFA?

 

The two popular methods for minimizing a DFA are-

 

 

In this article, we will discuss Minimization of DFA Using Equivalence Theorem.

 

Minimization of DFA Using Equivalence Theorem-

 

Step-01:

 

  • Eliminate all the dead states and inaccessible states from the given DFA (if any).

 

Dead State

 

All those non-final states which transit to itself for all input symbols in ∑ are called as dead states.

 

Inaccessible State

 

All those states which can never be reached from the initial state are called as inaccessible states.

 

Step-02:

 

  • Draw a state transition table for the given DFA.
  • Transition table shows the transition of all states on all input symbols in Σ.

 

Step-03:

 

Now, start applying equivalence theorem.

  • Take a counter variable k and initialize it with value 0.
  • Divide Q (set of states) into two sets such that one set contains all the non-final states and other set contains all the final states.
  • This partition is called P0.

 

Step-04:

 

  • Increment k by 1.
  • Find Pk by partitioning the different sets of Pk-1 .
  • In each set of Pk-1 , consider all the possible pair of states within each set and if the two states are distinguishable, partition the set into different sets in Pk.

 

Two states q1 and q2 are distinguishable in partition Pk for any input symbol ‘a’,

if δ (q1, a) and δ (q2, a) are in different sets in partition Pk-1.

 

Step-05:

 

  • Repeat step-04 until no change in partition occurs.
  • In other words, when you find Pk = Pk-1, stop.

 

Step-06:

 

  • All those states which belong to the same set are equivalent.
  • The equivalent states are merged to form a single state in the minimal DFA.

 

Number of states in Minimal DFA

= Number of sets in Pk

 

PRACTICE PROBLEMS BASED ON MINIMIZATION OF DFA-

 

Problem-01:

 

Minimize the given DFA-

 

 

Solution-

 

Step-01:

 

The given DFA contains no dead states and inaccessible states.

 

Step-02:

 

Draw a state transition table-

 

a b
q0 q1 q2
q1 q1 q3
q2 q1 q2
q3 q1 *q4
*q4 q1 q2

 

Step-03:

 

Now using Equivalence Theorem, we have-

P0 = { q0 , q1 , q2 , q3 } { q4 }

P1 = { q0 , q1 , q2 } { q3 } { q4 }

P2 = { q0 , q2 } { q1 } { q3 } { q4 }

P3 = { q0 , q2 } { q1 } { q3 } { q4 }

 

Since P3 = P2, so we stop.

From P3, we infer that states q0 and q2 are equivalent and can be merged together.

So, Our minimal DFA is-

 

 

Problem-02:

 

Minimize the given DFA-

 

 

Solution-

 

Step-01:

 

  • State q3 is inaccessible from the initial state.
  • So, we eliminate it and its associated edges from the DFA.

 

The resulting DFA is-

 

 

Step-02:

 

Draw a state transition table-

 

a b
q0 *q1 q0
*q1 *q2 *q1
*q2 *q1 *q2

 

Step-03:

 

Now using Equivalence Theorem, we have-

P0 = { q0 } { q1 , q2 }

P1 = { q0 } { q1 , q2 }

 

Since P1 = P0, so we stop.

From P1, we infer that states q1 and q2 are equivalent and can be merged together.

So, Our minimal DFA is-

 

 

Problem-03:

 

Minimize the given DFA-

 

 

Solution-

 

Step-01:

 

The given DFA contains no dead states and inaccessible states.

 

Step-02:

 

Draw a state transition table-

 

0 1
q0 q1 q3
q1 q2 *q4
q2 q1 *q4
q3 q2 *q4
*q4 *q4 *q4

 

Step-03:

 

Now using Equivalence Theorem, we have-

P0 = { q0 , q1 , q2 , q3 } { q4 }

P1 = { q0 } { q1 , q2 , q3 } { q4 }

P2 = { q0 } { q1 , q2 , q3 } { q4 }

 

Since P2 = P1, so we stop.

From P2, we infer that states q1 , q2 and q3 are equivalent and can be merged together.

So, Our minimal DFA is-

 

 

Problem-04:

 

Minimize the given DFA-

 

 

Solution-

 

Step-01:

 

  • State q5 is inaccessible from the initial state.
  • So, we eliminate it and its associated edges from the DFA.

 

The resulting DFA is-

 

 

Step-02:

 

Draw a state transition table-

 

0 1
q0 q1 q2
q1 q2 *q3
q2 q2 *q4
*q3 *q3 *q3
*q4 *q4 *q4

 

Step-03:

 

Now using Equivalence Theorem, we have-

P0 = { q0 , q1 , q2 } { q3 , q4 }

P1 = { q0 } { q1 , q2 } { q3 , q4 }

P2 = { q0 } { q1 , q2 } { q3 , q4 }

 

Since P2 = P1, so we stop.

From P2, we infer-

  • States q1 and q2 are equivalent and can be merged together.
  • States q3 and q4 are equivalent and can be merged together.

So, Our minimal DFA is-

 

 

Also Read- Construction of DFA

 

To gain better understanding about Minimization of DFA,

Watch this Video Lecture

 

Next Article- Converting DFA to Regular Expression

 

Get more notes and other study material of Theory of Automata and Computation.

Watch video lectures by visiting our YouTube channel LearnVidFun.

DFA to Regular Expression | Examples

DFA to Regular Expression-

 

The two popular methods for converting a DFA to its regular expression are-

 

 

  1. Arden’s Method
  2. State Elimination Method

 

In this article, we will discuss State Elimination Method.

 

State Elimination Method-

 

This method involves the following steps in finding the regular expression for any given DFA-

 

Step-01:

 

Thumb Rule

The initial state of the DFA must not have any incoming edge.

 

  • If there exists any incoming edge to the initial state, then create a new initial state having no incoming edge to it.

 

Example-

 

 

Step-02:

 

Thumb Rule

There must exist only one final state in the DFA.

 

  • If there exists multiple final states in the DFA, then convert all the final states into non-final states and create a new single final state.

 

Example-

 

 

Step-03:

 

Thumb Rule

The final state of the DFA must not have any outgoing edge.

 

  • If there exists any outgoing edge from the final state, then create a new final state having no outgoing edge from it.

 

Example-

 

 

Step-04:

 

  • Eliminate all the intermediate states one by one.
  • These states may be eliminated in any order.

 

In the end,

  • Only an initial state going to the final state will be left.
  • The cost of this transition is the required regular expression.

 

NOTE

The state elimination method can be applied to any finite automata.

(NFA, ∈-NFA, DFA etc)

 

Also Read- Construction of DFA

 

PRACTICE PROBLEMS BASED ON CONVERTING DFA TO REGULAR EXPRESSION-

 

Problem-01:

 

Find regular expression for the following DFA-

 

 

Solution-

 

Step-01:

 

  • Initial state A has an incoming edge.
  • So, we create a new initial state qi.

 

The resulting DFA is-

 

 

Step-02:

 

  • Final state B has an outgoing edge.
  • So, we create a new final state qf.

 

The resulting DFA is-

 

 

Step-03:

 

Now, we start eliminating the intermediate states.

 

First, let us eliminate state A.

  • There is a path going from state qi to state B via state A.
  • So, after eliminating state A, we put a direct path from state qi to state B having cost ∈.0 = 0
  • There is a loop on state B using state A.
  • So, after eliminating state A, we put a direct loop on state B having cost 1.0 = 10.

 

Eliminating state A, we get-

 

 

Step-04:

 

Now, let us eliminate state B.

  • There is a path going from state qi to state qf via state B.
  • So, after eliminating state B, we put a direct path from state qi to state qf having cost 0.(10)*.∈ = 0(10)*

 

Eliminating state B, we get-

 

 

From here,

 

Regular Expression = 0(10)*

 

NOTE-

 

In the above question,

  • If we first eliminate state B and then state A, then regular expression would be = (01)*0.
  • This is also the same and correct.

 

Problem-02:

 

Find regular expression for the following DFA-

 

 

Solution-

 

Step-01:

 

  • There exist multiple final states.
  • So, we convert them into a single final state.

 

The resulting DFA is-

 

 

Step-02:

 

Now, we start eliminating the intermediate states.

 

First, let us eliminate state q4.

  • There is a path going from state q2 to state qf via state q4.
  • So, after eliminating state q4 , we put a direct path from state q2 to state qf having cost b.∈ = b.

 

 

Step-03:

 

Now, let us eliminate state q3.

  • There is a path going from state q2 to state qf via state q3.
  • So, after eliminating state q3 , we put a direct path from state q2 to state qf having cost c.∈ = c.

 

 

Step-04:

 

Now, let us eliminate state q5.

  • There is a path going from state q2 to state qf via state q5.
  • So, after eliminating state q5 , we put a direct path from state q2 to state qf having cost d.∈ = d.

 

 

Step-05:

 

Now, let us eliminate state q2.

  • There is a path going from state q1 to state qf via state q2.
  • So, after eliminating state q2 , we put a direct path from state q1 to state qf having cost a.(b+c+d).

 

 

From here,

 

Regular Expression = a(b+c+d)

 

Problem-03:

 

Find regular expression for the following DFA-

 

 

Solution-

 

Step-01:

 

  • Initial state q1 has an incoming edge.
  • So, we create a new initial state qi.

 

The resulting DFA is-

 

 

Step-02:

 

  • Final state q2 has an outgoing edge.
  • So, we create a new final state qf.

 

The resulting DFA is-

 

 

Step-03:

 

Now, we start eliminating the intermediate states.

 

First, let us eliminate state q1.

  • There is a path going from state qi to state q2 via state q1 .
  • So, after eliminating state q1, we put a direct path from state qi to state q2 having cost ∈.c*.a = c*a
  • There is a loop on state q2 using state q1 .
  • So, after eliminating state q1 , we put a direct loop on state q2 having cost b.c*.a = bc*a

 

Eliminating state q1, we get-

 

 

Step-04:

 

Now, let us eliminate state q2.

  • There is a path going from state qi to state qf via state q2 .
  • So, after eliminating state q2, we put a direct path from state qi to state qf having cost c*a(d+bc*a)*∈ = c*a(d+bc*a)*

 

Eliminating state q2, we get-

 

 

From here,

 

Regular Expression = c*a(d+bc*a)*

 

Problem-04:

 

Find regular expression for the following DFA-

 

 

Solution-

 

Step-01:

 

  • State D is a dead state as it does not reach to any final state.
  • So, we eliminate state D and its associated edges.

 

The resulting DFA is-

 

 

Step-02:

 

  • Initial state A has an incoming edge (self loop).
  • So, we create a new initial state qi.

 

The resulting DFA is-

 

 

Step-03:

 

  • There exist multiple final states.
  • So, we convert them into a single final state.

 

The resulting DFA is-

 

 

Step-04:

 

Now, we start eliminating the intermediate states.

 

First, let us eliminate state C.

  • There is a path going from state B to state qf via state C.
  • So, after eliminating state C, we put a direct path from state B to state qf having cost b.b*.∈ = bb*

 

Eliminating state C, we get-

 

 

Step-05:

 

Now, let us eliminate state B.

  • There is a path going from state A to state qf via state B.
  • So, after eliminating state B, we put a direct path from state A to state qf having cost a.a*.(bb*+∈) = aa*(bb*+∈)

 

Eliminating state B, we get-

 

 

Step-06:

 

Now, let us eliminate state A.

  • There is a path going from state qi to state qf via state A.
  • So, after eliminating state A, we put a direct path from state qi to state qf having cost ∈.b*.(aa*(bb*+∈)+∈) = b*(aa*(bb*+∈)+∈)

 

Eliminating state A, we get-

 

 

From here,

 

Regular Expression = b*(aa*(bb*+∈)+∈)

 

We know, bb* + ∈ = b*

So, we can also write-

 

Regular Expression = b*(aa*b*+∈)

 

Problem-05:

 

Find regular expression for the following DFA-

 

 

Solution-

 

Step-01:

 

  • Since initial state A has an incoming edge, so we create a new initial state qi.
  • Since final state A has an outgoing edge, so we create a new final state qf.

 

The resulting DFA is-

 

 

Step-02:

 

Now, we start eliminating the intermediate states.

 

First, let us eliminate state B.

  • There is a path going from state C to state A via state B.
  • So, after eliminating state B, we put a direct path from state C to state A having cost b.b = bb.
  • There is a loop on state A using state B.
  • So, after eliminating state B, we put a direct loop on state A having cost a.b = ab.

 

Eliminating state B, we get-

 

 

Step-03:

 

Now, let us eliminate state C.

  • There is a loop on state A using state C.
  • So, after eliminating state C, we put a direct loop on state A having cost b.(a+bb) = b(a+bb)

 

Eliminating state C, we get-

 

 

Step-04:

 

Now, let us eliminate state A.

  • There is a path going from state qi to state qf via state A.
  • So, after eliminating state A, we put a direct path from state qi to state qf having cost ∈.(ab + b(a+bb))*∈ = (ab + b(a+bb))*

 

Eliminating state A, we get-

 

 

From here,

 

Regular Expression = (ab + b(a+bb))*

 

Problem-06:

 

Find regular expression for the following DFA-

 

 

Solution-

 

  • State B is a dead state as it does not reach to the final state.
  • So, we eliminate state B and its associated edges.

 

The resulting DFA is-

 

 

From here,

 

Regular Expression = a

 

Problem-07:

 

Find regular expression for the following DFA-

 

 

Solution-

 

Step-01:

 

  • There exist multiple final states.
  • So, we create a new single final state.

 

The resulting DFA is-

 

 

Step-02:

 

Now, we start eliminating the intermediate states.

 

First, let us eliminate state B.

  • There is a path going from state A to state qf via state B.
  • So, after eliminating state B, we put a direct path from state A to state qf having cost a.a*.∈ = aa*.

 

Eliminating state B, we get-

 

 

Step-03:

 

Now, let us eliminate state C.

  • There is a path going from state A to state qf via state C.
  • So, after eliminating state C, we put a direct path from state A to state qf having cost b.a*.∈ = ba*.

 

Eliminating state C, we get-

 

 

From here,

 

Regular Expression = aa* + ba*

 

Also Read- Minimization of DFA

 

To gain better understanding about Converting DFA to Regular Expression,

Watch this Video Lecture

 

Next Article- Arden’s Theorem

 

Get more notes and other study material of Theory of Automata and Computation.

Watch video lectures by visiting our YouTube channel LearnVidFun.

Left Factoring | Left Factoring Examples

Grammar With Common Prefixes-

 

If RHS of more than one production starts with the same symbol,

then such a grammar is called as

Grammar With Common Prefixes.

 

Example-

 

αβ1 / αβ2 / αβ3

(Grammar with common prefixes)

 

  • This kind of grammar creates a problematic situation for Top down parsers.
  • Top down parsers can not decide which production must be chosen to parse the string in hand.

To remove this confusion, we use left factoring.

 

Left Factoring-

 

Left factoring is a process by which the grammar with common prefixes is transformed

to make it useful for Top down parsers.

 

How?

 

In left factoring,

  • We make one production for each common prefixes.
  • The common prefix may be a terminal or a non-terminal or a combination of both.
  • Rest of the derivation is added by new productions.

 

The grammar obtained after the process of left factoring is called as Left Factored Grammar.

 

Example-

 

 

Also Read- Left Recursion

 

PRACTICE PROBLEMS BASED ON LEFT FACTORING-

 

Problem-01:

 

Do left factoring in the following grammar-

S → iEtS / iEtSeS / a

E → b

 

Solution-

 

The left factored grammar is-

S → iEtSS’ / a

S’ → eS / ∈

E → b

 

Problem-02:

 

Do left factoring in the following grammar-

A → aAB / aBc / aAc

 

Solution-

 

Step-01:

 

A → aA’

A’ → AB / Bc / Ac

Again, this is a grammar with common prefixes.

 

Step-02:

 

A → aA’

A’ → AD / Bc

D → B / c

This is a left factored grammar.

 

Problem-03:

 

Do left factoring in the following grammar-

S → bSSaaS / bSSaSb / bSb / a

 

Solution-

 

Step-01:

 

S → bSS’ / a

S’ → SaaS / SaSb / b

Again, this is a grammar with common prefixes.

 

Step-02:

 

S → bSS’ / a

S’ → SaA / b

A → aS / Sb

This is a left factored grammar.

 

Problem-04:

 

Do left factoring in the following grammar-

S → aSSbS / aSaSb / abb / b

 

Solution-

 

Step-01:

 

S → aS’ / b

S’ → SSbS / SaSb / bb

Again, this is a grammar with common prefixes.

 

Step-02:

 

S → aS’ / b

S’ → SA / bb

A → SbS / aSb

This is a left factored grammar.

 

Problem-05:

 

Do left factoring in the following grammar-

S → a / ab / abc / abcd

 

Solution-

 

Step-01:

 

S → aS’

S’ → b / bc / bcd / ∈

Again, this is a grammar with common prefixes.

 

Step-02:

 

S → aS’

S’ → bA / ∈

A → c / cd / ∈

Again, this is a grammar with common prefixes.

 

Step-03:

 

S → aS’

S’ → bA / ∈

A → cB / ∈

B → d / ∈

This is a left factored grammar.

 

Problem-06:

 

Do left factoring in the following grammar-

S → aAd / aB

A → a / ab

B → ccd / ddc

 

Solution-

 

The left factored grammar is-

S → aS’

S’ → Ad / B

A → aA’

A’ → b / ∈

B → ccd / ddc

 

To gain better understanding about Left Factoring,

Watch this Video Lecture

 

Next Article- Relationship With Left Recursion

 

Get more notes and other study material of Compiler Design.

Watch video lectures by visiting our YouTube channel LearnVidFun.

AVL Tree Properties | Problems on AVL Tree

AVL Tree-

 

Before you go through this article, make sure that you have gone through the previous article on AVL Trees.

 

We have discussed-

  • AVL trees are self-balancing binary search trees.
  • In AVL trees, balancing factor of each node is either 0 or 1 or -1.

 

In this article, we will discuss AVL Tree Properties.

 

AVL Tree Properties-

 

Important properties of AVL tree are-

 

Property-01:

 

Maximum possible number of nodes in AVL tree of height H

= 2H+1 – 1

 

Example-

 

Maximum possible number of nodes in AVL tree of height-3

= 23+1 – 1

= 16 – 1

= 15

Thus, in AVL tree of height-3, maximum number of nodes that can be inserted = 15.

 

 

We can not insert more number of nodes in this AVL tree.

 

Property-02:

 

Minimum number of nodes in AVL Tree of height H is given by a recursive relation-

N(H) = N(H-1) + N(H-2) + 1

 

Base conditions for this recursive relation are-

  • N(0) = 1
  • N(1) = 2

 

Example-

 

Minimum possible number of nodes in AVL tree of height-3 = 7

 

 

(For explanation, Refer problem-01)

 

Property-03:

 

Minimum possible height of AVL Tree using N nodes

= ⌊log2N⌋

 

Example-

 

Minimum possible height of AVL Tree using 8 nodes

= ⌊log28⌋

= ⌊log223

= ⌊3log22⌋

= ⌊3⌋

= 3

 

Property-04:

 

Maximum height of AVL Tree using N nodes is calculated using recursive relation-

N(H) = N(H-1) + N(H-2) + 1

 

Base conditions for this recursive relation are-

  • N(0) = 1
  • N(1) = 2

 

NOTE-

 

  • If there are n nodes in AVL Tree, its maximum height can not exceed 1.44log2n.
  • In other words, Worst case height of AVL Tree with n nodes = 1.44log2n.

 

PRACTICE PROBLEMS BASED ON AVL TREE PROPERTIES-

 

Problem-01:

 

Find the minimum number of nodes required to construct AVL Tree of height = 3.

 

Solution-

 

We know, minimum number of nodes in AVL tree of height H is given by a recursive relation-

 

N(H) = N(H-1) + N(H-2) + 1

 

where N(0) = 1 and N(1) = 2

 

Step-01:

 

Substituting H = 3 in the recursive relation, we get-

N(3) = N(3-1) + N(3-2) + 1

N(3) = N(2) + N(1) + 1

N(3) = N(2) + 2 + 1                (Using base condition)

N(3) = N(2) + 3                      …………(1)

 

To solve this recursive relation, we need the value of N(2).

 

Step-02:

 

Substituting H = 2 in the recursive relation, we get-

N(2) = N(2-1) + N(2-2) + 1

N(2) = N(1) + N(0) + 1

N(2) = 2 + 1 + 1                 (Using base conditions)

∴ N(2) = 4                          …………(2)

 

Step-03:

 

Using (2) in (1), we get-

N(3) = 4 + 3

∴ N(3) = 7

Thus, minimum number of nodes required to construct AVL tree of height-3 = 7.

 

Problem-02:

 

Find the minimum number of nodes required to construct AVL Tree of height = 4.

 

Solution-

 

We know, minimum number of nodes in AVL tree of height H is given by a recursive relation-

 

N(H) = N(H-1) + N(H-2) + 1

 

where N(0) = 1 and N(1) = 2

 

Step-01:

 

Substituting H = 4 in the recursive relation, we get-

N(4) = N(4-1) + N(4-2) + 1

N(4) = N(3) + N(2) + 1               …………(1)

 

To solve this recursive relation, we need the value of N(2) and N(3).

 

Step-02:

 

Substituting H = 2 in the recursive relation, we get-

N(2) = N(2-1) + N(2-2) + 1

N(2) = N(1) + N(0) + 1

N(2) = 2 + 1 + 1                 (Using base conditions)

∴ N(2) = 4                          …………(2)

 

Step-03:

 

Substituting H = 3 in the recursive relation, we get-

N(3) = N(3-1) + N(3-2) + 1

N(3) = N(2) + N(1) + 1

N(3) = 4 + 2 + 1                 (Using (2) and base condition)

∴ N(3) = 7                          …………(3)

 

Step-04:

 

Using (2) and (3) in (1), we get-

N(4) = 7 + 4 + 1

∴ N(4) = 12

Thus, minimum number of nodes required to construct AVL tree of height-4 = 12.

 

Problem-03:

 

What is the maximum height of any AVL tree with 10 nodes?

 

Solution-

 

For calculating the maximum height of AVL tree with n nodes, we use a recursive relation-

 

N(H) = N(H-1) + N(H-2) + 1

 

Step-01:

 

Substituting H = 2 in the recursive relation, we get-

N(2) = N(2-1) + N(2-2) + 1

N(2) = N(1) + N(0) + 1

N(2) = 2 + 1 + 1                (Using base conditions)

∴ N(2) = 4                          …………(1)

 

So, minimum number of nodes required to construct AVL tree of height-2 = 4.

 

Step-02:

 

Substituting H = 3 in the recursive relation, we get-

N(3) = N(3-1) + N(3-2) + 1

N(3) = N(2) + N(1) + 1

N(3) = 4 + 2 + 1                (Using (1) and base condition)

∴ N(3) = 7                          …………(2)

 

So, minimum number of nodes required to construct AVL tree of height-3 = 7.

 

Step-03:

 

Substituting H = 4 in the recursive relation, we get-

N(4) = N(4-1) + N(4-2) + 1

N(4) = N(3) + N(2) + 1

N(4) = 7 + 4 + 1                (Using (1) and (2))

∴ N(4) = 12

 

So, minimum number of nodes required to construct AVL tree of height-4 = 12.

 

But given number of nodes = 10 which is less than 12.

Thus, maximum height of AVL tree that can be obtained using 10 nodes = 3.

 

Problem-04:

 

What is the maximum height of any AVL tree with 77 nodes?

 

Solution-

 

For calculating the maximum height of AVL tree with n nodes, we use a recursive relation-

 

N(H) = N(H-1) + N(H-2) + 1

 

Step-01:

 

Substituting H = 2 in the recursive relation, we get-

N(2) = N(2-1) + N(2-2) + 1

N(2) = N(1) + N(0) + 1

N(2) = 2 + 1 + 1                (Using base conditions)

∴ N(2) = 4                          …………(1)

 

So, minimum number of nodes required to construct AVL tree of height-2 = 4.

 

Step-02:

 

Substituting H = 3 in the recursive relation, we get-

N(3) = N(3-1) + N(3-2) + 1

N(3) = N(2) + N(1) + 1

N(3) = 4 + 2 + 1                (Using (1) and base condition)

∴ N(3) = 7                          …………(2)

 

So, minimum number of nodes required to construct AVL tree of height-3 = 7.

 

Step-03:

 

Substituting H = 4 in the recursive relation, we get-

N(4) = N(4-1) + N(4-2) + 1

N(4) = N(3) + N(2) + 1

N(4) = 7 + 4 + 1                (Using (1) and (2))

∴ N(4) = 12                        …………(3)

 

So, minimum number of nodes required to construct AVL tree of height-4 = 12.

 

Step-04:

 

Substituting H = 5 in the recursive relation, we get-

N(5) = N(5-1) + N(5-2) + 1

N(5) = N(4) + N(3) + 1

N(5) = 12 + 7 + 1                (Using (2) and (3))

∴ N(5) = 20                          …………(4)

 

So, minimum number of nodes required to construct AVL tree of height-5 = 20.

 

Step-05:

 

Substituting H = 6 in the recursive relation, we get-

N(6) = N(6-1) + N(6-2) + 1

N(6) = N(5) + N(4) + 1

N(6) = 20 + 12 + 1                (Using (3) and (4))

∴ N(6) = 33                            …………(5)

 

So, minimum number of nodes required to construct AVL tree of height-6 = 33.

 

Step-06:

 

Substituting H = 7 in the recursive relation, we get-

N(7) = N(7-1) + N(7-2) + 1

N(7) = N(6) + N(5) + 1

N(7) = 33 + 20 + 1                (Using (4) and (5))

∴ N(7) = 54                            …………(6)

 

So, minimum number of nodes required to construct AVL tree of height-7 = 54.

 

Step-07:

 

Substituting H = 8 in the recursive relation, we get-

N(8) = N(8-1) + N(8-2) + 1

N(8) = N(7) + N(6) + 1

N(8) = 54 + 33 + 1                (Using (5) and (6))

∴ N(8) = 88                            …………(6)

 

So, minimum number of nodes required to construct AVL tree of height-8 = 88.

 

But given number of nodes = 77 which is less than 88.

Thus, maximum height of AVL tree that can be obtained using 77 nodes = 7.

 

Next Article- Insertion in AVL Tree

 

Get more notes and other study material of Data Structures.

Watch video lectures by visiting our YouTube channel LearnVidFun.

Left Recursion | Left Recursion Elimination

Recursion-

 

Recursion can be classified into following three types-

 

 

  1. Left Recursion
  2. Right Recursion
  3. General Recursion

 

1. Left Recursion-

 

  • A production of grammar is said to have left recursion if the leftmost variable of its RHS is same as variable of its LHS.
  • A grammar containing a production having left recursion is called as Left Recursive Grammar.

 

Example-

 

S → Sa / 

(Left Recursive Grammar)

 

  • Left recursion is considered to be a problematic situation for Top down parsers.
  • Therefore, left recursion has to be eliminated from the grammar.

 

Elimination of Left Recursion

 

Left recursion is eliminated by converting the grammar into a right recursive grammar.

 

If we have the left-recursive pair of productions-

Aα / β

(Left Recursive Grammar)

where β does not begin with an A.

 

Then, we can eliminate left recursion by replacing the pair of productions with-

→ βA’

A’ → αA’ / ∈

(Right Recursive Grammar)

 

This right recursive grammar functions same as left recursive grammar.

 

2. Right Recursion-

 

  • A production of grammar is said to have right recursion if the rightmost variable of its RHS is same as variable of its LHS.
  • A grammar containing a production having right recursion is called as Right Recursive Grammar.

 

Example-

 

S → aS / 

(Right Recursive Grammar)

 

  • Right recursion does not create any problem for the Top down parsers.
  • Therefore, there is no need of eliminating right recursion from the grammar.

 

Also Read- Types of Recursive Grammar

 

3. General Recursion-

 

  • The recursion which is neither left recursion nor right recursion is called as general recursion.

 

Example-

 

S → aSb / 

 

PRACTICE PROBLEMS BASED ON LEFT RECURSION ELIMINATION-

 

Problem-01:

 

Consider the following grammar and eliminate left recursion-

A → ABd / Aa / a

B → Be / b

 

Solution-

 

The grammar after eliminating left recursion is-

A → aA’

A’ → BdA’ / aA’ / 

B → bB’

B’ → eB’ / 

 

Problem-02:

 

Consider the following grammar and eliminate left recursion-

E → E + E / E x E / a

 

Solution-

 

The grammar after eliminating left recursion is-

E → aA

A → +EA / xEA / 

 

Problem-03:

 

Consider the following grammar and eliminate left recursion-

E → E + T / T

T → T x F / F

F → id

 

Solution-

 

The grammar after eliminating left recursion is-

E → TE’

E’ → +TE’ / 

T → FT’

T’ → xFT’ / 

F → id

 

Problem-04:

 

Consider the following grammar and eliminate left recursion-

S → (L) / a

L → L , S / S

Solution-

 

The grammar after eliminating left recursion is-

S → (L) / a

L → SL’

L’ → ,SL’ / 

 

Problem-05:

 

Consider the following grammar and eliminate left recursion-

S → S0S1S / 01

 

Solution-

 

The grammar after eliminating left recursion is-

S → 01A

A → 0S1SA / 

 

Problem-06:

 

Consider the following grammar and eliminate left recursion-

S → A

A → Ad / Ae / aB / ac

B → bBc / f

 

Solution-

 

The grammar after eliminating left recursion is-

S → A

A → aBA’ / acA’

A’ → dA’ / eA’ / 

B → bBc / f

 

Problem-07:

 

Consider the following grammar and eliminate left recursion-

A → AAα / β

Solution-

 

The grammar after eliminating left recursion is-

A → βA’

A’ → AαA’ / 

 

Problem-08:

 

Consider the following grammar and eliminate left recursion-

A → Ba / Aa / c

B → Bb / Ab / d

 

Solution-

 

This is a case of indirect left recursion.

 

Step-01:

 

First let us eliminate left recursion from A → Ba / Aa / c

 

Eliminating left recursion from here, we get-

A → BaA’ / cA’

A’ → aA’ / 

 

Now, given grammar becomes-

A → BaA’ / cA’

A’ → aA’ / 

B → Bb / Ab / d

 

Step-02:

 

Substituting the productions of A in B → Ab, we get the following grammar-

A → BaA’ / cA’

A’ → aA’ / 

B → Bb / BaA’b / cA’b / d

 

Step-03:

 

Now, eliminating left recursion from the productions of B, we get the following grammar-

A → BaA’ / cA’

A’ → aA’ / 

B → cA’bB’ / dB’

B’ → bB’ / aA’bB’ / 

 

This is the final grammar after eliminating left recursion.

 

Problem-09:

 

Consider the following grammar and eliminate left recursion-

X → XSb / Sa / b

S → Sb / Xa / a

Solution-

 

This is a case of indirect left recursion.

 

Step-01:

 

First let us eliminate left recursion from X → XSb / Sa / b

 

Eliminating left recursion from here, we get-

X → SaX’ / bX’

X’ → SbX’ / 

 

Now, given grammar becomes-

X → SaX’ / bX’

X’ → SbX’ / 

S → Sb / Xa / a

 

Step-02:

 

Substituting the productions of X in S → Xa, we get the following grammar-

X → SaX’ / bX’

X’ → SbX’ / 

S → Sb / SaX’a / bX’a / a

 

Step-03:

 

Now, eliminating left recursion from the productions of S, we get the following grammar-

X → SaX’ / bX’

X’ → SbX’ / 

S → bX’aS’ / aS’

S’ → bS’ / aX’aS’ / 

 

This is the final grammar after eliminating left recursion.

 

Problem-10:

 

Consider the following grammar and eliminate left recursion-

S → Aa / b

A → Ac / Sd / 

Solution-

 

This is a case of indirect left recursion.

 

Step-01:

 

First let us eliminate left recursion from S → Aa / b

This is already free from left recursion.

 

Step-02:

 

Substituting the productions of S in A → Sd, we get the following grammar-

S → Aa / b

A → Ac / Aad / bd / 

 

Step-03:

 

Now, eliminating left recursion from the productions of A, we get the following grammar-

S → Aa / b

A → bdA’ / A’

A’ → cA’ / adA’ / 

 

This is the final grammar after eliminating left recursion.

 

Also Read- Left Factoring

 

To gain better understanding about Left Recursion Elimination,

Watch this Video Lecture

 

Next Article- Types of Grammars

 

Get more notes and other study material of Theory of Automata and Computation.

Watch video lectures by visiting our YouTube channel LearnVidFun.