{
  "id": "amortized-analysis",
  "title": "Amortized Analysis",
  "lang": "en",
  "source_lang": "en",
  "summary": "Amortized analysis bounds the total cost of a sequence of operations, giving per-operation guarantees without assuming a probability distribution over inputs.",
  "category": "computer-science",
  "subcategory": "algorithms-theory",
  "type": "concept",
  "aliases": [
    "Amortised analysis"
  ],
  "generated_at": "2026-11-04",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/amortized-analysis/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/amortized-analysis/",
      "json": "https://aiwiki.page/zh/amortized-analysis.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "Amortized analysis is a method for analyzing the resource cost of an algorithm or data structure over a sequence of operations. Rather than bounding each operation in isolation, it bounds the total cost of the sequence and distributes that cost across its operations. An individual operation may be expensive, yet the amortized cost can remain small because expensive operations cannot occur frequently without enough cheaper operations between them. Unlike average-case analysis, amortized analysis does not require a probability distribution over inputs."
    },
    {
      "heading": "Definition and scope",
      "text": "Let a sequence of \\(m\\) operations have actual costs \\(c_1,c_2,\\ldots,c_m\\). Its total cost is\n\n\\[T(m)=\\sum_{i=1}^{m}c_i.\\]\n\nAn amortized bound establishes an upper bound on this total for every valid sequence covered by the analysis, starting from a specified initial state. If every such sequence costs at most \\(Cm\\), for a constant \\(C\\), the operations have constant amortized cost.\n\nMore generally, an analysis may assign an amortized charge \\(\\widehat{c}_i\\) to each operation such that\n\n\\[\\sum_{i=1}^{m}c_i\\leq \\sum_{i=1}^{m}\\widehat{c}_i.\\]\n\nDifferent operation types can have different charges. For example, insertion might have constant amortized cost while deletion has logarithmic amortized cost.\n\nAmortized analysis is most commonly used for time complexity, but the same reasoning can bound other additive costs, such as memory transfers, disk accesses, or individual bit changes. A valid analysis must specify its cost model: copying an element, for example, is not necessarily a constant-time operation if the element itself has variable size."
    },
    {
      "heading": "Relationship to worst-case and average-case analysis",
      "text": "Three kinds of guarantee must be distinguished:\n\n- Worst-case cost per operation bounds the cost of each individual operation, including the most expensive one.\n- Average-case cost averages over an explicitly or implicitly specified distribution of inputs.\n- Amortized cost bounds the total cost of an arbitrary valid sequence, allowing individual operations to exceed their assigned charges.\n\nAmortized analysis is therefore a form of worst-case analysis over sequences, not a claim that unfavorable inputs are unlikely. It exploits restrictions on how expensive operations can be combined.\n\nA resizing array illustrates the difference. An append that triggers resizing can take linear time, but a sequence of appends can still take linear total time. Thus, append has constant amortized cost without having constant worst-case cost.\n\nAmortization and randomization are independent ideas. A randomized algorithm can have an expected amortized bound, combining a sequence-level argument with an expectation over random choices. Such a guarantee should not be confused with a deterministic amortized bound."
    },
    {
      "heading": "Main methods",
      "text": "The three standard methods—aggregate analysis, the accounting method, and the potential method—provide different ways to establish sequence-level bounds. They often express the same underlying argument.\n\nAggregate analysis\n\nAggregate analysis directly bounds the total work done by a sequence and then divides by its length.\n\nConsider a binary counter initially equal to zero. Incrementing it changes all trailing one-bits to zero and changes the following zero-bit to one. A single increment may change many bits.\n\nOver \\(m\\) increments, the least significant bit changes \\(m\\) times, the next changes \\(\\lfloor m/2\\rfloor\\) times, and successively higher bits change less often. The total number of bit changes is\n\n\\[\\sum_{j\\geq 0}\\left\\lfloor\\frac{m}{2^j}\\right\\rfloor < 2m.\\]\n\nThe counter therefore has constant amortized increment cost in a model that charges per bit change, despite occasional increments that change a logarithmic number of bits. The counter must have sufficient capacity for the sequence being considered.\n\nAccounting method\n\nThe accounting method assigns charges to operations and treats any excess over actual cost as stored credit. Later expensive operations spend that credit. The proof must ensure that the accumulated credit never becomes negative.\n\nFor a stack supporting push, pop, and an operation that pops several elements, charge two units for each push:\n\n- One unit pays for inserting the element.\n- One unit remains attached to that element and pays for its eventual removal.\n\nBecause an element cannot be removed more than once without being inserted again, the stored credit pays for all successful removals. Constant operation overhead can be charged separately. Consequently, any sequence of \\(m\\) such operations starting with an empty stack has total cost \\(O(m)\\), even if one multiple-pop operation removes many elements.\n\nThese credits are mathematical bookkeeping; an implementation need not store them.\n\nPotential method\n\nThe potential method represents stored credit through a numerical function of the data structure's state.\n\nLet \\(D_i\\) be the state after operation \\(i\\), and let \\(\\Phi(D_i)\\) be its potential. Define\n\n\\[\\widehat{c}_i = c_i+\\Phi(D_i)-\\Phi(D_{i-1}).\\]\n\nSumming gives a telescoping identity:\n\n\\[\\sum_{i=1}^{m}c_i = \\sum_{i=1}^{m}\\widehat{c}_i +\\Phi(D_0)-\\Phi(D_m).\\]\n\nIf \\(\\Phi(D_0)=0\\) and the potential is nonnegative on every reachable state, the total actual cost is at most the total amortized cost. More generally, it is sufficient to ensure that the final potential is no smaller than the initial potential.\n\nAn operation that increases potential is charged for work it may cause later. An operation that decreases potential can perform actual work using previously accumulated credit. Individual amortized charges can be negative; the validity of the proof depends on the cumulative inequality, not on every charge being positive.\n\nThe potential method is particularly useful when the relevant stored work is a global property of a structure rather than something naturally attached to individual elements."
    },
    {
      "heading": "Dynamic arrays",
      "text": "A dynamic array stores elements in an array with a capacity that may exceed its current size. When an append finds the array full, the implementation can allocate a larger array and copy the existing elements.\n\nSuppose capacity doubles whenever the array is full. Over \\(m\\) appends from an empty array, the numbers of elements copied at successive resizes are\n\n\\[1,2,4,8,\\ldots\\]\n\nup to a term smaller than \\(m\\). Their sum is less than \\(2m\\). Including the \\(m\\) writes of newly appended elements gives fewer than \\(3m\\) element writes or copies in this simplified model. Append therefore has \\(O(1)\\) amortized cost.\n\nA potential-function proof\n\nLet \\(s\\) denote size and \\(b\\) denote capacity. Starting with size and capacity zero, let the first append allocate capacity one, and subsequently double capacity when full. Define\n\n\\[\\Phi=2s-b.\\]\n\nFor states reachable by appends alone, this potential is nonnegative.\n\nAn append that does not resize has actual cost one and increases the potential by two, giving amortized cost three.\n\nIf a full array has size and capacity \\(b\\geq 1\\), a resizing append costs \\(b+1\\): it copies \\(b\\) elements and writes one new element. The change in potential is\n\n\\[\\Delta\\Phi = \\bigl(2(b+1)-2b\\bigr)-(2b-b) = 2-b.\\]\n\nIts amortized cost is therefore\n\n\\[\\widehat{c}=(b+1)+(2-b)=3.\\]\n\nThe first append has amortized cost two, so the same constant upper bound covers every append.\n\nGrowth and shrinkage policies\n\nDoubling is not essential. Increasing capacity by any fixed multiplicative factor greater than one gives constant amortized append cost under the same element-copying model. A smaller growth factor generally reduces unused capacity but increases copying frequency.\n\nBy contrast, growing capacity by a fixed additive amount can require quadratic total copying over a long sequence of appends.\n\nSupporting deletion requires additional care. If an array doubles when full but immediately halves when it becomes half full, alternating insertions and deletions near a boundary can repeatedly trigger large copies. A common policy instead halves capacity only when occupancy falls to one quarter, subject to a minimum capacity. This separation between growth and shrink thresholds ensures that sufficiently many updates occur between large resizes."
    },
    {
      "heading": "Queues implemented with two stacks",
      "text": "A queue can be implemented using an input stack and an output stack:\n\n- Enqueue pushes onto the input stack.\n- Dequeue pops from the output stack.\n- If the output stack is empty, all elements from the input stack are moved to it before the dequeue continues.\n\nA transfer can take linear time, but each enqueued element is transferred at most once before removal.\n\nAssume each stack push or pop costs one unit, and define\n\n\\[\\Phi=2\\,|S_{\\mathrm{in}}|.\\]\n\nAn enqueue costs one and increases potential by two, so its amortized cost is three. A dequeue without transfer has amortized cost one. If a dequeue transfers \\(k\\) elements, it performs \\(2k+1\\) stack operations, but potential decreases by \\(2k\\), again leaving amortized cost one.\n\nThus, both queue operations have constant amortized cost, although a particular dequeue may be expensive."
    },
    {
      "heading": "Applications in advanced data structures",
      "text": "Amortized analysis is central to several structures whose performance depends on sequences rather than isolated operations.\n\n- Splay trees. These self-adjusting binary search trees restructure themselves after accesses. Standard operations have logarithmic amortized cost, although an individual operation can take linear time.\n- Fibonacci heaps. These priority queues defer structural cleanup. Insertion and decrease-key have constant amortized cost, while extract-min has logarithmic amortized cost.\n- Disjoint-set data structures. Combining path compression with union by rank or size gives extremely small amortized costs expressed using the inverse Ackermann function. The bound concerns a sequence of operations, not a worst-case guarantee of that magnitude for every individual operation.\n- Hash tables. Geometric resizing can be analyzed amortized, while collision handling requires a separate analysis. Constant expected amortized update time may therefore combine assumptions about hashing with an amortized argument about rebuilding.\n\nIn each case, the analysis identifies structural changes that either accumulate useful progress or consume progress accumulated earlier."
    },
    {
      "heading": "Assumptions and limitations",
      "text": "Initial state\n\nAn amortized guarantee depends on the initial state. A sequence beginning with a large, fully populated structure may incur substantial work immediately, without any earlier operations in the sequence to pay for it.\n\nA bound may therefore include an initial-state term:\n\n\\[T(m)=O\\bigl(m+\\Phi(D_0)\\bigr).\\]\n\nAlternatively, the analysis can include construction costs or restrict attention to sequences starting from an empty structure.\n\nPermitted operations\n\nA proof applies only to the operations it covers. A counter argument for increments does not automatically cover decrements, resets, or assignments. Likewise, a dynamic-array append bound does not establish constant amortized insertion at arbitrary positions, where shifting elements may dominate the cost.\n\nLatency\n\nA small amortized cost does not prevent individual pauses. Resizing, rebuilding, or restructuring may still cause unacceptable latency in a real-time system.\n\nDeamortization converts some sequence-level guarantees into worst-case per-operation guarantees, often by spreading a large task across subsequent operations. For example, an implementation may copy a bounded number of elements on each update while a resize is in progress. This requires additional state and a separate proof that unfinished work never falls behind.\n\nExecution model and state reuse\n\nA sequential amortized bound does not automatically account for contention, scheduling, or coordination costs in concurrent computing.\n\nState reuse also requires care. In a persistent data structure, several future branches may share an earlier version. Credit associated with that version cannot simply be spent independently on every branch without justification. The permitted execution structure is therefore part of the amortized argument, just as the operation set and initial state are."
    }
  ],
  "links": [
    "algorithm",
    "data-structure",
    "time-complexity",
    "randomized-algorithm",
    "expected-value",
    "bit",
    "stack-data-type",
    "dynamic-array",
    "array-data-structure",
    "queue-data-type",
    "binary-search-tree",
    "priority-queue",
    "hash-table",
    "real-time-operating-system",
    "concurrent-computing"
  ],
  "sources": [],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}