You are on page 1of 34

What is DSA?

DSA is defined as a combination of two separate but interrelated topics – Data Structure and Algorithms.

What is Data Structure?

A data structure is defined as a particular way of storing and organizing data in our devices to use the data
efficiently and effectively. It is also used for processing, retrieving, and storing data. The main idea behind
using data structures is to minimize the time and space complexities. An efficient data structure takes
minimum memory space and requires minimum time to execute the data.

What is Algorithm?

Algorithm is defined as a process or set of well-defined instructions that are typically used to solve a particular
group of problems or perform a specific type of calculation. To explain it in simpler terms, it is a set of
operations performed in a step-by-step manner to execute a task.

How to start learning DSA?

The first and foremost thing is dividing the total procedure into little pieces which need to be done
sequentially.

The complete process to learn DSA from scratch can be broken into 4 parts:

1. Learn about Time and Space complexities


2. Learn the basics of individual Data Structures
3. Learn the basics of Algorithms
4. Practice Problems on DSA

Data structures can be classified into two broad categories:

 Linear Data Structure: A data structure in which data elements are arranged sequentially or linearly,
where each element is attached to its previous and next adjacent elements, is called a linear data
structure. Examples are array, stack, queue, etc.
 Non-linear Data Structure: Data structures where data elements are not placed sequentially or linearly
are called non-linear data structures. Examples are trees and graphs.
Data structures are used in a wide range of computer programs and applications, including:

 Databases: Data structures are used to organize and store data in a database, allowing for efficient
retrieval and manipulation.
 Operating systems: Data structures are used in the design and implementation of operating systems to
manage system resources, such as memory and files.
 Computer graphics: Data structures are used to represent geometric shapes and other graphical elements
in computer graphics applications.
 Artificial intelligence: Data structures are used to represent knowledge and information in artificial
intelligence systems.

Advantages of Data Structures:

The use of data structures provides several advantages, including:

 Efficiency: Data structures allow for efficient storage and retrieval of data, which is important in
applications where performance is critical.
 Flexibility: Data structures provide a flexible way to organize and store data, allowing for easy modification
and manipulation.
 Reusability: Data structures can be used in multiple programs and applications, reducing the need for
redundant code.
 Maintainability: Well-designed data structures can make programs easier to understand, modify, and
maintain over time.

Data Type Data Structure


The data type is the form of a variable to which a Data structure is a collection of different kinds of data.
value can be assigned. It defines that the particular That entire data can be represented using an object
variable will assign the values of the given data and can be used throughout the program.
type only.
It can hold value but not data. Therefore, it is data It can hold multiple types of data within a single
less. object.
The implementation of a data type is known as Data structure implementation is known as concrete
abstract implementation. implementation.
There is no time complexity in the case of data In data structure objects, time complexity plays an
types. important role.
In the case of data types, the value of data is not While in the case of data structures, the data and its
stored because it only represents the type of data value acquire the space in the computer’s main
that can be stored. memory. Also, a data structure can hold different
kinds and types of data within one single object.
Data type examples are int, float, double, etc. Data structure examples are stack, queue, tree, etc.

Need of Data structure:

1. Data structure modification is easy.


2. It requires less time.
3. Save storage memory space.
4. Data representation is easy.
5. Easy access to the large database.
ARRAY
Array is a linear data structure that is a collection of similar data types. Arrays are stored in contiguous
memory locations. It is a static data structure with a fixed size. It combines data of similar types.

An array is a collection of items of same data type stored at contiguous memory locations.

This makes it easier to calculate the position of


each element by simply adding an offset to a base
value, i.e., the memory location of the first element
of the array (generally denoted by the name of the
array). The base value is index 0 and the difference
between the two indexes is the offset.

Remember: “Location of next index depends on the data type we use”.

Is the array always of a fixed size?

In C language, the array has a fixed size meaning once the size is given to it,
it cannot be changed i.e. you can’t shrink it nor can you expand it. The
reason was that for expanding if we change the size we can’t be sure (it’s
not possible every time) that we get the next memory location to us for free.
The shrinking will not work because the array, when declared, gets memory
statically allocated, and thus compiler is the only one that can destroy it.

SEARCH OPERATION:

In an unsorted array, the search operation can be performed by linear traversal from the first element to the
last element.

// C program to implement linear


// search in unsorted array
#include <stdio.h>

// Function to implement search operation


int findElement(int arr[], int n, int key)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == key)
return i;

// If the key is not found


return -1;
}
// Driver's Code
int main()
{
int arr[] = { 12, 34, 10, 6, 40 };
int n = sizeof(arr) / sizeof(arr[0]);

// Using a last element as search element


int key = 40;

// Function call
int position = findElement(arr, n, key);

if (position == -1)
printf("Element not found");
else
printf("Element Found at Position: %d",
position + 1);

return 0;
}

Output
Element Found at Position: 5

Time Complexity: O(N)


Auxiliary Space: O(1)

INSERT OPERATION:

1. Insert at the end:

In an unsorted array, the insert operation is faster as compared to a sorted array because we don’t have to
care about the position at which the element is to be placed.

#include <iostream>
using namespace std;

// Inserts a key in arr[] of given capacity.


// n is the current size of arr[]. This
// function returns n + 1 if insertion
// is successful, else n.
int insertSorted(int arr[], int n, int key, int capacity)
{
// Cannot insert more elements if n is
// already more than or equal to capacity
if (n >= capacity)
return n;

arr[n] = key;
return (n + 1);
}

