0% found this document useful (0 votes)
69 views68 pages

DSA-Module 1-Lecture 1-24-25

Uploaded by

yash.17896
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views68 pages

DSA-Module 1-Lecture 1-24-25

Uploaded by

yash.17896
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 68

DATA STRUCTURES AND

ANALYSIS

Ms. Minakshi Retharekar


DEPT OF IT
SHAH AND ANCHOR KUTCHHI
ENGINEERING COLLEGE, CHEMBUR
COURSE OBJECTIVES
1. Classify and apply the concepts of stacks, queues,
and linked lists in real life problem solving.
2. Understand the different methods for analysis of
algorithms.
3. Classify, apply, and analyze the concepts of trees &
graphs in real life problem solving.
4. List and examine the concepts of sorting, searching
techniques in real life problem solving.
MODULE I

Linear Data Structures


CONTENTS
❏ Introduction to Data Structures: Linear and
Non-Linear Data Structures, Static and
Dynamic Data Structures.
❏ Introduction to Stack: Push & Pop Operations
on Stack
❏ Introduction to Queue: Linear Queue, Circular
Queue, Priority Queue, Operation on Queue.
❏ Introduction to Linked List: Singly Linked List,
Circular Linked List, Doubly Linked List,
Operation On different types of Linked List.
INTRODUCTION

What is Data?
What is Algorithm?
Data structure affects the design of both structural &
functional aspects of a program.
Program=algorithm + Data Structure
You know that a algorithm is a step by step procedure to solve
a particular function.
Introduction to Data Structure
The name "Data Structure" is a combination of two words: "Data" and
"Structure". Let's go over each word individually:

● Data: Data is set of values that must be processed and stored in a


computer. Data can be anything from numbers to characters to dates to
photos to videos.
● Structure: Structure refers to how data is structured. It specifies the
relationship between various data elements and how they are kept in
memory. The structure has an impact on the efficiency of data operations.

ELEMENTARY DATA ORGANIZATION
? Data:set of values
? Elementary Data Items:
Who don't have sub-ordinate
E.g. Roll_no.
? Group Item:Composed of one or more sub-items
? e.g. Student Name:First, Middle, Last
? Record- Collection of data items
e.g. name, address, course, marks
? File- A collection of related records
e.g. Data of 60 students
Why we should learn Data Structures?

There are various reasons why learning data structures is important:


● Problem Solving: Data structures are an important tool for handling many
real-world problems. Understanding data structures can help you build more
efficient and effective programmes.
● Algorithm Design: To store and modify data, many algorithms rely on data
structures. A solid understanding of data structures is required for the
development of effective algorithms.
● Requirements for the Job: Data structure knowledge is frequently
required for software engineering and computer science employment.
Candidates with a thorough understanding of data structures and algorithms
are highly valued by employers.
● Competitive Programming: Data structure knowledge is essential for
competitive programming and coding competitions.
● Better Understanding of Computer Science: Data structures are a
fundamental concept in computer science and understanding them can
deepen your overall understanding of the field.
What is the need of Data Structure?

We need data structures because they provide efficient solutions to organizing and
manipulating data. They help to store and retrieve data in an organized manner, making it
easier to manage and analyze the information. Some specific reasons why data structures are
important include:

1. Improved Time Complexity: Using appropriate data structures can lead to better time
complexity, making it possible to solve problems more quickly. For example, searching
for an element in a sorted array is faster than searching for it in an unsorted array.
2. Better Space Complexity: Data structures can help to reduce the amount of memory
needed to store data. For example, using a linked list instead of an array can reduce the
amount of memory needed to store the same data.
3. Efficient Data Retrieval: Data structures make it easier to retrieve specific data
efficiently. For example, a hash table can retrieve data in constant time, while searching
through an unsorted array takes linear time.
4. Better Data Management: Data structures make it easier to manage and manipulate
data. For example, a stack can be used to implement an undo functionality in an
application.
5. Solving Complex Problems: Data structures can provide the foundation for efficient
algorithms, making it possible to solve complex problems in a reasonable amount of
time. For example, graph algorithms can be used to find the shortest path between two
points or to find the minimum spanning tree of a graph.
Characteristics of Data Structure

