Javatpoint Logo
Javatpoint Logo

DFA-based division in C++

In this article, you will learn the DFA-based division in C++ with its example.

Using a Deterministic Finite Automaton (DFA) to Check Divisibility

Division using deterministic finite automata (DFA) is a technique that can efficiently implement integer division in hardware. The basic idea is constructing a DFA that recognizes strings representing the division process bit by bit.

If you want to divide two n-bit integers A and B, you can construct a DFA with 2n+1 states that compute the division A/B one bit at a time, from the most significant bit to the least important bit.

The steps are:

  1. Construct a DFA with 2n+1 states labelled 0 to 2n. State 0 is the start state, and State 2n is the accepted State.
  2. The transition function of the DFA is defined based on the division algorithm:
    1. If the current State + the next bit of A is less than B, the transition to the new State is obtained by shifting left and adding the next bit of A.
    2. Otherwise, transition to the new State obtained by shifting left, adding the next bit of A and subtracting B.
  3. The DFA accepts when it reaches the accept State 2n, at which point the division is complete.
  4. The quotient is obtained by recording the number of subtractions performed in each State. The remainder is the last State reached.

It can be implemented in C++ using a DFA class with a transition table, current State, lookup functions, etc. The division can be performed by stepping through the DFA bit-by-bit while keeping track of the quotient and remainder.

The advantage of the DFA approach is that it only requires simple comparisons, additions and subtractions, making it very fast in hardware. It can compute an n-bit division in O(n) time with a fixed-size DFA.

Example:

Here is another explanation of DFA-based division using the example of division by 3:

The key idea is constructing a DFA with k states, where k is the divisor. The states represent the possible remainders 0 to k-1.

The transition function is based on the following:

  • Current State = the remainder so far
  • Next input bit = 0 or 1
  • Next state = (2 * current state + input bit) % k

It computes the new remainder after shifting left and adding the new bit. Modulo K maps it back to one of the k states.

For example, to check if a binary number is divisible by 3 (k=3):

  • States = {0, 1, 2}
  • start state = 0
  • final State = 0 (remainder 0 means divisible by 3)

Transitions:

  • State 0, input 0 -> 0 (2*0 + 0 = 0 % 3 = 0)
  • State 0, input 1 -> 1 (2*0 + 1 = 1 % 3 = 1)
  • State 1, input 0 -> 2 (2*1 + 0 = 2 % 3 = 2)
  • State 1, input 1 -> 0 (2*1 + 1 = 3 % 3 = 0)
  • State 2, input 0 -> 1 (2*2 + 0 = 4 % 3 = 1)
  • State 2, input 1 -> 2 (2*2 + 1 = 5 % 3 = 2)

To divide 6 (110 in binary) by 3:

  • Start at State 0
  • Input 1 -> new state 1
  • Input 1 -> new state 0
  • Input 0 -> new state 0

Reach final state 0, so 6 is divisible by 3.

For 4 (100 in binary):

  • Start at State 0
  • Input 1 -> new state 1
  • Input 0 -> new state 2
  • Input 0 -> new state 1

End in non-zero State, so remainder is 1.

This DFA approach allows division in O(n) time with simple state transitions. The final State gives the remainder, indicating if the number is divisible.

Implementation of C++ Code:

Output:

Enter the dividend (as an integer): 1234
Enter the divisor (as an integer): 2
Result: 617






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA