Hello! Welcome to your first lesson in the module on Computational Complexity.
In our previous modules, we focused on the fundamental question of computability: can a problem be solved by a Turing Machine at all? We explored the limits of computation, culminating in the discovery of undecidable problems like the Halting Problem and the equivalence problem for context-free languages ().
Now, we shift our focus. For the vast universe of problems that are decidable, we ask a new, more practical question: how efficiently can they be solved? This lesson marks our entry into complexity theory. Our goal today is to establish a formal, mathematical way to answer this question by defining the time complexity of a Turing Machine. This definition will be the bedrock for classifying problems and understanding concepts like the famous P vs. NP problem.
1. From Steps to Running Time
At its heart, a Turing Machine is a step-by-step model of computation. To measure how long a TM takes, we need to count these steps.

A "step" corresponds to one application of the TM's transition function, , which involves:
- Reading the symbol on the current tape cell.
- Changing to a new state.
- Writing a new symbol on the current tape cell.
- Moving the tape head left or right.
The total number of these steps a TM takes before it halts (in either an accept or reject state) is its running time.
2. Formalizing Time Complexity
While the idea of counting steps is simple, a formal definition requires care. The number of steps usually depends on the specific input string. A TM might be very fast for some inputs and slower for others, even if they have the same length.
To handle this, we will build our definition in a few stages.
ECS 120 Lesson 22 – Time Complexity
These lecture notes provide a clear, step-by-step development of the formal definition of time complexity. We will use it to ground our understanding.
Please read the following sections from this document: Start with the introduction on page 1 to see the motivation for shifting from computability to complexity. Read Section 1, 'Running-time Analysis of Algorithms,' which introduces the running-time function for a specific input word, rtM(w). Continue with Section 1.1, 'Input Size,' which clarifies that we measure complexity as a function of the input's length, n. Finally, read Section 1.2, 'Types of Analysis,' and pay close attention to 'Definition 1 (Time Complexity of Turing Machines)'. This is the core of today's lesson.
Let's summarize the key definitions from the reading:
-
Running time on an input
w: Denoted as , this is the total number of steps the TM performs before halting on the specific input string . -
Input size
n: We want to measure complexity as the input gets larger. By convention, we use the length of the input string, , as the measure of input size. -
Worst-Case Analysis: For a given size , there could be many input strings. We are typically interested in the "pessimistic" view: what is the maximum number of steps the machine might take for any input of that size? This is known as worst-case analysis.
This leads us to the central definition:
Definition: Time Complexity of a Turing Machine
Let be a deterministic Turing Machine that halts on all inputs. The time complexity (or running time) of is the function , where is the maximum number of steps that performs on any input word of length .
Formally:
If a TM has a time complexity of , we say it is a -time Turing Machine.
Test your understanding!
Consider a simple Turing Machine that, on any input , immediately moves its head to the right, then left, and then halts.
- What is ?
- What is the time complexity function ?
Show answer
- The machine performs two steps (one move right, one move left) regardless of the input content. So, .
- The running time is constant for any input of any length. The maximum number of steps for any input of length is always 2. Therefore, .
3. Asymptotic Analysis and Big-O Notation
Precisely calculating for complex TMs can be tedious, and the exact number might depend on low-level implementation details of the TM model. In computer science, we are usually more interested in how the running time grows as the input size increases.
Your experience with algorithm analysis in software development is directly applicable here. We use Big-O notation to capture this asymptotic behavior, ignoring constant factors and lower-order terms.
What is Time (in complexity theory)? - Easy Theory
This short video provides a great intuition for why we focus on worst-case complexity and use Big-O notation.
Watch the first three minutes of this video (from 00:00 to 03:03). It effectively explains the move from exact step counts to asymptotic complexity using Big-O.
So, instead of saying a TM runs in time, we simplify this and say it runs in time. This tells us the running time grows quadratically with the input size, which is the most important piece of information for large inputs.
When we say a TM has a running time of , it means there exists some function such that the time complexity of , , is at most for all sufficiently large .
4. Example: Analyzing a Palindrome-deciding TM
Let's apply these concepts to a concrete example. Consider a TM that decides the language .
A high-level algorithm for this TM could be:
- Read the first symbol of the string, cross it out (e.g., write a blank).
- Scan to the right end of the string.
- Check if the last symbol matches the one from step 1. If not,
reject. - If it matches, cross it out.
- Scan back to the left end of the remaining string.
- Repeat this process until the string is empty or a mismatch is found.
Let's analyze the time complexity of this algorithm for an input of length .
- One Pass: A single pass involves scanning from left to right ( steps) and then back from right to left ( steps). So, one pass takes time.
- Number of Passes: In each pass, we eliminate two characters from the string. We need to do this approximately times.
Combining these:
A more detailed analysis would show the first pass takes steps, the second takes , and so on, leading to a sum that is also .
Therefore, we say the time complexity of this TM is .
Test your understanding!
Consider a TM that decides the language . A common algorithm for this is to repeatedly scan the tape, crossing off one 'a' and one 'b' in each pass until no symbols are left.
What is the time complexity of this TM in Big-O notation? Briefly justify your answer.
Show answer
The time complexity is .
Justification:
Let the input have length . The algorithm will perform passes. In each pass, the TM must scan across the tape to find and cross off an 'a' and a 'b'. The length of the tape to be scanned is initially and decreases slightly with each pass, but remains for most of the process. Therefore, each of the passes takes time. The total time complexity is .
Conclusion
Today we laid the essential groundwork for analyzing the efficiency of algorithms within the framework of Turing Machines. This shift from "can it be computed?" to "how long does it take?" is the central theme of complexity theory.
Key Takeaways:
- The time complexity of a TM measures the number of discrete computational steps it takes to halt.
- We standardize this measure by using a worst-case analysis, defining the time complexity for an input size as the maximum time taken over all possible inputs of that length.
- The formal definition is .
- We use Big-O notation to describe the asymptotic growth of , allowing us to compare the efficiency of algorithms in a way that is independent of machine-specific details and constant factors.
Preview of the Next Lesson:
Now that we have a formal tool to measure the time taken by a TM, we can begin to group problems into "complexity classes" based on their resource requirements. In our next lesson, we will define the first and perhaps most important of these classes: P, the class of all decision problems that can be solved by a deterministic Turing Machine in polynomial time. This will be our first step in mapping the landscape of "efficiently solvable" problems.