1 / 35

Two algorithms for checking emptiness

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.

reuben-webb
Download Presentation

Two algorithms for checking emptiness

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Two algorithms for checking emptiness

  2. How to check for emptiness? • Is L(A) = ; ? • Need to check if there exists an accepting computation (passes through an accepting state infinitely often).

  3. 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 ?

  4. 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…

  5. 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.

  6. 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..

  7. 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

  8. Start from an initial state Hash table: q1 q1 1 q2 q3 Stack: q1 q4 q5 Mark in each node: - Start time - Finish time

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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)

  17. Example 12 16 15 1 2 3 7 4 Compute DFS Finish times 8 5 6 11 14 9 10 13

  18. Example 16 15 12 7 Finish times 6 11 14 10

  19. Example 16 15 12 7 GT 6 11 14 10 Swapped the direction of edges

  20. Example 16 15 12 7 GT 6 11 14 10 DFS from decreasing finish times: every tree is an SCC.

  21. 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.

  22. Notation • The automatonA: h , S, S0, T, Fi • Denote bysucc(s)the successors ofs 2 SinA.

  23. 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 ?

  24. 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.

  25. 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:={}

  26. The Double DFS algorithm dfs1 7 1 2 3 4 5 6 8 Stack 1 = {1 – 6} Table 1 = {1 – 6}

  27. The Double DFS algorithm dfs1 7 1 2 3 4 5 6 8 Stack 1 = {1 – 7} Table 1 = {1 – 7}

  28. 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.

  29. 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}

  30. 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…

  31. 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}

  32. 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.

  33. 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?)

  34. 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)

  35. 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|).

More Related