How to find the intersection of two NFA
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
The concept of representing languages using automata is a foundational aspect of theoretical computer science. Non-deterministic Finite Automata (NFA) are a type of automaton used to define languages. At times, it may be necessary to find the intersection of languages represented by two NFAs. This task aids in various applications ranging from lexical analysis to formal language processing. Here we delve into the technical aspect of determining the intersection of two NFAs.
Understanding NFAs
An NFA can be described formally by the tuple (Q, Σ, δ, q0, F) where:
• Q is a finite set of states.
• Σ is the input alphabet (finite set of symbols).
• δ: Q × Σε → P(Q) is the transition function where P(Q) denotes the power set of Q, representing transitions to multiple states for an input symbol.
• q0 is the start state (q0 ∈ Q).
• F is the set of accept states (F ⊆ Q).
Key Characteristics
• Non-Determinism: NFAs allow transitions to multiple states without specific criteria or an empty input (denoted by ε-transitions). • Acceptance: An NFA accepts a string if there exists a sequence of transitions (following the input symbols) from the start state to any accept state.
Finding the Intersection of Two NFAs
The intersection of two NFAs, A1 and A2, results in an NFA that accepts only those strings that are simultaneously accepted by A1 and A2. To achieve this, we use a construction analogous to building the Cartesian product of the two automata.
Construction Method
- Input Automata: • Let
A1 = (Q1, Σ, δ1, q0_1, F1)andA2 = (Q2, Σ, δ2, q0_2, F2)be the given NFAs. - Product Automaton Construction: • Define the Cartesian product
A = (Q, Σ, δ, q0, F)where the components are constructed as: •Q = Q1 × Q2: The set of states is the Cartesian product ofQ1andQ2. •Σ: The input alphabet for both automata. •δ: (q1, q2) ∈ Q × Σε → P(Q): Transition function defined by combining the individual transitions forA1andA2. •q0 = (q0_1, q0_2): The start state is the pair of the individual start states. •F = F1 × F2: The accept states are pairs where both components are accept states in their respective automata. - Transition Function Definition: • For each element
(q1, q2) ∈ Qand each inputa ∈ Σ, the transition is given by:
Example
Suppose we have:
• A1 with Q1 = \{0, 1\}, Σ = \{a, b\}, δ1 = \{0: \{a: \{1\}\}, 1: \{b: \{0\}\}\}, q0_1 = 0, F1 = \{1\}.
• A2 with Q2 = \{x, y\}, Σ = \{a, b\}, δ2 = \{x: \{a: \{y\}\}, y: \{b: \{x\}\}\}, q0_2 = x, F2 = \{y\}.
The intersection product NFA A will have:
• Q = \{(0, x), (0, y), (1, x), (1, y)\}
• q0 = (0, x)
• F = \{(1, y)\}
• Transition function δ would result in transitions such as:
Summary Table
| Component | Description |
States (Q) | Cartesian product of states of A1 and A2: Q = Q1 × Q2 |
Alphabet (Σ) | Common input alphabet of A1 and A2 |
Transitions (δ) | Defined for state pairs and input by combining each δ1 and δ2 transition using δ((q1, q2), a) = \{(p1, p2)\} |
Start State (q0) | Pair of initial states: q0 = (q0_1, q0_2) |
Accept States (F) | Pairs of accept states: F = F1 × F2 |
Conclusion
Combining NFAs to create their intersection is a powerful technique for language and automata theory applications. By leveraging cartesian product constructions and defining the transition functions accordingly, the resulting NFA captures precisely the language intersection. This process forms the basis for complex language operations and computational language processing within computer science.
Related reading
- How to find the intersection point between a line and a rectangle?
- How to find the kth largest element in an unsorted array of length n in On?
- How to find the kth smallest element in the union of two sorted arrays?
- How to find the Largest Difference in an Array
- how to find the least number of operations to compute xn
- How to find the length of a linked list that is having cycles in it?
- How to find the lexicographically smallest string by reversing a substring?
- How to find the lowest common ancestor of two nodes in any binary tree?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.