Nondeterministic Finite Automata in PHP

A few weeks ago, I wrote an article about an implementation of a DFA (deterministic finite automaton) in PHP. Today, I present a implementation for an automaton, that is not deterministic. It means, that the automaton have no, one or more than one edges (for an element from the input alphabet) to an other state. Before I start to give you a launch into how the NFA in PHP works, I give you a short introduction into the theory of nondeterministic finite automata.

To demonstrate an easy example, I use a NFA, that accepts a word, which includes the substring ‘abc’. I used this example also in a previous article (about DFAs). You can compare the following automaton with the DFA that accepts the same words and you would come to the conclusion, that the NFA is easier to build. Thats the truth, but on the other hand, in most (not all) cases the NFA needs more time to execute all simultanous calculations for a token from the alphabet.

An automaton needs (for formal description) an input alphabet. For that example, the alphabet is specified as:

for this example:
Σ = {a,b,c}

A few sentences ago i mentioned, that the NFA needs more time to execute all simultanous calculations. But why? Well, let us choose the input word ‘ccababca’. (For further demonstrations I will use s1, s2, s3 and s4 as names for the states [blue circle] from the left to the right) We start the simulation with the first two tokens ‘cc’. The NFA remains in s1 (state 1). After that the NFA simulates for the token ‘a’. In s1 with input ‘a’ the NFA can go to s2 and can also stay in s1 (self-loop). Here we have the main problem! The NFA simulates all possible calculations and that is the problem, that the NFA has. After token ‘a’ comes token ‘b’ and for that token, the NFA simulates s1 and s3.

The PHP implementation of the NFA works with the transition table of every state. Outward from the starting state(s), the algorithm is looking for a transition from the current state to another – the successor state. After the input word was read, the NFA checks whether it is currently in a final state or not. The formalization of a final state is defined with a thicker border line (as you can see in the sketch above the text). In the PHP implementation it is just a boolean value. You can also choose more than one starting states with:

$nfa = new NFA('test_nfa');
//adding new states
//declare starting states
$nfa->setStartingStates(array('s0','s1'));

The function, explained below, is very important to build NFA’s – but not neccessary. In the code example below, we don’t need this function, therefore it has an own example above.

The code example shows the code fragment, that has the equal functionality as the automaton in the sketch.

require 'NFA.class.php';

$nfa = new NFA('test_nfa');

$s1 = new NFA_State('s1');
$s1->setTransition('b', 's1');
$s1->setTransition('c', 's1');
$s1->setTransition('a', 's2');
$s1->setTransition('a', 's1');
$nfa->setState($s1);

$s2 = new NFA_State('s2');
$s2->setTransition('b', 's3');
$nfa->setState($s2);

$s3 = new NFA_State('s3');
$s3->setTransition('c', 's4');
$nfa->setState($s3); 

$s4 = new NFA_State('s4');
$s4->setTransition('c', 's4');
$s4->setTransition('b', 's4');
$s4->setTransition('a', 's4');
$s4->setFinal(true);
$nfa->setState($s4);

$nfa->setStartingState('s1');

if($nfa->run('accbbababcababa'))
  echo "NFA accepts the input word!";
else
  echo "NFA doesn't accept the input word!";

As you can see in this example, the NFA equals a pattern matcher. With a regular expression parser you are able to build small NFA’s for every part of this expressions. After that you can concatenate this automatons to an automaton, that accepts your complete expression.

You can download the source files here. (NFA.class.php, NFA_State.class.php and test.php)

