Click here to Skip to main content
6,595,444 members and growing! (21,201 online)
Email Password   helpLost your password?
General Programming » Algorithms & Recipes » General     Beginner

A Deterministic Finite Automaton Class in C#

By hanzzoid

A simple C# class implementation of a DFA for testing purposes.
C# 2.0, Windows, .NET 2.0VS2005, Dev, QA
Posted:31 May 2007
Views:11,254
Bookmarked:15 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
7 votes for this article.
Popularity: 2.16 Rating: 2.56 out of 5
4 votes, 57.1%
1

2

3

4
3 votes, 42.9%
5

Introduction

The above downloadable zip file contains just a small C# class, which is a simple implementation of a deterministic finite automaton (DFA). I encountered the matter during the theoretical computer science course at my college, and found it useful to be able to actually test all those DFA's we had to construct merely on paper.

Background

For those who don't know, a DFA is a theoretical machine model behind the concept of regular languages; and a DFA in particular is equivalent to a regular expression. For a complete introduction, see this wiki article.

Using the code

Just add the class to an existing project.

A simple example DFA for the regular language PARITY, which is the language of all binary strings with an odd number of ones, goes like this: f is the set of accepting states, delta is the transition matrix (aka state transition table, see comments within the code for more explanations about the structure of this matrix), and M our thereby defined DFA. In the for-loop, a little statistic testing is performed: the DFA-class contains a static RandomString method which is used here to generate a binary string of length 10.

Then, assuming you have a method called ArrayToString at your disposal (which should return a string), the random string is outputted, followed by the information whether the DFA M accepts this string.

 int[] f = new int[] { 1 };
 int[,] delta = new int[,] { { 0, 1 }, { 1, 0 } };

 DFA M = new DFA(delta, f);
 int[] s;

 for (int i = 0; i < 10; i++)
 {
     s = DFA.RandomString(10, 2);
    textBox1.AppendText(ArrayToString(s) + " - " 
    + M.Accepts(s).ToString() + System.Environment.Newline);
 } 

Points of Interest

The next step to perform could be a routine for the graphical display of such automata, but that seems to be more than an one-hour job...

Yours,

hanzzoid.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

hanzzoid


Member

Location: Germany Germany

Other popular Algorithms & Recipes articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 31 May 2007
Editor:
Copyright 2007 by hanzzoid
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project