• No se han encontrado resultados

Let us fix some finite alphabet Σ, inluding the blank symboland let Σ0= Σ\ {∗}. In this

chapter, when a Turing machine is used for computation, we assume that it has an input tape and output tape andk≥1 work tapes. At start, there is a word in Σ

0 written on the

input tape.

Thetime demandof a Turing machineTis a function timeT(n) defined as the maximum of

the number of steps taken byT over all possible inputs of lengthn. We assume timeT(n)≥n

(the machine must read the input; this is not necessarily so but we exclude only trivial cases with this assumption). It may happen that timeT(n) =.

Similarly, the function spaceT(n) is defined as the maximum number, over all inputs of lengthn, of all cells on all tapes to which the machine writes. (This way, the cells occupied by the input are not counted in the space requirement.) We assume that spaceT(n)1 (the

machine has some output).

A Turing machine T is called polynomial, if there is a polynomial f(n) such that timeT(n) = O(f(n)). This is equivalent to saying that there is a constant c such that

the time demand ofT is O(nc). We can define exponential Turing machines similarly (for

which the time demand is O(2nc

) for some c > 0), and also Turing machines working in polynomial and exponential space.

Now we consider a yes-or-no problem. This can be formalized as the task of deciding whether the input wordxbelongs to a fixed languageL ∈Σ

0.

We say that a language L ∈ Σ

4.2. TIME AND SPACE 59 by a Turing machine with time demand at most f(n). We denote by DTIME(f(n)) the class of languages whose time complexity is at most f(n). (The letter “D” indicates that we consider here only deterministic algorithms; later, we will also consider algorithms that are “nondeterministic” or use randomness). We denote by PTIME, or simply by P, the class of all languages decidable by a polynomial Turing machine. We define similarly when a laguage has space complexity at mostf(n), and also the language classes DSPACE(f(n)) and PSPACE (polynomial space).

Remark 4.2.1 It would be tempting to define the time complexity of a language Las the optimum time of a Turing machine that decides the language. Note that we were more careful above, and only defined when the time complexity isat mostf(n). The reason is that there may not be a best algorithm (Turing machine) solving a given problem: some algorithms may work better for smaller instances, some others on larger, some others on even larger etc. Section 3.3 contains a theorem that provides such examples.

Remark 4.2.2 When we say that the multiplication of two numbers of size n can be per- formed in time n2 then we actually find an upper bound on the complexity of a function

(multiplication of two numbers represented by the input strings) rather than a language. The classes DTIME(f(n)), DSPACE(f(n)), etc. are defined as classes of languages; corre- sponding classes of functions can also be defined.

Sometimes, it is easy to give a trivial lower bound on the complexity of a function. Con- sider e.g. the function isx·y wherexandyare numbers in binary notation. Its computation requires at least |x|+|y| steps, since this is the length of the output. Lower bounds on the complexity of languages are never this trivial, since the output of the computation deciding the language is a single bit.

How to define time on the RAM machine? The number of steps of the Random Access Machine is not the best measure of the “time it takes to work”. One could (mis)use the fact that the instructions operate on natural numbers of arbitrary size, and develop computational tricks that work in this model but use such huge integers that to turn them into practical computations would be impossible. For example, we can simulate vector addition by the addition of two very large natural numbers.

Therefore, we prefer to characterize the running time of RAM algorithms by two num- bers, and say that “the machine makes at most n steps on numbers with at most k bits”. Similarly, the space requirement is best characterized by saying that “the machine stores mostnnumbers with at mostkbits”.

If we want a single number to characterize the running time of a RAM computation, we can count as the time of a step not one unit but the number of bits of the integers occurring

in it (both register addresses and their contents). Since the number of bits of an integer is essentially base two logarithm of its absolute value, it is also usual to call this model logarithmic costRAM.)

In arithmetical and algebraic algorithms, it is sometimes convenient to count the arith- metical operations; on a Random Access Machine, this corresponds to extending the set of basic operations of the programming language to include the subtraction, multiplication, division (with remainder) and comparison of integers, and counting the number of steps in- stead of the running time. If we perform only a polynomial number of operations (in terms of the length of the input) on numbers with at most a polynomial number of digits, then our algorithm will be polynomial in the logarithmic cost model.