int main()
{
int arr[20] = { 12, 16, 20, 40, 50, 70 };
int capacity = sizeof(arr) / sizeof(arr[0]);
int n = 6;
int i, key = 26;

cout << "Before Insertion: ";


for (i = 0; i < n; i++)
cout << arr[i] << " ";

// Inserting key
n = insertSorted(arr, n, key, capacity);

cout << "\nAfter Insertion: ";


for (i = 0; i < n; i++)
cout << arr[i] << " ";

return 0;
}
Output
Before Insertion: 12 16 20 40 50 70
After Insertion: 12 16 20 40 50 70 26

Time Complexity: O(1)


Auxiliary Space: O(1)

2. Insert at any position

Insert operation in an array at any position can be performed by shifting elements to the right, which are on
the right side of the required position

// C++ Program to Insert an element


// at a specific position in an Array

#include <bits/stdc++.h>
using namespace std;

// Function to insert element


// at a specific position
void insertElement(int arr[], int n, int
x, int pos)
{
// shift elements to the right
// which are on the right side of pos
for (int i = n - 1; i >= pos; i--)
arr[i + 1] = arr[i];

arr[pos] = x;
}
// Driver's code
int main()
{
int arr[15] = { 2, 4, 1, 8, 5 };
int n = 5;

cout<<"Before insertion : ";


for (int i = 0; i < n; i++)
cout<<arr[i]<<" ";

cout<<endl;

int x = 10, pos = 2;

// Function call
insertElement(arr, n, x, pos);
n++;

cout<<"After insertion : ";


for (int i = 0; i < n; i++)
cout<<arr[i]<<" ";

return 0;
}

Output
Before insertion : 2 4 1 8 5
After insertion : 2 4 10 1 8 5

Time complexity: O(N)


Auxiliary Space: O(1)

DELETE OPERATION:

In the delete operation, the element to be deleted is searched using the linear search, and then the delete
operation is performed followed by shifting the elements.

// C++ program to implement delete operation


in a
// unsorted array
#include <iostream>
using namespace std;
// To search a key to be deleted
int findElement(int arr[], int n, int key);

// Function to delete an element


int deleteElement(int arr[], int n, int key)
{
// Find position of element to be deleted
int pos = findElement(arr, n, key);

if (pos == -1) {
cout << "Element not found";
return n;
}

// Deleting element
int i;
for (i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];

return n - 1;
}

// Function to implement search operation


int findElement(int arr[], int n, int key)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == key)
return i;

return -1;
}

// Driver's code
int main()
{
int i;
int arr[] = { 10, 50, 30, 40, 20 };

int n = sizeof(arr) / sizeof(arr[0]);


int key = 30;

cout << "Array before deletion\n";


for (i = 0; i < n; i++)
cout << arr[i] << " ";

// Function call
n = deleteElement(arr, n, key);
cout << "\n\nArray after deletion\n";
for (i = 0; i < n; i++)
cout << arr[i] << " ";

return 0;
}

Output
Array before deletion
10 50 30 40 20

Array after deletion


10 50 40 20

Time Complexity: O(N)


Auxiliary Space: O(1)

Applications, Advantages and Disadvantages of Array

Below are some applications of arrays.

 Storing and accessing data: Arrays are used to store and retrieve data in a specific order. For example, an
array can be used to store the scores of a group of students, or the temperatures recorded by a weather
station.
 Sorting: Arrays can be used to sort data in ascending or descending order. Sorting algorithms such as
bubble sort, merge sort, and quicksort rely heavily on arrays.
 Searching: Arrays can be searched for specific elements using algorithms such as linear search and binary
search.
 Matrices: Arrays are used to represent matrices in mathematical computations such as matrix
multiplication, linear algebra, and image processing.
 Stacks and queues: Arrays are used as the underlying data structure for implementing stacks and queues,
which are commonly used in algorithms and data structures.
 Graphs: Arrays can be used to represent graphs in computer science. Each element in the array represents
a node in the graph, and the relationships between the nodes are represented by the values stored in the
array.
 Dynamic programming: Dynamic programming algorithms often use arrays to store intermediate results of
sub problems in order to solve a larger problem.

Advantages of array data structure:

 Efficient access to elements: Arrays provide direct and efficient access to any element in the collection.
Accessing an element in an array is an O(1) operation, meaning that the time required to access an
element is constant and does not depend on the size of the array.
 Fast data retrieval: Arrays allow for fast data retrieval because the data is stored in contiguous memory
locations. This means that the data can be accessed quickly and efficiently without the need for complex
data structures or algorithms.
 Memory efficiency: Arrays are a memory-efficient way of storing data. Because the elements of an array
are stored in contiguous memory locations, the size of the array is known at compile time. This means that
memory can be allocated for the entire array in one block, reducing memory fragmentation.
 Versatility: Arrays can be used to store a wide range of data types, including integers, floating-point
numbers, characters, and even complex data structures such as objects and pointers.
 Easy to implement: Arrays are easy to implement and understand, making them an ideal choice for
beginners learning computer programming.
 Compatibility with hardware: The array data structure is compatible with most hardware architectures,
making it a versatile tool for programming in a wide range of environments.

Disadvantages of array data structure:

 Fixed size: Arrays have a fixed size that is determined at the time of creation. This means that if the size of
the array needs to be increased, a new array must be created and the data must be copied from the old
array to the new array, which can be time-consuming and memory-intensive.
 Memory allocation issues: Allocating a large array can be problematic, particularly in systems with limited
memory. If the size of the array is too large, the system may run out of memory, which can cause the
program to crash.
 Insertion and deletion issues: Inserting or deleting an element from an array can be inefficient and time-
consuming because all the elements after the insertion or deletion point must be shifted to accommodate
the change.
 Wasted space: If an array is not fully populated, there can be wasted space in the memory allocated for
the array. This can be a concern if memory is limited.
 Limited data type support: Arrays have limited support for complex data types such as objects and
structures, as the elements of an array must all be of the same data type.
 Lack of flexibility: The fixed size and limited support for complex data types can make arrays inflexible
