Data-structures and Algorithms using Python: Programming Series 101–2

Image for post
Image for post

Hope you guys are having fun learning python and getting acquainted with data-structures using python. Soon we will start designing algorithms using python but let’s build our fundamentals first.

In the last session I discussed why it is important to understand data structures and algorithms, what are the basic data-structures and how can we use those basic data-structures to build complex ones(eg. stack). We also discussed the time and space complexity. If you haven’t checked that post and are new to data-structures, feel free to check that post here.

Today we will develop data-structures that you already have implemented in real-life scenarios. We will also discuss their functions and readymade python packages for them as well as their time and space complexity.

Queue:

This is a non-primitive data-structure where data is stored in sequential order on a first come first serve basis. As the data storage follows sequential order in FIFO(first in first out order), it finds application in algorithms where data is accessed in this fashion.

This data-structure has the following functions:

  1. enqueue(): add elements to the queue.
  2. dequeue(): remove the elements from the queue.
  3. size(): get the number of elements in the queue.
  4. is_empty(): check if the queue is empty or not.
  5. front(): peek through the first element of the queue.
  6. back():peek through the last element of the queue.

Below are the time and space complexity for queue data-structure.

Time and Space Complexity:

Insertion: O(1)

Deletion/ Retrieval: O(1)

Space Complexity: O(n)

Pros and Cons:

The FIFO pattern of storage of elements is useful in certain applications — Eg. designing customer care support application, input-output based interrupts(typing from the keyboard) that needs to be registered by computer, task schedulers, etc.

Cons is that the data storage and access cannot be random, hence not suitable for the applications where memory management cannot be continuous.

You can follow me for tutorials on AI/machine learning, programming, data analytics, and BI. For any queries or doubts, I am available on LinkedIn.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store