Dsa Py
Dsa Py
organizing and storing data and the design of procedures (algorithms) for
solving problems, which operate on these data structures. DSA is one of
the most important skills that every computer science student must have.
It is often seen that people with good knowledge of these technologies are
better programmers than others and thus, crack the interviews of almost
every tech giant. This DSA tutorial aims to help you learn Data Structures
and Algorithms (DSA) quickly and easily.
What is an Array?
Array is a linear data structure that stores a collection of items of same
data type in contiguous memory locations. Each item in an array is
indexed starting with 0. We can directly access an array element by using
its index value.
Basic terminologies of Array
Array Index: In an array, elements are identified by their indexes.
Array index starts from 0.
Array element: Elements are items stored in an array and can be
accessed by their index.
Array Length: The length of an array is determined by the number of
elements it can contain.
Memory representation of Array
In an array, all the elements are stored in contiguous memory locations.
So, if we initialize an array, the elements will be allocated sequentially in
memory. This allows for efficient access and manipulation of elements.
Declaration of Array
Arrays can be declared in various ways in different languages. For better
illustration, below are some language-specific array declarations:
Initialization of Array
Arrays can be initialized in different ways in different languages. Below
are some language-specific array initializations:
Importance of Array
Assume there is a class of five students and if we have to keep records of
their marks in examination then, we can do this by declaring five variables
individual and keeping track of records but what if the number of students
becomes very large, it would be challenging to manipulate and maintain
the data.
What it means is that, we can use normal variables (v1, v2, v3, ..) when
we have a small number of objects. But if we want to store a large number
of instances, it becomes difficult to manage them with normal
variables. The idea of an array is to represent many instances in one
variable.
Types of Arrays
Arrays can be classified in two ways:
On the basis of Size
On the basis of Dimensions
Types of Arrays on the basis of Size:
1. Fixed Sized Arrays:
We cannot alter or update the size of this array. Here only a fixed size (i,e.
the size that is mentioned in square brackets []) of memory will be
allocated for storage. In case, we don’t know the size of the array then if
we declare a larger size and store a lesser number of elements will result
in a wastage of memory or we declare a lesser size than the number of
elements then we won’t get enough memory to store all the elements. In
such cases, static memory allocation is not preferred.
2. Insertion in Array:
We can insert one or multiple elements at any position in the array. Below
is the implementation of Insertion in Array in different languages:
3. Deletion in Array:
We can delete an element at any index in an array. Below is the
implementation of Deletion of element in an array:
4. Searching in Array:
We can traverse over an array and search for an element. Below is the
implementation of Deletion of element in an array:
Space Complexity:
Operation Best Case Average Case Worst Case
Advantages of Array
Arrays allow random access to elements. This makes accessing
elements by position faster.
Arrays have better cache locality which makes a pretty big difference
in performance.
Arrays represent multiple data items of the same type using a
single name.
Arrays are used to implement the other data structures like linked lists,
stacks, queues, trees, graphs, etc.
Disadvantages of Array
As arrays have a fixed size, once the memory is allocated to them, it
cannot be increased or decreased, making it impossible to store extra
data if required. An array of fixed size is referred to as a static array.
Allocating less memory than required to an array leads to loss of data.
An array is homogeneous in nature so, a single array cannot store
values of different data types.
Arrays store data in contiguous memory locations, which makes
deletion and insertion very difficult to implement. This problem is
overcome by implementing linked lists, which allow elements to be
accessed sequentially.
Applications of Array
They are used in the implementation of other data structures such as
array lists, heaps, hash tables, vectors, and matrices.
Database records are usually implemented as arrays.
It is used in lookup tables by computer.
2. String
A string is a sequence of characters, typically used to represent text. It is
considered a data type that allows for the manipulation and processing of
textual data in computer programs.
Operations on String :
o Concatenation: Joining two strings together.
o Comparison: Comparing two strings lexicographically.
o Substring extraction: Extracting a substring from a string.
o Search: Searching for a substring within a string.
o Modification: Changing or replacing characters within a
string.
Applications of String :
o Text processing
o Pattern matching
o Data validation
o Database management
Single-linked list
2. Double-linked list:
In a doubly linked list, each node contains references to both the next and
previous nodes. This allows for traversal in both forward and backward
directions, but it requires additional memory for the backward reference.
Double-linked list
. Hash
Hashing is a technique that generates a fixed-size output (hash value)
from an input of variable size using mathematical formulas called hash
functions. Hashing is used to determine an index or location for storing
an item in a data structure, allowing for efficient retrieval and insertion.
Key Concepts:
o Hash Function: A mathematical function that maps an input to
a hash value.
o Hash Table: A data structure that stores key-value pairs,
where the key is a hash value and the value is the actual data.
o Collision: When two different keys produce the same hash
value.
Types of Hash Functions :
o Division Method: Divides the input by a constant and uses
the remainder as the hash value.
o Mid Square Method: Squares the input and takes the middle
digits as the hash value.
o Folding Method: Divides the input into equal-sized blocks
and adds them together to get the hash value.
o Multiplication Method: Multiplies the input by a constant and
takes the fractional part as the hash value.
Collision Resolution Techniques :
o Separate Chaining (Open Hashing): Stores colliding elements
in a linked list at the corresponding hash value.
o Open Addressing (Closed Hashing): Uses various strategies
to find an alternative location for colliding elements within the
hash table.
Applications of Hashing:
o Efficiently storing and retrieving data in databases and file
systems.
o Verifying passwords and digital signatures.
o Distributing requests across multiple servers.
o Generating secure hashes for data integrity and
authentication.
9. Tree
A tree is a non-linear hierarchical data structure consisting of nodes connected by
edges, with a top node called the root and nodes having child nodes. It is used in
computer science for organizing data efficiently.
Traversal of Tree: Tree traversal methods are used to visit and process nodes
in a tree data structure. The three common traversal methods are:
o In-Order: Visit left subtree, current node, then right subtree.
o Pre-Order: Visit current node, left subtree, then right subtree.
o Post-Order: Visit left subtree, right subtree, then current node.
Classifications of Trees:
o Classifications of Trees refer to grouping trees based on certain
characteristics or criteria. This can involve categorizing trees based
on their balance factor, degree of nodes, ordering properties, etc.
Below are some important classification of Tree.
Classification Description
Type Description
By Ordering Trees can be classified Binary Left child < parent < right
based on the ordering Search Tree child for every node.
of nodes and subtrees (BST)
Classification Description
Type Description
Applications of Trees:
o File systems
o Databases
o XML documents
o Artificial intelligence
10. Graph
A Graph is a non-linear data structure consisting of a finite set of
vertices(or nodes) and a set of edges that connect a pair of nodes.
Traversals of Graph:
o Breadth-First Search (BFS): Visits nodes level by level.
o Depth-First Search (DFS): Visits nodes recursively, exploring
one branch at a time.
Applications of Graphs :
o Social networks
o Maps and navigation
o Scheduling
o Data mining
Related posts:
o Graph Representation
o Types of graphs
o Graph Tutorial
o Practice problems on Graph
Learn Algorithms
Once you have cleared the concepts of Data Structures, now its time to
start your journey through the Algorithms. Based on the type of nature
and usage, the Algorithms are grouped together into several categories,
as shown below:
1. Searching Algorithm
Searching algorithms are used to locate specific data within a larger set
of data. It helps find the presence of a target value within the data. There
are various types of searching algorithms, each with its own approach and
efficiency.
Most common searching algorithms:
o Linear Search: Iterative search from one end to the other.
o Binary Search: Divide-and-conquer search on a sorted array,
halving the search space at each iteration.
o Ternary Search: Divide-and-conquer search on a sorted array,
dividing the search space into three parts at each iteration.
Other searching algorithms:
o Jump Search
o Interpolation Search
o Exponential Search
Related Topics:
o Practice problems on Searching algorithm
2. Sorting Algorithm
Sorting algorithms are used to arranging the elements of a list in a
specific order, such as numerical or alphabetical. It organizes the items in
a systematic way, making it easier to search for and access specific
elements.
There are a lot of different types of sorting algorithms. Some widely used
algorithms are:
Sorting
Algorithm Description
Related Topics:
Sorting Algorithm Tutorial
Top Sorting Interview Questions and Problems
Practice problems on Sorting algorithm
3. Divide and Conquer Algorithm
Divide and conquer algorithms follow a recursive strategy to solve
problems by dividing them into smaller subproblems, solving those
subproblems, and combining the solutions to obtain the final solution.
Steps:
1. Divide: Partition the problem into smaller, independent subproblems.
2. Conquer: Recursively solve each subproblem.
3. Combine: Merge the solutions of the subproblems to obtain the final
solution.
Examples:
Merge Sort: Divides the array into two halves, sorts each half
recursively, and merges the sorted halves.
Quick Sort: Selects a pivot element, partitions the array into two
subarrays based on the pivot, and recursively sorts each subarray.
Binary Search: Divides the search space in half repeatedly until the
target element is found or the search space is exhausted.
Related Articles:
Divide and Conquer Tutorial
Practice problems on Divide And Conquer algorithm
4. Greedy Algorithms
As the name suggests, this algorithm builds up the solution one piece at a
time and chooses the next piece which gives the most obvious and
immediate benefit i.e., which is the most optimal choice at that moment.
So the problems where choosing locally optimal also leads to the global
solutions are best fit for Greedy.
Some Important Problem of Greedy Algorithms are:
Algorithm Description
Fractional Find the maximum total value of items that can be placed in
Knapsack the knapsack, allowing fractional inclusion of items.
Dijkstra’s Finds the shortest path from a source vertex to all other
Algorithm vertices in a weighted graph.
Kruskal’s
Finds the minimum spanning tree of a weighted graph.
Algorithm
Related Articles:
Greedy Algorithm Tutorial
Practice problems on Greedy algorithm
5. Recursion
Recursion is a programming technique where a function calls itself within
its own definition. It is usually used to solve problems that can be broken
down into smaller instances of the same problem. For Example: Towers
of Hanoi (TOH), Factorial Calculation and Fibonacci Sequence etc.
Steps:
Base Case: Define a condition that stops the recursive calls and
provides a solution.
Recursive Case: Define the steps to break down the problem into
smaller instances and make recursive calls.
Return: Return the solution from the recursive calls and combine them
to solve the original problem.
The point which makes Recursion one of the most used algorithms is that
it forms the base for many other algorithms such as Tree
traversals, Graph traversals, Divide and Conquers
Algorithms and Backtracking algorithms.
Related Topics:
Recursion Tutorial
Recursive Functions
Tail Recursion
Top 50 Problems on Recursion Algorithm for Interview
Practice problems on Recursion algorithm
6. Backtracking Algorithm
As mentioned earlier, the Backtracking algorithm is derived from the
Recursion algorithm, with the option to revert if a recursive solution fails,
i.e. in case a solution fails, the program traces back to the moment where
it failed and builds on another solution. So basically it tries out all the
possible solutions and finds the correct one.
Some important and most common problems of backtracking algorithms,
that you must solve before moving ahead, are:
Problem Description
Rat in a Finding a path from the starting position to the exit in a maze,
Maze with obstacles represented as walls.
Subset Sum Determining whether there exists a subset of the given set
Problem with a given sum.
Related Article:
Backtracking Tutorial
Practice problems on Backtracking algorithm
7. Dynamic Programming
Dynamic Programming is a method used in mathematics and computer
science to solve complex problems by breaking them down into simpler
subproblems. By solving each subproblem only once and storing the
results, it avoids redundant computations, leading to more efficient
solutions for a wide range of problems.
Key Concepts:
Optimal Substructure: The optimal solution to a problem can be
constructed from the optimal solutions to its subproblems.
Overlapping Subproblems: Subproblems are often repeated in the
larger problem, leading to redundant computations.
Memoization / Tabulation: Storing the solutions to subproblems to
avoid recomputation.
Some important and most common problems of dynamic programming
algorithms, that you must solve before moving ahead, are:
Problem Description
Maximum Subarray Finding the contiguous subarray with the largest sum
Sum within a given one-dimensional array.
Related Articles:
Tabulation vs Memoization
How to solve a Dynamic Programming Problem?
Dynamic Programming Tutorial
Top 50 Dynamic Programming Coding Problems for Interviews
Practice problems on Dynamic Programming algorithm
8. Graph Algorithms:
Graph algorithms in data structures and algorithms (DSA) are a set of
techniques and methods used to solve problems related to graphs, which
are a collection of nodes and edges. These algorithms are designed to
perform various operations on graphs, such as searching, traversing,
finding the shortest path, and determining connectivity. They are
essential for solving a wide range of real-world problems, including
network routing, social network analysis, and resource allocation.
Algorithm’s
Topic’s
Topic Algorithm Description
Description
Explores neighbor
Breadth-
vertices before
First Search
moving to the next
(BFS)
level of vertices.
It finds a minimum
spanning tree for a
connected weighted
Kruskal’s
graph. It adds the
Algorithm
Minimum Spanning smallest weight
Trees are the smallest edge that does not
trees that connect all form a cycle.
Minimum
nodes in a graph
Spanning
without forming It also finds a
Trees
cycles, achieved by minimum spanning
adding the shortest tree for a
edges possible. Prim’s connected weighted
Algorithm graph. It adds the
smallest weight
edge that connects
two trees.
Kahn’s Algorithm
finds a topological
Kahn’s
ordering of a
Algorithm
Topological sorting is directed acyclic
a linear ordering of the graph (DAG).
vertices in a directed
Topological acyclic graph (DAG)
Sorting such that for every DFS-based
directed edge from Algorithm use
vertex u to vertex v, u Depth-First Search
comes before v in the DFS-based
to perform
ordering. Algorithm
topological sorting
in a directed acyclic
graph (DAG).
Kosaraju’s
Algorithm is a two-
pass algorithm that
A strongly connected Kosaraju’s efficiently finds the
component (SCC) of a Algorithm strongly connected
directed graph is a components
Strongly maximal set of (SCCs) of a
Connected vertices such that directed graph.
Components there is a path from
every vertex in the set
to every other vertex Tarjan’s Algorithm
in the set. is another efficient
Tarjan’s
algorithm for finding
Algorithm
SCCs in a directed
graph
9. Pattern Searching
Pattern searching is a fundamental technique in DSA used to find
occurrences of a specific pattern within a given text.
Below are some some standard pattern searching algorithms:
Time
Algorithm Description Complexity
Related Topics:
Pattern Searching Tutorial
Practice Problem on Pattern Searching
10. Mathematical Algorithms
Mathematical algorithms are a class of algorithms that solve problems
related to mathematical concepts. They are widely used in various fields,
including Computer graphics, Numerical analysis, Optimization and
Cryptography.
Algorithm Description
Related Topics:
Practice Problem on Mathmatical Algorithm
11. Geometric Algorithms
Geometric algorithms are a class of algorithms that solve problems
related to geometry. Geometric algorithms are essential for solving a wide
range of problems in computer science, such as:
Algorithm Description
Closest Pair of Finds the two points in a set that are closest to each
Points other.
Line Intersection Determines whether two lines intersect and, if so, finds
Algorithm Description
Related Topics:
Practice Problem on Geometric Algorithms
12. Bitwise Algorithms
Bitwise algorithms are algorithms that operate on individual bits of
numbers. These algorithms manipulate the binary representation of
numbers to perform tasks such as bit manipulation, bitwise logical
operations (AND, OR, XOR), shifting bits, and setting or clearing
specific bits within a number. Bitwise algorithms are commonly used
in low-level programming, cryptography, and optimization
tasks where efficient manipulation of individual bits is required.
Topic Description
Counting Set
Counting the number of set bits (1s) in an integer.
bits
Related Topics:
Bitwise Algorithms Tutorial
Practice Problem on Bitwise Algorithms
13. Randomized Algorithms
Randomized algorithms are algorithms that use randomness to solve
problems. They make use of random input to achieve their goals, often
leading to simpler or more efficient solutions.
Types of Randomized Algorithms:
Las Vegas: Always produces a correct result, but the running time is
random.
Monte Carlo: May produce an incorrect result with a small probability,
but the running time is usually faster.
Important Algorithms that uses Randomization Algorithms:
Algorithm Description
0/1 Knapsack using Least Solving the 0/1 Knapsack problem using the
Cost Branch and Bound least cost branch and bound technique.
Related Topics:
Branch and Bound Algorithm Tutorial
Learn about Complexities
In Data Structures and Algorithms (DSA), the main goal is to solve
problems effectively and efficiently. To determine the efficiency of a
program, we look at two types of complexities:
1. Time Complexity: This tells us how much time our code takes to run.
2. Space Complexity: This tells us how much memory our code uses.
Asymptotic Notation:
To compare efficiencies of algorithms, we use asymptotic notation, a
mathematical tool that estimates time based on input size without
running the code. It focuses on the number of basic operations in the
program.
Notation Description