compared to other data structures such as linked lists and trees.

Advantages of Structure over Array:

 The structure can store different types of data whereas an array can only store similar data types.
 Structure does not have limited size like an array.
 Structure elements may or may not be stored in contiguous locations but array elements are stored in
contiguous locations.
 In structures, object instantiation is possible whereas in arrays objects are not possible.

Linked list:

A linked list is a linear data structure in which elements are not stored at contiguous memory locations. The
elements in a linked list are linked using pointers as shown in the below image:
Types of linked lists:

 Singly-linked list

 Doubly linked list

 Circular linked list

 Doubly circular linked list

Linked List

Characteristics of a Linked list:

A linked list has various characteristics which are as follows:

 A linked list uses extra memory to store links.

 During the initialization of the linked list, there is no need to know the size of the elements.

 Linked lists are used to implement stacks, queues, graphs, etc.

 The first node of the linked list is called the Head.

 The next pointer of the last node always points to NULL.

 In a linked list, insertion and deletion are possible easily.

 Each node of the linked list consists of a pointer/link which is the address of the next node.

 Linked lists can shrink or grow at any point in time easily.

Operations performed on Linked list:

A linked list is a linear data structure where each node contains a value and a reference to the next node. Here
are some common operations performed on linked lists:

 Initialization: A linked list can be initialized by creating a head node with a reference to the first node.
Each subsequent node contains a value and a reference to the next node.
 Inserting elements: Elements can be inserted at the head, tail, or at a specific position in the linked list.

 Deleting elements: Elements can be deleted from the linked list by updating the reference of the
previous node to point to the next node, effectively removing the current node from the list.

 Searching for elements: Linked lists can be searched for a specific element by starting from the head
node and following the references to the next nodes until the desired element is found.

 Updating elements: Elements in a linked list can be updated by modifying the value of a specific node.

 Traversing elements: The elements in a linked list can be traversed by starting from the head node and
following the references to the next nodes until the end of the list is reached.

 Reversing a linked list: The linked list can be reversed by updating the references of each node so that
they point to the previous node instead of the next node.

These are some of the most common operations performed on linked lists. The specific operations and
algorithms used may vary based on the requirements of the problem and the programming language used.

Applications of the Linked list:

Different applications of linked lists are as follows:

 Linked lists are used to implement stacks, queues, graphs, etc.

 Linked lists are used to perform arithmetic operations on long integers.

 It is used for the representation of sparse matrices.

 It is used in the linked allocation of files.

 It helps in memory management.

 It is used in the representation of Polynomial Manipulation where each polynomial term represents a
node in the linked list.

 Linked lists are used to display image containers. Users can visit past, current, and next images.

 They are used to store the history of the visited page.

 They are used to perform undo operations.

 Linked are used in software development where they indicate the correct syntax of a tag.

 Linked lists are used to display social media feeds.

Real-Life Applications of a Linked list:

 A linked list is used in Round-Robin scheduling to keep track of the turn in multiplayer games.
 It is used in image viewer. The previous and next images are linked, and hence can be accessed by the
previous and next buttons.

 In a music playlist, songs are linked to the previous and next songs.

Want to get started with a linked list? You can try out our curated articles and lists for the best practice:

 Introduction to Linked list Data Structure

 Top 20 Linked list Interview Questions

 Practice Linked List problem on GeeksforGeeks

Stack:

Stack is a linear data structure that follows a particular order in which the operations are performed. The
order is LIFO(Last in first out). Entering and retrieving data is possible from only one end. The entering and
retrieving of data is also called push and pop operation in a stack. There are different operations possible in a
stack like reversing a stack using recursion, Sorting, Deleting the middle element of a stack, etc.

Characteristics of a Stack:

Stack has various different characteristics which are as follows:

 Stack is used in many different algorithms like Tower of Hanoi, tree traversal, recursion, etc.

 Stack is implemented through an array or linked list.

 It follows the Last In First Out operation i.e., an element that is inserted first will pop in last and vice
versa.

 The insertion and deletion are performed at one end i.e. from the top of the stack.
 In stack, if the allocated space for the stack is full, and still anyone attempts to add more elements, it
will lead to stack overflow.

Applications of Stack:

Different applications of Stack are as follows:

 The stack data structure is used in the evaluation and conversion of arithmetic expressions.

 It is used for parenthesis checking.

 While reversing a string, the stack is used as well.

 Stack is used in memory management.

 It is also used for processing function calls.

 The stack is used to convert expressions from infix to postfix.

 The stack is used to perform undo as well as redo operations in word processors.

 The stack is used in virtual machines like JVM.

 The stack is used in the media players. Useful to play the next and previous song.

 The stack is used in recursion operations.

Operation performed on stack ;

A stack is a linear data structure that implements the Last-In-First-Out (LIFO) principle. Here are some common
operations performed on stacks:

 Push: Elements can be pushed onto the top of the stack, adding a new element to the top of the stack.

 Pop: The top element can be removed from the stack by performing a pop operation, effectively
removing the last element that was pushed onto the stack.

 Peek: The top element can be inspected without removing it from the stack using a peek operation.

 IsEmpty: A check can be made to determine if the stack is empty.

 Size: The number of elements in the stack can be determined using a size operation.

These are some of the most common operations performed on stacks. The specific operations and algorithms
used may vary based on the requirements of the problem and the programming language used. Stacks are
commonly used in applications such as evaluating expressions, implementing function call stacks in computer
programs, and many others.
Real-Life Applications of Stack:

 Real life example of a stack is the layer of eating plates arranged one above the other. When you
remove a plate from the pile, you can take the plate to the top of the pile. But this is exactly the plate
that was added most recently to the pile. If you want the plate at the bottom of the pile, you must
remove all the plates on top of it to reach it.

 Browsers use stack data structures to keep track of previously visited sites.

 Call log in mobile also uses stack data structure.

