Given:
- A char string
S
lengthl
containing only characters from'a'
to'z'
- A set of
ordered
substitution rulesR
(in the formX->Y
) wherex
,y
are single letters from'a' to 'z'
(eg,'a' -> ' e'
could be a valid rule but'ce'->'abc'
would never be a valid rule)
When a rule r
in R
is applied on S
, all letters of S
which are equal to the left side
of the rule r
would be replaced by the letter in the right side
of r
, if the rule r
cause any replacement in S
, r
is called triggered rule
.
Flowchart (Algorithm) :
(1) Alternately apply all
rules in R
(following the order of rules in R
) on S
.
(2) While (there exists any 'triggered rule' DURING (1) )
: repeat (1)
(3) Terminate
The question is: Is there any way to determine if with a given string S and set R, the algorithm would terminate or not (running forever)
Example1 : (manually executed)
S =
'abcdef'
R ={ 'a'->'b' , 'b' -> 'c' }
(the order is implied the order of appearance from left to right of each rule)
Ater running algorithm on S and R:
(1.1): 'abcdef' --> 'bbcdef' --> 'cccdef'
(2.1): repeat (1) because there are 2 replacements during the (1.1)
(1.2): 'cccdef'
(2.2): continue to (3) because there is no replacement during the (1.2)
(3) : terminate the algorithm
=> The algorithm terminate with the given S and R
Example2:
S =
'abcdef'
R ={ 'a'->'b' , 'b' -> 'a' }
(the order is implied the appearance order from left to right of each rule)
Ater running algorithm on S and R:
(1.1): 'abcdef' --> 'bbcdef' --> 'abcdef'
(2.1): repeat (1) because there are 2 replacements during the (1.1)
(1.2): 'abcdef
--> 'bbcdef'
--> 'abcdef'
(2.2): repeat (1) because there are 2 replacements during the (1.2)
(1.3): ...... that would be alike (1.1) forever....
The step (3) (terminate) is never reached.
=> The algorithm won't terminate with the given S and R.
- I worked on this and found no efficient algorithm for the question "if the algorithm halts".
First idea came to my mind was to
"find cycle"
of letters which are intriggered rules
but the number of rules may be too large for this idea to be ideal.The second one is to propose a
"threshold"
for the time of the repeat, if the threshold is exceeded, we conclude the algorithm would not terninate.The
"threshold"
could be choosen randomly, (as long as it big enough) - this approach is not really compelling.I am thinking that if there is any
upper bound
for the"threshold"
which ensures that we always get the right answer. And I came up withthreshold = 26
where 26 is the number of letter from 'a' to 'z' - but I can't prove that it true (or not). (I hope that It would be something like Bellman-Ford algorithm which determines negative cycle in a fixed number of step,..)How about you? Please help me find the answer (this is not a homework)
Thankyou for reading.