String matching with Finite Automata

Certainly! Here's the pseudocode for the String Matching with Finite Automata algorithm:

FiniteAutomatonStringMatch(pattern, text):
    n = length(text)
    m = length(pattern)
    automaton = ConstructAutomaton(pattern)
    occurrences = []

    currentState = 0

    for i = 0 to n - 1 do
        currentState = Transition(currentState, text[i])
        if currentState = m then
            occurrences.append(i - m + 1)

    return occurrences

ConstructAutomaton(pattern):
    m = length(pattern)
    automaton = empty automaton

    for state = 0 to m do
        for character in alphabet do
            nextState = ComputeTransition(pattern, state, character)
            automaton[state][character] = nextState

    return automaton

ComputeTransition(pattern, state, character):
    if state < length(pattern) and character = pattern[state] then
        return state + 1
    else:
        k = min(state, length(pattern))

        while k > 0 do
            k = k - 1
            if character = pattern[k] then
                return k + 1
        return 0

Transition(state, character):
    return automaton[state][character]

Let's go through the pseudocode step by step:

1. `n` represents the length of the text, and `m` represents the length of the pattern.

2. The `ConstructAutomaton` function constructs the finite automaton for the given pattern by iterating over all possible states and characters in the alphabet. It uses the `ComputeTransition` function to determine the next state for a given state and character. The automaton is represented as a 2D array.

3. The `ComputeTransition` function computes the transition from a given state and character. It first checks if the character matches the one at the current state. If it does, it returns the next state by incrementing the state by 1. Otherwise, it performs a backtracking loop to find the largest proper suffix of the pattern that matches the substring ending at the current state. It then returns the next state based on the suffix match.

4. The `Transition` function retrieves the next state in the finite automaton based on the current state and character.

5. The `FiniteAutomatonStringMatch` function performs the actual string matching. It initializes the current state to 0 and iterates over the text character by character. For each character, it transitions to the next state using the `Transition` function. If the current state is equal to the length of the pattern, a match is found, and the starting index of the match is added to the `occurrences` list.

6. Finally, the `occurrences` list is returned, containing the starting indices of pattern matches in the text.


Please note that the pseudocode assumes 0-based indexing for arrays and lists. The `alphabet` variable represents the set of characters in the input text and pattern.



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext