-1

The following pseudocode is from Introduction to Compiler Construction in a Java World book. This algorithm is supposed to output the epsilon closure of a set of a states of a nondeterministic finite machine (to convert it to a deterministic one eventually).

# Input: a set of states, S
# Output: epsilon_closure(S)

    Stack P.addAll(S) #a stack containing all states in S
    Set C.addAll(S)   #the closure initially contains the states in S

    while ! P.empty() do
         s = P.pop()
         for r in m(s, epsilon) do
            # m(s, epsilon) is a set of states
            if r not in C then
                P.push(r)
                C.add(r)
            end if
        end for

    end while
    return C

I know what epsilon closure is but unfortunately I'm having a hard time to understand how this code works.

Note: m() in the code is the transition function of the machine.

Algo
  • 841
  • 1
  • 14
  • 26

1 Answers1

-2

It just applies the transition function to each state in S. The states resulting from that application are collected in C. Once you've finished looking at each state in P, C contains the epsilon closure.

Note that the input S is not necessarily the complete set of states in the NFA, so that the intersection of S and m(s, epsilon) may not be empty.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • is `C` an array? Isn't it already filled with all the states from the begining (`C.addAll(S)`)? If so, how `r not in C` will ever be true? – Algo Nov 25 '15 at 17:48
  • `S` is a subset of the states of the NFA, not necessarily the set of all states. – chepner Nov 25 '15 at 17:56
  • It's terse because there isn't that much going on. What exactly are you stuck on? – chepner Nov 25 '15 at 18:49
  • 1
    Would be nice to mention what S is, why you have to find C in the subset construction algorithm, and how transitive closure works. I don't think OP knows these things – Matt Timmermans Nov 25 '15 at 18:56