Why Data Structures and Algorithms are important

geekdroid07

GeekDroiD

Posted on May 18, 2022

Why Data Structures and Algorithms are important

For years I have been wordering how a one-dimensional array actually works in memory (one of the most basic data structures in computer science) and when I started to read books about Data structures and Algorithms I found that a computer's memory can be viewed as a giant collection of cells and when a program declares an array, it allocates a contiguous set of empty cells for use in the program. So, if you were creating an array meant to hold five elements, your computer would find a group of five empty cells in a row and designate it to serve as your array:

array cells

That inspired me to learn more Data Structures because I can know how works what I'm using everyday to build better products.

Even a process as simple as preparing a bowl of cereal is technically an algorithm, as it involves following a defined set of steps to achieve the task at hand. The cereal-preparation algorithm follows these four steps (for me, at least):

  1. Grab a bowl.
  2. Pour cereal into the bowl.
  3. Pour milk into the bowl.
  4. Dip a spoon into the bowl.
  5. Enjoy šŸ˜‹

There are Data structures such as LinkedList, Stack, Queues, HashTable, Tree, Graph that help us to build better products for millions of users.

Example of the importance of each data structure:

HASH TABLE

Imagine youā€™re writing a program that allows customers to order fast food from a restaurant, and youā€™re implementing a menu of foods with their respective prices. You could, technically, use an array:

menu = [ ["french fries", 0.75], ["hamburger", 2.5],
["hot dog", 1.5], ["soda", 0.6] ]

This array contains several subarrays, and each subarray contains two elements. The first element is a string representing the food on the menu, and the second element represents the price of that food.

if this array were unordered, searching for the price of a given food would take O(N) steps since the computer would have to perform a linear search. If itā€™s an ordered array, the computer could do a binary search, which would take O(log N).

while O(log N) isnā€™t bad, we can do better. In fact, we can do much better. the data structure called hash table can be used to look up data in just O(1) time. By knowing how hash tables work under the hood and the right places to use them, you can leverage their tremendous lookup speeds in many situations.

LINKEDLIST

The data from linked lists can be scattered across different cells throughout the computerā€™s memory.

Connected data that is dispersed throughout memory are known as nodes. In a linked list, each node represents one item in the list. The big question, then, is: if the nodes are not next to each other in memory, how does the computer know which nodes are part of the same linked list?

This is the key to the linked list: each node also comes with a little extra information, namely, the memory address of the next node in the list. This extra piece of dataā€”this pointer to the next nodeā€™s memory addressā€”is known as a link. Here is a visual depiction of a linked list:

linkedlist

In this example, we have a linked list that contains four pieces of data: "a", "b", "c", and "d". However, it uses eight cells of memory to store this data, because each node consists of two memory cells. The first cell holds the actual data, while the second cell serves as a link that indicates where in memory the next node begins. The final nodeā€™s link contains null since the
linked list ends there.

(A linked listā€™s first node can also be referred to as its head, and its final node as its tail.)

The fact that a linked listā€™s data can be spread throughout the computerā€™s memory is a potential advantage it has over the array. An array, by contrast, needs to find an entire block of contiguous cells to store its data, which can get increasingly difficult as the array size grows.

STACK

A stack stores data in the same way arrays doā€”itā€™s simply a list of elements.

The one catch is that stacks have the following three constraints:
ā€¢ Data can be inserted only at the end of a stack.
ā€¢ Data can be deleted only from the end of a stack.
ā€¢ Only the last element of a stack can be read.

You can think of a stack as an actual stack of dishes; you canā€™t look at the face of any dish other than the one at the top. Similarly, you canā€™t add any dish except to the top of the stack, nor can you remove any dish besides the one at the top. (At least, you shouldnā€™t.) In fact, most computer science literature refers to the end of the stack as its top, and the beginning of the stack
as its bottom.

this diagrams will reflect this terminology by viewing stacks as vertical arrays,

STACK

Stacks are useful for undo\redo operation in word processors, Expression evaluation and syntax parsing, many virtual machines like JVM are stack oriented.

QUEUE

You can think of a queue as a line of people at the movie theater. The first one in the line is the first one to leave the line and enter the theater. With queues, the first item added to the queue is the first item to be removed. Thatā€™s why computer scientists apply the acronym ā€œFIFOā€ to queues: First In, First Out.

As with a line of people, a queue is usually depicted horizontally. Itā€™s also common to refer to the beginning of the queue as its front, and the end of the queue as its back.

Queues are arrays with three restrictions:
ā€¢ Data can be inserted only at the end of a queue.
ā€¢ Data can be deleted only from the front of a queue.
ā€¢ Only the element at the front of a queue can be read.

