Docsity

Time Complexity - Lecture Slides | 22C 131, Study notes of Computer Science

Toggle sidebar
bg1
22c:131 Limits of Computation
Hantao Zhang
http://www.cs.uiowa.edu/∼hzhang/c131
The University of Iowa
Department of Computer Science
Time Complexity
Limits of Computation – p.1/35
Example computation
Consider the decidable language A={0n1n|n≥0}and the
following TM M1deciding A:
M1= "On input string w:
1. Scan across the tape and reject if a 0is found to the right of a 1
2. Repeat as long as both 0-s and 1-s remain of the tape:
(a) Scan across the tape, crossing off a single 0and a single 1
3. If 0-s still remain after all 1-s have been crossed off, or is 1-s still
remain after all 0-s have been crossed off reject. Otherwise, if neither
0-s nor 1-s remain of the tape, accept.
Limits of Computation – p.2/35
Analyzing an Algorithm
How much time does take M1to decide A?
It is
Number of steps the TM has (this may depends on several
parameters).
Limits of Computation – p.3/35
Simplifying Conventions
1. The running time of an algorithm is a function of the
length of the string representing the input on the
algorithm
2. In worst-case-analysis we consider the longest running
time of all inputs of a particular length
3. In average-case analysis we consider the average of all
the running times of inputs of a particular length
Limits of Computation – p.4/35
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Time Complexity - Lecture Slides | 22C 131 and more Study notes Computer Science in PDF only on Docsity!

22c:131 Limits of Computation

Hantao Zhang

http://www.cs.uiowa.edu/

∼

hzhang/c

The University of Iowa

Department of Computer Science

Time Complexity

Limits of Computation – p.1/

Example computation

Consider the decidable language

A

n

n

n

and the

following TM

M

1

deciding

A

M

1

= "On input string

w

  1. Scan across the tape and

reject

if a

is found to the right of a

  1. Repeat as long as both

-s and

-s remain of the tape:

(a) Scan across the tape, crossing off a single

and a single

  1. If

-s still remain after all

-s have been crossed off, or is

-s still

remain after all

-s have been crossed off

reject

. Otherwise, if neither

-s nor

-s remain of the tape,

accept

Limits of Computation – p.2/

Analyzing an Algorithm

How much time does take

M

1

to decide

A

It is

Number of steps the TM has (this may depends on severalparameters).

Limits of Computation – p.3/

Simplifying Conventions

1. The running time of an algorithm is a function of the

length of the string representing the input on thealgorithm

2. In

worst-case-analysis

we consider the longest running

time of all inputs of a particular length

3. In

average-case analysis

we consider the average of all

the running times of inputs of a particular length

Limits of Computation – p.4/

Time Complexity of a TM

Let

M

be a deterministic Turing machine that halts on all

inputs. The

running time

or

time complexity

of

M

is a

function

f

N → N

where

f

n

is the maximum number of

steps that

M

uses on any input of length

n

If

f

n

is the running time of

M

, we say that

M

runs in

time

f

n

and that

M

is an

f

n

time Turing machine.

Customarily

n

represents the length of the input

Limits of Computation – p.5/

Big-O and small-O notation

The exact running time of an algorithm may be acomplex-expression. Therefore usually this is just estimated. One convenient form of the estimation is so called

asymptotic

analysis

which determines the running time of the algorithm on large

inputs. In the asymptotic analysis one may consider only the highest orderterm of the expression for the running time of the algorithm,disregarding both the coefficient of that term and any lower orderterms. This is valid because the value of the highest order term dominatesthe value of other terms on large inputs.

Limits of Computation – p.6/

Examples

Consider the function

f

n

n

3

n

2

n

Disregarding the coefficient

, we say that

f

is asymptotically at most

n

3

The asymptotic notation, or big-O notation for describing theestimation defined by

f

n

is

f

n

O

n

3

Limits of Computation – p.7/

