Chapter 1 Data Structure-1
Chapter 1 Data Structure-1
The example mentioned above, such as ID, Age, Gender, First, Middle, Last,
Street, Area, etc., are elementary data items, whereas the Name and the
Address are group data items.
First, it must be loaded enough into the structure to reflect the actual
relationship of the data with a real-world object.
Second, the formation should be so simple that one can efficiently
process the data whenever necessary.
Arrays
Queues
Stacks
Linked lists
Graphs
Family of trees and
Table of contents
Tree: In this case, the data often has a hierarchical relationship between the
different elements. The data structure representing this relationship is called
a rooted tree graph or tree.
Graph: In this case, the data sometimes has relationships between pair
elements, which do not necessarily follow a hierarchical structure. Such a
data structure is called a graph.
You will learn more about Linear and Non-linear Data Structures in
subsequent lessons.
1.1.1 Abstract data Types
Abstract Data type (ADT) is a type (or class) for objects whose behavior is
defined by a set of values and a set of operations. The definition of ADT
only mentions what operations are to be performed but not how these
operations will be implemented. It does not specify how data will be
organized in memory and what algorithms will be used for implementing
the operations. It is called “abstract” because it gives an implementation-
independent view.
Features of ADT:
1.1.2 Abstraction
Data abstraction is the reduction of a particular body of data to a simplified
representation of the whole. Abstraction, in general, is the process of
removing characteristics from something to reduce it to a set of essential
elements. To this end, data abstraction creates a simplified representation of
the underlying data, while hiding its complexities and associated operations.
In computing, data abstraction is commonly used in object-oriented
programming (OOP) and when working with a database management system
1.2. Algorithms
What is an Algorithm?
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.
Dataflow of an Algorithm
Step 1: Start
Step 2: Declare three variables a, b, and sum.
Step 3: Enter the values of a and b.
Step 4: Add the values of a and b and store the result in the sum variable, i.e.,
sum=a+b.
Factors of an Algorithm
The following are the factors that we need to consider for designing
an algorithm:
o Modularity: If any problem is given and we can break that problem into
small-small modules or small-small steps, which is a basic definition of an
algorithm, it means that this feature has been perfectly designed for the
algorithm.
o Correctness: The correctness of an algorithm is defined as when the given
inputs produce the desired output, which means that the algorithm has been
designed algorithm. The analysis of an algorithm has been done correctly.
o Maintainability: Here, maintainability means that the algorithm should be
designed in a very simple structured way so that when we redefine the
algorithm, no major change will be done in the algorithm.
o Functionality: It considers various logical steps to solve the real-world
problem.
o Robustness: Robustness means that how an algorithm can clearly define
our problem.
o User-friendly: If the algorithm is not user-friendly, then the designer will not
be able to explain it to the programmer.
o Simplicity: If the algorithm is simple then it is easy to understand.
o Extensibility: If any other algorithm designer or programmer wants to use
your algorithm then it should be extensible.
Algorithm Analysis
Algorithm Complexity
1. sum=0;
2. // Suppose we have to calculate the sum of n numbers.
3. for i=1 to n
4. sum=sum+i;
5. // when the loop ends then sum holds the sum of the n numbers
6. return sum;
In the above code, the time complexity of the loop statement will be at least
n, and if the value of n increases, then the time complexity also increases.
While the complexity of the code, i.e., return sum will be constant as its
value is not dependent on the value of n and will provide the result in one
step only. We generally consider the worst-time complexity as it is the
maximum time taken for any given input size.
Auxiliary space: The extra space required by the algorithm, excluding the
input size, is known as an auxiliary space. The space complexity considers
both the spaces, i.e., auxiliary space, and space used by the input.
So,
Complexity analysis
Complexity analysis is defined as a technique to characterize the time
taken by an algorithm with respect to input size (independent from the
machine, language and compiler). It is used for evaluating the variations of
execution time on different algorithms.
What is the need for Complexity Analysis?
Complexity Analysis determines the amount of time and space resources
required to execute it.
It is used for comparing different algorithms on different input sizes.
Complexity helps to determine the difficulty of a problem.
often measured by how much time and space (memory) it takes to solve
a particular problem
How does Complexity affect any algorithm?
Time complexity of an algorithm quantifies the amount of time taken by an
algorithm to run as a function of length of the input. While, the space
omplexity of an algorithm quantifies the amount of space or memory taken
by an algorithm to run as a function of the length of the input.