Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 310 – Fall 2006 Pacific University CS310 Complexity Section 7.1 November 27, 2006.

Similar presentations


Presentation on theme: "CS 310 – Fall 2006 Pacific University CS310 Complexity Section 7.1 November 27, 2006."— Presentation transcript:

1 CS 310 – Fall 2006 Pacific University CS310 Complexity Section 7.1 November 27, 2006

2 CS 310 – Fall 2006 Pacific University Running time A = {0 K 1 K | k >=0 } –TM, M, can decide this language –how long (how many steps?) will it take a single-tape TM to accept or reject a string? The running time of M is a function where f(n) is the max. number of steps that M requires on any input of length n –worst case running time M is a “f(n) time TM” –TM is our model for computation, so –TM  algorithm

3 CS 310 – Fall 2006 Pacific University Example f(n) = 5n 3 + 4n 2 + 6n + 1 –for large n, 5n 3 dominates this equation –coefficient 5 is immaterial –we say f(n) = n 3 –the goal here is to see how the running time grows as n increases

4 CS 310 – Fall 2006 Pacific University Big Oh Asymptotic analysis –estimate runtime of algorithm (or TM) on large inputs –only look at highest order term –allows us to compare runtime of two algorithms

5 CS 310 – Fall 2006 Pacific University Definition: Big Oh f, g are functions: f,g: N  R + f(n) = O(g(n)) if positive ints c and n 0 exist such that for every int n >= n 0 f(n) <= c*g(n) g(n) is an asymptotic upper bound for f(n) some constant multiple of g(n) eventually dominates f(n)

6 CS 310 – Fall 2006 Pacific University Example f(n) = 5n 3 + 2n 2 + 22n + 6 O(f(n)) = n 3 let c = 6 and n 0 = 10 5n 3 + 2n 2 + 22n + 6 <= 6n 3 –for every n >= n 0 O(f(n)) = n 4 as well, but we want the tightest upper bound

7 CS 310 – Fall 2006 Pacific University Logarithms if x = log 2 n then 2 x = n so log b 2 x = log b n so x log b 2 = log b n so x = (log b n) / (log b 2) so log b (n) = O(log 2 n) for any base because log b 2 is a constant

8 CS 310 – Fall 2006 Pacific University Example f(n) = 3n log 2 n + 5nlog 2 (log 2 n) + 2 f(n) = O(g(n)) = ? Since log 2 n <= n then log 2 (log 2 n) <= log 2 (n) so f(n) = O(n log 2 n)

9 CS 310 – Fall 2006 Pacific University Analyzing Algorithms A = {0 k 1 k | k>=0} on input of length n: 1) scan, reject if 0 found to right of a 1 2) if both 0’s and 1’s remain, scan, cross off single 0, single 1 3) if 0’s remain after 1’s crossed off or conversely, reject. otherwise accept.

10 CS 310 – Fall 2006 Pacific University Analysis Step 1: scan, verify: n steps forward, n steps back: 2n steps so O(n) Step 2: scan, cross off 0 and 1 each scan. Each scan uses O(n) steps, n/2 scans at most, so O(n 2 ) Step 3: Scan, accept or reject O(n) Total: O(n) + O(n 2 ) + O(n) –O(n 2 )

11 CS 310 – Fall 2006 Pacific University Algorithm Can we write an algorithm that runs better than O(n 2 )? 1) Scan across the tape, reject is a 0 is found to the right of a 1 2) Repeat as long as some 0s and 1s remain: 2a) Scan across the tape checking if the total number of 1s and 0s is even or odd. if odd, reject. 2b) Scan across the tape, crossing off every other 0 starting with the first 0 and then crossing off every other 1 starting with the first 1. 3) if no 0s and no 1s remain, accept. Otherwise reject. Analysis?

12 CS 310 – Fall 2006 Pacific University Algorithm If we had a two tape TM, could we do this in O(n)? –linear time?

13 CS 310 – Fall 2006 Pacific University Complexity relationships between models Theorem 7.8: let t(n) >= n, every t(n) time multitape TM has an equivalent O((t(n) 2 ) time single-tape TM. Theorem 7.9: Every t(n) >= n time ND single tape TM has an equivalent 2 O(t(n)) time deterministic single tape TM


Download ppt "CS 310 – Fall 2006 Pacific University CS310 Complexity Section 7.1 November 27, 2006."

Similar presentations


Ads by Google