Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI 3130: Formal languages and automata theory Andrej Bogdanov The Chinese University of Hong Kong Polynomial.

Similar presentations


Presentation on theme: "CSCI 3130: Formal languages and automata theory Andrej Bogdanov The Chinese University of Hong Kong Polynomial."— Presentation transcript:

1 CSCI 3130: Formal languages and automata theory Andrej Bogdanov http://www.cse.cuhk.edu.hk/~andrejb/csc3130 The Chinese University of Hong Kong Polynomial time Fall 2011

2 Running time When you do laundry, you want to know: How soon will the laundry be done? When you run a program, you want to know: How soon will my program finish? M 01001

3 Efficiency Undecidable problems: We cannot find solutions in any finite amount of time Decidable problems: We can solve them, but it could take a very long time decidable A TM PCP efficient

4 Efficiency The running time depends on the input For longer inputs, we should allow more time Efficiency is measured as a function of input size decidable A TM PCP efficient

5 Running time The running time of TM M is the function t M (n) : t M (n) = maximum number of steps that M takes on any input of length n On input x, until you reach # Read and cross off first a or b before # Read and cross off first a or b after # If there is a mismatch, reject M:M: If all symbols but # are crossed off, accept O(n) times O(n) steps L = {w#w: w ∈ { a, b }} running time: O(n 2 )

6 Running time L = { 0 n 1 n : n ≥ 0} On input x, Until everything is crossed off: Move head to left end of tape Cross off the leftmost 0 Cross off the following 1 If everything is crossed off, accept. M:M: O(n) times O(n) steps running time: O(n 2 )

7 A faster way L = { 0 n 1 n : n ≥ 0} On input x, Until everything is crossed off: Move head to left end of tape Find the parities of number of 0 s and 1 s If one is even and other odd, reject Otherwise, cross off every other 0 and every other 1 If everything is crossed off, accept. M:M: O(log n) times O(n) steps running time: O(n log n) O(n) steps

8 Running time vs. model What if we have a two-tape Turing Machine? L = { 0 n 1 n : n ≥ 0} On input x, Check that the input is of the form 0 * 1 * Copy 0 * part of input on second tape Until ☐ is reached, Cross off next 1 from first tape and next 0 from second tape If both tapes reach ☐ at same time, accept M:M: O(n) steps running time: O(n)

9 Running time vs. model How about a java program? L = { 0 n 1 n : n ≥ 0} running time: O(n) M(string x) { n = x.len; if n % 2 == 0 reject; else for (i = 1; i <= n/2; i++) if x[i] != 0 reject; if x[n-i+1] != 1 reject; accept; } 1 -tape TM O(n log n) 2 -tape TM O(n) java O(n) The running time can change depending on the model of computation!

10 Measuring running time What does it mean when we say: One “time unit” in all mean different things! “This algorithm runs in time T ” javaRAM machineTuring Machine if (x > 0) y = 5*y + x; write r3;  (q 3, a ) = (q 7, b, R)

11 Efficiency and the Church-Turing thesis The Church-Turing thesis says all these are equivalent in computing power… … but not in running time! java RAM machine Turing Machine multitape TM

12 The Cobham-Edmonds thesis However, there is an extension to the Church-Turing thesis that says For any realistic models of computation M 1 and M 2 : So any task that takes time t on M 1 can be done in time (say) O(t 2 ) or O(t 3 ) on M 2 M 1 can be simulated on M 2 with at most polynomial slowdown

13 Efficient simulation The running time of a program depends on the model of computation… … but in the grand scheme, this is irrelevant java RAM machinetwo tape TMordinary TM fastslow Every reasonable model of computation can be simulated efficiently on every other

14 Example of efficient simulation Recall simulating two tapes on a single tape M … 010 … 01  = {0, 1, ☐ } S … 01010##0#10   ’ = {0, 1, ☐, 0, 1, ☐, #}   #

15 Running time of simulation Each move of the multiple tape TM might require traversing the whole single tape after t steps O(s) steps of single tape TM s = rightmost cell ever visited s ≤ 2t + O(1) 1 step of 2-tape TM t steps of 2-tape O(ts) = O(t 2 ) single tape steps multi-tape TMsingle tape TM quadratic slowdown

16 Simulation slowdown Cobham-Edmonds Thesis: 2-tape TMjava ordinary TMRAM machine O(t)O(t)O(t)O(t) O(t2)O(t2) O(t2)O(t2) O(t)O(t)O(t)O(t) M 1 can be simulated on M 2 with at most polynomial slowdown

17 The class P decidable regular context-free efficient P is the class of languages that can be decided on a TM with polynomial running time in the input length By the CE thesis, we can replace “ordinary TM” by any realistic model of computation multi-tape TMjavaRAM

18 Examples of languages in P L 01 = {0 n 1 n : n > 0} L G = {x: x is generated by G} PATH = {(G, s, t): G is a graph with a path from node s to node t} G is some CFG context-free P (efficient) decidable L 01 LGLG PATH P is the class of languages that are decidable in polynomial time (in the input length)

19 Context-free languages in polynomial time Let L be a context-free language, and G be a CFG for L in Chomsky Normal Form For cells in last row If there is a production A  x i Put A in table cell ii For cells st in other rows If there is a production A  BC where B is in cell sj and C is in cell jt Put A in cell st x 1 x 2 … x n 11 22nn 12 23 …… 1k1k CYK algorithm On input x of length n, running time is O(n 3 ) ✔

20 Paths in polynomial-time s t PATH = { 〈 G, s, t 〉 : G is a graph with a path from node s to node t} M := On input 〈 G, s, t 〉, where G is a graph with nodes s and t Place a mark on node s. Repeat until no additional nodes are marked: Scan the edges of G. If there is an edge so that a is marked and b is not marked, mark b. If t is marked accept, otherwise reject. x x x x x O(m) time O(n) times O(nm) running time: ✔ G has n vertices, m edges

21 Hamiltonian paths A Hamiltonian path in G is a path that visits every node exactly once UHAMPATH = { 〈 G, s, t 〉 : G is a graph with a Hamiltonian path from s to t} st We don’t know if UHAMPATH is in P, and we believe it is not


Download ppt "CSCI 3130: Formal languages and automata theory Andrej Bogdanov The Chinese University of Hong Kong Polynomial."

Similar presentations


Ads by Google