350 likes | 491 Views
Two algorithms for checking emptiness. How to check for emptiness?. Is L (A) = ; ? Need to check if there exists an accepting computation (passes through an accepting state infinitely often). Emptiness and accepting runs.
E N D
How to check for emptiness? • Is L(A) = ; ? • Need to check if there exists an accepting computation (passes through an accepting state infinitely often).
Emptiness and accepting runs • If there is an accepting computation, then it contains at least one accepting state an infinite # of times. • This state must appear in a cycle. • So, find a reachable accepting state on a cycle. • How ?
Emptiness and accepting runs • There can be an exponential number of cycles in a graph! • But we want stay polynomial in the size of the graph…
Finding accepting runs • Rather than looking for cycles, look for SCCs: • A maximal Strongly Connected Component (SCC): a maximal set of nodes where each node is reachable from all others. • Find a reachable SCC with an accepting node.
Finding accepting runs • Finding SCC’s is linear in the size of the graph. • Relies on a modified DFS, in which the ‘finishing time’ (the time leaving a node) is recorded. • Let us recall DFS..
Program DFS For each initial state s dfs(s) end DFS Procedure dfs(s) for each s’ such that R(s,s’) do If new(s’) then dfs(s’) end dfs. Depth First Search
Start from an initial state Hash table: q1 q1 1 q2 q3 Stack: q1 q4 q5 Mark in each node: - Start time - Finish time
Continue with a successor Hash table: q1 q1 q2 1 q2 q3 2 Stack: q1 q2 q4 q5 Mark in each node: - Start time - Finish time
One successor of q2. Hash table: q1 q1 q2 q4 1 q2 q3 2 Stack: q1 q2 q4 q4 3 q5 Mark in each node: - Start time - Finish time
Backtrack to q2 (no new successors for q4). Hash table: q1 q1 q2 q4 1 q2 q3 2 Stack: q1 q2 q4 4 3 q5 Mark in each node: - Start time - Finish time
Backtracked to q1 Hash table: q1 q1 q2 q4 1 5 q2 q3 2 Stack: q1 q4 4 3 q5 Mark in each node: - Start time - Finish time
Second successor to q1. Hash table: q1 q1 q2 q4 q3 1 5 q2 q3 2 Stack: 6 q1 q3 q4 4 3 q5 Mark in each node: - Start time - Finish time
Backtrack again to q1. Hash table: 8 q1 q1 q2 q4 q3 1 5 q2 q3 2 Stack: 6 7 q1 q4 4 3 q5 Mark in each node: - Start time - Finish time
An algorithm for finding SCCs We need the following definition for describing the algorithm: The transpose of G, written GT, is derived from G by reversing its edges. GT G
An algorithm for finding SCCs • Strongly-Connected-Components(G) • Call DFS(G) to compute finish[v] for each vertex in G. • Call Modified-DFS(GT), where the main loop of Modified-DFS(GT) processes vertices in order of decreasing finish[v]. • Each tree in the depth-first forest of Modified-DFS(GT) is a strongly connected component of G. Running time = running time of DFS = Q(V + E)
Example 12 16 15 1 2 3 7 4 Compute DFS Finish times 8 5 6 11 14 9 10 13
Example 16 15 12 7 Finish times 6 11 14 10
Example 16 15 12 7 GT 6 11 14 10 Swapped the direction of edges
Example 16 15 12 7 GT 6 11 14 10 DFS from decreasing finish times: every tree is an SCC.
An alternative algorithm • The algorithm we saw so far for checking emptiness: • Identify SCC’s • Return “yes” if one of them contains an accepting state • We will see a better alternative.
Notation • The automatonA: h , S, S0, T, Fi • Denote bysucc(s)the successors ofs 2 SinA.
dfs1 dfs2 The Double DFS algorithm • The first DFS finds a state f 2 F • The second DFS attempts to close a loop around it. • The trick is: how to avoid exploring the entire graph for each accepting state ?
DFS1(s) { push(s,Stack1); hash(s,Table1); for each t 2 Succ(s) {if t Ï Table1 then DFS1(t);} if s 2 F then DFS2(s); pop(Stack1); } DFS2(s) { push(s, Stack2); hash(s, Table2) ; for each t2 Succ(s) do { if tis onStack1exit(“not empty”); else if t Ï Table2 then DFS2(t) } pop( Stack2); } The Double-DFS algorithm Upon finding an accepting cycle, Stack1, Stack2, t, determines a witness: an accepting cycle reached from an initial state.
procedure Main() { foreach s 2 S_0 { if s Ï Table1 then DFS1(s); } output(“empty”); exit; } procedure DFS1(s) { push(s,Stack1); hash(s,Table1); foreach t 2 Succ(s) { if t Ï Table1 then DFS1(t); } if s 2 F then DFS2(s); pop(Stack1); } procedure DFS2(s){ push(s, Stack2); hash(s, Table2) ; for each t 2 Succ(s) do { if tis onStack1exit(“not empty”); else if t Ï Table2 then DFS2(t) } pop(Stack2); } The Double DFSalgorithm (full version) Input: A Initialize: Stack1:={}, Stack2:={} Table1:={}, Table2:={}
The Double DFS algorithm dfs1 7 1 2 3 4 5 6 8 Stack 1 = {1 – 6} Table 1 = {1 – 6}
The Double DFS algorithm dfs1 7 1 2 3 4 5 6 8 Stack 1 = {1 – 7} Table 1 = {1 – 7}
The Double DFS algorithm dfs1 7 1 2 3 4 5 6 8 Stack 1 = {1 – 6} Table 1 = {1 – 7} For the first time we identified an accepting state for which all the successors were already explored. Now it’s DFS2’s turn to try to close the loop.
The Double DFS algorithm dfs2 7 1 2 3 4 5 6 8 Stack 1 = {1 – 6} Stack 2 = {7} Table 1 = {1 – 7} Table 2 = {7}
The Double DFS algorithm dfs1 7 1 2 3 4 5 6 8 Stack 1 = {1 – 4} Stack 2 = {} Table 1 = {1 – 7} Table 2 = {7} Still has successors…
The Double DFS algorithm dfs1 7 1 2 3 4 5 6 8 Stack 1 = {1 – 4,8} Stack 2 = {} Table 1 = {1 – 8} Table 2 = {7}
The Double DFS algorithm dfs1 7 1 2 3 4 5 6 8 Stack 1 = {1 – 4} Stack 2 = {} Table 1 = {1 – 8} Table 2 = {7} Again we identified a bad state for which all successors were already explored. Now it’s DFS’2 turn to try to close the loop.
The Double DFS algorithm dfs2 7 1 2 3 4 5 6 8 Stack 1 = {1 – 4} Stack 2 = {5 - 6} Table 1 = {1 – 8} Table 2 = {5 - 7} No point continuing to what is already in Table 2 (why?)
The Double DFS algorithm dfs2 7 1 2 3 4 5 6 8 Stack 1 = {1 – 4} Stack 2 = {8} Table 1 = {1 – 8} Table 2 = {5 – 8} Bingo! Found a cycle ! (DFS2 progresses to node 3 which is on Stack1 but not in Table 2)
Theorems about the D-DFS algorithm • The Double-DFS algorithm outputs “empty” iff L(A) = ;. • If it outputs “not empty”, then content of Stack1 + Stack2 + tdefines an accepted (looping) word. • The algorithm runs in (worst-case) time and space: O(|A|).