The following are some of the main characteristics of data structures:

1. Representation of Data: Data structures define a way of representing data in a


computer's memory, making it possible to store, manipulate, and access data
efficiently.
2. Access Techniques: Different data structures provide different techniques for
accessing data stored within them, such as random access or sequential access.
3. Storage Organization: Data structures define the organization of data in memory,
such as linear or hierarchical organization.
4. Insertion and Deletion Operations: Different data structures support different
methods for adding and removing elements, such as insertion at the end or deletion
from the front.
5. Time and Space Complexity: Data structures can have different time and space
complexities, depending on the operations they support and the way they organize
data.
6. Adaptability: Some data structures are more adaptable to certain types of data and
operations than others. For example, a stack is more suitable for problems that require
Last-In-First-Out (LIFO) behavior, while a queue is better suited for problems that
require First-In-First-Out (FIFO) behavior.
7. Flexibility: Different data structures have different degrees of flexibility, such as the
ability to dynamically grow or shrink in size, or the ability to efficiently insert or delete
elements in the middle.
Advantages of Data Structure

The following are some of the main advantages of using data structures:

1. Better Data Organization: Data structures provide a way of organizing


data in a meaningful and efficient manner, making it easier to access and
manipulate data.
2. Increased Data Retrieval Efficiency: Data structures can provide fast and
efficient retrieval of data, which is essential in many real-world applications.
3. Efficient Data Manipulation: Data structures can provide efficient methods
for adding, deleting, and modifying data, which is important in dynamic
applications.
4. Improved Code Reusability: Reusable data structures can be used in
many different applications, reducing the time and effort required to write
and maintain code.
5. Better Problem-Solving Capability: Data structures provide a way of
modeling real-world problems and solving them in a more efficient and
elegant manner.
6. Reduced Memory Requirements: Data structures can be designed to use
memory more efficiently, reducing the overall memory requirements of an
application.
7. Increased Data Security: Data structures can be designed to provide
INTRODUCTION
That means, algorithm is a set of instruction written to carry
out certain tasks & the data structure is the way of
organizing the data with their logical relationship retained.
To develop a program of an algorithm, we should select an
appropriate data structure for that algorithm.
Therefore algorithm and its associated data structures from a
program.
DEFINITION

Data structure is representation of the logical relationship


existing between individual elements of data.
In other words, a data structure is a way of organizing all data
items that considers not only the elements stored but also
their relationship to each other.
CLASSIFICATION OF DATA
STRUCTURE
Data structure are normally divided into two broad
categories:
⚫ Primitive Data Structure

⚫ Non-Primitive Data Structure

The main difference between primitive and


non-primitive data structures is that Primitive data
structures are considered as the fundamental data
structure while non-primitive data structures are
defined by the user with the help of primitive data
structures.
CLASSIFICATION OF DATA
STRUCTURE
CLASSIFICATION OF DATA
STRUCTURE
PRIMITIVE DATA
STRUCTURE
There are basic structures and directly operated upon by the
machine instructions.
In general, there are different representation on different
computers.
Integer, Floating-point number, Character constants, string
constants, pointers etc, fall in this category.
example.
A variable with the data type integer can allow storing the
values of integer type only, a variable with the float data
type can allow storing the values of float data type and the
variable with the character data type allows storing
character values only.
NON-PRIMITIVE DATA
STRUCTURE
There are more sophisticated data structures.
These are derived from the primitive data structures.
The non-primitive data structures emphasize on
structuring of a group of homogeneous (same type) or
heterogeneous (different type) data items.
Lists, Stack, Queue, Tree, Graph are example of
non-primitive data structures.
The design of an efficient data structure must take
operations to be performed on the data structure.
NON-PRIMITIVE DATA
STRUCTURE
The non-primitive data structure is further classified into two
parts i.e. linear data structure and non-linear data structure.
Linear data structures are considered sequential data structures
and sequential data structures store elements in a sequence in the
memory.
For instance, the second element of the data structure is stored
after the first element and then after the second element, the third
element of the data structure is stored and the fourth element is
stored after the third and so on.
Eg. Linked List, Array, and Stack
The non-linear data structure is considered as a random type of
data structure.
Eg. Graphs and trees.
LINEAR AND NON-LINEAR DATA STRUCTURES
STATIC AND DYNAMIC DATA STRUCTURES
STATIC AND DYNAMIC DATA STRUCTURES
Static Data Structure Dynamic Data Structure

