DSA - Module 1
DSA - Module 1
23EC29 23EC28
Data Structure & Algorithm
Module 1: Introduction to Data Structures
2. The array size is fixed and 2. The array size is not fixed and
cannot be changed. can be changed.
Example: Given the base address of an array A[1300 ............1900] as 1020 and the size of
each element is 2 bytes in the memory, find the address of A[1700].
Solution:
Given:
Base address B = 1020
Lower Limit/Lower Bound of subscript LB = 1300
Storage size of one element store in any array W = 2 Byte
Subset of element whose address to be found I = 1700
Formula used:
Address of A[I] = B+W (1 - LB)
Solution:
Address of A[1700] = 1020 + 2 * (1700 - 1300)
= 1020 + 2 * (400)
= 1020 + 800
Address of A[1700] = 1820
Application of Stack:
A stack is a data structure that uses LIFO order.
Some Applications of a stack are:
• Converting infix to postfix expressions.
• Undo/Redo button/operation in word processors.
• Syntaxes in languages are parsed using stacks.
• It is used in many virtual machines like JVM
• Forward-backward surfing in the browser.
• History of visited websites.
• Message logs and all messages you get are arranged in a stack.
• Call logs, E-mails, Google photos' any gallery, YouTube downloads, Notifications (latest
appears first).
• Scratch card's earned after Google pay transaction.
Application of Queue:
A queue is a data structure that uses FIFO order.
Some applications of a queue are:
• Operating System uses queues for job scheduling.
• To handle congestion in the networking queue can be used.
• Data packets in communication are arranged in queue format.
• Sending an e-mail, it will be queued.
• Server while responding to request
• Uploading and downloading photos, first kept for uploading/downloading will be
completed first (Not if there is threading)
• Most internet requests and processes use queue.
• While switching multiple applications, windows use circular queue.
• In Escalators, Printer spooler, Car washes queue.
• A circular queue is used to maintain the playing sequence of multiple players in a game.
Application of Graph:
Graph is a data structure where data is stored in a collection of interconnected vertices
(nodes) and edges (paths).
Some applications of a graph are:
• Facebook's Graph API uses the structure of Graphs.
• Google's Knowledge Graph also has to do something with Graph.
• Dijkstra algorithm or the shortest path first algorithm also uses graph structure to find the
smallest path between the nodes of the graph.
• The GPS navigation system also uses shortest path APIs.
• Networking components have a huge application for graph
• Facebook, Instagram, and all social media networking sites every user is Node
• Data organization
• React's virtual DOM uses graph data structures.
• MS Excel uses DAC (Directed Acyclic Graphs)
Application of Tree:
Trees are hierarchical structures having a single root node.
Some applications of the trees are:
• XML Parser uses tree algorithms.
• The decision-based algorithm is used in machine learning which works upon the
algorithm of the tree.
• Databases also use tree data structures for indexing.
• Domain Name Server(DNS) also uses tree structures.
• File explorer/my computer of mobile/any computer
• BST used in computer Graphics
• Posting questions on websites like Quora, the comments are a child of questions.
• Parsers(XML parser).
• Code Compression(zip).
• DOM in Html.
• Evaluate an expression (i.e., parse).
• Integral to compilers/automata theory.
• To store the possible moves in a chess game.
disadvantages
• Memory access: Some complex data structures can have slow memory access.
• Developer experience: Working with complex data structures requires good
programming skills and experience.
• Fixed size: Arrays have a fixed size, which can limit their use.
BIG O NOTATION
Big O notation is used to measure the performance of any algorithm by providing the order of
growth of the function.
It gives the upper bound on a function by which we can make sure that the function will never
grow faster than this upper bound.
We want the approximate runtime of the operations performed on data structures.
We are not interested in the exact runtime.
Big O notation will help us to achieve the same.
3. Theta Notation
Theta notation encloses the function from above and below. Since it represents the upper and
the lower bound of the running time of an algorithm, it is used for analyzing the average-case
complexity of an algorithm. The execution time serves as both a lower and upper bound on the
algorithm's time complexity. It exists as both, the most, and least boundaries for a given input
value.
4. Little o asymptotic notation
Big-O is used as a tight upper bound on the growth of an algorithm's effort (this effort is
described by the function f(n)), even though, as written, it can also be a loose upper bound.
"Little-o" (o()) notation is used to describe an upper bound that cannot be tight.
1. Time Complexity.
The time complexity of an algorithm is defined as the amount of time taken by an algorithm to
run as a function of the length of the input. Note that the time to run is a function of the length
of the input and not the actual execution time of the machine on which the algorithm is running
on
How is Time complexity computed?
To estimate the time complexity, we need to consider the cost of each fundamental instruction
and the number of times the instruction is executed.
• If we have statements with basic operations like comparisons, return statements,
assignments, and reading a variable. We can assume they take constant time each O(1).
Statement 1: int a=5;
// reading a variable
statement 2; if( a==5) return
true; // return statement
statement 3; int x= 4>5 ? 1:0;
// comparison
statement 4; bool flag=true;
// Assignment
T(n) = t(statement1) +
t(statement2) + ... +
t(statementN);
For the above example, the loop will execute n times, and it will print "GeeksForGeeks" N
number of times. so the time taken to run this program is:
For 2D arrays, we would have nested loop concepts, which means a loop inside a loop.
For the above example, the cout statement will execute n*m times, and it will print
"GeeksForGeeks" N*M number of times. so the time taken to run this program is:
However, just because you have n calls total doesn't mean it takes O(n) space.