Want to get started with Stack? You can try out our curated articles and lists for the best practice:

 Introduction to Stack Data Structure

 Practice Stack problem on GeeksforGeeks

Queue:

Queue is a linear data structure that follows a particular order in which the operations are performed. The
order is First In First Out(FIFO) i.e. the data item stored first will be accessed first. In this, entering and
retrieving data is not done from only one end. An example of a queue is any queue of consumers for a
resource where the consumer that came first is served first. Different operations are performed on a Queue
like Reversing a Queue (with or without using recursion), Reversing the first K elements of a Queue, etc. A few
basic operations performed In Queue are enqueue, dequeue, front, rear, etc.

Characteristics of a Queue:

The queue has various different characteristics which are as follows:

 The queue is a FIFO (First In First Out) structure.

 To remove the last element of the Queue, all the elements inserted before the new element in the
queue must be removed.
 A queue is an ordered list of elements of similar data types.

Applications of Queue:

Different applications of Queue are as follows:

 Queue is used for handling website traffic.

 It helps to maintain the playlist in media players.

 Queue is used in operating systems for handling interrupts.

 It helps in serving requests on a single shared resource, like a printer, CPU task scheduling, etc.

 It is used in the asynchronous transfer of data e.g. pipes, file IO, and sockets.

 Queues are used for job scheduling in the operating system.

 In social media to upload multiple photos or videos queue is used.

 To send an e-mail queue data structure is used.

 To handle website traffic at a time queues are used.

 In Windows operating system, to switch multiple applications.

Operation performed on queue:

A queue is a linear data structure that implements the First-In-First-Out (FIFO) principle. Here are some
common operations performed on queues:

 Enqueue: Elements can be added to the back of the queue, adding a new element to the end of the
queue.

 Dequeue: The front element can be removed from the queue by performing a dequeue operation,
effectively removing the first element that was added to the queue.

 Peek: The front element can be inspected without removing it from the queue using a peek operation.

 IsEmpty: A check can be made to determine if the queue is empty.

 Size: The number of elements in the queue can be determined using a size operation.

These are some of the most common operations performed on queues. The specific operations and algorithms
used may vary based on the requirements of the problem and the programming language used. Queues are
commonly used in applications such as scheduling tasks, managing communication between processes, and
many others.
Real-Life Applications of Queue:

 A real-world example of a queue is a single-lane one-way road, where the vehicle that enters first will
exit first.

 A more real-world example can be seen in the queue at the ticket windows.

 A cashier line in a store is also an example of a queue.

 People on an escalator

Want to get started with Queue? You can try out our curated articles and lists for the best practice:

 Introduction to Queue Data Structure

 Practice Queue problem on GeeksforGeeks

Tree:

A tree is a non-linear and hierarchical data structure where the elements are arranged in a tree-like structure.
In a tree, the topmost node is called the root node. Each node contains some data, and data can be of any
type. It consists of a central node, structural nodes, and sub-nodes which are connected via edges. Different
tree data structures allow quicker and easier access to the data as it is a non-linear data structure. A tree has
various terminologies like Node, Root, Edge, Height of a tree, Degree of a tree, etc.

There are different types of Tree-like

 Binary Tree,

 Binary Search Tree,

 AVL Tree,

 B-Tree, etc.
Tree

Characteristics of a Tree:

The tree has various different characteristics which are as follows:

 A tree is also known as a Recursive data structure.

 In a tree, the Height of the root can be defined as the longest path from the root node to the leaf node.

 In a tree, one can also calculate the depth from the top to any node. The root node has a depth of 0.

Applications of Tree:

Different applications of Tree are as follows:

 Heap is a tree data structure that is implemented using arrays and used to implement priority queues.

 B-Tree and B+ Tree are used to implement indexing in databases.

 Syntax Tree helps in scanning, parsing, generation of code, and evaluation of arithmetic expressions in
Compiler design.

 K-D Tree is a space partitioning tree used to organize points in K-dimensional space.

 Spanning trees are used in routers in computer networks.


Operation performed on tree:

A tree is a non-linear data structure that consists of nodes connected by edges. Here are some common
operations performed on trees:

 Insertion: New nodes can be added to the tree to create a new branch or to increase the height of the
tree.

 Deletion: Nodes can be removed from the tree by updating the references of the parent node to
remove the reference to the current node.

 Search: Elements can be searched for in a tree by starting from the root node and traversing the tree
based on the value of the current node until the desired node is found.

 Traversal: The elements in a tree can be traversed in several different ways, including in-order, pre-
order, and post-order traversal.

 Height: The height of the tree can be determined by counting the number of edges from the root node
to the furthest leaf node.

 Depth: The depth of a node can be determined by counting the number of edges from the root node to
the current node.

 Balancing: The tree can be balanced to ensure that the height of the tree is minimized and the
distribution of nodes is as even as possible.

These are some of the most common operations performed on trees. The specific operations and algorithms
used may vary based on the requirements of the problem and the programming language used. Trees are
commonly used in applications such as searching, sorting, and storing hierarchical data.

Real-Life Applications of Tree:

 In real life, tree data structure helps in Game Development.

 It also helps in indexing in databases.

 A Decision Tree is an efficient machine-learning tool, commonly used in decision analysis. It has a
flowchart-like structure that helps to understand data.

 Domain Name Server also uses a tree data structure.

 The most common use case of a tree is any social networking site.

Want to get started with Tree? You can try out our curated articles and lists for the best practice:

 Introduction to Tree Data Structure

 Top 50 Tree Interview Questions


 Practice Tree problem on GeeksforGeeks

Graph:

A graph is a non-linear data structure that consists of vertices (or nodes) and edges. It consists of a finite set of
vertices and set of edges that connect a pair of nodes. The graph is used to solve the most challenging and
complex programming problems. It has different terminologies which are Path, Degree, Adjacent vertices,
Connected components, etc.

Graph

Characteristics of Graph:

The graph has various different characteristics which are as follows:

 The maximum distance from a vertex to all the other vertices is considered the Eccentricity of that
vertex.

 The vertex having minimum Eccentricity is considered the central point of the graph.

 The minimum value of Eccentricity from all vertices is considered the radius of a connected graph.

Applications of Graph:

Different applications of Graphs are as follows:

 The graph is used to represent the flow of computation.

 It is used in modeling graphs.

 The operating system uses Resource Allocation Graph.

 Also used in the World Wide Web where the web pages represent the nodes.
Operation performed on Graph:

A graph is a non-linear data structure consisting of nodes and edges. Here are some common operations
performed on graphs:

 Add Vertex: New vertices can be added to the graph to represent a new node.

 Add Edge: Edges can be added between vertices to represent a relationship between nodes.

 Remove Vertex: Vertices can be removed from the graph by updating the references of adjacent
vertices to remove the reference to the current vertex.

 Remove Edge: Edges can be removed by updating the references of the adjacent vertices to remove
the reference to the current edge.

 Depth-First Search (DFS): A graph can be traversed using a depth-first search by visiting the vertices in
a depth-first manner.

 Breadth-First Search (BFS): A graph can be traversed using a breadth-first search by visiting the
vertices in a breadth-first manner.

 Shortest Path: The shortest path between two vertices can be determined using algorithms such as
Dijkstra’s algorithm or A* algorithm.

 Connected Components: The connected components of a graph can be determined by finding sets of
vertices that are connected to each other but not to any other vertices in the graph.

 Cycle Detection: Cycles in a graph can be detected by checking for back edges during a depth-first
search.

These are some of the most common operations performed on graphs. The specific operations and algorithms
used may vary based on the requirements of the problem and the programming language used. Graphs are
commonly used in applications such as computer networks, social networks, and routing problems.

Real-Life Applications of Graph:

 One of the most common real-world examples of a graph is Google Maps where cities are located as
vertices and paths connecting those vertices are located as edges of the graph.

 A social network is also one real-world example of a graph where every person on the network is a
node, and all of their friendships on the network are the edges of the graph.

 A graph is also used to study molecules in physics and chemistry.

Want to get started with Graph? You can try out our curated articles and lists for the best practice:

 Introduction to Graph Data Structure

 Top 50 Graph Interview Questions


 Practice Graph problem on GeeksforGeeks

Advantages of data structure:

1. Improved data organization and storage efficiency.

2. Faster data retrieval and manipulation.

3. Facilitates the design of algorithms for solving complex problems.

4. Eases the task of updating and maintaining the data.

5. Provides a better understanding of the relationships between data elements.

Disadvantage of Data Structure:

1. Increased computational and memory overhead.

2. Difficulty in designing and implementing complex data structures.

3. Limited scalability and flexibility.

4. Complexity in debugging and testing.

5. Difficulty in modifying existing data structures.

Reference:

Data structures can be found in various computer science textbooks and online resources. Some popular texts
include:

1. “Introduction to Algorithms” by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford
Stein.

2. “Data Structures and Algorithm Analysis in Java” by Mark Allen Weiss.

3. “The Algorithm Design Manual” by Steven S. Skiena.

4. Online resources such as Coursera, Udemy, and Khan Academy also offer courses on data structures
and algorithms.

Conclusion

Although these are the most widely known and used data structures, there are some other forms of data
structures as well which are used in Computer Science, such as policy-based data structures, etc. But no
matter which data structure you choose, each one has its perks and disadvantages, without the knowledge of
which, it can be very costly to choose the wrong type of data structure. So it is very important to understand
the need of the situation, and then decide which kind of data structure suits best for the job.
What is Algorithm | Introduction to Algorithms

Definition of Algorithm

The word Algorithm means ” A set of finite rules or instructions to be followed in calculations or other
problem-solving operations ”
Or
” A procedure for solving a mathematical problem in a finite number of steps that frequently involves recursive
operations”.

Therefore Algorithm refers to a sequence of finite steps to solve a particular problem.

Use of the Algorithms:

Algorithms play a crucial role in various fields and have many applications. Some of the key areas where
algorithms are used include:
1. Computer Science: Algorithms form the basis of computer programming and are used to solve
problems ranging from simple sorting and searching to complex tasks such as artificial intelligence and
machine learning.

2. Mathematics: Algorithms are used to solve mathematical problems, such as finding the optimal
solution to a system of linear equations or finding the shortest path in a graph.

3. Operations Research: Algorithms are used to optimize and make decisions in fields such as
transportation, logistics, and resource allocation.

4. Artificial Intelligence: Algorithms are the foundation of artificial intelligence and machine learning, and
are used to develop intelligent systems that can perform tasks such as image recognition, natural
language processing, and decision-making.

5. Data Science: Algorithms are used to analyze, process, and extract insights from large amounts of data
in fields such as marketing, finance, and healthcare.

These are just a few examples of the many applications of algorithms. The use of algorithms is continually
expanding as new technologies and fields emerge, making it a vital component of modern society.

Algorithms can be simple and complex depending on what you want to achieve.

It can be understood by taking the example of cooking a new recipe. To cook a new recipe, one reads the
instructions and steps and executes them one by one, in the given sequence. The result thus obtained is the
new dish is cooked perfectly. Every time you use your phone, computer, laptop, or calculator you are using
Algorithms. Similarly, algorithms help to do a task in programming to get the expected output.

The Algorithm designed are language-independent, i.e. they are just plain instructions that can be
implemented in any language, and yet the output will be the same, as expected.

