0% found this document useful (0 votes)
20 views19 pages

DS Unit - 1

Notes

Uploaded by

Pavani akumarthi
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)
20 views19 pages

DS Unit - 1

Notes

Uploaded by

Pavani akumarthi
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/ 19

UNIT -1

INTRODUCTION TO THEORY OF DATA STRUCTURES

*Introduction to theory of data structures:


Data structure is a branch of computer science that unleashes the
knowledge of how the data should be organised, how the flow of data should be
controlled and how the data structure should be designed and implemented to
reduce the complexity and increase the efficiency of the algorithm.
The data structure is a study of
• How the data can be organised in the computer’s memory.
• How to control the data in the memory
• How to implement a data structure
• How to reduce the complexity of data structure
• How to increase efficiency of algorithm
The theory of structure not only introduces you to data structures, but also
helps you to understand and use the concept of abstraction, analyse problems step
by step and develop algorithms to solve real world problems. It enables you to
design and implement various data structures for example stacks, queues, linked
lists, trees and graphs. Effective use of principles of data structures increases
efficiency of algorithms to solve problems like searching and sorting.
Need of a data structure
The structure helps you to understand the relationship of one data element
with another and organise it within the memory. Sometimes the organisation may
be simple and can be very clearly.
For Example list of names of months in
your year we can see the names of months
have a linear relationship between them and
this can be stored in a sequential location.
Similarly a set of data that represents
location of historical places in the country
for example each historical place the
location is given by the country name
followed by state name, followed by city
name and then followed by the historical
place name. We can see that such data from
form a hierarchal relationship between
them and must be represented in the
memory using the hierarchal type of data
structure.

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.

The set of operations that allows us to


perform various string related operations like
storing and fetching a string, joining two
strings, finding the length of strings etc .The use
of concrete data structure using design creates
a lot of difficulties and requires much more
efforts, such a problem can be avoided by using
abstract data type in the design process.

The internal representation of basic data types are represented in the


computer memory as integer, float and character.
Integer representation
An integer is a basic data type which is commonly used for storing
negative as well as non negative integer numbers. The non-negative data is
represented using binary number system. In this each bit, position represents the
power of two. The rightmost bit position represents 2 0 which is 1 and the next
represents 21 which is 2 and then 22 which is 4 and so on. For example 00100110
represent the Integer as 21 + 22 + 25 =2 + 4 + 32 =38.
For negative binary numbers the methods of representation used are
one’s compliment and two’s compliment.
In one’s compliment method the number is represented by complementing
each bit its value to the opposite bit setting. For example 00100110 represent 38.
After complicating complementing it becomes 11011001 which is used to
represent -38.
In twos complement method one is added to ones compliment
representation of negative number. For example minus -38 is represented by
11011001 which on adding 1 to it will become.

2
11011001
+1
-------------------
11011010 which represents -38 in two’s complement.
-------------------

Real number representation:


The method used to represent real numbers in computers is floating
point notation. In this notation the real number is represented by a number called
a mantissa, times a base raised to an inducer power called exponent.
Ex: Conversion of real number to binary number
(10.1)10
The Binary representation of 10 is 1010
The Binary representation of 0.1 is
0.1 * 2 = 0.2 →0
0.2 * 2 = 0.4 →0
0.4 * 2 = 0.8 →0
0.8 * 2 = 1.6 →1
0.6 * 2 = 1.2 →1
0.2 * 2 = 0.4 →0
1010
000110

