0% found this document useful (0 votes)
4 views

Data Types & Abstract Data Type

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Data Types & Abstract Data Type

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

14/08/2024

DATA TYPES & ABSTRACT


DATA TYPE

RAJYALAKSHMI UMMADISETTI
SENIOR LECTURER IN CME
DATA TYPES

14/08/2024
It
is the kind of data which tells the
compiler (or interpreter) how the
programmer intends to use the data.

 Primitive: basic building block


(boolean, integer, float, char etc.)
 Composite: any data type (struct,
array, string etc.) composed of
primitives or composite types.
 Abstract: data type that is
defined by its behaviour (tuple,
set, stack, queue, graph etc). 2
14/08/2024
DATA TYPES

Ifwe consider a composite type,


such as a ‘string’, it describes a
data structure which contains a
sequence of char primitives
(characters), and as such is referred
to as being a ‘composite’ type.

Whereas the
underlying implementation of the
string composite type is typically
implemented using an array data
structure. 3
The ADT is special kind
of datatype, whose
behavior is defined by a

14/08/2024
set of values and set of
operations.

ABSTRACT The ADT is made of with


primitive datatypes, but
operation logics are
DATATYPE hidden.

The definition of ADT only


mentions what operations
are to be performed but
not how these operations
will be implemented.
4
It does not specify how data will be
organized in memory and what

14/08/2024
algorithms will be used for
implementing the operations.
It is called “abstract” because it
gives an implementation-
independent view. The process of
providing only the essentials and
hiding the details is known as
abstraction.

ABSTRACT Some examples of ADT are Stack,


Queue, List etc.
DATATYPE 5
Stack −

• For example, a stack is a typical abstract data type.

14/08/2024
ABSTRA Items stored in a stack can only be added and
removed in certain order – the last item added is the
first item removed. We call these operations, pushing

CT DATA
and popping. In this definition, we haven‘t specified
have items are stored on the stack, or how the items
are pushed and popped. We have only specified the

TYPES-
valid operations that can be performed.

EXAMPL Stack −

• isFull(), This is used to check whether stack is full or not

ES • isEmpty(), This is used to check whether stack is empty or


not
• push(x), This is used to push x into the stack
• pop(), This is used to delete one element from top of the
stack
• peek(), This is used to get the top most element of the stack 6
• size(), this function is used to get number of elements
present into the stack
Stack −

14/08/2024
ABSTRA
CT DATA
TYPES- Queue −

EXAMPL • isFull(), This is used to check whether queue is full or

ES
not
• isEmpty(), This is used to check whether queue is
empty or not
• insert(x), This is used to add x into the queue at the
rear end
• delete(), This is used to delete one element from the 7
front end of the queue
• size(), this function is used to get number of
List −
size(), this function is used to
get number of elements present
into the list
Abstract insert(x), this function is used to
insert one element into the list
Data remove(x), this function is used

Types- to remove given element from


the list
Examples get(i), this function is used to
get element at position i
replace(x, y), this function is
used to replace x with y value

14/08/2024
ALGORITHM- CHARACTERISTICS
An algorithm is a finite sequence of instructions, each of which has a clear meaning and can

14/08/2024
be performed with a finite amount of effort in a finite length of time. No matter what the
input values may be, an algorithm terminates after executing a finite number of
instructions.
In addition every algorithm must satisfy the following criteria:

Input: there are zero or more quantities, which are externally supplied;

Output: at least one quantity is produced;

Definiteness: each instruction must be clear and unambiguous;

Finiteness: if we trace out the instructions of an algorithm, then for all cases the algorithm
will terminate after a finite number of steps;
Effectiveness: every instruction must be sufficiently basic that it can in principle be carried
9
out by a person using only pencil and paper. It is not enough that each operation be definite,
but it must also be feasible.
Analys Analysing an algorithm means calculating
is the resources required for the algorithm.

ALGORITH memory,
Resource
M s
communication bandwidth,
computation time etc.,

ANALYSIS
Desig
1. Try to save time (Time complexity).
n 2. Try to save space (Space complexity).
Goals
14/08/2024 10
TIME COMPLEXITY

14/08/2024
 Time Complexity: The time needed by an
algorithm expressed as a function of the
size of a problem.
 The time complexity of a program is the
amount of computer time it needs to run to
completion.
 The limiting behavior of the complexity as
size increases is called the asymptotic time
complexity.
 It is the asymptotic complexity of an
algorithm, which ultimately determines the
size of problems that can be solved by the
algorithm.
11
ANALYSIS OF ALGORITHMS

14/08/2024
A complete analysis of the running time of an algorithm involves
the following steps:
 Implement the algorithm completely.

 Determine the time required for each basic operation.

 Identify unknown quantities that can be used to describe the

frequency of execution of the basic operations.


 Develop a realistic model for the input to the program.

 Analyze the unknown quantities, assuming the modelled input.

 Calculate the total running time by multiplying the time by the

frequency for each operation, then adding all the products.


12
SPACE COMPLEXITY
 The space complexity of a program is the amount of memory it needs to run to
completion. The space need by a program has the following components:
 Instruction space: Instruction space is the space needed to store the compiled
version of the program instructions.
 The amount of instructions space that is needed depends on factors such as:
 The compiler used to complete the program into machine code.
 The compiler options in effect at the time of compilation
 The target computer.
 Data space: Data space is the space needed to store all constant and variable
