You are on page 1of 34

Amortized Analysis: Aggregate Method,

Accounting Method, Potential Method (for


Stack data structure)
What is amortized analysis ?
-Amortized analysis is a method used in computer science to analyze the average performance of an
algorithm over multiple operations.
- Amortized analysis is used for algorithms where an occasional operation is very slow, but most of
the other operations are faster. In Amortized Analysis, we analyze a sequence of operations and
guarantee a worst-case average time that is lower than the worst-case time of a particularly expensive
operation
- Amortized analysis is a technique used in computer science to analyze the average-case time
complexity of algorithms that perform a sequence of operations, where some operations may be more
expensive than others.
-The idea is to spread the cost of these expensive operations over multiple operations, so that the
average cost of each operation is constant or less.
- Amortized time complexity in data structures analysis will give the average time taken per
operation, which will be better than worst-case time complexity.
Amortized analysis-
• 1- Aggregate Analysis
• 2- Accounting Method
• 3- Potential Method
In Amortized Analysis, we analyze a sequence of operations and
guarantee a worst-case average time that is lower than the worst-case
time of a particularly expensive operation.

eg- 501 items (explaining Asymptotic and Amortized)

501*500==250000 500*1+500*1==1000
Augmented Stack TC1 Asymptotic TC2 for Amortized

Push O(1) TC1= for n operation TC2= for n operation

Push O(1) =O(n) * n(no.of =O(n) + O(1)+O(1)....


operation)
POP O(1) = O(n) + O(n)
= O(n2)
Multipop O(n) =O(n)

Condition For single Op=

False, if k>n. O(n) =O(1)

True ,Otherwise
Explaining with stack DS
• Push(S, x) pushes an element x onto the stack S
• Pop(S) pops the top element x of S and returns x
• Multipop(S, k) pops the min(k, |S|) top elements of S
• Running time: Push(S,x) is O(1), Pop(S) is O(1),
• Multipop(S,k) is O(min(k,|S|)) when implemented by a linked list.
Example = (on board)
1.Aggregate Method
• Simplest method amongs all
• Suppose that a sequence of n operations takes T(n) time.
• Then the amortized cost per operation is defined to be T(n)/n.
• The amortized cost applies to each operation, even when there are
several different types of operations.
• Amortized analysis is useful for designing efficient algorithms for
data structures such as dynamic arrays, priority queues, and disjoint-
set data structures.
• The total number of pops or multipops in the entire sequence of
operations is <= the total number of pushes.
• Suppose that the maximum number of Push operations in the
sequence is n. So the time for entire sequence is O(n).
• Amortized cost per operation: O(n)/n = O(1).
It provides a guarantee that the average-case time complexity of an
operation is constant, even if some operations may be expensive.
2.Accounting Method

• We assign an amortized cost to each operation, where the actual cost


might be lower or higher than the amortized cost.
• The sum of all the amortized costs in a sequence must be at least the
sum of all the actual costs, since we would like to bound the total cost
of the sequence by the sum of amortized costs.
• How can we ensure this property?
• Assign a random cost (amortize cost CI^) to the 1st operation where 1
unit of amount pays for 1 unit for work
• If the actual cost ie ci of operation is less than the alloted cost (CI^), ie
amortized cost remaining amount is sae and can be used later for
costier operations
• For each operation in the sequence, we have
• if the amortized cost > actual cost then the difference is stored as a
credit with an object in the data structure.
• if the amortized cost < actual cost then use stored credits to make up
for the difference.
• We have to use stored credit when amortized cost is less than actual
cost to make up for the difference, so that sum of costs is always
nonnegative
• Example :
• stack assigning random cost
• push- 2 actual cost o(1)
• pop- 0 O(1)
• multipop- 0 O(1)
• Let us use the following
• amortized costs:
• Push: 2
• Pop: 0
• Multipop: 0
• The Push operation has cost 1; the additional 1 is stored as credit with
the pushed element.
• There is always enough credit to pay for each operation. Each
amortized cost is O(1)
• Thus, the cost of the entire sequence of n operations is O(n)
3.Potential Method

• A method implemented to analyze the amortized time and space


complexity of a data structure, a measure of its performance over
sequences of operations that eliminates the cost of infrequent but
expensive operations.
• Same as accounting method: something prepared is used later
• Difference – the prepared work not as credit, but as ‘potential energy’
or ‘potential’
• The term potential is associated with the data structure as a whole
rather than with specified objects within the data structure
• The amortize cost of each operation is therefore its actual cost plus
increase in potential due to the operation
• Find the worst case time t(n), for n operation
• Check for each push , pop and multipop operation
• Example
• 1- push
• 2- pop

3- multipop
• So amortize cost for each operation is O(1), and total amortized cost
of n operation is O(n)
• Since total amortize cost is an upper bound of actual cost , the worst
case cost on n operations is O(n)
• T(n)=n
Probabilistic and Randomized Algorithm
• Probabilistic approach to algorithm and Randomized Analysis
• Indicator Random Variable (IRV)
• Analysis of Hiring Problem
The Hiring Problem

