BRAINWARE UNIVERSITY
CLASS NOTES DATA STRUCTURE & ALGORITHMS (ESCG302)
MODULE -1
Data Structure
Data Structure is a branch of Computer Science. The study of data structure allows us to
understand the organization of data and the management of the data flow in order to increase
the efficiency of any process or program. Data Structure is a particular way of storing and
organizing data in the memory of the computer so that these data can easily be retrieved and
efficiently utilized in the future when required. The data can be managed in various ways, like
the logical or mathematical model for a specific organization of data is known as a data
structure.
The scope of a particular data model depends on two factors:
1. First, it must be loaded enough into the structure to reflect the definite correlation of
the data with a real-world object.
2. Second, the formation should be so straightforward that one can adapt to process the
data efficiently whenever necessary.
Some examples of Data Structures are Arrays, Linked Lists, Stack, Queue, Trees, etc. Data
Structures are widely used in almost every aspect of Computer Science, i.e., Compiler Design,
Operating Systems, Graphics, Artificial Intelligence, and many more.
Data Structures are the main part of many Computer Science Algorithms as they allow the
programmers to manage the data in an effective way. It plays a crucial role in improving the
performance of a program or software, as the main objective of the software is to store and
retrieve the user's data as fast as possible.
Basic Terminologies related to Data Structures
Data Structures are the building blocks of any software or program. Selecting the suitable data
structure for a program is an extremely challenging task for a programmer.
The following are some fundamental terminologies used whenever the data structures are
involved:
1. Data: We can define data as an elementary value or a collection of values. For example, the
Employee's name and ID are the data related to the Employee.
2. Data Items: A Single unit of value is known as Data Item.
3. Group Items: Data Items that have subordinate data items are known as Group Items. For
example, an employee's name can have a first, middle, and last name.
Prepared by: Department of CSE (Brainware University, Barasat)
BRAINWARE UNIVERSITY
CLASS NOTES DATA STRUCTURE & ALGORITHMS (ESCG302)
4. Elementary Items: Data Items that are unable to divide into sub-items are known as
Elementary Items. For example, the ID of an Employee.
5. Entity and Attribute: A class of certain objects is represented by an Entity. It consists of
different Attributes. Each Attribute symbolizes the specific property of that Entity. For
example,
Attributes ID Name Gender Job Title
Values 1234 Stacey M. Hill Female Software Developer
Entities with similar attributes form an Entity Set. Each attribute of an entity set has a range
of values, the set of all possible values that could be assigned to the specific attribute.
The term "information" is sometimes utilized for data with given attributes of meaningful or
processed data.
1. Field: A single elementary unit of information symbolizing the Attribute of an Entity is known
as Field.
2. Record: A collection of different data items are known as a Record. For example, if we talk
about the employee entity, then its name, id, address, and job title can be grouped to form the
record for the employee.
3. File: A collection of different Records of one entity type is known as a File. For example, if
there are 100 employees, there will be 25 records in the related file containing data about each
employee.
Understanding the Need for Data Structures
As applications are becoming more complex and the amount of data is increasing every day,
which may lead to problems with data searching, processing speed, multiple requests handling,
and many more. Data Structures support different methods to organize, manage, and store data
efficiently. With the help of Data Structures, we can easily traverse the data items. Data
Structures provide Efficiency, Reusability, and Abstraction.
Why should we learn Data Structures?
1. Data Structures and Algorithms are two of the key aspects of Computer Science.
Prepared by: Department of CSE (Brainware University, Barasat)
BRAINWARE UNIVERSITY
CLASS NOTES DATA STRUCTURE & ALGORITHMS
2. Data Structures allow us to organize and store data, whereas Algorithms allow us to process
that data meaningfully.
3. Learning Data Structures and Algorithms will help us become better Programmers.
4. We will be able to write code that is more effective and reliable.
5. We will also be able to solve problems more quickly and efficiently.
Understanding the Objectives of Data Structures
Data Structures satisfy two complementary objectives:
1. Correctness: Data Structures are designed to operate correctly for all kinds of inputs based on
the domain of interest. In order words, correctness forms the primary objective of Data
Structure, which always depends upon the problems that the Data Structure is meant to solve.
2. Efficiency: Data Structures also requires to be efficient. It should process the data quickly
without utilizing many computer resources like memory space. In a real-time state, the
efficiency of a data structure is a key factor in determining the success and failure of the process.
Understanding some Key Features of Data Structures
Some of the Significant Features of Data Structures are:
1. Robustness: Generally, all computer programmers aim to produce software that yields
correct output for every possible input, along with efficient execution on all hardware
platforms. This type of robust software must manage both valid and invalid inputs.
2. Adaptability: Building software applications like Web Browsers, Word Processors,
and Internet Search Engine include huge software systems that require correct and
efficient working or execution for many years. Moreover, software evolves due to
emerging technologies or ever-changing market conditions.
3. Reusability: The features like Reusability and Adaptability go hand in hand. It is
known that the programmer needs many resources to build any software, making it a
costly enterprise. However, if the software is developed in a reusable and adaptable
way, then it can be applied in most future applications. Thus, by executing quality data
structures, it is possible to build reusable software, which appears to be cost-effective
and timesaving.
Prepared by: Department of CSE (Brainware University, Barasat)
BRAINWARE UNIVERSITY
CLASS NOTES DATA STRUCTURE & ALGORITHMS
Need Of Data Structure:
The structure of the data and the synthesis of the algorithm are relative to each other. Data
presentation must be easy to understand so the developer, as well as the user, can make an efficient
implementation of the operation.
Data structures provide an easy way of organising, retrieving, managing, and storing data.
Here is a list of the needs for data.
• Data structure modification is easy.
• It requires less time.
• Save storage memory space.
• Data representation is easy.
• Easy access to the large database
Classification/Types of Data Structures:
1. Linear Data Structure
2. Non-Linear Data Structure.
Linear Data Structure:
• Elements are arranged in one dimension ,also known as linear dimension.
• Example: lists, stack, queue, etc.
Non-Linear Data Structure
• Elements are arranged in one-many, many-one and many-many dimensions.
• Example: tree, graph, table, etc.
Most Popular Data Structures:
1. Array:
An array is a collection of data items stored at contiguous memory locations. The idea is to store
multiple items of the same type together. 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).
Prepared by: Department of CSE (Brainware University, Barasat)
BRAINWARE UNIVERSITY
CLASS NOTES DATA STRUCTURE & ALGORITHMS
Array Data Structure
2. Linked Lists:
Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at
a contiguous location; the elements are linked using pointers.
Linked Data Structure
3. Stack:
Stack is a linear data structure which follows a particular order in which the operations are performed.
The order may be LIFO(Last In First Out) or FILO(First In Last Out). In stack, all insertion and
deletion are permitted at only one end of the list.
Prepared by: Department of CSE (Brainware University, Barasat)
BRAINWARE UNIVERSITY
CLASS NOTES DATA STRUCTURE & ALGORITHMS
Stack Operations:
• push(): When this operation is performed, an element is inserted into the stack.
• pop(): When this operation is performed, an element is removed from the top of the stack
and is returned.
• top(): This operation will return the last inserted element that is at the top without
removing it.
• size(): This operation will return the size of the stack i.e. the total number of elements
present in the stack.
• isEmpty(): This operation indicates whether the stack is empty or not.
4. Queue:
Like Stack, Queue is a linear structure which follows a particular order in which the operations are
performed. The order is First In First Out (FIFO). In the queue, items are inserted at one end and
deleted from the other end. A good example of the queue is any queue of consumers for a resource
where the consumer that came first is served first. The difference between stacks and queues is in
removing. In a stack we remove the item the most recently added; in a queue, we remove the item the
least recently added.
Prepared by: Department of CSE (Brainware University, Barasat)
BRAINWARE UNIVERSITY
CLASS NOTES DATA STRUCTURE & ALGORITHMS
Queue Data Structure
Queue Operations:
• Enqueue(): Adds (or stores) an element to the end of the queue..
• Dequeue(): Removal of elements from the queue.
• Peek() or front(): Acquires the data element available at the front node of the queue
without deleting it.
• rear(): This operation returns the element at the rear end without removing it.
• isFull(): Validates if the queue is full.
• isNull(): Checks if the queue is empty.
5. Binary Tree:
Unlike Arrays, Linked Lists, Stack and queues, which are linear data structures, trees are hierarchical
data structures. A binary tree is a tree data structure in which each node has at most two children,
which are referred to as the left child and the right child. It is implemented mainly using Links.
A Binary Tree is represented by a pointer to the topmost node in the tree. If the tree is empty, then
the value of root is NULL. A Binary Tree node contains the following parts.
1. Data
2. Pointer to left child
3. Pointer to the right child
Prepared by: Department of CSE (Brainware University, Barasat)
BRAINWARE UNIVERSITY
CLASS NOTES DATA STRUCTURE & ALGORITHMS
Binary Tree Data Structure
6. Binary Search Tree:
A Binary Search Tree is a Binary Tree following the additional properties:
• The left part of the root node contains keys less than the root node key.
• The right part of the root node contains keys greater than the root node key.
• There is no duplicate key present in the binary tree.
A Binary tree having the following properties is known as Binary search tree (BST).
Binary Search Tree Data Structure
Prepared by: Department of CSE (Brainware University, Barasat)
BRAINWARE UNIVERSITY
CLASS NOTES DATA STRUCTURE & ALGORITHMS
7. Heap:
A Heap is a special Tree-based data structure in which the tree is a complete binary tree. Generally,
Heaps can be of two types:
• Max-Heap: In a Max-Heap the key present at the root node must be greatest among the
keys present at all of its children. The same property must be recursively true for all sub-
trees in that Binary Tree.
• Min-Heap: In a Min-Heap the key present at the root node must be minimum among the
keys present at all of its children. The same property must be recursively true for all sub-
trees in that Binary Tree.
Max and Min Heap
8. Hashing Data Structure:
Hashing is an important Data Structure which is designed to use a special function called the Hash
function which is used to map a given value with a particular key for faster access of elements. The
efficiency of mapping depends on the efficiency of the hash function used.
Let a hash function H(x) maps the value x at the index x%10 in an Array. For example, if the list of
values is [11, 12, 13, 14, 15] it will be stored at positions {1, 2, 3, 4, 5} in the array or Hash table
respectively.
Hash Data Structure
Prepared by: Department of CSE (Brainware University, Barasat)
BRAINWARE UNIVERSITY
CLASS NOTES DATA STRUCTURE & ALGORITHMS
11. Graph:
Graph is a data structure that consists of a collection of nodes (vertices) connected by edges. Graphs
are used to represent relationships between objects and are widely used in computer science,
mathematics, and other fields. Graphs can be used to model a wide variety of real-world systems,
such as social networks, transportation networks, and computer networks.
Graph Data Structure
Applications of Data Structures:
Data structures are used in various fields such as:
• Operating system
• Graphics
• Computer Design
• Blockchain
• Genetics
• Image Processing
• Simulation,
• etc.
Design a data structure that supports insert, delete, search
Design a data structure that supports the following operations in O(1) time.
• insert(x): Inserts an item x to the data structure if not already present.
• remove(x): Removes item x from the data structure if present.
• search(x): Searches an item x in the data structure.
Following are detailed operations.
• insert(x)
1. Check if x is already present by doing a hash map lookup.
2. If not present, then insert it at the end of the array.
3. In the hash table, x is added as the key, and the last array index as the index.
• remove(x)
1. Check if x is present by doing a hash map lookup.
2. If present, then find its index and remove it from a hash map.
Prepared by: Department of CSE (Brainware University, Barasat)
BRAINWARE UNIVERSITY
CLASS NOTES DATA STRUCTURE & ALGORITHMS
3. Swap the last element with this element in an array and remove the last
element.
Swapping is done because the last element can be removed in O(1) time.
4. Update index of the last element in a hash map.
• search(x)
1. Do a lookup for x in hash map.
What is an Algorithm?
An algorithm is a process or a set of rules required to perform calculations or some other
problem-solving operations especially by a computer. The formal definition of an algorithm is
that it contains the finite set of instructions which are being carried in a specific order to
perform the specific task. It is not the complete program or code; it is just a solution (logic) of
a problem, which can be represented either as an informal description using a Flowchart or
Pseudocode.
Characteristics of an Algorithm
The following are the characteristics of an algorithm:
o Input: An algorithm has some input values. We can pass 0 or some input value to an algorithm.
o Output: We will get 1 or more output at the end of an algorithm.
o Unambiguity: An algorithm should be unambiguous which means that the instructions in an
algorithm should be clear and simple.
o Finiteness: An algorithm should have finiteness. Here, finiteness means that the algorithm
should contain a limited number of instructions, i.e., the instructions should be countable.
o Effectiveness: An algorithm should be effective as each instruction in an algorithm affects the
overall process.
o Language independent: An algorithm must be language-independent so that the instructions
in an algorithm can be implemented in any of the languages with the same output.
Dataflow of an Algorithm
o Problem: A problem can be a real-world problem or any instance from the real-world problem
for which we need to create a program or the set of instructions. The set of instructions is known
as an algorithm.
o Algorithm: An algorithm will be designed for a problem which is a step by step procedure.
Prepared by: Department of CSE (Brainware University, Barasat)
BRAINWARE UNIVERSITY
CLASS NOTES DATA STRUCTURE & ALGORITHMS
o Input: After designing an algorithm, the required and the desired inputs are provided to the
algorithm.
o Processing unit: The input will be given to the processing unit, and the processing unit will
produce the desired output.
o Output: The output is the outcome or the result of the program.
Why do we need Algorithms?
We need algorithms because of the following reasons:
o Scalability: It helps us to understand the scalability. When we have a big real-world problem,
we need to scale it down into small-small steps to easily analyze the problem.
o Performance: The real-world is not easily broken down into smaller steps. If the problem can
be easily broken into smaller steps means that the problem is feasible.
Importance of Algorithms
1. Theoretical importance: When any real-world problem is given to us and we break the
problem into small-small modules. To break down the problem, we should know all the
theoretical aspects.
2. Practical importance: As we know that theory cannot be completed without the practical
implementation. So, the importance of algorithm can be considered as both theoretical and
practical.
Algorithm Analysis
The algorithm can be analyzed in two levels, i.e., first is before creating the algorithm, and
second is after creating the algorithm. The following are the two analysis of an algorithm:
o Priori Analysis: Here, priori analysis is the theoretical analysis of an algorithm which is done
before implementing the algorithm. Various factors can be considered before implementing the
algorithm like processor speed, which has no effect on the implementation part.
o Posterior Analysis: Here, posterior analysis is a practical analysis of an algorithm. The practical
analysis is achieved by implementing the algorithm using any programming language. This
analysis basically evaluate that how much running time and space taken by the algorithm.
Prepared by: Department of CSE (Brainware University, Barasat)
BRAINWARE UNIVERSITY
CLASS NOTES DATA STRUCTURE & ALGORITHMS
Algorithm Complexity
The performance of the algorithm can be measured in two factors:
o Time complexity: The time complexity of an algorithm is the amount of time required to
complete the execution. The time complexity of an algorithm is denoted by the big O notation.
Here, big O notation is the asymptotic notation to represent the time complexity. The time
complexity is mainly calculated by counting the number of steps to finish the execution. Let's
understand the time complexity through an example.
o Space complexity: An algorithm's space complexity is the amount of space required to
solve a problem and produce an output. Similar to the time complexity, space
complexity is also expressed in big O notation.
The Need for Analysis
In this chapter, we will discuss the need for analysis of algorithms and how to choose a better
algorithm for a particular problem as one computational problem can be solved by different
algorithms.
By considering an algorithm for a specific problem, we can begin to develop pattern recognition
so that similar types of problems can be solved by the help of this algorithm.
Algorithms are often quite different from one another, though the objective of these algorithms
are the same. For example, we know that a set of numbers can be sorted using different
algorithms. Number of comparisons performed by one algorithm may vary with others for the
same input. Hence, time complexity of those algorithms may differ. At the same time, we need to
calculate the memory space required by each algorithm.
Analysis of algorithm is the process of analyzing the problem-solving capability of the algorithm
in terms of the time and size required (the size of memory for storage while implementation).
However, the main concern of analysis of algorithms is the required time or performance.
Generally, we perform the following types of analysis −
• Worst-case − The maximum number of steps taken on any instance of size a.
• Best-case − The minimum number of steps taken on any instance of size a.
• Average case − An average number of steps taken on any instance of size a.
Asymptotic Analysis of algorithms (Growth of function)
Resources for an algorithm are usually expressed as a function regarding input. Often this
function is messy and complicated to work. To study Function growth efficiently, we reduce
the function down to the important part.
Let f (n) = an2+bn+c
Prepared by: Department of CSE (Brainware University, Barasat)
BRAINWARE UNIVERSITY
CLASS NOTES DATA STRUCTURE & ALGORITHMS
2
In this function, the n term dominates the function that is when n gets sufficiently large.
Dominate terms are what we are interested in reducing a function, in this; we ignore all
constants and coefficient and look at the highest order term concerning n.
Asymptotic notation:
The word Asymptotic means approaching a value or curve arbitrarily closely (i.e., as some
sort of limit is taken).
Asymptotic analysis
It is a technique of representing limiting behavior. The methodology has the applications across
science. It can be used to analyze the performance of an algorithm for some large data set.
1. In computer science in the analysis of algorithms, considering the performance of algorithms
when applied to very large input datasets
The simplest example is a function ƒ (n) = n2+3n, the term 3n becomes insignificant compared
to n2 when n is very large. The function "ƒ (n) is said to be asymptotically
equivalent to n2 as n → ∞", and here is written symbolically as ƒ (n) ~ n2.
Asymptotic notations are used to write fastest and slowest possible running time for an
algorithm. These are also referred to as 'best case' and 'worst case' scenarios respectively.
"In asymptotic notations, we derive the complexity concerning the size of the input. (Example
in terms of n)"
"These notations are important because without expanding the cost of running the algorithm,
we can estimate the complexity of the algorithms."
Why is Asymptotic Notation Important?
1. They give simple characteristics of an algorithm's efficiency.
2. They allow the comparisons of the performances of various algorithms.
Asymptotic Notations:
Asymptotic Notation is a way of comparing function that ignores constant factors and small
input sizes. Three notations are used to calculate the running time complexity of an algorithm:
1. Big-oh notation: Big-oh is the formal method of expressing the upper bound of an
algorithm's running time. It is the measure of the longest amount of time. The function f (n) =
Prepared by: Department of CSE (Brainware University, Barasat)
BRAINWARE UNIVERSITY
CLASS NOTES DATA STRUCTURE & ALGORITHMS
O (g (n)) [read as "f of n is big-oh of g of n"] if and only if exist positive constant c and such
that
1. f (n) ⩽ k.g (n)f(n)⩽k.g(n) for n>n0n>n0 in all case
Hence, function g (n) is an upper bound for function f (n), as g (n) grows faster than f (n)
For Example:
1. 1. 3n+2=O(n) as 3n+2≤4n for all n≥2
2. 2. 3n+3=O(n) as 3n+3≤4n for all n≥3
Hence, the complexity of f(n) can be represented as O (g (n))
2. Omega () Notation: The function f (n) = Ω (g (n)) [read as "f of n is omega of g of n"] if
and only if there exists positive constant c and n0 such that
F (n) ≥ k* g (n) for all n, n≥ n0
For Example:
f (n) =8n2+2n-3≥8n2-3
=7n2+(n2-3)≥7n2 (g(n))
Prepared by: Department of CSE (Brainware University, Barasat)
BRAINWARE UNIVERSITY
CLASS NOTES DATA STRUCTURE & ALGORITHMS
Thus, k1=7
Hence, the complexity of f (n) can be represented as Ω (g (n))
3. Theta (θ): The function f (n) = θ (g (n)) [read as "f is the theta of g of n"] if and only if there
exists positive constant k1, k2 and k0 such that
k1 * g (n) ≤ f(n)≤ k2 g(n)for all n, n≥ n0
For Example:
3n+2= θ (n) as 3n+2≥3n and 3n+2≤ 4n, for n
k1=3,k2=4, and n0=2
Hence, the complexity of f (n) can be represented as θ (g(n)).
The Theta Notation is more precise than both the big-oh and Omega notation. The function f
(n) = θ (g (n)) if g(n) is both an upper and lower bound.
Prepared by: Department of CSE (Brainware University, Barasat)