Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Add functions rec_word() and find_word() to FastAutomaton, and modify…
Browse files Browse the repository at this point in the history
… function is_empty().
  • Loading branch information
mercatp committed Jul 19, 2016
1 parent ce64048 commit 42b95c5
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 6 deletions.
48 changes: 48 additions & 0 deletions src/sage/combinat/words/automataC.c
Expand Up @@ -739,6 +739,54 @@ bool emptyLangage (Automaton a)
return res;
}

bool findWord_rec (Automaton a, int e, int n, Dict *w, bool verb)
{
if (a.e[e].final)
{
if (verb)
printf("Alloue un mot de taille %d.\n", n);
*w = NewDict(n);
return true;
}
//indique que le sommet a été vu
a.e[e].final |= 2;
//parcours les fils
int i;
for (i=0;i<a.na;i++)
{
if (a.e[e].f[i] != -1)
{
if (a.e[a.e[e].f[i]].final & 2)
continue; //ce fils a déjà été vu
if (findWord_rec(a, a.e[e].f[i], n+1, w, verb))
{
if (verb)
{
printf("w[%d] = %d\n", n, i);
}
w->e[n] = i;
return true;
}
}
}
return false;
}

//rend un mot dans le langage de a
bool findWord (Automaton a, Dict *w, bool verb)
{
if (a.i == -1)
return false;
bool res = findWord_rec(a, a.i, 0, w, verb);
//remet les états finaux
int i;
for (i=0;i<a.n;i++)
{
a.e[i].final &= 1;
}
return res;
}

inline int contract (int i1, int i2, int n1)
{
return i1+n1*i2;
Expand Down
2 changes: 2 additions & 0 deletions src/sage/combinat/words/automataC.h
Expand Up @@ -13,6 +13,8 @@ struct Dict
};
typedef struct Dict Dict;

bool findWord (Automaton a, Dict *w, bool verb); //rend un mot dans le langage de a

Dict NewDict (int n);
void FreeDict (Dict d);
void printDict (Dict d);
Expand Down
41 changes: 35 additions & 6 deletions src/sage/combinat/words/cautomata.pyx
Expand Up @@ -79,6 +79,7 @@ cdef extern from "automataC.h":
bool IsCompleteAutomaton (Automaton a)
bool CompleteAutomaton (Automaton *a)
Automaton BiggerAlphabet (Automaton a, Dict d, int nna) #copy the automaton with a new bigger alphabet
bool findWord (Automaton a, Dict *w, bool verb)
void Test ()

#dictionnaire numérotant l'alphabet projeté
Expand Down Expand Up @@ -1070,6 +1071,33 @@ cdef class FastAutomaton:
res = equalsLangages(self.a, a2.a, d, minimized)
sig_off()
return Bool(res)

def find_word (self, bool verb=False):
sig_on()
cdef Dict w
res = findWord (self.a[0], &w, verb)
sig_off()
if not res:
return None
r = []
for i in range(w.n):
r.append(self.A[w.e[i]])
FreeDict(w)
return r

#determine if the word is recognized by the automaton or not
def rec_word (self, list w):
cdef int e = self.a.i
if e == -1:
return False
d = {}
for i,a in enumerate(self.A):
d[a] = i
for a in w:
e = self.a.e[e].f[d[a]]
if e == -1:
return False
return Bool(self.a.e[e].final)

def add_state (self, bool final):
sig_on()
Expand Down Expand Up @@ -1154,14 +1182,15 @@ cdef class FastAutomaton:

#tell if the language of the automaton is empty
#(this function is not very efficient)
def is_empty (self, ext=True):
if ext:
return self.emonde().emonde_inf().n_states() == 0
else:
return self.emonde().n_states() == 0
def is_empty (self, ext=False):
return (self.find_word() is None)
#if ext:
# return self.emonde().emonde_inf().n_states() == 0
#else:
# return self.emonde().n_states() == 0

#determine if the languages intersect
def intersect (self, FastAutomaton b, ext=True):
def intersect (self, FastAutomaton b, ext=False):
return not self.intersection(b).is_empty(ext)

def random_word (self, int nmin=-1, int nmax=100):
Expand Down

0 comments on commit 42b95c5

Please sign in to comment.