Formally

Let

f

and

g

be functions,

f, g

N → R

. We say that

f

n

O

g

n

if positive integers

c

and

n

0

exist such that

n

n

0

f

n

cg

n

When

f

n

O

g

n

we say that

g

n

is an

upper bound

for

f

n

; more precisely,

g

n

is an

asymptotic upper bound

for

f

n

, which emphasizes the suppression of constant factors.

Limits of Computation – p.8/

Small-O notation

Big-O notation says that a function is asymptotically

no

more than

another function

Small-O notation says that a function is asymptotically less than

another function

Formally: for

f, g

N → R

, we say that

f

n

o

g

n

if

lim

n

→∞

f

(

n

)

g

(

n

)

. That is,

f

n

o

g

n

means that for

any

real

c >

there exists

n

0

such that

f

n

< cg

n

for

all

n > n

0

Limits of Computation – p.13/

Examples

Check that:

n

o

n

n

o

n log

log

n

n log

log

n

o

n log

n

n log

n

o

n

2

n

2

o

n

3

f

n

is never

o

f

n

Limits of Computation – p.14/

Analyzing Algorithms

Consider the TM algorithm

M

1

that decides the language

A

n

n

n

M

1

= "On input string

w

  1. Scan across the tape and

reject

if a

is found to the right of a

  1. Repeat as long as both

-s and

-s remain of the tape:

(a) Scan across the tape, crossing off a single

and a single

  1. If

-s still remain after all

-s have been crossed off, or is

-s still

remain after all

-s have been crossed off

reject

. Otherwise, if neither

-s nor

-s remain of the tape,

accept

Limits of Computation – p.15/

On input of length

n

Consider each of the three stages separately:

  1. In stage 1 the machine scans the tape in

n

steps to verify that the

input is of the form

∗

∗

; repositioning the tape at the left end uses

another

n

steps. So, the total number of steps is

n

, i.e.,

O

n

  1. Each scan of the tape in stage 2 and 2(a) is performed in

O

n

Because each scan crosses off a

and a

, at most

n/

scans occur.

I.e., the total number of steps is

n/

O

n

O

n

2

  1. In stage 3 the machine makes a single scan to decide whether to

accept or reject taking

O

n

steps

Total number of steps is

O

n

O

n

2

O

n

O

n

2

Limits of Computation – p.16/

Notations

Let

t

N → N

be a function. The time complexity class

TIME

t

n

is the collection of languages that are decidable

by an

O

t

n

time TM.

Since language

A

n

n

n

is decided by

M

1

in

O

n

2

steps,

A

