I'm trying for several days to simulate a non-deterministic finite automata, using a map I'm storing state transitions, exactly as indicated in this post.
The problem is they are missing the nondeterministic transitions, ie those which by the same symbol, lead me to different states. Here's my code:
#include <iostream>
#include <map>
#include <utility>
#include <iterator> // for ostream_iterator
using namespace std;
int main (){
freopen ("testMap.in", "r", stdin);
int testCases;
int i, j;
int stateOrigin, stateDestination;
char transitionCharacter ;
int numberTransitions=8;
typedef map<pair<int, char>, int> transitions;
transitions trans;
for (j=0; j<numberTransitions;j++){
cin>> stateOrigin>>stateDestination>>transitionCharacter;
trans.insert(transitions::value_type(std::make_pair(stateOrigin,transitionCharacter), stateDestination ));
}
map<pair<int, char>, int>::iterator p = trans.begin();
for( p = trans.begin(); p != trans.end(); p++ ){
cout << p->first.first<< " "<<p->first.second<<" "<<p->second<<endl;
}
return 0;
}
When I print the entire contents of the map, this tell me:
0 a 0
1 b 1
1 c 2
3 d 4
4 d 4
and the expected output is:
0 0 a
0 1 a
1 1 b
1 2 c
1 3 c
3 4 d
4 4 d
4 5 d
What am I doing wrong. In another question, answered that the best way to simulate the transitions of nondeterministic finite automaton is to use map, but use map is appropriate in this type of problem or can be solved in any way?. Why these values are lost?
It is convenient to change the map structure ? i.e:
typedef map<pair<int, int>, char> transitions;
A map is a 1-to-1
relationship between a key and a value, so it can represent a deterministic automata.
A non-deterministic automata can be represented by a 1-to-many
associative container.
I.e. you need a std::multimap
or you can keep on using a std::map
with the value type a different container:
typedef map<pair<int, int>, vector<char> > transitions;
so you can have multiple char
values for each int
pair.