DS Unit - 1
DS Unit - 1
The data structure helps you to analyse the data, store it and organise it in a logical
or mathematical manner.
1
*Data representation
Various methods are used to represent data in computers. The basic unit
of data representation is Bit. The value of your bit is one of the two manually
exclusive possibilities zero or one. Various combinations of two values of bit are
used to represent the data in a different manner in different systems. 8 bits together
for me one byte which represents a character and one or more than one characters
are used to form a string.
2
11011001
+1
-------------------
11011010 which represents -38 in two’s complement.
-------------------
(10.1)10 = (1010.000110)2
Character representation:
The information in computers is not always interpreted numerically.
For example to store the salary of an employee we use the integer representation
of data but with salary we also need to store the name of the employee which
requires a different data representation. Such information is usually represented in
character string form.
There are different codes available to store data in character form such as
BCD, EBCDIC and ASCII.
For example if 8 bits are used to represent a character then up to 28
= 256 different characters can be represented as bit pattern. 11000000 is used to
represent the character ‘A’ and 11000001 is used to represent the character ‘B’.
Then finally AB can be represented as 1100000011000001.
3
The definition of ADT only mentions what operations are to be performed
but not how these operations will be implemented. It doesn’t specify how data will
be organized in memory and what algorithm will be used for implementing the
operations.
->ADT provides data abstraction and encapsulation.
Data Abstraction:-
The process of providing only the essentials and hiding the details is
known as Abstraction.
Encapsulation:-
The process of binding both data and operations together in a single unit is called
encapsulation.
The abstract data type makes data abstraction more formal. An abstract data type
(ADT) consists of the following
1) A collection of data 2) A set of operations on the data and 3) Set of rules that
governing the interaction of operations.
The ADT concept is similar and related to the classical systems of
mathematics, where an entity is defined in terms of operations and axioms (rules).
For example The user only needs to know what a data type (int, float, char
etc..) can do but not how it will do.
*Data types: Each programming language has its own set of data types. There
are several definitions that define a data type. Few of these definitions are given
below
4
-> The term data type refers to the implementation of the mathematical
model specified by an ADT.
-> A data type is the abstract concept defined by a set of logical properties.
In general a data type can be defined as an abstract concept that defines an
internal representation of data in the memory .Once the concept of data type is
made independent of the hardware capabilities of the computer limitless number
of data types can be considered.
Primitive data types: Primitive data types are those which are predefined way
of storing data by the system and the set of operations that can be performed on
these data are also predefined.
• Primitive data types are predefined types of data, which are supported by
programming language.
• Primitive data types are the basic data structures that directly operate upon
the machine instructions.
Primitive data are only single values, they are not have any special capabilities
EX: - int a;
In variable “a” we can store only one value.
5
*Data structure and structured type
The term data structure refers to a set of computer variables that are
connected in some logical or mathematical manner. A data structure can be
defined as the structural relationship present within the data set and thus should
be viewed as 2 tuple. (N, R) where ‘N’ is the finite set of relationship among those
nodes. For example, in a tree data structure each node is related to each other in
a ‘ parent child’ relationship. Thus, a large volume of data can be represented
using a tree data structure and relationship between each data can also be shown.
6
The above figure represents a list of atomic nodes maintained using list of nodes.
In each node, type represents the type of data stored in the atomic node to which
the list node points. 1 stands for integer type 2 stands for real number and 3 stands
for character type or any different assumption can be made at implementation
level to indicate different data types.
*Differences between Abstract Data Types, Data Types And Data Structures
To avoid the confusion between abstract data types, data types, and data
structures, it is relevant to understand the relationship between the three.
• An abstract data type is the specification of the data type which specifies
the logical and mathematical model of the data type.
• A data type is the implementation of an abstract data type.
• Data structure refers to the collection of computer variables that are
connected in some specific manner.
Thus, there seems to be an open relationship between the three, that is, a data
type has its root in the abstract data type and a data structure comprises a set
of computer variables of same or different data types.
* Refinement stages
The best approach to solve a complex problem is to divide it into
smaller parts such that each part becomes an independent module which is
easy to manage. An example of this approach is the system Development Life
Cycle (SDLC) methodology. This helps in understanding the problem,
analyzing solutions, and handling the problems efficiently. Different problems
have different number of refinement stages, but in general, there are four
levels of refinement processes.
• Conceptual or abstract level
• Algorithmic or data structures
• Programming or implementation
• Applications
Conceptual level : At this level we decide how the data is related to each other,
and what operations are needed. Details about how to store data and how various
operations are performed on that data not decided at this level.
7
Algorithmic or data Structure level : At data structure level we decide about
the operations on the data as needed by our problems. For example, we decide
what kind of data structure will be required to solve the problem. Whereas for the
evaluation of any expression into prefix or postfix, stacks will be used.
Programming or Implementation level
At implementation level, we decide the details of how the data structures will be
represented in the computer memory. For example, we decide whether the linked
lists will be implemented with pointers or with the cursors in an array.
Application level
This level settles all details required for particular application such as names for
variables or special requirements for the operations imposed by applications.
8
Design: - The requirements gathered in the previous phase acts as an input to the
design phase. In this design phase, a plan of actions made before the actual
developmental process can start. [Algorithm, Flowchart]
Implementation : In this phase, the designed algorithms and converted into
program code. Using any of the high – level languages.
Testing : - In this phase, all the implementations are tested together to ensure that
overall system works as a whole product.
S/W deployment : - After the code is tested and the s/w or the program has been
approved by the user it is deployed in the market.
Maintenance : - Maintenance and enhancements are ongoing activities. If the
cost of the maintenance phase exceeds 25% of the prior phase cost. Then it is
better to re – build the S/W.
*Program Design:
Program design can be considered as an important phase of the software
development life cycle. It is in this phase that the algorithm and data structures to
solve a problem are proposed. Some of the various pints that can help us evaluate
the program designs are as follows:
1. As the design stage involves taking the specification and designing
solutions to the problems, the designer needs to adopt a design strategy.
2. The strategy adopted while designing should be according to the given
specification.
3. Another important point which should be kept in mind while developing a
solution strategy is that it should work correctly in all conditions.
4. A large program should be divided into small modules and sub modules by
following one of the two decomposition approaches –top- down approach
and bottom-up approach.
*Algorithm : Algorithm can be defined as a step by step procedure to solve a
particular problem. It consists of simple English statements.
Characteristics of an algorithm :
Each and every algorithm has five important characteristics
1) Input : If may accept zero or more inputs
2) Output : It should produce at least one output
3) Definiteness : Each instruction must be clear, well defined and precise. There
should not be any ambiguity.
4) Finiteness : It should be a sequence of finite instructions that means, it should
end after a fixed time. It should not enter into a infinite loop.
9
5) Effectiveness : It means, that operations must be simple and are carried out in
a finite time.
Example : Write an algorithm to compute the area of a circle
Algorithm name : - Area of a circle
Step 1 : - Read radius
Step 2 : - [Compute area] Area = 3.14 *radius * radius
Step 3 : - [Print the area]
Print “Area of a circle” Area
Step 4 : - [End of Algorithm] Stop.
Types of Algorithms : -
Generally, Algorithm uses three types of control structures, namely sequence,
decision & repetition.
Sequence: - Sequence means that each algorithm is executed in the specified
order
Ex : - Algorithm : - Addition of two numbers
Step 1 : - Read A, B
Step 2 : - [Compute Addition]
Addition = A+B
Step 3 : - [Print the addition]
Print “addition is = Addition
Step 4 : - Stop
Decision: - Decision statements are used when the execution of a process depends
on the outcome of the condition.
A decision statement can also be stated in the following manner.
If condition
Then process 1
Else
Process 2
Algorithm: Equality of two numbers
Step 1 : - Read a, b
10
Step 2 : - If a = b
Then print “equal”
Else
Print “Not equal”
Step 3 : - Stop
Repetition: -
Repetition means, executing one or more steps for a number of times.
Ex : - Algorithm : Print 10 natural number
Step 1 : Set i = 1
Step 2 : Repeat step 3 and 4 while i< = 10
Step 3 : Print i
Step 4 : set i = i+1
Step 5 : Stop
11
Bottom – up Approach :
A bottom – up design approach starts with designing the most basic or
primitive components and proceeds to higher – level components. Bottom – up
method works with layers of abstraction. Starting from the very bottom, the
operations that provide a layer of abstraction are implemented.
*Complexity
When the complexity in context of computers, we call it computational
complexity. Computational complexity is a characterization of the time or space
requirements for solving a problem by a particular algorithm.
Such that f(n) is the largest amount of time needed by the algorithm to solve the
problem of size n. Function ‘f’, is usually called the time complexity function.
The time complexity of a program/ algorithm is the amount of computer time
that it needs to run to completion.
The space complexity of a program / algorithm is the amount of memory that it
needs to run to completion.
Time complexity: Time required to execute the algorithm.
While measuring the time complexity of an algorithm, we concentrate on
developing only the frequency count for all key statements.
12
Consider the algorithm given below
14
Categories of Complexity Big O
Constant time 0(1)
Logarithmic time 0(log n)
Linear time 0(n)
Logarithmic time 0(n log n)
Quadratic time 0(n2)
Cubic time 0(n3)
Exponential time 0(2n)
In order to determine the space complexity of an algorithm, the following two
tasks are necessary to be done.
1. Implementation of the program for a particular algorithm is required
2. The size of the input n is required to know the memory that each item will
hold.
*Algorithm analysis : There are different ways of solving a problem and there
are different algorithms which can be designed to solve a problem. Therefore,
there is a difference between a problem and an algorithm. However, there are
many different ways to solve the problem, and some of the solutions may be more
efficient than the others.
Analysis of algorithms focuses on computation of space and time
complexity.
Space can be defined in terms of space required to store the instructions and
data.
where as the time is the computer time an algorithm might require for its
execution, which usually depends on the size of the algorithm and input.
There are different types of time complexities which can be analysed for an
algorithm :
• Best case time complexity
• Average case time complexity
• Worst case time complexity
Best case time complexity : It is the shortest running time of an algorithm for
all inputs of given size i.e., minimum number of steps required to reach at the
result. It uses the lowest resources.
15
Worst case time complexity: The worst case time complexity of an algorithm
is a measure of the maximum time that the algorithm will require for an input
of given size , i.e., maximum number of steps taken for all the possible input
of size n. In this type of complexity, the key operation is executed maximum
number of times.
Average case time complexity : If the running time falls between the best
case and worst case the complexity is called Average case. It is oftenly very
useful but more difficult to compute.
*Structured approach to programming
Structured programming is a subset of software engineering. It is a
method for designing and coding programs in a systematic, organized manner.
The term “structured programming” was coined by Dijkstra in the article “
Structured Programming”. We deal with various tools required for building a
structured program. Which has defined program as follows :
Program = algorithm + data structures
Some of the control structures which can be used for structured programming
are given below.
16
Decision: - Decision statements are used when the execution of a process depends
on the outcome of the condition.
A decision statement can also be stated in the following manner.
If condition
Then process 1
Else
Process 2
Syntax:-
return type function name (parameters)
{
Local variable declarations;
.
.
Terminating condition;
.
.
Value = rfunction name (expression);
}
Where
rfunction name - Is the name of the recursion function.
Value - Is the value to be returned to the calling function.
Terminating condition - Is a stopping statement for the recursion
function.
A ‘C’ function declaration for factorial is
17
Principles of Recursion :
Some basic principles which are used while designing algorithms with
recursion are:
Find the key step : When beginning with the design of algorithms through
recursion one should try to find out the ‘Key step’ for the solution.
Find a stopping Rule: The stopping rule indicates that the problem. It is done
and the execution of the algorithm can be stopped.
Outline your algorithm: After determining the key step for the problem and
finding the cases which are to be handled, the next step is to combine these
two using an “if” statement to select between them.
Check termination : Care should be taken to ensure that the recursion will
always terminate after the finite number of steps.
18
* Tips and techniques for writing Programs in ‘C’:
A program in any language is a collection of one of more functions. Every
function is a collection of statements which performs a specific task. For example
, a program written in ‘C’ can have the following format:
19