Memory allocation Memory is allocated at Memory is allocated at


compile-time run-time

Size Size is fixed and cannot be Size can be modified during


modified runtime

Memory utilization Memory utilization may be Memory utilization is efficient


inefficient as memory can be reused

Access Access time is faster as it is Access time may be slower


fixed due to indexing and pointer
usage

Examples Arrays, Stacks, Queues, Trees Lists, Trees (with variable


(with fixed size) size), Hash tables
DESCRIPTION OF VARIOUS
DATA STRUCTURES
DESCRIPTION OF VARIOUS
DATA STRUCTURES :
ARRAYS
An array is defined as a set of finite number of
homogeneous elements or same data items.
It means an array can contain one type of data only,
either all integer, all float-point number or all character.
ARRAYS

Simply, declaration of array is as follows:


int arr[10]
Where int specifies the data type or type of elements arrays
stores.
“arr” is the name of array & the number specified inside the
square brackets is the number of elements an array can store,
this is also called sized or length of array.
ARRAYS
Following are some of the concepts to be remembered
about arrays:
⚫ The individual element of an array can be accessed by
specifying name of the array, following by index or
subscript inside square brackets.
⚫ The first element of the array has index zero[0]. It
means the first element and last element will be
specified as:arr[0] & arr[9]
Respectively.
⚫ Array can always be read or written through loop. If
we read a one-dimensional array it require one loop
for reading and other for writing the array.
ARRAYS
⚫ For example: Reading an array
For(i=0;i<=9;i++)
scanf(“%d”,&arr[i]);
⚫ For example: Writing an array
For(i=0;i<=9;i++)
printf(“%d”,arr[i]);
ARRAYS
⚫ If we are reading or writing
two-dimensional array it would require
two loops. And similarly the array of a
N dimension would required N loops.
⚫ Some common operation performed on
array are:
Creation of an array
Traversing an array
LISTS

A lists (Linear linked list) can be defined as a collection of


variable number of data items.
Lists are the most commonly used non-primitive data
structures.
An element of list must contain at least two fields, one for
storing data or information and other for storing address of
next element.
As you know for storing address we have a special data
structure of list the address must be pointer type.
LISTS
Technically each such element is referred to as a node,
therefore a list can be defined as a collection of nodes as
show bellow:

[Linear Liked List]


LISTS

Types of linked lists:


⚫ Single linked list
⚫ Doubly linked list
⚫ Single circular linked list
⚫ Doubly circular linked list
STACK
A stack is also an ordered collection
of
elements like arrays, but it has a
special feature that deletion and
insertion of elements can be done
only from one end called the top of
the stack (TOP)
Due to this property it is also called
as
last in first out type of data
structure (LIFO).
STACK

It could be through of just like a stack of plates placed on table in


a party, a guest always takes off a fresh plate from the top and the
new plates are placed on to the stack at the top.
It is a non-primitive data structure.
When an element is inserted into a stack or removed from the
stack, its base remains fixed where the top of stack changes.
STACK
Insertion of element into stack is called PUSH and
deletion of element from stack is called POP.
The bellow show figure how the operations take place on
a stack:

PUSH POP

