1. XP
1
DATA STRUCTURE AND ALGORITHM
CHAPTER 1: INTRODUCTION
Presented By:
Er.Ganesh Ram Suwal
BE | ME Computer Engineering
Email: suwalganesh@gmail.com
Lecturer
January 2021
2. XP
2
INTRODUCTION
Fact: Fact is a Universal Truth (either TRUE or FALSE). It is an
occurrence in the world. Fact is a raw data. It is collected from
questionnaires, survey or from data collection process.
Data: It is simply a collection of fact. Data may be a single value or
a set of values. The organization of data leads to the structuring of
data.
Information: When we
process data we get certain
knowledge and that knowledge
is known as an Information.
Data is a bit of information but
not an information itself.
Figure 1.1: Data and Information
3. XP
3
DATA STRUCTURE
Data Structure is defined as it is the representation of logical
relationship existing between individual elements of data. The data
structure is a way of organizing all the data items that considers
not only the elements but also their relationship to each other.
So data structures are the building blocks of a program. The
selection of a data structure must focuses on following two things
1. The data structure must reflects the relationship existing
between the data.
2. The data structure must be simple to process data effectively
whenever required
4. XP
4
DATA STRUCTURE
Data Structure specifies four things
1. Organization of Data
2. Accessing methods
3. Degree of associativity
4. Processing for Information
Data structure can be defined as triple {D, F, A}
D is data
F is set of Functions and
A is set of axioms.
Permitted Data values + Operations = Data Type
Organized Data + Allowed Operations = Data Structure
5. XP
5
DATA STRUCTURE - USES
Data Structure is extensively used in
Database Management System (DBMS)
Data Mining and Warehousing
Compiler Design
Network Analysis
Numerical Analysis
Artificial Intelligence
Operating System
Image Processing
Computer Graphics, etc…
6. XP
6
DATA STRUCTURE - CLASSIFICATION
Primitive and Non Primitive
Primitive Data Structure
Non Primitive Data Structure
According to the Nature of Size
Static Data Structure
Dynamic Data Structure
According to its occurrence
Linear Data Structure
Non Linear Data Structure
Homogeneous and Non-Homogeneous
Homogeneous Data Structure
Non Homogeneous (Heterogeneous) Data Structure
7. XP
7
1. Primitive and Non Primitive DATA STRUCTURE
Primitive data structure
Primitive Data Structures are the basic data structure and they are
directly operated by a machine instructions.
The representation of primitive data structure is different on different
computers
character, integer, floating point numbers, strings
Non Primitive data structure
Non Primitive Data Structures are complex data structures that are
derived from the basic data structure
Non-primitive data structure emphasis on structuring the homogeneous
and heterogeneous data elements
Array, List, Files
8. XP
8
Primitive and Non Primitive DATA STRUCTURE
Figure 1.2: Primitive and Non Primitive Data Structure Classification
9. XP
9
2. According to the Nature of Size DATA STRUCTURE
Static Data Structure
The data structure which have a fixed data size is called static data
structure. The size cannot be vary at run time.
An array is used in static data structure
Char name[100] – the array size is 100 so maximum upto 100 characters
can be stored by array variable ‘name’
Dynamic Data Structure
The size of data structures are vary not fixed is called dynamic data
structure. It uses the concept of pointer.
DMA (Dynamic Memory Allocation) is used in dynamic data structure.
Allocation of memory is done whenever the data required and de-
allocation of memory is done whenever the data is no more required
The size of data structure is grow and shrink
Int *ptr
DMA functions malloc(), calloc(), realloc() and free() are used
10. XP
10
3. According to its occurrence DATA STRUCTURE
Linear Data Structure
In linear data structure the data elements are stored in a consecutive
memory locations or data are stored in a sequential manner.
For example: array, stack, queue etc
Non Linear Data Structure
In non-linear data structure the data elements are not stored in a
consecutive memory location or non-sequential manner.
These data structures are used to represent the hierarchical relationship
between data elements.
For example: tree, graph etc.
11. XP
11
According to its occurrence DATA STRUCTURE
Figure 1.3: Linear and Non Linear Data Structure Classification
12. XP
12
4. Homogeneous and Non-Homogeneous DATA STRUCTURE
Homogeneous Data Structure
The data structure which store same types of data elements is called
homogeneous data structure
For example: array,
Int a[150] – array variable ‘a’ can store 150 data elements and all the
elements must be of integer type only
Non Homogeneous Data Structure
The data structure which store different types of data elements is called
non homogeneous data structure
For example: structure
Struct student{
Int roll_number;
Char name[100];
Float marks;
}s1;
Note: student structure can store integer, character and float type of data elements
13. XP
13
DATA STRUCTURE - OPERATIONS
1. CREATIONS: The process of creating a new data structure
2. INSERTIONS: The process of inserting data element into an existing data
structure
3. DELETIONS: The process of deleting data element from a data structure
4. TRAVERSING: The process of visiting each and every element of a data
structure
5. CONCATENATION: The process of concatenating one data structure into
another
6. SEARCHING: The process of finding the desired data element from a set of
data elements of a data structure
7. SORTING: The process of arranging a set of data elements in some specific
order for example ascending order, descending order
8. DISPLAY: The process of sowing all the elements of a data structure
9. DESTROYING: The process of destroying the whole data structure
14. XP
14
DATA STRUCTURE - EXAMPLES
1. ARRAY
2. STACK
3. QUEUE
4. LINKED LIST
5. TREE
6. GRAPH
7. SORTING
8. SEARCHING
15. XP
15
DATA STRUCTURE - ARRAY
Array is a data structure that is used to store a set of homogeneous data
element in the form of index, value pairs in a consecutive memory location
under the same name
char name[7] = {‘A’, ‘B’, ‘C’, ‘D’, ‘E’};
name[0] = ‘A’
name[1] = ‘B’
name[2] = ‘C’
name[3] = ‘D’
name[4] = ‘E’
Figure 1.4: Showing Array
16. XP
16
DATA STRUCTURE - STACK
Stack is non primitive linear data structure in which the insertion of data element
and deletion of data element is done from only one end called the Top of the
Stack (TOS).
TOP of a STACK is initialize to -1 (TOP = -1)
Figure 1.5: Stack
Stack works on the principle of LIFO (Last in First
Out).
The Process of inserting data element into a
Stack is called PUSH operation and in every
PUSH operation the variable TOP is incremented
by 1. If TOP = MAX-1 then the STACK is overflow
condition.
The Process of deleting data element from a
Stack is called POP operation and in every POP
operation the variable TOP is decremented by 1.
If TOP = -1 then the STACK is Underflow
condition.
17. XP
17
DATA STRUCTURE - QUEUE
QUEUE is non primitive linear data structure in which the data elements are
inserted from one end called rear of a queue and deleted from another end
called front of a queue.
REAR and FRON of a QUEUE is initialize to -1 (REAR = -1, FRONT=-1)
Figure 1.6: Queue
Queue works on the principle of FIFO (First in First Out).
The Process of inserting data element into a Queue is called ENQUEUE
operation and in every QUEUE operation the variable REAR is incremented
by 1.
The Process of deleting data element from a Queue is called DEQUEUE
operation and in every DEQUEUE operation the variable FRONT is
incremented by 1.
18. XP
18
DATA STRUCTURE – LINKED LIST
Lists are the most commonly used non primitive linear data structures.
List can be defined as a collection of variable number of data elements.
An element of a list is called node and each node of a list contains at least two
fields one for storing actual information and another field is used for storing
address of next element (use pointer).
Types Of Linked List
Singly Linked List
Circular Linked List
Doubly Linked List
Circular Doubly Linked List
Figure 1.7: Singly Linked List
19. XP
19
DATA STRUCTURE - TREE
Tree is a non primitive nonlinear data structures in which data elements are
arranged or sorted in a sorted order.
A tree is a finite set of data elements (nodes). The data elements in a tree are
represent the hierarchical relationship between various elements.
Figure 1.8: Tree
20. XP
20
DATA STRUCTURE - GRAPH
Graph is a nonlinear data structure used to represent many kinds of physical
structures.
Its application is in Computer network, Engineering Science, Mathematics,
Chemistry etc.
Thus a graph G is a collection of two sets V and E, where V is the vertices
v0,v1,……. vn – 1 and E is the collection of edges e1, e2, …….en. this can b0e
represented as
G = (V, E) where,
Figure 1.9: Graph
V(G) = (v0, v1,……….vn) or
set of vertices
E(G) = (e1, e2,……….en) or
set of edges
21. XP
21
DATA STRUCTURE - SORTING
A sorting is a process of arranging data-elements in some particular order.
The order may be either in ascending or descending or some priority based
order.
Types of Sorting
Internal Sorting
External Sorting
Types of Internal Sorting
Bubble sort
Selection sort
Insertion sort
Quick sort
Figure 1.10: Sorting
Merge sort
Shell sort
Radix sort
Heap sort
22. XP
22
DATA STRUCTURE - SEARCHING
A Searching is a process of identifying a data element from a set of data with a
help of key. Where key may be either internal key or external key.
Types of Searching
Internal Searching
External Searching
Types of Internal Searching
Sequential Search
Binary Search
Interpolation searching
Searching Cases
Best Case: Search success with minimum time or comparisons.
Worst Case: Search takes maximum time or comparisons or search failed
Average Case: Search takes average time.
23. XP
23
DATA STRUCTURE - IMPLEMENTATION
1. Static Implementation of Data Structure
Array is used in static implementation of data structure
The size of data structure cannot be varying during the execution time
It is useful, if the size of the data structure is fixed.
Int a[150] – array variable ‘a’ can store 150 data
Advantages:
Searching is faster as elements are in continuous memory locations
Compiler manage the memory
Disadvantages:
The size is fixed. It must be known at design time.
Insertion and deletion involves unnecessary shifting the rest of the elements
2. Dynamic Implementation of Data Structure
Pointer is used in Dynamic implementation of data structure
24. XP
24
DATA STRUCTURE – DYNAMIC IMPLEMENTATION(contd…)
Dynamic memory allocation (Allocate the memory whenever required and de-
allocate the memory whenever the memory is no longer needed) leads the
data structure dynamic
The size of data structure may grow and shrink during the execution time.
Various DMA functions are used. For example, There are four library
functions malloc( ), calloc( ), free( ) and realloc( ) for memory management.
These functions are defined within header file stdlib.h and alloc.h
For example:
Int *ptr;
Syntax: Ptr = (data_type*)malloc(sizeof(data_type));
For example: Ptr = (int*)malloc(sizeof(int));
Syntax: Ptr = (data_type*)calloc(no_of_bnlock,size_of_each_block);
Syntax: Ptr = realloc(ptr,new_size);
Syntax: Free(ptr);
25. XP
25
DATA STRUCTURE – ABSTRACT DATA TYPE (ADT)
An Abstract Data Type is a mathematical model where various mathematical
operations are defined.
The abstract data type is special kind of data type, whose behavior is defined
by a set of values and set of operations.
The keyword “Abstract” is used as we can use these data types, we can
perform different operations. But how those operations are working that is
totally hidden from the user.
When we use abstract data types,
our programs divided into two types:
1. The application: The part that
uses the abstract data type.
2. The implementation: The part
that implements the abstract
data type.
Figure 1.11: Abstract Data Type
26. XP
26
ALGORITHM
Algorithm is a step by step procedure to solve the problem. An
algorithm is a sequence of instructions designed in such a way that
if the instructions are executed in the specified sequence, the
desired result will be obtained.
Characteristics of Algorithm
Each and every instruction should be precise and
unambiguous. Instruction should be concise and efficient.
Each instruction should be such that it can be performed in a
finite time.
Instructions should not be repeated infinitely. This ensures
that the algorithm will ultimately terminate.
After performing the instructions, that is after the algorithm
terminates, the desire for results must be obtained.
27. XP
27
ALGORITHM - EXAMPLE
Example 1:
Write an algorithm for checking odd and even. The number is entered
by a user.
Step1: Enter a number from user
Step2: Divide the entered number by 2 and check the remainder
If the remainder is 0,
Print the given number is even
Else
Print the given number is odd
Step3: Stop
28. XP
28
ALGORITHM - Complexity Analysis
Time Complexity
Time Complexity is defined as the amount of time required by a
system to solve a particular problem.
Time Complexity is depend on implementation of the algorithm,
programming language, optimizing the capabilities of the
computer used, the CPU speed, other hardware characteristics etc
The time complexity also depends on the amount of data inputted
to an algorithm
Space Complexity
The Space complexity is defined as the amount of space required
by a system to solve a particular problem.
The space needed by the following components
1. Instruction space
2. Data space
3. Environment stack space
29. XP
29
ALGORITHM - Design Approach
Greedy algorithm
Divide and conquer algorithm
Backtracking
Randomized algorithms
There are typically three scenarios of Complexity analysis:
1.Best case: What is the best case or least amount of time this
code/algorithm would need to execute.
2.Worst case: What is the worst case or maximum amount of time
this code/algorithm need to execute.
3.Average case: As the name suggests it the average amount of
time required to execute this code.
30. XP
30
ALGORITHM - Asymptotic Analysis
Asymptotic notation is the simplest and easiest way of describing
the running time of an algorithm.
It represents the efficiency and performance of an algorithm in a
systematic and meaningful manner.
Asymptotic analysis is the theoretical analysis of an algorithm.
The following notations are used for asymptotic analysis
1.Big Oh(O) notation
2.Omega (Ω) notation
3.Theta (θ) notation
31. XP
31
1. Big Oh(O) Notation
It is used to express the upper bound of the running time of an
algorithm.
It is denoted by ‘O’. Using this notation, we can compute the
maximum possible amount of time that an algorithm will take for its
completion.
Definition: consider f(n) and g(n) be
two positive functions of n, where n is
the size of the input data. Then f(n) is
big-Oh of g(n), if and only if there
exists a positive constant C and an
integer n0, such thatf(n) ≤ C g(n) and
n>n0
Here, f(n) = Og((n))
Figure 1.12: Big-Oh Notation
32. XP
32
Big Oh(O) Notation – Contd…
1 O(1) Constant time
2 O(n) linear time of “order N”
3 O(N2) quadratic time of “order N squared”
4 O(log N) logarithmic time of “order log N”
5 O(N log N) logarithmic time of “order N log N”
6 O(N!) factorial time of “order N factorial”
33. XP
33
2. Big Omega (Ω) Notation
This notation gives a Lower bound for a function to within a
constant factor.
We write f(n) = Ω(g(n)) if there are positive constant n0and C such
that to the right of n0,the value of f(n) always lies on or above
Cg(n).
Definition: Let us consider f(n) and
g(n) be two positive functions of n,
where n is the size of the input data.
Then, f(n) is omega of g(n), if and only
if there exists a positive constant C
and an integer n0, such that f(n) ≥ C
g(n) and n>n0
Here, f(n) = Ω(g(n))
Figure 1.13: Big-Omega Notation
34. XP
34
3. Big Theta (θ) Notation
The Theta notation is a method that is used to express the running
time of an algorithm between the lower and upper bounds.
Theta notation is denoted by θ. using this notation; we can
compute the average time that an algorithm will take for its
completion.
Definition: Let us consider f(n) and
g(n) be two positive functions of n,
where n is the size of the input data.
Then, f(n) is theta of g(n), if and only
if there exists two positive constants
C1 and C2, such that,C1 g(n) ≤ f(n) ≤
C2 g(n)
Here, f(n) = θ(g(n))
Figure 1.14: Big-Oh Notation