250 likes | 343 Views
CSci 4011. INHERENT LIMITATIONS OF COMPUTER PROGRAMS. WHEN A COMPUTER IS USEFUL…. ENCODING CHESS. A 8⨉8 chess board has 64 squares and 32 pieces:. Each square can have one of 13 values: 4 bits. +1 bit encodes turn information. CHESS = { 〈 B 〉 | B is a chess board and
E N D
CSci 4011 INHERENT LIMITATIONS OF COMPUTER PROGRAMS
ENCODING CHESS A 8⨉8 chess board has 64 squares and 32 pieces: Each square can have one of 13 values: 4 bits +1 bit encodes turn information. CHESS = { 〈B〉 | B is a chess board and white canforce a win. }
CHESS IS DECIDABLE can_white_force_win(board, b_list): if (board ∈b_list): return NO # black can force stalemate if (is_check_mate_black(board)): return YES if (is_check_mate_white(board)): return NO if (turn(board) = white): cw= NO else: cw= YES for each m ∈legal_moves(board): m_can_win = can_white_force_win(move(board,m), b_list+board) if (turn(board) == white): cw = cwor m_can_win else: cw = cwand m_can_win return cw
HOW LONG TO SAVE THE DAY? · 376 How many moves at any level? W ≥9Q + 1K + 2B + 2R + 2N = 376 · 1051 How many levels? D ≥(64)32⨉2 = 2193 ≈1051 1052 So, at most, WD = 10 positions By remembering whether each board B ∈CHESS, we can explore “only”D ≈ 1051positions. Can we do better?
DIVIDING BUGS A circuit is a collection of booleangates and inputs connected by wires: x1 x2 x0 It is satisfiable if some setting of inputs makes it output 1. ∧ ¬ It can be encoded by listing for the inputs and operation of each gate. ⋁ ⋁ CIRCUIT-SAT = {〈C〉 | C is a satisfiable circuit }
CIRCUIT-SAT IS DECIDABLE is_satisfiable(C): for each input value x ∈{0n, 0n-11, … 1n}: if (circuit_value(C,x)=1): return TRUE. return FALSE; How long to check the Pentium division circuit? 2 64-bit inputs: at most 2128 calls to circuit_value Around 1042 steps. Can we do better?
STABLE MARRIAGES A High School has N boys and N girls. Each has a ranked list of dates for the 1951 Senior Prom. Bob Charlie Albert A,B,C A,C,B B,C,A B,A,C C,A,B C,B,A Betty Carol Alice An unstablecouple prefer each other to their current dates. STABLE = { 〈B,G〉 | There is a pairing with no unstable couple}
COMPLEXITY THEORY Studies what can and can’t be computed under limited resources such as time, space, etc Today: Time complexity
DECISION VS SEARCH Decision Problems Search Problems Is there a winning move for white? Find a winning move for white Is there an input to satisfy the circuit? Find a satisfying input. Is there a stable pairing? Find a stable pairing.
MEASURING TIME COMPLEXITY We measure time complexity by counting the elementary steps required for a machine to halt Consider the language A = { 0k1k | k 0 } 1. Scan across the tape and reject if the string is not of the form 0m1n 2k 2. Repeat the following if both 0s and 1s remain on the tape: Scan across the tape, crossing off a single 0 and a single 1 2k2 3. If 0s remain after all 1s have been crossed off, or vice-versa, reject. Otherwise accept. 2k
Definition: Let M be a TM that halts on all inputs. The running time or time-complexity of M is the function ƒ: ℕ ℕ, where ƒ(n) is the maximum number of steps that M uses on any input of length n.
ASYMPTOTIC ANALYSIS 5n3 + 2n2 + 22n + 6 = O(n3)
BIG-O Let f and g be two functions f, g : N R+. We say that f(n) = O(g(n)) if positive integers c and n0 exist so that for every integer n n0 f(n) cg(n) When f(n) = O(g(n)), we say that g(n) is an asymptotic upper bound for f(n) 5n3 + 2n2 + 22n + 6 = O(n3) If c = 6 and n0 = 10, then 5n3 + 2n2 + 22n + 6 cn3
= O(n4.1) 2n4.1 + 200283n4 + 2 3n log2 n + 5n log2log2 n = O(n log2 n) = O(n log10 n) n log10 n78 log10 n = log2 n / log2 10 O(n log2 n) = O(n log10 n) = O(n log n)
EXAMPLES Is f(n) = O(g(n)) or g(n) = O(f(n))? f(n) g(n) 4n2 2n nlog n n! (log n)log n nlog log n
WHY USE BIG-O? Suppose there is a k-state TM for L that runs in time f(n). Then there is a O(k2c)-state TM for L that runs in time f(n)/c: 1 0 1 0 1 1 0 1 1 0 1 0 1 1 1 0 10 10 11 01 10 10 11 10
Definition: TIME(t(n)) = { L | L is a language decided by a O(t(n)) time Turing Machine } A = { 0k1k | k 0 } TIME(n2) CIRCUIT-SAT TIME(2n)
A = { 0k1k | k 0 } TIME(n log n) Cross off every other 0 and every other 1. If the # of 0s and 1s left on the tape is odd, reject 00000000000001111111111111 x0x0x0x0x0x0xx1x1x1x1x1x1x xxx0xxx0xxx0xxxx1xxx1xxx1x xxxxxxx0xxxxxxxxxxxx1xxxxx xxxxxxxxxxxxxxxxxxxxxxxxxx
We can prove that a TM cannot decide A in less time than O(n log n)
Can A = { 0k1k | k 0 } be decided in time O(n) with a two-tape TM? Scan all 0s and copy them to the second tape. Scan all 1s, crossing off a 0 from the second tape for each 1.
Different models of computation yield different running times for the same language!
Theorem: Let t(n) be a function such that t(n) n. Then every t(n)-time multi-tape TM has an equivalent O(t(n)2) single tape TM
SIMULATING MULTIPLE TAPES ● ● ● ● L#100#0#□#R L#100#0#1#R 100 L#100#□#□#R qi qi1 qjRSS qj qi1□□ qi1□ qj101RSS 1. “Format” tape. 2. For each move of the k-tape TM: Scan left-to-right, finding current symbols Scan left-to-right, writing new symbols Scan left-to-right, moving each tape head: If a tape head goes off right end, insert blank. If tape head goes off left end, move back right.
COUNTING STEPS O(n+k) steps 1. “Format” tape. Do t(n) times: 2. For each move of the k-tape TM: Scan left-to-right, finding current symbols 2t(n) steps 2t(n) steps Scan left-to-right, writing new symbols 2t(n) steps Scan left-to-right, moving each tape head: If a head goes off right end, insert blank. If a head goes off left end, move back right. 2kt(n) steps Total = t(n)⨉((6+2k) t(n)) + O(n+k) = O(t2(n))