[STACK]
STACK
The stack can be implemented into two ways:
⚫ Using arrays (Static implementation)
⚫ Using pointer (Dynamic
implementation)
QUEUE

Queue are first in first out type of data structure (i.e. FIFO)
In a queue new elements are added to the queue from one end
called REAR end and the element are always removed from
other end called the FRONT end.
The people standing in a railway reservation row are an
example of queue.
QUEUE
Each new person comes and stands at the end of the row
and person getting their reservation confirmed get out of
the row from the front end.
The bellow show figure how the operations take place on
a stack:

10 20 30 40 50

front rear
QUEUE
The queue can be implemented into two ways:
⚫ Using arrays (Static implementation)
⚫ Using pointer (Dynamic
implementation)
TREES
A tree can be defined as finite set of data items (nodes).
Tree is non-linear type of data structure in which data
items are arranged or stored in a sorted sequence.
Tree represent the hierarchical relationship between
various elements.
TREES

In trees:
There is a special data item at the top of hierarchy called the
Root of the tree.
The remaining data items are partitioned into number of
mutually exclusive subset, each of which is itself, a tree
which is called the sub tree.
The tree always grows in length towards bottom in data
structures, unlike natural trees which grows upwards.
TREES
The tree structure organizes the data into branches,
which related the information.

A root

B C

D E F G
GRAPH
Graph is a mathematical non-linear data structure
capable of representing many kind of physical structures.
It has found application in Geography, Chemistry and
Engineering sciences.
Definition: A graph G(V,E) is a set of vertices V and a set
of edges E.
GRAPH
An edge connects a pair of vertices and many have
weight such as length, cost and another measuring
instrument for according the graph.
Vertices on the graph are shown as point or circles and
edges are drawn as arcs or line segment.
GRAPH

Example of graph:

6
v2 v5
v1 v3
10

v1 8 11
15
9 v2
v3 v4 v4

[a] Directed & [b] Undirected Graph


Weighted Graph
GRAPH
Types of Graphs:
⚫ Directed graph
⚫ Undirected graph
⚫ Weighted graph
⚫ Connected graph
⚫ Non-connected graph
OPERATIONS ON DATA STRUCTURES

The most commonly used operation on data structure are


broadly categorized into following types:
⚫ Create
⚫ Selection
⚫ Insertion
⚫ Updating
⚫ Searching
⚫ Sorting
⚫ Merging
⚫ Destroy or Delete
⚫ Traversing
ANALYSIS OF ALGORITHM

? Algorithm is step by step instructions to solve


particular problem.
? It is considered to be an effective procedure for
solving a problem in finite number of steps.
?
DIFFERENT APPROACHES TO
DESIGNING AN ALGORITHM
? Algorithms are used to manipulate the data
contained in data structures.
? When working with data structures, algorithms
are used to perform operations on the stored
data.
? A complex algorithm is often divided into
smaller units called modules.
? This process of dividing an algorithm into
modules is called modularization.
? Advantages of modularization:
⚫ It makes the complex algorithm simpler to design
and implement.
⚫ Each module can be designed independently.
There are two main approaches to design an
algorithm—top-down approach and bottom-up
approach,
TOP DOWN APPROACH
? A top-down design approach starts by dividing
the complex algorithm into one or more
modules.
? Top-down design method is a form of stepwise
refinement where we begin with the topmost
module and incrementally add modules that it
calls.
?
BOTTOM UP APPROACH
? A bottom-up approach is just the reverse of
top-down approach.
? In the bottom-up design, we start with
designing the most basic or concrete modules
and then proceed towards designing higher level
modules.
Analysis of algorithm
Analyzing an algorithm means determining the amount of
resources (such as time and memory) needed to execute it
Complexity:
► Time Complexity: The time complexity of an algorithm is
basically the running time of a program as a function of the
input size.
► Space complexity: The space complexity of an algorithm is
the amount of computer memory that is required during the
program execution as a function of the input size.
What is the Need for Analysis of the Algorithm ?
● By analyzing an algorithm, we get an idea of how the
algorithm will scale with the variation (change) in the input
size.
● It is to compare various algorithms for solving the same
problem. Through this, we can determine which algorithm
will suit best the problem.
● Analysis of the algorithms also helps us in understanding
the space that will be taken by our code.