(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.

*Abstract data types:


Abstract Data type (ADT) is a type for objects whose behaviour is defined
by a set of values and a set of operations.

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.

Example of an ADT specification for a stack

A stack is a set of finite number of elements, where insertion or removal of


an element is allowed from one end only. Any new element that joins the set is
kept at the top and only the element at the topmost position can be taken out.
There are a number of methods for specifying an ADT. The example below
uses the informal notation to specify STACK abstract data type.

Specification of abstract data type STACK as follows


Abstract typedef < 1,2,3,----n,top>STACK:
Condition top = null
Abstract STACK PUSH( data)
Precondition: top !=n
Operation : insert( data)
/*top points to the most recently inserted elements*/
Abstract : pop()
Preconditions top!=NULL
Operations: Remove (top)
Top-> current data

*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.

Why do we need it a Data type:


We can think of a universal data type that may hold any value like character, float
or any complex number. The use of such data type has two advantages.

1. Large volume of memory will be occupied by even a small size of data.


2. Different types of data require different representations interpretations of
bit strings while reading or writing
Thus we see that the data type facilitates the optimum use of memory as well as
defined way to interpret the bit string for different types of data.

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.

They have different representation on different computers .

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.

=>primitive data types are:-


1. Integer 2.float 3.char 4.Boolean.

1. Integer:-It is used to represent number without decimal part.


Ex:-2,5,78 etc.
2. Float:-It is used to represent number with decimal part.
Ex:-2.34,56.78
3. Character:-It is used to represent character.
Ex:- ‘a’,’b’,’c’ etc.
4. Boolean:-It is used to represent logical values. i.e true or false.

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.

A structured type refers to a data structure which is made up of one or


more elements known as components. These elements are simpler data structures
that exist in the language. The study of these structured type data structures
involves an analysis of how simple structures combine to form the composite and
how to extract an element from the composite. In “C” programming language,
the structure type is implemented using struct keyword.
*Atomic Type:
Generally, a data structure is represented by a memory block, which has two
parts:
1. Data storage
2. Address storage
This facilitates in storing the data and relating it to some other data by
means of storing pointers in address part.
An atomic type data is a data structure that contains only the data items
and not the pointers. Thus, for a list of data items, several atomic type nodes may
exist each with a single data item corresponding to one of the data types. This list
is maintained using a list node to which contains pointers to these atomic nodes
and a type indicator indicating the type of atomic node to which it points.
Whenever a test node is inserted in the list, its address is stored in the next free
element of the list of pointers.

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.

*Principles of Programming and Analysis of Algorithm:


Software Engineering: Software Engineering is the theory and practice of
methods helpful for the construction and maintenance of large software systems.
Development of a good software is tedious process which continues for long
time. There are many stages in the software development cycle . The process is
often referred to as Software Development Life Cycle(SDLC).In SDLC, the
output from one stage becomes the input to the next stage.
System Development Life Cycle:
The entire S/W or program development process is divided into no. of phases,
where each phase performs a defined task.

Requirement analysis : - In this phase, the user’s requirements a gathered to


know why the program / software has to be built.

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

*Different Approaches to designing an alogorithm :


A complex system may be divided into smaller units called modules.
The advantage of modularity is that it allows the principle of separation of
concerns to be applied into two phases. Modularity enhances design clarity,
implementation, debugging, testing, documenting, and maintenance of the
product.
There are two possible approaches to design an algorithm. They are
1. Top down approach
2. Bottom up approach
Top down approach :
A top – down design approach starts by identifying the major components of
the system or program, decomposing them into their lover – level components.
Top – down design method takes the form of stepwise refinement. In this, we
start with the topmost module and incrementally add modules. Thus, in top –
down approach we start from an abstract design.

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

➢ In the algorithm A we may find the statement a = a+1 is independent and


is not contained within any loop. Therefore, the number of times this shall
be executed is 1. We can say that the frequency cont of algorithm A is 1.
➢ In the second algorithm, i.e., B, the key statement out of three statements
is the assignment operation a = a+1. Because this statement is contained
within a loop, the number of times it is executed is n, as the loop runs for
n times. The frequency count for this algorithm is n.
➢ According to the third algorithm, the frequency count for the statement a =
a+1 is n2 as the inner loop runs n times, each time the outer loop runs. The
outer loop also runs for n times. n2 is said to be different in increasing order
of magnitude just like 1, 10, 100 depending upon the n.
Space complexity: Amount of memory used by algorithm for execution.
The space needed by the program is the sum of the following components:
Fixed space requirement: This includes the instruction space, for simple
variable, fixed size structured variables and constants.
Variable space requirement: This contains of space needed by structured
variables whose size depends on particular instance of variables. It also includes
the additional space required when the function uses, recursion.
*Big ‘O’ notation :
Big ‘O’ notation was introduced in 1984 by Paul Bachmann.
• Big O notation has been developed as the standard for comparing
algorithms. The main aim of this notation is to identify the term which
helps to determine the time complexity of an algorithm.
• It is most commonly used notation for specifying complexity.
If f(n) represents the computing time of some algorithm and g(n) represents a
known standard function like n, n2, n log n, etc., then to write.
13
f(n) is 0 g(n)

For example, for a function f(n)


Means that f(n) of n is equal to biggest order of function g(n). This implies only
when :
|f(n)!Cg(n)| For all sufficiently large integers n, where C is the
constant.
From the above statements, we can say that the computing time of an algorithm
is O(g(n)), we mean that its execution takes no more than a constant time g(n) , n
is the parameter which characterizes the input and outputs.

Why Big ‘O’ Notation


Big O notation helps to determine the time as well as space complexity of the
algorithms. Using the big O notation, the time taken by the algorithm and the
space required to run the algorithm can ascertained. This information is useful to
develop and design efficient algorithms in terms of time and space complexity.

Most common computing times of algorithm


If the complexity of any algorithm is 0(1), it means that the computing time of
the algorithm is constant 0(n) and it is called linear time which implies that it is
directly proportional to n. 0(n2) is called the quadratic time, 0(n3) is the cubic
time, 0(2n) is exponential time, 0(log n) and 0(n log n) are the logarithmic times.
The common computing times of algorithms in the order of performance are as
follows :

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.

Sequence: - Sequence means that each algorithm is executed in the specified


order

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

Repetition:- Repetition means, executing one or more steps for a number of


times.
*Recursion:-
Recursion means function calling itself, that is a function which performs
a particular task is repeatedly calling itself.
There must be an exclusive stopping statement in a recursive function.
Otherwise function it enters into an infinite logic.

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy