4.6.22 · D4Theory of Computation

Exercises — P — polynomial time

3,438 words16 min readBack to topic

Prerequisite links you may want open while solving: Time complexity & Big-O, Turing Machine, TIME complexity classes, Cobham–Edmonds thesis, Polynomial-time reductions, NP — nondeterministic polynomial time.


Level 1 — Recognition

(Can you read the definition and apply it directly?)

Exercise 1.1 — Is it a decision problem?

Classify each as a decision problem (yes/no) or not: (a) "Is prime?" (b) "Sort this list." (c) "Does graph have a path from to ?" (d) "How many edges does have?"

Recall Solution 1.1

A decision problem asks a yes/no question — formally, "is the input in language ?"

  • (a) Yes/no ✅ decision problem (?).
  • (b) Not a decision problem — it outputs a rearranged list, not yes/no. (Its decision version would be "is this list already sorted?")
  • (c) Yes/no ✅ decision problem (reachability).
  • (d) Not — it outputs a number, not yes/no. (Decision version: "does have edges?")

Exercise 1.2 — Read the growth table

Using the parent note's table, at the parameter value (here is the plain problem parameter, the number , not a bit-length) which is larger, or ? By roughly what factor?

Recall Solution 1.2

. . is larger by a factor of about roughly 9 billion times larger. The exponential has already run away. (This growth gap is drawn in the figure under Level 2.)

Exercise 1.3 — Which are in P?

Which of these are known to be in P? (a) reachability (b) Multiplying two -digit numbers (c) Deciding whether is prime (d) Sorting numbers.

Recall Solution 1.3

First reframe the non-decision ones. P is a class of decision languages only, so (b) and (d) — which produce outputs, not yes/no — must be converted to yes/no versions before we can even ask "is it in P?" (their function versions live in FP, a separate class):

  • (b) Multiplication → the decision language ("is the product of and ?").
  • (d) Sorting → the decision language ("is this list sorted?"). (An equivalent useful version: "does the sorted list have element in position ?")

Now judging membership, all four decision problems are in P:

  • (a) reachability: BFS in where are the vertex and edge counts. ✅
  • (b) "?": compute the product by schoolbook multiplication in (with = number of digits), then compare to in . ✅
  • (c) " prime?": AKS, polynomial in bits. ✅
  • (d) "sorted?": one left-to-right scan comparing adjacent items, for items. ✅

Membership in P only needs one polynomial algorithm to exist — not the fastest.


Level 2 — Application

(Count steps and bound them. Here = number of items in the array.)

Exercise 2.1 — Bound a nested loop

An algorithm on an array of items runs two nested loops, the inner doing work per pair. Give the step count and state whether it is in P.

Recall Solution 2.1

Let be some fixed positive constant (the work per inner iteration, independent of ). Outer loop times, inner loop times: is a polynomial in input size (the array has items). ✅ In P.

Exercise 2.2 — Three nested loops

Same setup but three nested loops, each running times. Step count? Still in P?

Recall Solution 2.2

With again a fixed positive constant independent of : This is a polynomial with — a constant exponent, independent of — so it stays in P. ✅ The figure below shows why three loops is still fine: read the vertical axis (steps) against the horizontal axis (input size ). The green curve and blue curve both stay near the floor, while the red curve shoots off the top of the chart near . The lesson to observe: a fixed power of grows slowly compared to , no matter how many loops (as long as their count is a constant).

Figure — P — polynomial time

Exercise 2.3 — Reachability on a specific graph

A graph has vertices and edges (recall = vertex count, = edge count). Give an upper bound on BFS steps, and check it against the polynomial bound (with ).

Recall Solution 2.3

BFS visits each vertex once and inspects each edge a constant number of times, so its cost is at most for some fixed constant . Here , so BFS uses at most steps. The parent bound (treating ): a simple graph has at most edges (see the notation box), so So the exact work is bounded by the polynomial — comfortably polynomial.


Level 3 — Analysis