TIME(

n

2

TIME

n

2

contains all languages decidable in

O

n

2

time.

Is there a TM that decides

A

asymptotically faster? I.e.,is

A

in

TIME

t

n

for

t

n

o

n

2

? One can cross 2

’s and 2

’s in stage 2

which cuts the number of scans by half but running time does notchange.

Limits of Computation – p.17/

M

a

TIME

n log n

TM

M

2

= "On input string

w

  1. Scan across the tape and

reject

if a 0 is found to the right of a 1

  1. Repeat as long as some

’s and some

’s remain on the tape:

(a) Scan across the tape, checking whether the total number of

’s

and

’s remaining on the tape is even or odd. If it is odd

reject

(b) Scan again across the tape, crossing every other 0 starting with

the first 0, and then crossing off every other 1 starting with thefirst 1

  1. If no

’s and no

’s remain of the tape,

accept

; otherwise

reject

Limits of Computation – p.18/

Analysis

  1. Stage 1 takes

O

n

steps

  1. Stage 2 takes

log

2

n

O

n

O

n log

n

n

steps

  1. Stage 3 takes

O

n

steps

Total number of steps is asymptotically

O

n log

2

n

, that is,

A

TIME(

n log n

Note:

any language that can be decided in

o

n log n

on a single-tape TM

is regular; since

A

is not regular no faster algorithm performed by a single-

tape TM exists to decide it.

Limits of Computation – p.19/

Deciding

A

in

O

n

The following two-tape TM

M

3

decides

A

in linear time, i.e.,

in

O

n

time

M

3

= "On input string

w

on tape 1:

  1. Scan across the tape 1 and reject if a 0 is found to the right of a 12. Scan across the

’s on tape 1 until the first 1; at the same time copy

the

’s on tape 2.

  1. Scan across the

’s on tape 1 until the end of the tape is discovered.

For each 1 read on tape 1, cross off a 0 on tape 2. If all

’s are

crossed off before all 1 are read

reject

  1. If all

’s have been crossed off

accept

; if any

’s remain

reject

Limits of Computation – p.20/

Proof of Theorem 7.

For

M

a

k

-tape TM that runs in

t

n

time, the single-tape TM

S

operates

as follows:

  1. Initially

S

puts its tape into the format that represents all the

k

-tapes

of

M

and then simulates

M

  1. To simulate one step of

M

S

scan all the info stored on its tape to

determine the symbol under

M

’s heads.

  1. Then

S

makes another pass over its tape to update the tape contents

and head positions.

  1. If one on

M

’s heads moves rightward onto a previously unread

position on its tape,

S

must increase the amount of space by shifting

a portion of its tape one cell to the right

Limits of Computation – p.25/

Analyzing

S

For each step of

M

S

makes two passes over the active portion of its

tape: first to obtain the info necessary to determine the next moveand second to carry out the move. The upper-bound of the length of the active portion of

S

’s tape is the

sum of the length of active portions of

M

’s

k

tapes, which are

bounded by

t

n

, i.e., upper-bound of the active portion of

S

’s tape is

O

t

n

Limits of Computation – p.26/

Simulation time

To simulate each of

M

’s steps,

S

performs two scans, each using

O

t

n

time, i.e., one step of

M

is simulated by

S

in

O

t

n

time.

Sine by assumption

M

perform

t

n

steps, total time taken by

S

to

simulate

M

is

O

t

n

taken by the initial step plus

t

n

O

t

n

to

perform the simulation, i.e., time complexity of

S

is

O

t

2

n

Limits of Computation – p.27/

Corallary

Let

t

n

and

s

n

be functions, where

t

n

, s

n

n

If a

multitape Turing machine takes

t

n

time and

s

n

space for

any input of length

n

, then there is an equivalent

O

t

n

s

n

time single-tape Turing machine.

Limits of Computation – p.28/

Nondeterministic TM

Let

N

be a nondeterministic TM that is a decider. The run-

ning time of

N

is the function

f

N → N

, where

f

n

is the

maximum number of steps that

N

makes on any branch of its

computation, on any input of length

n

, as shown in Figure 1

Limits of Computation – p.29/

Deterministic and nondeterministic TM

f

(

n

)

p

accept/reject

? p?

...

p ?p ?p?

Deterministic

p

@R

p

p

@R

p

p

p

@R

p

p

...

p

accept

p

reject

Nondeterministic

f

(

n

)

Figure 1:

Measuring deterministic and nondeterministic time

Limits of Computation – p.30/

Note

Definition of the running time of a nondeterministic TMis not intended to correspond to any real-worldcomputing device. The running time of a nondeterministic TM is a usefulabstraction that assists in characterizing the complexityof an important class of computational problems.

Limits of Computation – p.31/

Theorem 7.

Let

t

n

be a function, where

t

n

n

. Then every

t

n

time

nondeterministic single-tape Turing machine has anequivalent

O

(

t

(

n

))

time deterministic single-tape Turing

machine. Proof idea:

Let

N

be a nondeterministic TM running in time

t

n

. We con-

struct a deterministic TM

D

that simulates

N

by searching

N

’s nondeter-

ministic computation tree

Limits of Computation – p.32/