liveBook · Manning

Linear time complexity

As explained in Advanced Algorithms and Data Structures

Linear time is a running-time bound proportional to the number of vertices and edges or other input elements being processed.

Linear time describes a running time that grows in proportion to the input being processed. The relevant input may be a list of goods, the characters in a string, cache entries, or another collection of elements. The bound describes how work scales, not whether the operation is fast in every practical setting. section 1.3.4 section 6.2.5

The input measure must be stated before interpreting a linear bound:

  • For a greedy unbounded-knapsack algorithm, the work is linear in the already-sorted list of goods; sorting first changes the overall cost to linearithmic. section 1.3.4
  • For a trie’s longest-prefix operation, the work is linear in the searched string’s length, written O(m) for a string of length m. section 6.2.5
  • For Bloom-filter insertion, variable-length string keys make hashing linear in key length, while bounded-length keys can make the operation effectively constant with respect to stored elements. section 4.9.3
  • For a simple LFU cache implementation, updating an entry can become linear in the number of cache elements when many counters tie and entries must be reordered. section 7.5.2

A cache update that becomes linear

Imagine a list of n cache entries whose frequency counters are equal. Accessing the entry at the end may require n−1 node swaps to move it toward the front. The operation is therefore linear in the number of entries for that edge case. Replacing the list with a priority queue changes the design and avoids this particular reordering pattern. section 7.5.2

Figure 7.10 An inefficient implementation of an LFU cache. When all elements of a list with n entries have the same value for counter, and one element at the end of the list is accessed, we will have to perform n-1 node swaps to bubble up the updated entry to the front of the list.

Shows how equal cache frequencies force an accessed entry through nearly the entire list, making the linear worst case visible. section 7.5.2

How Advanced Algorithms and Data Structures covers it

How deep it goes
Explains linear behavior through concrete bounds and assumptions, but stops short of formal proofs.
Examples
Uses fractional knapsack, longest-prefix search, Bloom-filter insertion, and LFU list bubbling as concrete cases.
What it assumes
Assumes familiarity with input sizes, running time, and basic data structures such as lists, tries, and priority queues.
What you can do after
Evaluate running-time trade-offs by identifying the input measure and checking whether preprocessing or representation changes the bound.
How it connects
Connects linear time with greedy algorithms, sorting, tries, Bloom filters, LFU caches, priority queues, and binary heaps.

Also explained in

Compare how 8 books explain linear time complexity →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Advanced Algorithms and Data Structures. The section links point to the parts of the book it draws on.