Computational Complexity
Polynomial Time
Exponential Time
Algorithms
Complexity Theory

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.

Practice algorithms

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 nn), can be upper-bounded by a polynomial expression. Mathematically, a polynomial time algorithm has a time complexity of O(nk)O(n^k), where kk 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 O(nlogn)O(n \log n) 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 O(2f(n))O(2^{f(n)}), where f(n)f(n) is a linear or polynomial function in nn.

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, O(n!)O(n!), representing an exponential time complexity when considering O(2n)O(2^n) growth.

Summary Table

AspectPolynomial TimeExponential Time
Time ComplexityO(nk)O(n^k) where kk is a constantO(2f(n))O(2^{f(n)}) or more complex
FeasibilityPractical for large inputsImpractical for large inputs
EfficiencyConsidered efficientConsidered inefficient
Algorithm ClassP (polynomial)Often NP-complete
DeterminismDeterministicNon-deterministic (for some NP)
Example ProblemsSorting, graph algorithmsTSP, 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.