93 thoughts on “Nondeterministic Finite Automata in PHP

  1. Hence, it is a little bit frustrating to think about the fact that not all food vending machines have actually
    been changed yet to much healthier versions.

  2. ‘Are people really searching online for my product or services’.
    The Internet has changed the way we attain information forever and
    Google has been the main driving force and proponent behind this instant access to information. But in general here are
    the services that an online business owner should expect
    from SEO experts and other agencies and consultants:.
    A reputable SEO company won’t have any qualms about
    connecting potential clients with former ones.

  3. You really make it appear so easy along with your presentation however I to find this matter to be actually one thing which I believe I would never understand.

    It sort of feels too complicated and extremely extensive for
    me. I am looking forward for your next submit,
    I will attempt to get the hang of it!

  4. Another major difference between the two is that while PPC is a paid form of advertising,
    organic search engine optimization is absolutely free.
    The usual SEO methods include keyword research, link building and such.
    Thematic relevance is of key importance for the creation of quality backlinks.
    Tests with a model like Page – Rank have shown that the system is not infallible.

  5. Each of them has a different function according to the intensity of the misspelled keywords.
    Any business to conduct business on the web should have a
    web site created especially for creating an awareness of their products
    and ser-vices to the planet at large. Unlike TV, radio and other traditional marketing channels that need big budgets to be effective,
    SEO can be cost effective. You have to take price quotes from different SEO companies locally and
    internationally.

  6. Many small companies prefer to hire SEO specialists as consultants
    rather than full time employees, unless they have a certain amount of websites that need to
    be continuously maintained and optimized. The trends in this industry change and new
    developments take place only when Google comes
    up with some novel feature that ends up breaking every site’s SEO and compels them to consider a
    change in their current content presentation in order
    to do well. Depending on how how much time you have you can do
    this about once per week. Further the sites whose ranking using the search
    engines is good will attract potential clients, which results
    to mores sales.

  7. Each of them has a different function according to the
    intensity of the misspelled keywords. The usual SEO methods include keyword research, link building and
    such. Thematic relevance is of key importance for the creation of quality
    backlinks. Further the sites whose ranking using the search engines is good will attract potential clients, which results to mores sales.

  8. Get testimonials from the clients you already worked with, about your
    work and experiences. When Page – Rank was patented
    the patent was assigned to Stanford University.
    Auro – IN has a strong team that is built on the delivery of
    outstanding campaign results, customer service and appreciation,
    and high levels of technical capabilities and values.
    A guy named Alan Emtage, a student at the University of Mc –
    Gill, developed the first search engine for the Internet in 1990.

  9. If your site is relatively sound, the most important results for
    you to consider are the keyword hits. When Page – Rank was patented the patent was
    assigned to Stanford University. Depending on how how much time you
    have you can do this about once per week. Tests with a model like Page – Rank have shown that
    the system is not infallible.

  10. Although Yahoo isn’t the biggest, or the highest ranking search engine on the internet, it is still one
    of the most essential, and if you want use search engine optimization and promotion as a
    main technique in your marketing arsenal, you unquestionably need to get listed here.

    The process of SEO is the series of steps that are undertaken to ensure that a website is visible among internet
    users to an optimal level. Forgetting to write for
    an audience is one of the biggest mistakes that bloggers make.
    This mode of advertisement was faster than the earlier ones and reached more people but
    it had its own limitations.

  11. Each of them has a different function according to the
    intensity of the misspelled keywords. Improving Site
    Structure – This second part tells you how to properly structure URLs.
    While effective SEO needn’t be difficult, it does take work.
    Eventbrite is an online party-planning tool with KISS (Keep It Simple
    Stupid) design so it is usually a top rated choice among event planners (organizers).

  12. Many small companies prefer to hire SEO specialists as consultants rather
    than full time employees, unless they have a certain amount of websites that need to
    be continuously maintained and optimized.
    Any business to conduct business on the web should
    have a web site created especially for creating an awareness of their products and ser-vices to the planet at large.
    Depending on how how much time you have you can do this about once per week.
    This mode of advertisement was faster than the earlier ones and
    reached more people but it had its own limitations.

  13. Each of them has a different function according to the intensity of
    the misspelled keywords. The trends in this industry change and new developments take place only when Google comes up with some novel feature that ends up breaking
    every site’s SEO and compels them to consider a change in their current
    content presentation in order to do well. I am hoping to show you
    that I can help you achieve top rankings in the sites like I have for all my other clients.
    Eventbrite is an online party-planning tool with KISS (Keep It Simple Stupid) design so it is usually a top rated choice among
    event planners (organizers).

  14. ‘Are people really searching online for my product or services’.
    Improving Site Structure – This second part tells you how to properly structure URLs.
    Your baseline will tell you where your company began so you can measure positive or negative ROI (return on your investment).

    There are two techniques that combine to form a
    complete SEO.

  15. Get testimonials from the clients you already worked with, about your work and experiences.

    So what are you going to put in their search box is
    “website optimization tips”. Depending on how how much time
    you have you can do this about once per week.
    Further the sites whose ranking using the search engines is
    good will attract potential clients, which results to mores sales.

  16. Although Yahoo isn’t the biggest, or the highest ranking search engine on the internet,
    it is still one of the most essential, and if you want use search engine
    optimization and promotion as a main technique in your marketing arsenal, you unquestionably need to get
    listed here. Two: Research different services – Assuming
    that you are going to hire someone to do it for you, the next step is to research as many different services as possible.
    While effective SEO needn’t be difficult, it does take work.

    Further the sites whose ranking using the search engines is good will attract potential clients, which results to mores sales.

  17. Gone are the days of one way marketing or merely just posting comments about your site or other related topics.

    When Page – Rank was patented the patent was assigned to Stanford University.
    Your baseline will tell you where your company began so you can measure positive or negative
    ROI (return on your investment). There are two techniques that combine
    to form a complete SEO.

  18. Gone are the days of one way marketing or merely just posting comments about your site or other
    related topics. So what are you going to put in their search box
    is “website optimization tips”. Thematic relevance
    is of key importance for the creation of quality backlinks.
    Webmaster follows a long process to promote a website in top search engines
    (Google, Yahoo and Bing).

  19. ‘Are people really searching online for my product or services’.
    The Internet has changed the way we attain information forever and Google has been the
    main driving force and proponent behind this instant access to information. Depending on how how much time you have you can do this about once per week.
    Further the sites whose ranking using the search
    engines is good will attract potential clients,
    which results to mores sales.

  20. Each of them has a different function according to the
    intensity of the misspelled keywords. The Internet has changed the
    way we attain information forever and Google has been the main driving force and proponent
    behind this instant access to information. Thematic relevance is of key importance for the creation of quality backlinks.
    Tests with a model like Page – Rank have shown that the system is not infallible.

  21. Tile boards can easily be cut using a circular saw and you
    can use adhesive to attach it to your wall. Remember that you still have to do
    your thing even during the renovation so better set up a temporary bathroom.
    Hopefully the sections above have contributed to your understanding of major bathroom remodeling.

  22. I think the admin of this web page is really working hard in support of his web page, as
    here every stuff is quality based data.

  23. Although Yahoo isn’t the biggest, or the highest ranking search engine on the internet, it is still one of the
    most essential, and if you want use search engine optimization and promotion as a main technique in your marketing arsenal, you unquestionably need to get listed here.
    Improving Site Structure – This second part tells you how
    to properly structure URLs. But it is extremely important
    that anyone looking to hire an SEO professional be aware
    of the differences. A guy named Alan Emtage, a student at
    the University of Mc – Gill, developed the first
    search engine for the Internet in 1990.

  24. Gone are the days of one way marketing or merely just posting comments about
    your site or other related topics. The trends in this industry change and
    new developments take place only when Google comes up
    with some novel feature that ends up breaking every site’s
    SEO and compels them to consider a change in their current content presentation in order to do well.
    Your baseline will tell you where your company began so you can measure positive or negative ROI (return on your
    investment). A guy named Alan Emtage, a student at the University
    of Mc – Gill, developed the first search engine for the Internet in 1990.

Leave a Reply

Your email address will not be published. Required fields are marked *