2. What is a Data Structure?
Data structure is a way of storing and organizing data ef f
i
ciently
such that the required operations on them can be performed be
efficient with respect to time as well as memory.
Simply, Data Structure are used to reduce complexity (mostly the
time complexity) of the code. Data structures can be two types :
1. Static Data Structure
2. Dynamic Data Structure
3. What is an Abstract Data Type?
Abstract Data Type (ADT) -
1) An opportunity for an acronym
2) Mathematical description of an object and the set of operations on the object
4. Data Structures : Algorithms
◦
◦
Algorithm
A high level, language independent description of a step-by-step process for solving a problem
Data Structure
A set of algorithms which implement an ADT
5. Why so many data structures?
◦
◦
◦
◦
Ideal data structure:
fast, elegant, memory efficient
Generates tensions:
timevs. space
performancevs. elegance
generalityvs. simplicity
one operation’s performancevs.
another’s
◦
◦
◦
◦
◦
◦
Dictionary ADT
list
binary search tree
AVL tree
Splay tree
Red-Black tree
hash table
6. Code Implementation
◦
◦
◦
◦
◦
Theoretically
abstract base class describes ADT
inherited implementations implement data structures
can change data structures transparently (to client code)
Practice
different implementations sometimes suggest different interfaces
(generality
vs . simplicity)
performance of a data structure may influence form of client code
(time
vs . space, one operation
vs . another)
7. ADT Presentation Algorithm
◦
◦
◦
◦
◦
◦
◦
Present an ADT
Motivate with some applications
Repeat until browned entirely through
develop a data structure for the ADT
analyze its properties
efficiency
correctness
limitations
ease of programming
Contrast data structure’s strengths and weaknesses
understand when to use each one
8. Static and Dynamic Data Structure
•
•
Static Data Structure vs Dynamic Data Structure
Static data structures, such as arrays, have a fixed size
and are allocated at compile-time.
Dynamic data structures, on the other hand, have a
variable size and are allocated at run-time.
9. Static Data Structure
In Static data structure the size of the structure is fixed. The content of
the data structure can be modified but without changing the memory
space allocated to it.
Example of Static Data Structures: Array
10. Dynamic Data Structure
In Dynamic data structure the size of the structure is not fixed and can be
modified during the operations performed on it. Dynamic data structures
are designed to facilitate change of data structures in the run time.
Example of Dynamic Data Structures: Linked List
11. Static and Dynamic Data Structure
Aspect Static Data Structure Dynamic Data Structure
Memory allocation
Memory is allocated at compile-
time
Memory is allocated at run-time
Size
Size is fixed and cannot be
modified
Size can be modified during
runtime
Memory utilization
Memory utilization may be
inefficient
Memory utilization is efficient as
memory can be reused
Access Access time is faster as it is fixed
Access time may be slower due
to indexing and pointer usage
Examples
Arrays, Stacks, Queues, Trees
(with fixed size)
Lists, Trees (with variable size),
Hash tables
12. Operations on Data Structures
There are different types of operations that can be performed for the
manipulation of data in every data structure. Some operations are
explained and illustrated below:
Traversing
Searching
Insertion
Deletion
Other methods for operations.
Create, select, update, sort, merge, split data
13. Traversing Data Structure
Traversing a Data Structure means to visit the element stored in it. It
visits data in a systematic manner. This can be done with any type of DS.
int main()
{
// Initialise array
int arr[] = { 1, 2, 3, 4 };
// size of array
int N = sizeof(arr) / sizeof(arr[0]);
// Traverse the element of arr[]
for (int i = 0; i N; i++) {
// Print the element
cout arr[i] ' ';
}
return 0;
}
14. Searching Data Structure
Searching means to find a particular element in the given data-structure.
It is considered as successful when the required element is found.
Searching is the operation which we can performed on data-structures
like array, linked-list, tree, graph, etc.
for (int i = 0; i N; i++) {
// If Element is present then
// print the index and return
if (arr[i] == K) {
cout Element found!;
return;
}
}
cout Element Not found!;
}
15. Insertion Data Structure
It is the operation which we apply on all the data-structures.
Insertion means to add an element in the given data structure.
The operation of insertion is successful when the required
element is added to the required data-structure.
It is unsuccessful in some cases when the size of the data
structure is full and when there is no space in the data-structure
to add any additional element.
The insertion has the same name as an insertion in the data-
structure as an array, linked-list, graph, tree. In stack, this
operation is called Push. In the queue, this operation is called
Enqueue.
16. Insertion Data Structure
int main()
{
// Initialise array
int arr[4];
// size of array
int N = 4;
// Insert elements in array
for (int i = 1; i 5; i++) {
arr[i - 1] = i;
}
// Print array element
printArray(arr, N);
return 0;
}
17. Deletion Data Structure
It is the operation which we apply on all the data-structures.
Deletion means to delete an element in the given data structure.
The operation of deletion is successful when the required
element is deleted from the data structure.
The deletion has the same name as a deletion in the data-
structure as an array, linked-list, graph, tree, etc.
In stack, this operation is called Pop. In Queue this operation is
called Dequeue.
18. Other methods for Data Structures
Create
Select
Update
Sort
Merge
Split
19. Types of Data Structures
Linear
Data structure where data elements are arranged
sequentially or linearly where each and every element is
attached to its previous and next adjacent
Non Linear
Data structures where data elements are not arranged
sequentially or linearly are called non-linear data structures
20. Linear Data Structures
Array
The array is a type of data structure that stores elements of the same type. These are the
most basic and fundamental data structures
Stack
The data structure follows the rule of LIFO (Last In-First Out) where the data last added
element is removed first.
Queue
This structure is almost similar to the stack as the data is stored sequentially. The difference
is that the queue data structure follows FIFO which is the rule of First In-First Out
Linked List
Linked lists are the types where the data is stored in the form of nodes which consist of an
element of data and a pointer.
21. Non Linear Data Structure
Trees
A tree data structure consists of various nodes linked together. The structure of a tree is
hierarchical that forms a relationship like that of the parent and a child.
Graphs
Graphs are those types of non-linear data structures which consist of a definite quantity
of vertices and edges.
22. Linear and Non Linear Data
Structures
Linear Data Structures Non Linear Data Structures
1.
In a linear data structure, data elements are
arranged in a linear order where each and every
element is attached to its previous and next
adjacent.
In a non-linear data structure, data elements
are attached in hierarchically manner.
2. In linear data structure, single level is involved.
Whereas in non-linear data structure, multiple
levels are involved.
3.
Its implementation is easy in comparison to non-
linear data structure.
While its implementation is complex in
comparison to linear data structure.
4.
In linear data structure, data elements can be
traversed in a single run only.
While in non-linear data structure, data
elements can’t be traversed in a single run only.
23. Linear Data Structure Non Linear Data Structure
5.
In a linear data structure, memory is not
utilized in an efficient way.
While in a non-linear data structure, memory is
utilized in an efficient way.
6.
Its examples are: array, stack, queue, linked
list, etc.
While its examples are: trees and graphs.
7.
Applications of linear data structures are
mainly in application software development.
Applications of non-linear data structures are in
Artificial Intelligence and image processing.
8.
Linear data structures are useful for simple
data storage and manipulation.
Non-linear data structures are useful for
representing complex relationships and data
hierarchies, such as in social networks, file
systems, or computer networks.
9.
Performance is usually good for simple
operations like adding or removing at the
ends, but slower for operations like searching
or removing elements in the middle.
Performance can vary depending on the
structure and the operation, but can be
optimized for specific operations.
24. Data Structure and Data Type
Data Type is the kind or form of a variable which is being used
throughout the program. It defines that the particular variable
will assign the values of the given data type only
Data Structure is the collection of different kinds of data. That
entire data can be represented using an object and can be used
throughout the entire program.
25. Data Structure and a Data Type
Data Types Data Structures
Implementation through Data Types is a form of
abstract implementation
Implementation through Data Structures is called
concrete implementation
Can hold values and not data, so it is data less
Can hold different kind and types of data within one
single object
Values can directly be assigned to the data type
variables
The data is assigned to the data structure object
using some set of algorithms and operations like
push, pop and so on.
No problem of time complexity
Time complexity comes into play when working with
data structures
Examples: int, float, double Examples: stacks, queues, tree
26. How to analyze the programs
There are many criteria upon which we can judge a program, for instance:
(i) Does it do what we want it to do?
(ii) Does it work correctly according to the original specifications of the
task?
(iii) Is there documentation which describes how to use it and how it
works?
(iv) Are subroutines created in such a way that they perform logical sub-
functions?
(v) Is the code readable?
27. Introduction to algorithm
An algorithm is any well-defined computational procedure
that takes some value, or set of values, as input and
produces some value, or set of values, as output.
An algorithm as a tool for solving a well-specified
computational problem
28. Asymptotic Notation
1.
2.
3.
The main idea of asymptotic analysis is to have a measure of
the efficiency of algorithms that don’t depend on machine-
specific constants and don’t require algorithms to be
implemented and time taken by programs to be compared.
There are mainly three asymptotic notations:
Big-O Notation (O-notation)
Omega Notation (Ω-notation)
Theta Notation (Θ-notation)
29. Big O Notation
Big-O notation represents the upper
bound of the running time of an algorithm.
Therefore, it gives the worst-case
complexity of an algorithm.
If f(n) describes the running time of an
algorithm, f(n) is O(g(n)) if there exist a
positive constant C and n0 such that, 0 ≤
f(n) ≤ cg(n) for all n ≥ n0
It returns the highest possible output
value (big-O)for a given input.
30. Omega Notation
Omega notation represents the lower
bound of the running time of an
algorithm. Thus, it provides the best
case complexity of an algorithm.
The execution time serves as a lower
bound on the algorithm’s time
complexity.
It is defined as the condition that
allows an algorithm to complete
statement execution in the shortest
amount of time.
31. Theta Notation
Theta notation encloses the function
fro m abo v e and be l o w. Si nc e i t
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
l o w e r a n d u p p e r b o u n d o n t h e
algorithm’s time complexity.
It exist as both, most, and least
boundaries for a given input value.
32. Time Complexity and Space
Complexity
Time Complexity:
The time complexity of an algorithm quantifies the amount of time taken
by an algorithm to run as a function of the length of the input
Space Complexity:
Problem-solving using computer requires memory to hold temporary data
or final result while the program is in execution. The amount of memory
required by the algorithm to solve given problem is called space
complexity of the algorithm.
33. Analysis of Algorithm
Time Efficiency — Indicates how fasts algorithm runs
Space Efficiency — How much extra memory the algorithm
needs to complete its execution
Simplicity — Generating sequence of instructions which are
easy to understand
Generality — Range of inputs it can accept .