Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction to Amortized Analysis
2.
Aggregate Method
3.
Example of Amortized Analysis on Dynamic Array
4.
Key takeaways
Last Updated: Mar 27, 2024

Amortized Time Complexity in Data Structures

Author Riya
1 upvote

Introduction to Amortized Analysis

 Amortized analysis is a method for analyzing a sequence of operations in an algorithm and gives the average time or space taken by the algorithm over the complete sequence. It is useful when a few operations in the sequence are very slow, and others are faster to execute. Amortized time complexity in data structures analysis will give the average time taken per operation, which will be better than worst-case time complexity.

The examples of Data Structures for which we need to study ‘amortized time complexity in data structures’ include Dynamic array, Disjoint sets, Hash Tables, etc.

Recommended topic, kth largest element in an array , hash function in data structure

Aggregate Method

This section will discuss the method used for calculating amortized time complexity in data structures. The aggregate method calculates the average time complexity of operations in the amortized analysis by using the following mathematical formula:

Example of Amortized Analysis on Dynamic Array

This section will discuss the example of amortized time complexity in Data structure.

Suppose we have to store elements and we don’t know the number of elements in advance. So, we can use a dynamic array to store the elements. If we declare a large size of the array initially, then we have to compromise with the space complexity. So, we start with a zero-size dynamic array and start inserting elements. Whenever a new element comes, and we have a vacant space in the array, it takes O(1) time to insert a new element into the array. But whenever there is not a vacant space in the array, the size of the array is doubled. So, if there is no vacant space in the array at the time of nth element insertion, it takes O(n) time for inserting that element into the dynamic array.

This kind of dynamic array is implemented using ArrayList in Java and vectors in C++.

See the figure below for a better understanding of amortized time complexity in data structures:

Here, we can observe that when (n-1) is a power of 2, we come to a case when we don’t have a vacant space in the dynamic array and we need to double its size to accommodate the new element. In this case, the time complexity is O(n), else the time complexity to insert the new element is O(1).

The amortized cost of insertion:

Read More - Time Complexity of Sorting Algorithms and  Euclid GCD Algorithm

Key takeaways

This article discussed the concept of “Amortized time complexity in Data Structures.”

Also read - Data Structure MCQ

If you want to solve problems on data structures and algorithms for practice, you can visit Coding Ninjas Studio.

You can also consider our Online Coding Courses such as the DSA in PythonC++ DSA CourseDSA in Java Course to give your career an edge over others.

Live masterclass