Queues are useful for transport and operations research where various entities are stored and held to be processed later in the queue performs the function of a buffer.

also exists priority queues where is helpful is in an application that manages the triage system for a hospital emergency room. In the Emergency, we donā€™t treat people strictly in the order in which they arrived. Instead, we treat people in the order of the severity of their symptoms. If someone suddenly arrives with a life-threatening injury, that patient will be placed at the front of the queue, even if the person with the flu had arrived hours earlier.

Letā€™s say our triage system ranked the severity of a patientā€™s condition on a scale of 1 to 10, with 10 being the most critical. Our priority queue may look like this:

QUEUE

In determining the next patient to treat, we will always select the patient at the front of the priority queue, since that personā€™s need is the most urgent. In this case, the next patient weā€™d treat is Patient C.

If a new patient now arrives with a condition severity of 3, weā€™ll initially place this patient at the appropriate spot within the priority queue. Weā€™ll call this person Patient E:

this is a description

TRIE

Unlike Array and Linked List, which are linear data structures, tree is hierarchical (or non-linear) data structure.

Like most other trees, the trie is a collection of nodes that point to other nodes. However, the trie is not a binary tree. Whereas a binary tree doesnā€™t allow any node to have more than two child nodes, a trie node can have any number of child nodes

There are many types of trees and maybe you have used it the DOMAIN OBJECT MODEL(DOM) tree, File System Tree in your computer and even OBJECT ORIENTED PROGRAMMING(OOP) inheritance is a tree, these are unordered trees and Fibonacci Tree, Binomial tree, binary tree are examples of ordered trees

Exists many applications for trees:

  • Heap is a tree data structure which is implemented using arrays and used to implement priority queues.
  • B-Tree and B+ Tree: They are used to implement indexing in databases.
  • Syntax Tree: Used in Compilers.
  • K-D Tree: A space partitioning tree used to organize points in K dimensional space.
  • Trie: Used to implement dictionaries with prefix lookup.
  • Suffix Tree: For quick pattern searching in a fixed text.
  • Manipulate hierarchical data.
  • Make information easy to search (tree traversal).
  • Manipulate sorted lists of data.
  • As a workflow for compositing digital images for visual effects.
  • Router algorithms.

trees everywhere

GRAPH

A data is a non-linear and abstract data structure it is defined by this behavior and not by the underlying mathematical model, it consists of a set of nodes also known as vertices, these nodes are connected by edges, a graph can be direct and indirect.

A graph is a data structure that specializes in relationships, as it easily conveys how data is connected.

Here is a visualization of our social network, displayed as a graph:

GRAPH

Each person is represented by a node, and each line indicates a friendship with another person. If you look at Alice, for example, you can see that she is friends with Bob, Diana, and Fred, since her node has lines that connect to their nodes.

Graphs are helpful for connections/relations in social networking sites, Routing, networks of communication, data organization, etc.

In computer science graph theory is used for the study of algorithms like:

  • Dijkstra's Algorithm
  • Prims's Algorithm
  • Kruskal's Algorithm
  • Graphs are used to define the flow of computation.
  • Graphs are used to represent networks of communication.
  • Graphs are used to represent data organization.
  • Graph transformation systems work on rule-based in-memory manipulation of graphs. Graph databases ensure transaction-safe, persistent storing and querying of graph structured data.
  • Graph theory is used to find shortest path in road or a network.
  • In Google Maps, various locations are represented as vertices or nodes and the roads are represented as edges and graph theory is used to find the shortest path between two nodes.

At the end each Data Structures has a reason why they exists.

Suppose you are working in a Facebook company. You come up with an optimal solution of a problem (like sorting a list of users from India) with time complexity of O(n Log n) instead of O(n^2) and assume that n for the problem here for the company in real life scenario is 100 million (very fair assumption considering the number of users registered on Facebook exceeds 1 billion). n Log n would be 800 million, while n^2 would be 10^7 billion. In cost terms, you can see that the efficiency has been improved more than 10^7 times, which could be a huge saving in terms of server cost and time.

Now you might have got that companies want to hire a smart developer who can make the right decision and save company resources, time, and money. So before you give the solution to use a Hash table instead of List to solve a specific problem think about the big scale and all the case scenarios carefully. It can generate revenue for the company or the company can lose a huge amount of money.

I hope you guys found this resume helpful šŸ¤“šŸ§

šŸ’– šŸ’Ŗ šŸ™… šŸš©
geekdroid07
GeekDroiD

Posted on May 18, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related