dfa division in csharp

To implement division in DFA, we need to first obtain the DFA for the regular expression that represents the dividend and the DFA for the regular expression that represents the divisor. We can then use these two DFAs to construct the DFA for the regular expression that represents the quotient.

Here is a sample code that demonstrates how to perform division in C# with DFA:

main.cs
using System;
using System.Collections.Generic;

class Program 
{
    static void Main() 
    {
        // DFA for the dividend (1011)*
        var dividend = new Dictionary<(int, char), int> 
        {
            [(0, '1')] = 1,
            [(0, '0')] = -1,
            [(1, '0')] = 2,
            [(1, '1')] = 1,
            [(2, '1')] = 3,
            [(2, '0')] = -1,
            [(3, '0')] = 0,
            [(3, '1')] = 1,
            [(4, '0')] = 4,
            [(4, '1')] = 4,
            [(5, '0')] = 5,
            [(5, '1')] = 5,
            [(6, '0')] = 6,
            [(6, '1')] = 6
        };

        // set of accepting states for the dividend DFA
        var dividendAccept = new HashSet<int> {0};

        // DFA for the divisor 10
        var divisor = new Dictionary<(int, char), int> 
        {
            [(0, '1')] = 1,
            [(0, '0')] = 0,
            [(1, '0')] = 2,
            [(1, '1')] = -1, 
            [(2, '0')] = 0, 
            [(2, '1')] = 3, 
            [(3, '0')] = 3,
            [(3, '1')] = -1,
            [(4, '0')] = 4,
            [(4, '1')] = 4,
            [(5, '0')] = 5,
            [(5, '1')] = 5,
            [(6, '0')] = 6,
            [(6, '1')] = 6
        };

        // set of accepting states for the dividend DFA
        var divisorAccept = new HashSet<int> {2};

        // construct the quotient DFA
        var quotient = new Dictionary<(int, char), int>();
        var quotientAccept = new HashSet<int>();

        foreach (var dState in dividend)
        {
            var qState = (dState.Key.Item1, dState.Key.Item2 == '0' ? 0 : divisor[dState.Value, dState.Key.Item2]);
            
            if (dividendAccept.Contains(dState.Key.Item1) && divisorAccept.Contains(qState.Item2))
                quotientAccept.Add(qState.Item1);

            quotient[dState.Key] = qState.Item1;
        }

        Console.WriteLine("Dividend DFA");
        foreach (var state in dividend)
            Console.WriteLine($"{state.Key.Item1} --{state.Key.Item2}--> {state.Value}");

        Console.WriteLine("\nDivisor DFA");
        foreach (var state in divisor)
            Console.WriteLine($"{state.Key.Item1} --{state.Key.Item2}--> {state.Value}");

        Console.WriteLine("\nQuotient DFA");
        foreach (var state in quotient)
            Console.WriteLine($"{state.Key.Item1} --{state.Key.Item2}--> {state.Value}");

        Console.WriteLine("\nQuotient Accepting States: " + string.Join(',', quotientAccept));
    }
}
2495 chars
81 lines

In this example, we have defined the DFAs for the dividend (1011)* and the divisor 10. We then use these two DFAs to construct the DFA for the quotient. The final DFA is output to the console along with the accepting states for the quotient DFA.

Note that in the foreach loop that constructs the quotient DFA, we use the DFA for the divisor to determine the next state based on the input symbol. We also check if the current state in the dividend DFA and the next state in the quotient DFA are both accepting states. If so, we add the current state in the dividend DFA to the set of accepting states for the quotient DFA.

gistlibby LogSnag