(Spot why a bound is or isn't polynomial in the right variable.)

Exercise 3.1 — The primality trap

Trial division tests divisors up to . For with bits (here is the bit-length, the formal input size), express the step count in terms of and explain why this does not prove .

Recall Solution 3.1

Steps . Since , That is exponential in , the true input size (bits). A polynomial bound must look like with a constant; is not of that form (the exponent grows with ). So trial division is not a P proof. PRIMES is in P — but via the AKS algorithm, which runs in .

Exercise 3.2 — Numeric feel for the trap

Take (so bits). Compare trial-division steps () against a hypothetical algorithm.

Recall Solution 3.2

Trial division: steps. steps. Ratio — trial division already does ~16× more work at a tiny 40-bit input, and the gap explodes as grows.

Exercise 3.3 — Is in P?

Is an algorithm running in steps (with a fixed positive constant) in P? Is it "practical"? Reconcile.

Recall Solution 3.3

Yes, it is in P: is a polynomial with exponent — a constant independent of — so it lies inside , and hence inside . (Recall = problems decidable by a deterministic TM in steps.) Practical? No — even gives steps, more than atoms in the universe. Reconcile: P is a theoretical boundary of feasibility, not a promise of speed. It survives as a useful notion because real-world poly algorithms tend to have small exponents (2, 3, occasionally higher). "In P" ≠ "quick," it means "in-principle tractable."


Level 4 — Synthesis

(Combine algorithms; use closure properties.)

Exercise 4.1 — Composing two poly algorithms

Subroutine runs in . Algorithm calls once for each of items, plus extra work. Total time? Still polynomial?

Recall Solution 4.1

is called times: . Add the extra : ✅ Polynomial. This is the closure under composition argument from the parent: poly number of calls to a poly subroutine, plus poly glue, stays poly.

Exercise 4.2 — Poly of a poly

Algorithm produces an output of length , then feeds it to (which runs in ). Total time in terms of ?

Recall Solution 4.2

on input of length costs . Substitute : Add 's own cost (say ) → still . ✅ Polynomial. This uses : a polynomial of a polynomial is a polynomial. That closure is exactly why P is chosen as the feasibility class.

Exercise 4.3 — Closure under complement

via decider running in . Show and give its bound.

Recall Solution 4.3

Build : run on (cost ), then flip its answer (yes↔no) in . decides correctly and runs in . ✅ , same polynomial degree. This is why P is closed under complement — see Polynomial-time reductions for the general composition machinery.


Level 5 — Mastery

(Prove a subtle membership; reason about model-robustness.)

Exercise 5.1 — Same number, different encoding

Consider deciding "is even?" when is given (a) in binary ( bits) versus (b) in unary (a string of ones, length ). Is the problem in P under each encoding? What does this teach?

Recall Solution 5.1

(a) Binary: just read the last bit — , or to scan the string. Polynomial in . ✅ In P. (b) Unary: the input length is itself, so scanning it is — but is the input size here, so is linear in the input length, hence polynomial in that size. ✅ Also in P (under the unary measure). Lesson: "in P" is always relative to the encoding length. A problem can flip between exponential-looking and polynomial purely by changing the encoding (this is exactly the primality trap — value vs bits). Reasonable encodings are within polynomial factors of each other; pathological ones like unary are not, so we standardise on binary. See Cobham–Edmonds thesis.

Exercise 5.2 — Model robustness

A single-tape TM simulating a -tape TM incurs at most a quadratic slowdown. If the -tape machine runs in , bound the single-tape running time and conclude membership in P is unchanged.

Recall Solution 5.2

Step 1 — WHAT the slowdown theorem says (state the bound). A standard TM result: any computation that a -tape machine performs in steps can be reproduced by a single-tape machine in at most steps. Why quadratic: to mimic one step of the multitape machine, the single-tape machine sweeps across its whole tape to find and update the simulated head positions; after steps the used tape has length , and each of the simulated steps costs an sweep, giving . Step 2 — WHY this settles P. Plug in the given -tape runtime : This is still a polynomial with a constant exponent (), so the language, decidable in on a single-tape machine, is in the same class it was in on the -tape machine. Step 3 — WHY that means P is model-robust. The exponent merely doubled (); a polynomial squared is still a polynomial (, closure from Exercise 4.2). So switching machine models can only multiply the exponent by a constant — it can never turn polynomial into exponential. That is precisely why "is it in P?" gives the same answer on a single-tape TM, a -tape TM, a RAM, or your laptop. This model-independence is the content of the Cobham–Edmonds thesis; see Cobham–Edmonds thesis and Turing Machine.

Exercise 5.3 — Prove a membership from scratch

Prove that is in P. State the algorithm, count steps in the input size, and give the polynomial bound. (Here = number of vertices, = number of edges.)

Recall Solution 5.3

Encoding: represent by its adjacency matrix, a table of size bits, so that testing whether any single edge exists is an table lookup. Algorithm: for every unordered triple of vertices , check whether all three edges are present. Accept iff some triple is a triangle. Step count: number of triples (see the notation box for ); each triple checks edges in (adjacency matrix). Total: Input size: with the adjacency-matrix representation the input is bits, so , giving a polynomial with constant exponent . ✅ Either way — in or in bit-length — the bound is a fixed-degree polynomial, so .


Wrap-up recall

Connections