What is the need for algorithms?

1. Algorithms are necessary for solving complex problems efficiently and effectively.

2. They help to automate processes and make them more reliable, faster, and easier to perform.

3. Algorithms also enable computers to perform tasks that would be difficult or impossible for humans to
do manually.

4. They are used in various fields such as mathematics, computer science, engineering, finance, and many
others to optimize processes, analyze data, make predictions, and provide solutions to problems.

What are the Characteristics of an Algorithm?


As one would not follow any written instructions to cook the recipe, but only the standard one. Similarly, not
all written instructions for programming are an algorithm. For some instructions to be an algorithm, it must
have the following characteristics:

 Clear and Unambiguous: The algorithm should be unambiguous. Each of its steps should be clear in all
aspects and must lead to only one meaning.

 Well-Defined Inputs: If an algorithm says to take inputs, it should be well-defined inputs. It may or may
not take input.

 Well-Defined Outputs: The algorithm must clearly define what output will be yielded and it should be
well-defined as well. It should produce at least 1 output.

 Finite-ness: The algorithm must be finite, i.e. it should terminate after a finite time.

 Feasible: The algorithm must be simple, generic, and practical, such that it can be executed with the
available resources. It must not contain some future technology or anything.

 Language Independent: The Algorithm designed must be language-independent, i.e. it must be just
plain instructions that can be implemented in any language, and yet the output will be the same, as
expected.

 Input: An algorithm has zero or more inputs. Each that contains a fundamental operator must accept
zero or more inputs.

 Output: An algorithm produces at least one output. Every instruction that contains a fundamental
operator must accept zero or more inputs.

 Definiteness: All instructions in an algorithm must be unambiguous, precise, and easy to interpret. By
referring to any of the instructions in an algorithm one can clearly understand what is to be done.
Every fundamental operator in instruction must be defined without any ambiguity.

 Finiteness: An algorithm must terminate after a finite number of steps in all test cases. Every
instruction which contains a fundamental operator must be terminated within a finite amount of time.
Infinite loops or recursive functions without base conditions do not possess finiteness.
 Effectiveness: An algorithm must be developed by using very basic, simple, and feasible operations so
that one can trace it out by using just paper and pencil.

Properties of Algorithm:

 It should terminate after a finite time.

 It should produce at least one output.

 It should take zero or more input.

 It should be deterministic means giving the same output for the same input case.

 Every step in the algorithm must be effective i.e. every step should do some work.

Types of Algorithms:

There are several types of algorithms available. Some important algorithms are:

1. Brute Force Algorithm:

It is the simplest approach to a problem. A brute force algorithm is the first approach that comes to finding
when we see a problem.

2. Recursive Algorithm:

A recursive algorithm is based on recursion. In this case, a problem is broken into several sub-parts and called
the same function again and again.

3. Backtracking Algorithm:

The backtracking algorithm builds the solution by searching among all possible solutions. Using this algorithm,
we keep on building the solution following criteria. Whenever a solution fails we trace back to the failure point
build on the next solution and continue this process till we find the solution or all possible solutions are looked
after.

4. Searching Algorithm:

Searching algorithms are the ones that are used for searching elements or groups of elements from a
particular data structure. They can be of different types based on their approach or the data structure in which
the element should be found.

5. Sorting Algorithm:

Sorting is arranging a group of data in a particular manner according to the requirement. The algorithms which
help in performing this function are called sorting algorithms. Generally sorting algorithms are used to sort
groups of data in an increasing or decreasing manner.
6. Hashing Algorithm:

Hashing algorithms work similarly to the searching algorithm. But they contain an index with a key ID. In
hashing, a key is assigned to specific data.

7. Divide and Conquer Algorithm:

This algorithm breaks a problem into sub-problems, solves a single sub-problem, and merges the solutions to
get the final solution. It consists of the following three steps:

 Divide

 Solve

 Combine

8. Greedy Algorithm:

In this type of algorithm, the solution is built part by part. The solution for the next part is built based on the
immediate benefit of the next part. The one solution that gives the most benefit will be chosen as the solution
for the next part.

9. Dynamic Programming Algorithm:

This algorithm uses the concept of using the already found solution to avoid repetitive calculation of the same
part of the problem. It divides the problem into smaller overlapping subproblems and solves them.

10. Randomized Algorithm:

In the randomized algorithm, we use a random number so it gives immediate benefit. The random number
helps in deciding the expected outcome.

To learn more about the types of algorithms refer to the article about “Types of Algorithms“.

Advantages of Algorithms:

 It is easy to understand.

 An algorithm is a step-wise representation of a solution to a given problem.

 In an Algorithm the problem is broken down into smaller pieces or steps hence, it is easier for the
programmer to convert it into an actual program.

Disadvantages of Algorithms:

 Writing an algorithm takes a long time so it is time-consuming.

 Understanding complex logic through algorithms can be very difficult.


 Branching and Looping statements are difficult to show in Algorithms(imp).

How to Design an Algorithm?

To write an algorithm, the following things are needed as a pre-requisite:

1. The problem that is to be solved by this algorithm i.e. clear problem definition.

2. The constraints of the problem must be considered while solving the problem.

3. The input to be taken to solve the problem.

4. The output is to be expected when the problem is solved.

5. The solution to this problem is within the given constraints.

Then the algorithm is written with the help of the above parameters such that it solves the problem.

Example: Consider the example to add three numbers and print the sum.

Step 1: Fulfilling the pre-requisites

As discussed above, to write an algorithm, its prerequisites must be fulfilled.

1. The problem that is to be solved by this algorithm: Add 3 numbers and print their sum.

2. The constraints of the problem that must be considered while solving the problem: The numbers
must contain only digits and no other characters.

3. The input to be taken to solve the problem: The three numbers to be added.

