liveBook · Manning

Amortized analysis

As explained in Grokking Data Structures

Amortized analysis establishes a bound on the combined cost of a sequence of operations by distributing occasional expensive operations across the sequence.

Amortized analysis bounds the total cost of a sequence of operations rather than promising that every individual operation is fast. An occasional expensive operation can be spread across many inexpensive ones, producing a worst-case guarantee for the combined sequence. section 4.11

Use the distinction between analysis types to interpret performance:

  • Worst-case analysis reports how slow one unlucky operation can be. section 4.11
  • Average-case analysis describes expected performance but does not guarantee a particular run. section 4.11
  • Amortized analysis guarantees the growth of the total cost across many operations. section 4.11

Dynamic-array insertion

A dynamic array normally inserts in O(1), but inserting into a full array may resize it and copy every element. Starting with capacity one and doubling it moves 1 + 2 + 4 = 7 elements to reach eight elements. Across n insertions, the copying totals O(n), so the sequence has O(n) amortized time even though one insertion may be slow. section 5.16 section 5.19

def _double_size(self):
    old_array = self._array
    self._array = core.Array(self._capacity * 2, self._typecode) #1
    self._capacity *= 2
    for i in range(self._size):                                  #2
        self._array[i] = old_array[i]
#1 After saving a reference to the old array in a local variable, we can create a new array twice as large.
#2 We need to copy all the elements from the old array to the new one.

Inspect the resize operation that allocates a doubled array and copies the existing elements. section 5.16

How Grokking Data Structures covers it

How deep it goes
Explains sequence-level guarantees with dynamic-array intuition but stops short of formal analysis.
Examples
Uses repeated hash-table operations and dynamic-array resizing, including growth from capacity one to eight.
What it assumes
Builds on worst-case analysis, average-case analysis, and Big-O notation while contrasting their guarantees.
What you can do after
Evaluate repeated-operation costs without treating one occasional resize or slow operation as the whole performance story.
How it connects
Connects worst-case analysis, average-case analysis, Big-O notation, dynamic arrays, and hash tables.

Also explained in

Compare how 3 books explain amortized analysis →

Related concepts

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