Polynomial time and exponential time
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Polynomial time and exponential time are essential concepts in computational complexity theory, a branch of computer science that studies the intrinsic difficulty of computational problems and categorizes them based on the resources required to solve them. Understanding these time complexities can help in evaluating the performance of algorithms and determining the feasibility of solving certain problems within reasonable time frames.
Polynomial Time
An algorithm runs in polynomial time if its execution time, as a function of the size of the input data (denoted as ), can be upper-bounded by a polynomial expression. Mathematically, a polynomial time algorithm has a time complexity of , where is a constant non-negative integer.
Characteristics
- Deterministic: Algorithms corresponding to P (polynomial-time) are deterministic, meaning they output a result in predictable time for given input size.
- Feasibility: Polynomial time algorithms are considered efficient and practical for large inputs.
- Common Operations: Sorting algorithms like merge sort and quicksort, as well as graph algorithms like Dijkstra's algorithm, run in polynomial time.
Example: Merge Sort
Consider the merge sort algorithm, which sorts an array by dividing it into halves, recursively sorting each half, and then merging the sorted halves. Merge sort runs in time, a polynomial time complexity. This efficiency makes merge sort viable for large datasets.
Exponential Time
In contrast, an algorithm runs in exponential time if its execution time doubles with each addition of a single input unit. This generally means the time complexity can be expressed as , where is a linear or polynomial function in .
Characteristics
- Infeasibility: Exponential time algorithms quickly become impractical as input size increases, often reserved for small inputs.
- Non-deterministic: Algorithms that fall into the class NP (nondeterministic polynomial time) often require exponential time unless a polynomial-time solution is known.
- Complex Problems: Many NP-complete problems, such as the traveling salesman problem and the boolean satisfiability problem (SAT), do not have known polynomial-time solutions.
Example: The Traveling Salesman Problem (TSP)
The TSP requires finding the shortest possible route that visits a given set of cities and returns to the origin city. The brute-force solution involves checking all possible permutations of city visits, which grows in factorial time complexity, , representing an exponential time complexity when considering growth.
Summary Table
| Aspect | Polynomial Time | Exponential Time |
| Time Complexity | where is a constant | or more complex |
| Feasibility | Practical for large inputs | Impractical for large inputs |
| Efficiency | Considered efficient | Considered inefficient |
| Algorithm Class | P (polynomial) | Often NP-complete |
| Determinism | Deterministic | Non-deterministic (for some NP) |
| Example Problems | Sorting, graph algorithms | TSP, SAT, some recursive problems |
Additional Subtopics
NP-Completeness
While polynomial and exponential time classifications provide insights into algorithm efficiency, computational complexity further introduces NP-completeness. A problem is NP-complete if it is both in NP and as hard as any problem in NP, meaning if any NP-complete problem can be solved in polynomial time, then all problems in NP have polynomial solutions. This is known as the P vs NP problem, one of the most important open questions in computer science.
Real-World Applications
Understanding these complexities is critical when designing software for real-world applications. For instance, cryptographic algorithms rely on exponential time complexity to be secure against brute-force attacks. Conversely, optimizing logistics and scheduling requires polynomial-time solutions for practical implementation.
Conclusion
Grasping the difference between polynomial time and exponential time is crucial for computer scientists and software engineers. It equips them with the knowledge to select appropriate algorithms, assess their computational feasibility, and anticipate potential challenges in scaling. More broadly, these concepts are pivotal in understanding the limits of computation and the pursuit of finding more efficient algorithms for complex problems.
Related reading
- Polynomial time solution for Tetris Puzzle
- Poor man's authentication algorithm?
- Popularity decay algorithm for popular website posts
- Possible Interview Question How to Find All Overlapping Intervals
- Poor performance of log4j2 in combination with Kafka
- Poor performance with Spark streaming, Kafka and multiple topics
- Possible permutations of BST's input
- potential On solution to Longest Increasing Subsequence

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.