• Suppose that you need to hire a new office assistant. Your previous
attempts at hiring have been unsuccessful, and now you have decided
to use an hiring agency.
• The employment agency will send you one candidate each day
• Your job will be to interview that person and then decide whether to
either hire that person or not. You must pay the agency a small fee to
interview an applicant.
• To actually hire an applicant is more costly, however since you must
fire your current office assistant and pay a large hiring fee to the
agency
• You decide that after interviewing each applicant , if that applicant is
better qualified than the current assistant, you will free the current
assistant and hire the new applicant.
HIRIRNG PROBLEM
• Here we are not concerned with the running time of of HIRE-ASSISTANT, but
instead with the cost incurred by interviewing and hiring.
• The procedure assumes that you are able to, after interviewing candidate i,
determine if candidate i is the best candidate you have seen so far. To initialize, the
procedure creates a dummy candidate numbered 0, who is less qualified than each
of the other candidates.
In either case, we are counting the number of times certain basic
operations are executed.
Interviewing has a low cost, say ci, whereas hiring is expensive,
costing ch.
Let m be the number of people hired. Then the total cost associated
with this algorithm is O(nci + mch).
No matter how many people we hire, we always interview n
candidates and thus always incur the cost nci associated with
interviewing.
We, therefore, concentrate on analyzing mch, the hiring cost. This
quantity varies with each run of the algorithm.
The hiring problem models how often we update our notion of which
element is currently winning.
Probabilistic analysis:
In order to perform a probabilistic analysis, we must use knowledge of, or
make assumptions about, the distribution of the inputs.
Then we analyze our algorithm, computing an expected running time. The
expectation is taken over the distribution of the possible inputs. Thus we
are, in effect, averaging the running time over all possible inputs.
We must be very careful in deciding on the distribution of inputs. For some
problems, it is reasonable to assume something about the set of all possible
inputs, and we can use probabilistic analysis as a technique for designing an
efficient algorithm and as a means for gaining insight into a problem. For
other problems, we cannot describe a reasonable input distribution, and in
these cases we cannot use probabilistic analysis.
For the hiring problem, we can assume that the applicants come in a random
order.
Randomized analysis:
• We can therefore rank each candidate with a unique number from 1 through n,
using rank(i) to denote the rank of applicant i, and adopt the convention that a
higher rank corresponds to a better qualified applicant.
• The ordered list <rank(1), rank(2), ..., rank(n)> is a permutation of the list <1, 2,
..., n> saying that the applicants come in a random order is equivalent to saying
that this list of ranks is equally likely to be any one of the n! permutations of the
numbers 1 through n.
• Alternatively, we say that the ranks form a uniform random permutation; that is,
each of the possible n! permutations appear with equal probability.
Randomized algorithm
• Yet we often can use probability and randomness as a tool for algorithm design
and analysis, by making the behavior of part of the algorithm random.
• In order to develop a randomized algorithm for the hiring problem, we must have
greater control over the order in which we interview the candidates.
• We will, therefore, change the model slightly. We will say that the employment
agency has n candidates, and they send us a list of the candidates in advance. On
each day, we choose, randomly, which candidate to interview.
• Although we know nothing about the candidates (besides their names), we have
made a significant change.
• Instead of relying on a guess that the candidates will come to us in a random
order, we have instead gained control of the process and enforced a random
order.
Indicator random variable
In order to analyze many algorithms, including the hiring problem, we will use
indicator random variables.
Indicator random variables provide a convenient method for converting between
probabilities and expectations.
Suppose we are given a sample space S and an event A. Then the indicator random
variable I {A} associated with event A is defined as
• As a simple example, let us determine the expected number of heads that we
obtain when flipping a fair coin. Our sample space is S = {H, T}, and we define a
random variable Y which takes on the values H and T, each with probability 1/2.
• We can then define an indicator random variable XH, associated with the coin
coming up heads, which we can express as the event Y = H.
• This variable counts the number of heads obtained in this flip, and it is 1 if the
coin comes up heads and 0 otherwise.
Indicator random variable
• Let us compute the number of heads in n coin flips. let Xi be the indicator random
variable associated with the event in which the ith flip comes up heads. Let Yi be the
random variable denoting the outcome of the ith flip,
Xi = I{Yi = H}.
• Let X be the random variable denoting the total number of heads in the n coin flips, so
that

• We wish to compute the expected number of heads, so we take the


expectation of both sides of the above equation to obtain
• this equation sums ups to 1/i ie O(logn) -as per ln n + O(1)


Analysis of the hiring problem using indicator random
variables
• Returning to the hiring problem, we now wish to compute the
expected number of times that we hire a new office assistant.
• Let X be the random variable whose value equals the number of times
we hire a new office assistant. We could then apply the definition of
expected value to obtain.
• Let Xi be the indicator random variable associated with the event in
which the ith candidate is hired

You might also like