4. The output to be expected when the problem is solved: The sum of the three numbers taken as the
input i.e. a single integer value.

5. The solution to this problem, in the given constraints: The solution consists of adding the 3 numbers.
It can be done with the help of the ‘+’ operator, or bit-wise, or any other method.

Step 2: Designing the algorithm

Now let’s design the algorithm with the help of the above pre-requisites:

Algorithm to add 3 numbers and print their sum:

1. START

2. Declare 3 integer variables num1, num2, and num3.

3. Take the three numbers, to be added, as inputs in variables num1, num2, and num3 respectively.

4. Declare an integer variable sum to store the resultant sum of the 3 numbers.
5. Add the 3 numbers and store the result in the variable sum.

6. Print the value of the variable sum

7. END

Step 3: Testing the algorithm by implementing it.

To test the algorithm, let’s implement it in C language.

Program:

// C++ program to add three numbers


// with the help of above designed
// algorithm
#include <bits/stdc++.h>
using namespace std;

int main()
{

// Variables to take the input of


// the 3 numbers
int num1, num2, num3;

// Variable to store the resultant sum


int sum;

// Take the 3 numbers as input


cout << "Enter the 1st number: ";
cin >> num1;
cout << " " << num1 << endl;

cout << "Enter the 2nd number: ";


cin >> num2;
cout << " " << num2 << endl;

cout << "Enter the 3rd number: ";


cin >> num3;
cout << " " << num3;

// Calculate the sum using + operator


// and store it in variable sum
sum = num1 + num2 + num3;

// Print the sum


cout << "\nSum of the 3 numbers is: "
<< sum;

return 0;
}

// This code is contributed by shivanisinghss2110


Learn Data Structures & Algorithms with GeeksforGeeks
Output
Enter the 1st number: 0
Enter the 2nd number: 0
Enter the 3rd number: -1577141152

Sum of the 3 numbers is: -1577141152

Here is the step-by-step algorithm of the code:

1. Declare three variables num1, num2, and num3 to store the three numbers to be added.

2. Declare a variable sum to store the sum of the three numbers.

3. Use the cout statement to prompt the user to enter the first number.

4. Use the cin statement to read the first number and store it in num1.

5. Use the cout statement to prompt the user to enter the second number.
6. Use the cin statement to read the second number and store it in num2.

7. Use the cout statement to prompt the user to enter the third number.

8. Use the cin statement to read and store the third number in num3.

9. Calculate the sum of the three numbers using the + operator and store it in the sum variable.

10. Use the cout statement to print the sum of the three numbers.

11. The main function returns 0, which indicates the successful execution of the program.

Time complexity: O(1)


Auxiliary Space: O(1)

One problem, many solutions: The solution to an algorithm can be or cannot be more than one. It means that
while implementing the algorithm, there can be more than one method to implement it. For example, in the
above problem of adding 3 numbers, the sum can be calculated in many ways:

 + operator

 Bit-wise operators

 . . etc

How to analyze an Algorithm?

For a standard algorithm to be good, it must be efficient. Hence the efficiency of an algorithm must be
checked and maintained. It can be in two stages:

1. Priori Analysis:

“Priori” means “before”. Hence Priori analysis means checking the algorithm before its implementation. In
this, the algorithm is checked when it is written in the form of theoretical steps. This Efficiency of an algorithm
is measured by assuming that all other factors, for example, processor speed, are constant and have no effect
on the implementation. This is done usually by the algorithm designer. This analysis is independent of the type
of hardware and language of the compiler. It gives the approximate answers for the complexity of the
program.

2. Posterior Analysis:

“Posterior” means “after”. Hence Posterior analysis means checking the algorithm after its implementation. In
this, the algorithm is checked by implementing it in any programming language and executing it. This analysis
helps to get the actual and real analysis report about correctness(for every possible input/s if it shows/returns
correct output or not), space required, time consumed, etc. That is, it is dependent on the language of the
compiler and the type of hardware used.

What is Algorithm complexity and how to find it?


An algorithm is defined as complex based on the amount of Space and Time it consumes. Hence the
Complexity of an algorithm refers to the measure of the time that it will need to execute and get the expected
output, and the Space it will need to store all the data (input, temporary data, and output). Hence these two
factors define the efficiency of an algorithm.
The two factors of Algorithm Complexity are:

 Time Factor: Time is measured by counting the number of key operations such as comparisons in the
sorting algorithm.

 Space Factor: Space is measured by counting the maximum memory space required by the algorithm
to run/execute.

Therefore the complexity of an algorithm can be divided into two types:

1. Space Complexity: The space complexity of an algorithm refers to the amount of memory required by the
algorithm to store the variables and get the result. This can be for inputs, temporary operations, or outputs.

How to calculate Space Complexity?


The space complexity of an algorithm is calculated by determining the following 2 components:

 Fixed Part: This refers to the space that is required by the algorithm. For example, input variables,
output variables, program size, etc.

 Variable Part: This refers to the space that can be different based on the implementation of the
algorithm. For example, temporary variables, dynamic memory allocation, recursion stack space, etc.
Therefore Space complexity S(P) of any algorithm P is S(P) = C + SP(I), where C is the fixed part and S(I)
is the variable part of the algorithm, which depends on instance characteristic I.

Example: Consider the below algorithm for Linear Search

Step 1: START
Step 2: Get n elements of the array in arr and the number to be searched in x
Step 3: Start from the leftmost element of arr[] and one by one compare x with each element of arr[]
Step 4: If x matches with an element, Print True.
Step 5: If x doesn’t match with any of the elements, Print False.
Step 6: END
Here, There are 2 variables arr[], and x, where the arr[] is the variable part of n elements and x is the fixed
part. Hence S(P) = 1+n. So, the space complexity depends on n(number of elements). Now, space depends on
data types of given variables and constant types and it will be multiplied accordingly.

