Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7976465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:54:57+00:00 2026-06-04T08:54:57+00:00

For some days I’m trying to do a program for simulate a nondeterministic finite

  • 0

For some days I’m trying to do a program for simulate a nondeterministic finite automaton (NFA), more specifically, a string recognizer. After several failures, thanks to the user Konrad Rudolph, I could implement a solution based on this pseudocode:

Well, in an NFA you have a set of current states, and in each step you go through all current states, and for each, you select all valid transitions. Those combined sets form your new state set.

At the end, you check whether the intersection of the current states and the accepting states is non-empty.

In Pseudo-code this looks as follows:

current = { initial }
    for each char in input:
        next = { }
        for each state in current:
            for each transition in transitions[state][char]:
                next.append(target_of(transition))
        current = next
if len(intersection(current, accepting)) > 0:
    print "String accepted"
else:
    print "String rejected"

This can be translated, line by line, into C++ code. He recommended to make this easy, use std::set<int> for the current and next sets

Herés my implementation in c++:

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <utility>
#include <vector>

using namespace std;

int main (){

    int testCases, i, j,k, cont=1,finalStates,numberInputs,stateOrigin, stateDestination;
    int numberStates, numberTransitions, initialState;
    int  numberFinals;
    char transitionCharacter ;

    set<int> current;
    set<int> next;
    set<int>::iterator it;
    set <int> final;
    set<int> the_intersection;  // Destination of intersect
    map<pair<int, int>, char>::iterator p;
    string inputString;

    typedef std::pair<int, int> trigger;
    std::map<trigger, char> transitions;
    map<trigger, char>::iterator r;

    cin>> testCases;
    for (i=0;i< testCases;i++){
        
        final.clear();
        next.clear();
        current.clear();
        the_intersection.clear();
        transitions.clear();
        cin>>numberStates>>numberTransitions>>initialState>>numberFinals;

        for (j=0;j<numberFinals;j++){
            cin>>finalStates;
            final.insert(finalStates);
        }

        for (j=0; j<numberTransitions;j++){
            cin>> stateOrigin>>stateDestination>>transitionCharacter;
            transitions.insert(make_pair(make_pair(stateOrigin, stateDestination), transitionCharacter));
        }

        cin>>numberInputs;
        current.insert (initialState);
        cout<<"Test Case #"<<cont++<<":"<<endl;
        
        for (j=0; j<numberInputs;j++){
            current.clear();
            current.insert (initialState);
            the_intersection.clear();
            cin>> inputString;
            cout<<inputString<<" ";
            
            /// ///////////////Konrad Rudolph's solution /////////////////
            for (k=0; k<inputString.size();k++){
                next.clear();
                for (it = current.begin(); it!=current.end(); it++){
                    for (r =transitions.begin(); r!=transitions.end(); r++){
                        if ( ((*r).first.first == *it) && ( (*r).second == inputString[k] ) ){
                            next.insert((*r).first.second);
                        }
                    }
                    current = next;
                }
            }
            std::set_intersection(current.begin(), current.end(), final.begin(), final.end(), std::inserter(the_intersection, the_intersection.end()));

            if (the_intersection.empty()){
                cout<<"Rejected"<<endl;
            }else {
                cout<<"Acepted"<<endl;
            }
            
            /// ///////////////////////////////////////////////////////
        }
        cout<<endl;
    }
return 0;
}

I have this input:

1
6 8 0 2
2
5
0 0 a
0 1 a
1 1 b
1 2 c
1 3 c
3 4 d
4 4 d
4 5 d
5
aaabcccc
aabbbbcdc
abbcdddcc
abc
acdddddd

the expected output is:

Test Case #1:
aaabcccc Rejected
aabbbbcdc Rejected
abbcdddcc Rejected
abc Acepted
acdddddd Acepted

However, my code produces as output:

Test Case #1:
aaabcccc Rejected
aabbbbcdc Rejected
abbcdddcc Rejected
abc Acepted
acdddddd

And for the last string of the test case the program is doing nothing, just does not stop run. My question is Why does my program crashes with this particular input. I designed the same automaton NFA in JFlap and recognizes this last input

acdddddd

.

enter image description here

(0, a) = 1
(1, c) = 2
(2, d) = 3
(3, d) = 4
(4, d) = 4
(4, d) = 4
(4, d) = 4
(4, d) = 5

What mistake I’m having in the implementation of my code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T08:54:58+00:00Added an answer on June 4, 2026 at 8:54 am

    You want to do;

    for each char in input:
        next = { }
        for each state in current:
            for each transition in transitions[state][char]:
                next.append(target_of(transition))
        current = next
    

    but what you’re doing is;

    for each char in input:
        next = { }
        for each state in current:
            for each transition in transitions[state][char]:
                next.append(target_of(transition))
            current = next
    

    Subtle, but reassigning current while looping over it may cause your hang and will definitely not give the desired result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

after some days of trying and waitin' for answers on the springsource forums I'll
After read a lot about ball physics, watch some examples and few days of
for some days now I have been trying to make a simple mod_rewrite rule
Some days age, I mistakenly pasted a URL in my Java program in a
some days are just like this. Ok, I'm trying to fetch some text using
For some reason, after some days working with XCode4, it simply stopped showing the
I have been trying for some days to use the channel.send_message API in GAE
I have been trying since some days now to find a concrete answer for
In a nutshell: my JBoss instance is running ok, but after some days it's
after some days learning html and css i have got used to programming in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.