I am currently learning dynamic programming and i can't figure this problem out. Could someone give me an algorithm for it? : Consider a directed graph G = (V,E) where each edge is labeled with a character from an alphabet Sigma, and we designate a special vertex s as the start vertex, and another f as the final vertex. We say that G accepts a string A = a1a2 . . . an if there is a path from s to f of n edges whose labels spell the sequence A. Design an O((|V | + |E|)n) dynamic programming algorithm to determine whether or not A is accepted by G.
1 Answer
Let
first (str) return the first letter of str
Let len(str) return the length of str
Let rem(str) return str with the first character stripped off.
func (str, v1) =
true if
len(str)=0 and s == f
or
func(rem(str), v2) is true for any v2 such that there exists an edge connecting v1, v2 labeled first(str)
The values of f (str, v) can be memoised to avoid unnecessary recursive calls.
3 Comments
Apiwat Chantawibul
concise answer, but (1) where do you return
false (2) the FSM is edge-labelled, so checking v2 == first(str) is a bit weird. maybe label(edge(v1,v2)) == first(str).Apiwat Chantawibul
Sorry for the first remark, now I see where
false value originate.Tarik
@Billiska Thanks for your remark. For some reason, I thought that the vertices held the labels. Corrected my answer accordingly. As for being concise, I generally do not believe in spoon feeding, especially for what appears to be a students' homework.
O(S+V+E), whereSis the length ofA.