103 Performance Analysis
103 Performance Analysis
2 Performance Analysis
What is an algorithm?
Recipe, process, method, technique, procedure,
routine,… with following requirements:
1. Finiteness
terminates after a finite number of steps
2. Definiteness
rigorously and unambiguously specified
3. Input
valid inputs are clearly specified
4. Output
can be proved to produce the correct output
given a valid input
5. Effectiveness
steps are sufficiently simple and basic
3 Performance Analysis
Design and Analysis of Algorithms
• Analysis: predict the cost of an algorithm in terms of resources and
performance
4 Performance Analysis
Problem & Algorithm
An instance of a problem consists of all inputs needed to
compute a solution to the problem.
An algorithm is said to be correct if for every input instance,
it halts with the correct output.
A correct algorithm solves the given computational
problem.
An incorrect algorithm might not halt at all on some
input instance, or it might halt with other than the desired
answer.
5 Performance Analysis
Example: sorting
Statement of problem:
Input:
A sequence of n numbers
<a1, a2, …, an>
Output:
A reordering of the input sequence
<b1, b2, …, bn>
so that bi ≤ bj whenever i < j
6 Performance Analysis
Example: sorting
Sorting algorithms:
• Selection sort?
problem • Insertion sort?
• Merge sort?
• …
algorithm
7 Performance Analysis
Selection sort
Input:
array a[0], …, a[n-1]
Output:
array a sorted in non-decreasing order
Algorithm:
for i=0 to n-1
swap a[i] with smallest of a[i],…a[n-1]
8 Performance Analysis
Some well-known computational problems
Sorting
Searching
Shortest paths in a graph
Minimum spanning tree
Primality testing
Traveling salesman problem
Knapsack problem
Chess
Towers of Hanoi
Program termination
9 Performance Analysis
Basic issues related to algorithms
How to design algorithms ?
How to express algorithms ?
Proving correctness of algorithms
Efficiency
Theoretical analysis
Empirical analysis
Optimality
10 Performance Analysis
Algorithm design strategies
Brute force
Divide and conquer
Decrease and conquer
Transform and conquer
Greedy approach
Dynamic programming
Backtracking
Branch and bound
Space and time tradeoffs
11 Performance Analysis
Analysis of algorithms
How good is the algorithm?
Correctness
Time efficiency
Space efficiency
Simplicity
Generality
Does there exist a better algorithm?
Lower bounds
Optimality
12 Performance Analysis
Why study algorithms and performance?
Algorithms help us to understand scalability.
Performance often draws the line between what is feasible
and what is impossible.
Algorithmic mathematics provides a language for talking
about program behavior.
Performance is the currency of computing.
The lessons of program performance generalize to other
computing resources.
Speed is fun!
13 Performance Analysis
Algorithm Design and Analysis Process
14 Performance Analysis
Performance Analysis
The efficiency of an algorithm can be decided by measuring the
performance of an algorithm.
Performance of an algorithm is a process of making evaluative
judgement about algorithms.
Performance of an algorithm means predicting the resources which
are required to an algorithm to perform its task.
The performance of an algorithm depends upon two factors.
– 1) Amount of time required by an algorithm to execute (known as
time complexity).
– 2) Amount of storage required by an algorithm (known as Space
complexity).
Performance analysis of an algorithm is the process of
calculating space and time required by that algorithm.
15 Performance Analysis
Performance of an algorithm
Generally, the performance of an algorithm depends on the following
elements...
1. Whether that algorithm is providing the exact solution for the
problem?
2. Whether it is easy to understand?
3. Whether it is easy to implement?
4. How much space (memory) it requires to solve the problem?
5. How much time it takes to solve the problem? etc.,
16 Performance Analysis
The goodness of an algorithm
17 Performance Analysis
Need for analysis
18 Performance Analysis
Complexity
19 Performance Analysis
Internal and external factors
internal factors
The algorithm’s efficiency, in terms of:
• Time required to run
• Space (memory storage)required to run
external factors
Speed of the computer on which it is run
Quality of the compiler
Size of the input to the algorithm
Complexity measures the internal factors (usually more
interested in time than space)
20 Performance Analysis
Two ways of finding complexity
21 Performance Analysis
Experimental study
Write a program implementing the algorithm
Run the program with inputs of varying size and
composition
Get an accurate measure of the actual running time
Use a method like System.currentTimeMillis()
Plot the results
22 Performance Analysis
Java Code – Simple Program
import java.io.*; for(i=0;i<N;i++)
class for1 for(j=0;j<i;j++)
{ Sum++;
public static void main(String args[]) long
throws Exception end=System.currentTimeMillis()
{ ;
int N,Sum; long time=end-start;
N=10000; // N value to be changed. System.out.println(" The start time
is : "+start);
Sum=0;
System.out.println(" The end time
long is : "+end);
start=System.currentTimeMillis();
System.out.println(" The time
int i,j; taken is : "+time);
}
}
23 Performance Analysis
Example plot
24 Performance Analysis
Limitations of Experiments
It is necessary to implement the algorithm, which may be
difficult
Results may not be indicative of the running time on other
inputs not included in the experiment.
In order to compare two algorithms, the same hardware
and software environments must be used
Experimental data though important is not sufficient
25 Performance Analysis
Theoretical Analysis
Uses a high-level description of the algorithm
instead of an implementation
Characterizes running time as a function of the
input size, n.
Takes into account all possible inputs
Allows us to evaluate the speed of an algorithm
independent of the hardware/software environment
26 Performance Analysis
Space Complexity
28 Performance Analysis
Space Complexity
29 Performance Analysis
Representation in computer
Computers use a fixed number of bits to represent an integer. The
commonly-used bit-lengths for integers are 8-bit, 16-bit, 32-bit or 64-bit
….
Unsigned Integers: can represent zero and positive integers.
Signed Integers: can represent zero, positive and negative integers.
Integers. Integers are commonly stored using a word of memory,
which is 4 bytes or 32 bits, so integers from 0 up to 4,294,967,295 (232 -
1) can be stored.
Below are the integers 1 to 5 stored as four-byte values (each
row represents one integer).
30 Performance Analysis
IEEE Standard 754 Floating Point Numbers
The IEEE Standard for Floating-Point Arithmetic (IEEE 754) is a
technical standard for floating-point computation which was
established in 1985 by the Institute of Electrical and Electronics
Engineers (IEEE).
The standard addressed many problems found in the diverse floating
point implementations that made them difficult to use reliably and
reduced their portability.
IEEE Standard 754 floating point is the most common representation
today for real numbers on computers, including Intel-based PC’s,
Macs, and most Unix platforms.
31 Performance Analysis
IEEE Standard 754 Floating Point Numbers
IEEE 754 has 3 basic components:
The Sign of Mantissa – This is as simple as the name. 0 represents
a positive number while 1 represents a negative number.
The Biased exponent –The exponent field needs to represent both
positive and negative exponents. A bias is added to the actual
exponent in order to get the stored exponent.
The Normalised Mantissa – The mantissa is part of a number in
scientific notation or a floating-point number, consisting of its
significant digits. Here we have only 2 digits, i.e. O and 1. So a
normalised mantissa is one with only one 1 to the left of the decimal.
32 Performance Analysis
IEEE Standard 754 Floating Point Numbers
33 Performance Analysis
IEEE Standard 754 Floating Point Numbers
34 Performance Analysis
Space Complexity
The space complexity of an algorithm is the amount of memory it
needs to run to completion.
Space requirements of an algorithm
The fixed part of an Algorithm (C)
includes space for
o Instructions
o Simple variables
o Fixed size component variables
o Space for constants
o Etc..
Variable Part of an Algorithm SP(I)
35 Performance Analysis
Why Space Complexity
37 Performance Analysis
Space Complexity S(P)=C + SP(I)
Fixed Space Requirements (C)
Independent of the characteristics of the inputs and outputs
instruction space
space for simple variables, fixed-size structured variable, constants
Variable Space Requirements (SP(I))
depend on the instance characteristic I
number, size, values of inputs and outputs associated with I
recursive stack space, formal parameters, local variables,
return address
38 Performance Analysis
Space Complexity Cont…
A variable part of an algorithm is consists of the
space needed by component variable whose size
depends on the particular problem instance being
solved
The variable part includes space for
Component variables whose size is dependant on the
particular problem instance being solved
Recursion stack space
Etc..
39 Performance Analysis
Space complexity of a program that sums all integer
elements in an array:
40 Performance Analysis
Space complexity of recursive algorithm
• The function 'fibonacci(int n)' computes n'th number of fibonacci
sequence.
• In general, if f(n) denotes n'th number of fibonacci sequence then f(n)
= f(n-1) + f(n-2).
• For this recurrence relation, f(0) = 0 and f(1) = 1 are terminating
conditions.
41 Performance Analysis
Time complexity of recursive algorithm
In this recursion tree, each
state(except f(0) and f(1)) generates
two additional states and total
number of states generated are 15.
In general, total number of states are
approximately equal to 2n for
computing n'th fibonacci
number(f(n)).
Each state denotes a function call to
'fibonacci()' which does nothing but
to make another recursive call.
The total time taken to compute nth
number of fibonacci sequence is
O(2n).
42 Performance Analysis
Space complexity of recursive algorithm
Let's try to understand in general when the stack frames are
generated and for how long are they kept in the memory?
When a function 'f1' is called from function 'f0', stack frame
corresponding to this function 'f1' is created.
This stack frame is kept in the memory until call to function 'f1' is not
terminated.
This stack frame is responsible for saving the parameters of function
'f1', local variables in the function 'f1' and return address of caller
function(function 'f0').
Now when this function 'f1' calls another function 'f2', stack frame
corresponding to 'f2' is also generated and is kept in the memory until
call to 'f2' is not terminated.
When call to 'f2' is made, the call stack which has stack frames in it
looks like following -
43 Performance Analysis
Runtime stack space for recursive algorithm
44 Performance Analysis
Space complexity of recursive algorithm
In the recursion tree for computing 5th fibonacci number, when left
and bottom most state f(1) is getting executed, the call sequence
which led to this state would be f(5)->f(4)->f(3)->f(2)->f(1) and all
the corresponding stack frames would be present in the memory and
when f(1) is returned its stack frame would be removed from the
memory(or call stack).
Space complexity of recursive algorithm is proportional to maximum
depth of recursion tree generated.
If each function call of recursive algorithm takes O(m) space and if
the maximum depth of recursion tree is 'n' then space
complexity of recursive algorithm would be O(nm).
45 Performance Analysis
Space Complexity comparison of Sorting Algorithms
46 Performance Analysis
Time Complexity
Why Time Complexity ?
48 Performance Analysis
Why Time Complexity ?
Often more important than space complexity
space available (for computer programs!) tends to be
larger and larger
time is still a problem for all of us
49 Performance Analysis
Time Complexity
50 Performance Analysis
Time Complexity
T(P)=C+tP(I)
Compile time (C) independent of instance characteristics.
Run (execution) time denoted as tP (Instance)
Many of the factors tp depends on are not known at the time a
program is conceived it, is reasonable to attempt only to
estimate tp
tP(n)=caADD(n)+csSUB(n)+clLDA(n)+cstSTA(n) …
Running time of an algorithm as a function of input size n for
large n, where n denotes the instance characteristics.
Expressed using only the highest-order term in the expression
for the exact running time.
51 Performance Analysis
Time Complexity comparison of Sorting Algorithms
52 Performance Analysis
Program Steps
A program step is a syntactically or semantically meaningful program
segment whose execution time is independent of the instance
characteristics.
Example : abc = a + b + b * c + (a + b - c) / (a + b) + 4.0
Regard as the same unit
machine independent
53 Performance Analysis
Methods to compute the step count
1. Introduce variable count into programs
2. Tabular method
Determine the total number of steps contributed by each
statement
step per execution frequency
add up the contribution of all statements
54 Performance Analysis
Method –I: to determine step count
In the first method, we introduce a new variable, count, into the
program.
This is a global variable with initial value count =0.
Statements to increment count by the appropriate amount are
introduced into the program.
This is done so that each time a statement in the original program is
executed, count is incremented by the step count of that statement.
55 Performance Analysis
Algorithm 1.6 with count statement
56 Performance Analysis
Step table for Algorithm 1.6
57 Performance Analysis
Algorithm 1.6 with count statement
58 Performance Analysis
Algorithm 1.7 with count statement
59 Performance Analysis
Step table for Algorithm 1.7
60 Performance Analysis
Matrix addition
61 Performance Analysis
Matrix addition
62 Performance Analysis
Matrix addition
63 Performance Analysis
Method –II: to determine step count
To determine the step count of an algorithm, we first determine
the number of steps per execution (s/e) of each statement and the
total number of times (i.e., frequency) each statement is executed.
The step count method is one of the method to analyze the algorithm.
In this method, we count number of times one instruction is
executing.
The table lists the total number of steps per execution(s/e) of the
statement and the total number of times(i.e., frequency)each
statement is executed. The s/e of a statement is the amount by which
the count changes as a result of the execution of that statement.
By combining these two quantities, the total contribution of each
statement is obtained. By adding the contributions of all statements,
the step count for the entire algorithm is obtained.
64 Performance Analysis
Matrix Addition
65 Performance Analysis
Algorithm nth Fibonacci
67 Performance Analysis
Summary
The time complexity of an algorithm is given by the number of steps taken
by the algorithm to compute the function it was written for.
The number of steps is itself a function of the instance characteristics
Although any specific instance may have several characteristics(e.g., the
number of inputs, the number of outputs, the magnitudes of the inputs and
outputs), the number of steps is computed as a function of some subset of
these.
Our motivation to determine step counts is to be able to compare the time
complexities of two algorithms that compute the same function and also to
predict the growth in run time as the instance characteristics change.
Determining the exact step count(best case, worst case, or average)of an
algorithm can prove to be an exceedingly difficult task.
Expending immense effort to determine the step count exactly is not a very
worth while endeavor, since the notion of a step is itself inexact
68 Performance Analysis
Amortized Analysis
Amortized Analysis
• In computer science, amortized analysis is a method for
analyzing a given algorithm's time complexity, or how much of a
resource, especially time or memory, it takes to execute.
• In an amortized analysis, the time required to perform a
sequence of data-structure operations is averaged over all
the operations performed.
• Amortized analysis can be used to show that the average cost
of an operation is small, if one averages over a sequence of
operations, even though a single operation within the sequence
might be expensive.
• Amortized analysis differs from average-case analysis in that
probability is not involved.
70 Performance Analysis
Amortized Analysis
In an amortized analysis, the time required to perform a sequence
of data-structure operations is averaged over all the operations
performed.
Amortized analysis differs from average-case analysis in that
probability is not involved; an amortized analysis guarantees
the average performance of each operation in the worst case.
71 Performance Analysis
Amortized complexity
Amortized complexity is the total expense per operation,
evaluated over a sequence of operations. The idea is to guarantee the
total expense of the entire sequence, while permitting individual
operations to be much more expensive than the amortized cost.
72 Performance Analysis
Amortized Analysis: Data structures
Data structures typically support several different types of operations, each
with its own cost (e.g., time cost or space cost).
The idea behind amortized analysis is that, even when expensive operations
must be performed, it is often possible to get away with performing them
rarely, so that the average cost per operation is not so high.
It is important to realize that these “average costs” are not expected values;
there needn’t be any random events.
Instead, we are considering the worst-case average cost per operation in a
sequence of many operations.
In other words, we are interested in the asymptotic behavior of the function
C(n) = 1/n . (worst-case total cost of a sequence of n operations)
(possibly with some condition on how many times each type of operation
may occur). “Worst-case” means that no adversary could choose a sequence
of n operations that gives a worse running time.
73 Performance Analysis
Binomial tree
74 Performance Analysis
Binomial Queue
Binomial Trees:
B0 B1 B2
B3
Each tree “doubles”
the previous.
B4
75 Performance Analysis
76 Performance Analysis
Amortized Analysis
Suppose we have an ADT and we want to analyze its operation using
amortized time analysis.
Amorized time analysis is based on the following equation, which
applies to each individual operation of this ADT.
amortized cost = actual cost + accounting cost
The creative part is to design a system of accounting costs for
individual operations that achieves the two goals:
1. In any legal sequence of operations, beginning from the creation of
the ADT object being analyzed, the sum of the accounting cost is
nonnegative.
2. Although the actual cost may fluctuate widely from one individual
operation to the next, it is feasible to analyze the amortized cost of
each operation.
77 Performance Analysis
Amortized Analysis:
• Not just consider one operation, but a sequence of operations on a
given data structure.
• Average cost over a sequence of operations.
Probabilistic analysis:
– Average case running time: average over all possible inputs for one
algorithm (operation).
– If using probability, called expected running time.
Amortized analysis:
– No involvement of probability
– Average performance on a sequence of operations, even some
operation is expensive.
– Guarantee average performance of each operation among the
sequence in worst case.
78 Performance Analysis
Three Methods of Amortized Analysis
• Aggregate analysis:
– Total cost of n operations/n,
• Accounting method:
– Assign each type of operation an (different) amortized cost
– overcharge some operations,
– store the overcharge as credit on specific objects,
– then use the credit for compensation for some later operations.
• Potential method:
– Same as accounting method
– But store the credit as “potential energy” and as a whole.
79 Performance Analysis
Three Methods of Amortized Analysis
1. Aggregate Method: we determine an upper bound T(n) on the
total sequence of n operations. The cost of each will then be T(n)/n.
The other two methods we shall study, the accounting method and
the potential method, may assign different amortized costs to
different types of operations.
80 Performance Analysis
[1] Aggregate Method
In the aggregate method of amortized analysis, we show that for all n, a
sequence of n operations takes worst-case time T(n) in total.
In the worst case, the average cost, or amortized cost, per operation is
therefore T(n) / n.
The amortized cost applies to each operation, even when there are several
types of operations in the sequence.
Example: Stack operations
The two fundamental stack operations, each of which takes O(1) time:
PUSH(S, x) pushes object x onto stack S.
POP(S) pops the top of stack S and returns the popped object.
Since each of these operations runs in O(1) time, let us consider the cost of
each to be 1. The total cost of a sequence of n PUSH and POP operations is
therefore n, and the actual running time for n operations is therefore (n).
81 Performance Analysis
Aggregate Method: Stack operations
Let us add the stack operation MULTIPOP(S, k), which removes
the k top objects of stack S, or pops the entire stack if it contains less
than k objects.
In the following pseudocode, the operation STACK-
EMPTY returns TRUE if there are no objects currently on the stack,
and FALSE otherwise.
83 Performance Analysis
Aggregate Method: The action of MULTIPOP on a stack S
84 Performance Analysis
Aggregate Method: Stack operations
Let us analyze a sequence of n PUSH, POP,
and MULTIPOP operations on an initially empty stack.
The worst-case cost of a MULTIPOP operation in the sequence
is O(n), since the stack size is at most n.
The worst-case time of any stack operation is therefore O(n), and
hence a sequence of n operations costs O(n2), since we may
have O(n) MULTIPOP operations costing O(n) each.
Although this analysis is correct, the O(n2) result, obtained by
considering the worst-case cost of each operation individually, is not
tight.
Using the aggregate method of amortized analysis, we can obtain a
better upper bound that considers the entire sequence
of n operations.
85 Performance Analysis
Aggregate Method: Stack operations
In fact, although a single MULTIPOP operation can be expensive, any
sequence of n PUSH, POP, and MULTIPOP operations on an initially empty
stack can cost at most O(n). Why?
Each object can be popped at most once for each time it is pushed.
Therefore, the number of times that POP can be called on a nonempty
stack, including calls within MULTIPOP, is at most the number
of PUSH operations, which is at most n.
For any value of n, any sequence of n PUSH, POP,
and MULTIPOP operations takes a total of O(n) time. The amortized
cost of an operation is the average: O(n)/n = O(1).
We emphasize again that although we have just shown that the average cost,
and hence running time, of a stack operation is O(1), no probabilistic
reasoning was involved.
We actually showed a worst-case bound of O(n) on a sequence of n operations.
Dividing this total cost by n yielded the average cost per operation, or the
amortized cost.
86 Performance Analysis
[2] Accounting Method
In the accounting method of amortized analysis, we assign differing
charges to different operations, with some operations charged more
or less than they actually cost.
The amount we charge an operation is called its amortized cost.
When an operation's amortized cost exceeds its actual cost, the
difference is assigned to specific objects in the data structure as
credit.
Credit can be used later on to help pay for operations whose
amortized cost is less than their actual cost. Thus, one can view the
amortized cost of an operation as being split between its actual cost
and credit that is either deposited or used up.
This is very different from the aggregate method, in which all
operations have the same amortized cost.
87 Performance Analysis
Accounting Method
If we want analysis with amortized costs to show that in the worst case the
average cost per operation is small, the total amortized cost of a sequence of
operations must be an upper bound on the total actual cost of the sequence.
Moreover, as in the aggregate method, this relationship must hold for all
sequences of operations. Thus, the total credit associated with the data
structure must be nonnegative at all times, since it represents the amount by
which the total amortized costs incurred exceed the total actual costs
incurred.
If the total credit were ever allowed to become negative (the result of
Undercharging early operations with the promise of repaying the account
later on), then the total amortized costs incurred at that time would be
below the total actual costs incurred; for the sequence of operations up to
that time, the total amortized cost would not be an upper bound on the
total actual cost. Thus, we must take care that the total credit in the data
structure never becomes negative.
88 Performance Analysis
Accounting Method: Stack
When we push a plate onto a stack, we use $1 to pay actual cost of the
push and we leave $1 on the plate. At any point, every plate on the
stack has a dollar on top of it.
When we execute a pop operation, we charge it nothing and pay its
cost with the dollar that is on top of it.
89 Performance Analysis
Accounting Method: Stack
Note that the amortized cost of MULTIPOP is a constant (0), whereas
the actual cost is variable. Here, all three amortized costs are O(l),
although in general the amortized costs of the operations under
consideration may differ asymptotically.
Suppose we use a dollar bill to represent each unit of cost. We start
with an empty stack. When we push a plate on the stack, we use 1
dollar to pay the actual cost of the push and are left with a credit of 1
dollar (out of the 2 dollars charged), which we put on top of the
plate. At any point in time, every plate on the stack has a dollar of
credit on it.
The dollar stored on the plate is prepayment for the cost of popping it
from the stack.
90 Performance Analysis
Accounting Method: Stack
When we execute a POP operation, we charge the operation nothing
and pay its actual cost using the credit stored in the stack.
To pop a plate, we take the dollar of credit off the plate and use it to
pay the actual cost of the operation. Thus, by charging the PUSH
operation a little bit more, we needn't charge the POP operation
anything.
Moreover, we needn't charge MULTIPOP operations anything either.
To pop the first plate, we take the dollar of credit off the plate and use
it to pay the actual cost of a POP operation.
To pop a second plate, we again have a dollar of credit on the plate to
pay for the POP operation, and so on. Thus, we have always charged
at least enough up front to pay for MULTIPOP operations.
91 Performance Analysis
Accounting Method: Stack
In other words, since each plate on the stack has 1 dollar of credit on
it, and the stack always has a nonnegative number of plates, we have
ensured that the amount of credit is always nonnegative. Thus, for any
sequence of n PUSH, POP, and MULTIPOP operations, the total
amortized cost is an upper bound on the total actual cost. Since the
total amortized cost is O(n), so is the total actual cost.
92 Performance Analysis
[3] Potential Method
Instead of representing prepaid work as credit stored with specific
objects, the potential method represents the prepaid work as potential
energy than can be released to pay for future operations.
Potential is associated with the data structure as a whole rather than
with specific objects.
Example:
We start with an initial data structure D0 on which n operations are
performed.
Let ci be the cost the ith operations and Di be the data structure that
results after applying the ith operation to data structure Di-1
A potential function Ø maps Di to a real number Ø(Di) which is the
potential associated with Di
93 Performance Analysis
Potential Method
amortized cost of the ith operation with respect to potential function
is defined by
The amortized cost of each operation is therefore its actual cost plus
the increase in potential due to the operation. By the above equation
the total amortized cost of the n operations is
94 Performance Analysis
Potential Method
95 Performance Analysis
Potential Method
The amortized costs defined by the following equations depend on
the choice of the potential function .
96 Performance Analysis
Dynamic Tables: Dynamically expanding and contracting a table
In some applications, we do not know in advance how many objects
will be stored in a table. We might allocate space for a table, only to
find out later that it is not enough.
The table must then be reallocated with a larger size, and all objects
stored in the original table must be copied over into the new, larger
table.
Similarly, if many objects have been deleted from the table, it may be
worthwhile to reallocate the table with a smaller size.
. Using amortized analysis, we shall show that the amortized cost of
insertion and deletion is only O(1), even though the actual cost of an
operation is large when it triggers an expansion or a contraction.
Moreover, we shall see how to guarantee that the unused space in a
dynamic table never exceeds a constant fraction of the total space.
97 Performance Analysis
Dynamic Tables
We assume that the dynamic table supports the operations TABLE-INSERT
and TABLE-DELETE.
TABLE-INSERT inserts into the table an item that occupies a single slot,
that is, a space for one item.
TABLE-DELETE can be thought of as removing an item from the table,
thereby freeing a slot.
The details of the data-structuring method used to organize the table are
unimportant.
We define the load factor (T) of a nonempty table T to be the number of
items stored in the table divided by the size (number of slots) of the table.
We assign an empty table (one with no items) size 0, and we define its load
factor to be 1. If the load factor of a dynamic table is bounded below by a
constant, the unused space in the table is never more than a constant
fraction of the total amount of space.
98 Performance Analysis
Dynamic Tables
Let us assume that storage for a table is allocated as an array of slots. A
table fills up when all slots have been used or, equivalently, when its
load factor is 1.
When the table becomes full, i.e. load factor = 1, then the next
operation triggers an expansion.
We allocate a new area twice the size of the old and copy all items
from the old table to the new table.
If an operation does not trigger an expansion, its cost is 1. If it does
trigger, its cost is num [T] + 1.
99 Performance Analysis
Conclusions
Amortized costs can provide a clean abstraction of data-structure
performance.
Any of the analysis methods can be used when an amortized analysis is
called for, but each method has some situations where it is arguably
the simplest or most precise.
A sequence of n operations is performed on a data structure. The ith
operation costs i if i is an exact power of 2, and 1 otherwise. Use an
aggregate method of analysis to determine the amortized cost per
operation.
Different schemes may work for assigning amortized costs in the
accounting method, or potentials in the potential method, sometimes
yielding radically different bounds.