Asymptotic Notations
Asymptotic notations in general tell us about how good an algorithm
performs when compared to another algorithm.
we cannot simply compare two algorithms directly
the performance of algorithms varies with variations in the
operating systems, the processors, etc
So, even though we've calculated the performance of two algorithms
on a particular system, there are chances that they will perform
differently on different systems.
To tackle the above-stated issues, we use the asymptotic analysis to
compare the space and time complexity of the algorithms
The asymptotic analysis analyzes two algorithms based on their
performance when the input size is varied (increased or decreased).
Algorithm Growth Rate
► We measure the algorithms time requirement as a function
of the problem size.
► Problem size depends on the application
► e.g. no. of elements in the list for sorting

• If problem size is ‘n’-


• Algorithm A may require 5*n2, time
• Algorithm B may require 7*n time

• An algorithms proportional time requirements known as


Growth Rate
Worst case, Average case and Best case Time
Complexity
● Best case:It gives us the lower bound on the running time of the algorithm for any
given input. In simple words, it states that any program will need at least (greater than
or equal to) that time to run
● The best case time or space complexity is often represented in terms of the Big Omega
(Ω) notation.
Example for Best Case Analysis :
Let us take the example of the linear search to calculate the best time complexity. Suppose,
you have an array of numbers and you have to search for a number.
int linear_search(int arr, int l, int target) {
int i;
for (i = 0; i < l; i++) {
if (arr[i] == target) {
return arr[i]
}
}
return -1
}
Worst Case Analysis of Algorithms

The worst-case analysis of algorithms gives us the upper bound on


the running time of the algorithm for any given input.
It states that any program will need maximum that time (less than or
equal to) to run.
For example, suppose we have an algorithm that has the worst-case
time complexity is O(N2)
Then we can say that the program will take a maximum of O(N2)
time to run, for an input of size N it can never take more time than
that to run.
The worst-case time or space complexity is often represented in
terms of the Big Oh (O) notation.
Example
Suppose, this time the element we are trying to find is at the end of
the array. So, we will have to traverse the whole array before finding
the element. Hence, we can say that the worst case for this algorithm
is O(N) itself.
Average Case Analysis of Algorithms

The average case analysis of algorithms takes the sum of


the running time on every possible input, and after that,
it takes the average. So, in this case, the execution time
of the algorithm acts as both the lower and upper bound
The average case time or space complexity is often
represented in terms of the Big theta (Θ), (Θ) notation.
Example
suppose we need to find any element which is present in
the mid of our array.
So, for that, we need to traverse at least the half length of
the array. In other words, it will take us O(n/2) time for
us to traverse the half-length.
Stack
► A stack is called a last-in-first-out (LIFO) collection.
► A stack is a sequence of items that are accessible at only
one end of the sequence called TOP/PEEP
Stack
► PUSH : It is used to insert items into the stack.
► POP: It is used to delete items from stack.
Example
Algorithms of Stack
Algorithm for Insertion in STACK: PUSH
Operation
Algorithm for Deletion from STACK: POP
Operation
EXAMPLE
PEEK OPERATION: DISPLAY TOP ELEMENT
EXAMPLES
The stack contains 10, 20, 22,26,28,30 with 30
being at top of the stack. Show digramatically the
effect of PUSH 46, PUSH 48, POP, POP, POP,
PUSH 82.
EXAMPLES
The five items A, B, C, D and E are pushed in a stack one
after the other starting from A. The stack is popped 4
times and 2 elements J,K, pushed back on the stack. Now
1 item is popped from the stack the popped item is
_________.

You might also like