Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Laboratory Manual

Advanced Algorithms Laboratory (PCCO6020L)


TY B. Tech. Computer Engineering Semester-VI

Experiment No.: 1

Aim: Experiment on Amortized Analysis.

Software Requirement: - VS Code or GDB Compiler.


Theory:

Concept: - Amortized analysis is applied on data structures that support many operations. The
sequence of operations and the multiplicity of each operation is application specific or the
associated algorithm specific. Classical asymptotic analysis gives worst case analysis of each
operation without taking the effect of one operation on the other, whereas amortized analysis
focuses on a sequence of operations, an interplay between operations, and thus yielding an analysis
which is precise and depicts a micro-level analysis.

Definition: - Time required to perform a sequence of data structure operations is averaged overall
operations performed. If we want to analyze the complexity of a sequence like this, one simple
approach might be to determine an upper bound on run-time for each individual operation in that
sequence. Summing these values together, we can achieve an upper bound on the run-time of our
entire sequence.
Amortization analysis attempts to solve this problem by averaging out the cost of more
“expensive” operations across an entire sequence. More formally, amortized analysis finds the
average cost of each operation in a sequence, when the sequence experiences it’s worst-case. This
way, we can estimate a more precise bound for the worst-case cost of our sequence.

Dynamic Array

Consider an array A that starts off empty, with length N = 1 and number of elements n = 0. We can
add an element to the end of the array, but whenever the array becomes full we need to
automatically assign some more space.

So, after a couple of insertions, our dynamic array might look like this:

The Aggregate Method will always treat every operation the same way, and cannot deduce
different amortized costs for multiple types of operations in a sequence.

Accounting Method can be easily applied to multiple operation types to deduce the amortized cost
of each.
Following are steps to creating a dynamic table using amortized analysis:

1. Define the problem: We want to create a data structure that allows for efficient insertion
and deletion of elements from a table of variable size.
2. Choose a data structure: We will use an array to represent the table, and we will
dynamically resize the array as needed to accommodate more or fewer elements.
3. Initialize the data structure: We will start with an empty array of a fixed size, and we will
keep track of the number of elements in the array.
4. Define the cost of each operation: We will assign a cost of 1 to each insertion or deletion
operation, and we will assign a cost of n to each resize operation, where n is the current
size of the array.
5. Analyze the worst-case cost of each operation:

Insertion: When the array is not full, the cost of an insertion is 1, as it simply involves
adding the new element to the end of the array. When the array is full, a resize operation is
needed to double the size of the array, resulting in a cost of n. However, this resize
operation only needs to be performed once for every n insertions, so the amortized cost of
each insertion is (1 + n/n) = 2.

Deletion: The cost of a deletion is 1, as it simply involves removing the element from the
array. There is no need for a resize operation when an element is deleted.

Resize: When a resize operation is performed, the cost is n, as it involves copying all the
elements from the old array to the new array. However, as mentioned earlier, a resize
operation only needs to be performed once for every n insertions, so the amortized cost of
each resize operation is (n/n) = 1.

6. Implement the operations:

Insertion: When an element is inserted into the array, if the array is not full, the element
is added to the end of the array and the number of elements is incremented. If the array is
full, a new array is created with twice the size of the old array, all the elements are copied
to the new array, the old array is deleted, and the new element is added to the end of the
new array. The number of elements is incremented.

Deletion: When an element is deleted from the array, the element is removed from its
position in the array, all the elements to the right of the deleted element are shifted one
position to the left, and the number of elements is decremented.

Resize: When a resize operation is performed, a new array is created with twice the size of
the old array, all the elements are copied to the new array, the old array is deleted, and the
number of elements is not changed.

7. Test the implementation: Test the implementation by performing a variety of insertion and
deletion operations, and verifying that the array is resized correctly and that all elements
are present in the correct positions.

And that's it! By analyzing the worst-case cost of each operation using amortized analysis, we can
ensure that our dynamic table is efficient for a wide range of input sizes.

The above Amortized Analysis was done for Dynamic Array example is called Aggregate Method.
There are two more powerful ways to do Amortized analysis called Accounting
Method and Potential Method.

You might also like