values.
 Data space has two components:
 Space needed by constants and simple variables in program.
 Space needed by dynamically allocated objects such as arrays and class
instances.
 Environment stack space: The environment stack is used to save information
14/08/2024 13
needed to resume execution of partially completed functions.
WHY ANALYZE AN ALGORITHM?

14/08/2024
 The reason for analyzing an algorithm is to discover its
characteristics in order to evaluate its suitability for various
applications or compare it with other algorithms for the same
application.
 Moreover, the analysis of an algorithm can help us understand it

better, and can suggest informed improvements.


 Algorithms tend to become shorter, simpler, and more elegant

during the analysis process.

14
ALGORITHM ANALYSIS
Algorithm analysis can be performed at two different stages, before
implementation and after implementation, as
1) Priori analysis: The resource(such as memory & space) requirements
are obtained by formulating a function based on theory.
 It is defined as theoretical analysis of an algorithm. i.e., independent of

programming languages and machine design structure.


 Efficiency of algorithm is measured by assuming that all other factors

e.g. speed of processor, are constant and have no effect on


implementation.
 The O-notation is used in priori analysis.

14/08/2024 15
PRIORI ANALYSIS

14/08/2024
 In theoretical analysis of algorithms it is common to estimate
their complexity in the asymptotic sense, i.e., to estimate the
complexity function for arbitrarily large input. Big O notation, Big-
omega notation and Big-theta notation are used to this end.
 Rule of thumb: Simple programs can be analyzed by counting

the nested loops of the program. A single loop over n items yields
f( n ) = n. A loop within a loop yields f( n ) = n2. A loop within a
loop within a loop yields f( n ) = n3.
 Rule of thumb: Given a series of for loops that are sequential,

the slowest of them determines the asymptotic behavior of the


program. Two nested loops followed by a single loop is
asymptotically the same as the nested loops alone, because the 16
nested loops dominate the simple loop.
ASYMPTOTIC COMPARISON OPERATOR -
NUMERIC COMPARISON OPERATOR

14/08/2024
 Our algorithm is o( something ) A number is < something
 Our algorithm is O( something ) A number is ≤ something
 Our algorithm is Θ( something ) A number is = something
 Our algorithm is Ω( something ) A number is ≥ something
 Our algorithm is ω( something ) A number is > something

17
POSTERIORI ANALYSIS

2) Posteriori Analysis: This is defined as empirical analysis of an algorithm.


 The actual amount of space and time taken by the algorithms are

calculated during execution.


 It is dependent of programming languages and machine design

structure.
 The chosen algorithm is implemented using programming language.

 Next the chosen algorithm is executed on target computer machine.

 In this analysis, actual statistics like running time and space needed are

collected.

14/08/2024 18
POSTERIORI ANALYSIS

14/08/2024
 Run-time analysis Orders of growth
 Run-time analysis is a theoretical classification that estimates

and anticipates the increase in running time (or run-time) of


an algorithm as its input size (usually denoted as n)
increases. Run-time efficiency is a topic of great interest in
computer science: A program can take seconds, hours or
even years to finish executing, depending on which algorithm
it implements (see also performance analysis, which is the
analysis of an algorithm's run-time in practice).

19
ANALYSIS TYPES
 The algorithm complexity can be best, average or worst case analysis. The

14/08/2024
algorithm analysis can be expressed using Big O notation. Best, worst, and
average cases of a given algorithm express what the resource usage is at
least, at most and on average, respectively. The big o notation simplifies the
comparison of algorithms.
 Best Case : Best case performance used in computer science to describe an
algorithm’s behavior under optimal conditions. An example of best case
performance would be trying to sort a list that is already sorted using some
sorting algorithm. E.G. [1,2,3] --> [1,2,3]
 Average Case: Average case performance measured using the average
optimal conditions to solve the problem. For example a list that is neither best
case nor, worst case order that you want to be sorted in a certain order. E.G.
[2,1,5,3] --> [1,2,3,5] OR [ 2,1,5,3] --> [5,3,2,1]
 Worst Case: Worst case performance used to analyze the algorithm's
behavior under worst case input and least possible to solve the problem. It
determines when the algorithm will perform worst for the given inputs. An 20

example of the worst case performance would be a a list of names already


sorted in ascending order that you want to sort in descending order.
ALGORITHM PERFORMANCE AND COMPLEXITY

14/08/2024
 Algorithms efficiency described in terms of Time and Space.
 The time efficiency calculated using CPU utilization.
 The Space efficiency calculated using memory and disk usage of an
algorithm.
 The developer should know the difference between
Performance and complexity.
 The complexity analysis does not depend on any computer

resource. It may change based on the input size.


 The algorithm performance is machine independent and

does not depend on any other factors.

21
COMPLEXITY OF ALGORITHMS

14/08/2024
 The complexity of an algorithm M is the function f(n) which
gives the running time and/or storage space requirement of
the algorithm in terms of the size ‗n‘ of the input data.
Mostly, the storage space required by an algorithm is simply
a multiple of the data size ‗n‘. Complexity shall refer to the
running time of the algorithm.
 The function f(n), gives the running time of an algorithm,

depends not only on the size ‗n‘ of the input data but also on
the particular data. The complexity function f(n) for certain
cases are: 1. Best Case : The minimum possible value of f(n)
is called the best case. 2. Average Case : The expected value
of f(n). 3. Worst Case : The maximum value of f(n) for any key 22
possible input

You might also like