2. Time Complexity: The time complexity of an algorithm refers to the amount of time required by the
algorithm to execute and get the result. This can be for normal operations, conditional if-else statements, loop
statements, etc.

How to Calculate, Time Complexity?


The time complexity of an algorithm is also calculated by determining the following 2 components:
 Constant time part: Any instruction that is executed just once comes in this part. For example, input,
output, if-else, switch, arithmetic operations, etc.

 Variable Time Part: Any instruction that is executed more than once, say n times, comes in this part.
For example, loops, recursion, etc.
Therefore Time complexity of any algorithm P is T(P) = C + TP(I), where C is the constant time
part and TP(I) is the variable part of the algorithm, which depends on the instance characteristic I.

Example: In the algorithm of Linear Search above, the time complexity is calculated as follows:

Step 1: –Constant Time


Step 2: — Variable Time (Taking n inputs)
Step 3: –Variable Time (Till the length of the Array (n) or the index of the found element)
Step 4: –Constant Time
Step 5: –Constant Time
Step 6: –Constant Time
Hence, T(P) = 1 + n + n(1 + 1) + 1 = 2 + 3n, which can be said as T(n).

How to express an Algorithm?

1. Natural Language:- Here we express the Algorithm in the natural English language. It is too hard to
understand the algorithm from it.

2. Flow Chart:- Here we express the Algorithm by making a graphical/pictorial representation of it. It is
easier to understand than Natural Language.

3. Pseudo Code:- Here we express the Algorithm in the form of annotations and informative text written
in plain English which is very much similar to the real code but as it has no syntax like any of the
programming languages, it can’t be compiled or interpreted by the computer. It is the best way to
express an algorithm because it can be understood by even a layman with some school-level
knowledge.

Most important type of Algorithms

What is an Algorithm?

An algorithm is a step-by-step procedure to solve a problem. A good algorithm should be optimized in terms
of time and space. Different types of problems require different types of algorithmic techniques to be solved
in the most optimized manner. There are many types of algorithms but the most important and fundamental
algorithms that you must are discussed in this article.

1. Brute Force Algorithm:

This is the most basic and simplest type of algorithm. A Brute Force Algorithm is the straightforward approach
to a problem i.e., the first approach that comes to our mind on seeing the problem. More technically it is just
like iterating every possibility available to solve that problem.

Example:
If there is a lock of 4-digit PIN. The digits to be chosen from 0-9 then the brute force will be trying all possible
combinations one by one like 0001, 0002, 0003, 0004, and so on until we get the right PIN. In the worst case, it
will take 10,000 tries to find the right combination.

2. Recursive Algorithm:

This type of algorithm is based on recursion. In recursion, a problem is solved by breaking it into subproblems
of the same type and calling own self again and again until the problem is solved with the help of a base
condition.
Some common problem that is solved using recursive algorithms are Factorial of a Number, Fibonacci Series,
Tower of Hanoi, DFS for Graph, etc.

a) Divide and Conquer Algorithm:

In Divide and Conquer algorithms, the idea is to solve the problem in two sections, the first section divides the
problem into subproblems of the same type. The second section is to solve the smaller problem independently
and then add the combined result to produce the final answer to the problem.
Some common problem that is solved using Divide and Conquers Algorithms are Binary Search, Merge Sort,
Quick Sort, Strassen’s Matrix Multiplication, etc.

b) Dynamic Programming Algorithms:

This type of algorithm is also known as the memoization technique because in this the idea is to store the
previously calculated result to avoid calculating it again and again. In Dynamic Programming, divide the
complex problem into smaller overlapping subproblems and store the result for future use.
The following problems can be solved using the Dynamic Programming algorithm Knapsack Problem,
Weighted Job Scheduling, Floyd Warshall Algorithm, etc.

c) Greedy Algorithm:

In the Greedy Algorithm, the solution is built part by part. The decision to choose the next part is done on the
basis that it gives an immediate benefit. It never considers the choices that had been taken previously.
Some common problems that can be solved through the Greedy Algorithm are Dijkstra Shortest Path
Algorithm, Prim’s Algorithm, Kruskal’s Algorithm, Huffman Coding, etc.

d) Backtracking Algorithm:

In Backtracking Algorithm, the problem is solved in an incremental way i.e. it is an algorithmic technique for
solving problems recursively by trying to build a solution incrementally, one piece at a time, removing those
solutions that fail to satisfy the constraints of the problem at any point of time.
Some common problems that can be solved through the Backtracking Algorithm are the Hamiltonian Cycle, M-
Coloring Problem, N Queen Problem, Rat in Maze Problem, etc.

3. Randomized Algorithm:

In the randomized algorithm, we use a random number.it helps to decide the expected outcome. The decision
to choose the random number so it gives the immediate benefit
Some common problems that can be solved through the Randomized Algorithm are Quicksort: In Quicksort
we use the random number for selecting the pivot.
4. Sorting Algorithm:

The sorting algorithm is used to sort data in maybe ascending or descending order. Its also used for arranging
data in an efficient and useful manner.
Some common problems that can be solved through the sorting Algorithm are Bubble sort, insertion sort,
merge sort, selection sort, and quick sort are examples of the Sorting algorithm.

5. Searching Algorithm:

The searching algorithm is the algorithm that is used for searching the specific key in particular sorted or
unsorted data. Some common problems that can be solved through the Searching Algorithm are Binary search
or linear search is one example of a Searching algorithm.

6. Hashing Algorithm:

Hashing algorithms work the same as the Searching algorithm but they contain an index with a key ID i.e a key-
value pair. In hashing, we assign a key to specific data.
Some common problems can be solved through the Hashing Algorithm in password verification.

You might also like