Algorithm Specification and Data Abstraction
Dr. Ashutosh Satapathy
Assistant Professor, Department of CSE
VR Siddhartha Engineering College
Kanuru, Vijayawada
September 22, 2022
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 1 / 40
Outline
1 Algorithm Specification
Introduction to Algorithm
Algorithm Design and Data Structure
Analyzing an Algorithm
Algorithm Design Approaches
Pseudocode Conventions
2 Data Abstraction
Data Types
Abstract Data Types
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 2 / 40
Outline
1 Algorithm Specification
Introduction to Algorithm
Algorithm Design and Data Structure
Analyzing an Algorithm
Algorithm Design Approaches
Pseudocode Conventions
2 Data Abstraction
Data Types
Abstract Data Types
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 3 / 40
Introduction to Algorithm
An algorithm is a well defined computational procedure that takes
some value, or set of values, as input and produces some value, or set
of values as output.
It is thus a sequence of computational steps that transform the input
into the output.
The algorithm describes a computational procedure for achieving that
input/output relationship.
An algorithm is a finite set of instructions which accomplish a
particular task.
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 4 / 40
Introduction to Algorithm
Every algorithm must satisfy the following criteria.
1 Input: There are some (possibly empty) input data which are
externally supplied to the algorithm.
2 Output: There will be at least one output.
3 Definiteness: Each instruction/step of the algorithm must be clear
and unambiguous.
4 Finiteness: If we trace out the instruction/step of an algorithm, then
for all cases, the algorithm will terminate after a finite number of
steps.
5 Effectiveness: The steps of an algorithm must be sufficiently basic
that it can be carried out by a person mechanically using pen and
paper.
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 5 / 40
Introduction to Algorithm
Algorithm 1 Swap two numbers using third variable
1: start
2: read two values into two variables a, b
3: declare third variable, temp
4: temp = a
5: a = b
6: b = temp
7: write a, b ▷ Display a and b values
8: stop
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 6 / 40
Outline
1 Algorithm Specification
Introduction to Algorithm
Algorithm Design and Data Structure
Analyzing an Algorithm
Algorithm Design Approaches
Pseudocode Conventions
2 Data Abstraction
Data Types
Abstract Data Types
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 7 / 40
Algorithm Design and Data Structure
To write a computer program for solving a problem we must start with the
following four tasks:
Identify the data items and their relationship.
Find the operations that must be performed on these data items.
Determine the best method to represent these data items in
computer’s memory.
Select a suitable programming language which can efficiently code the
identified data representation.
Note
These data items can be arranged in different ways depending on the
logical relationship between them. Arrangement of these data items is
called data structures. E.g., arrays, records, lists, and files
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 8 / 40
Algorithm Design and Data Structure
Important
The storage of data in primary memory is called as data structure
where as the storage of data in secondary memory is called file
structure.
The representation or the data structure in computer’s memory which
is known as storage structure.
Selection of a programming language best suited to represent the
data structure.
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 9 / 40
Outline
1 Algorithm Specification
Introduction to Algorithm
Algorithm Design and Data Structure
Analyzing an Algorithm
Algorithm Design Approaches
Pseudocode Conventions
2 Data Abstraction
Data Types
Abstract Data Types
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 10 / 40
Analyzing an Algorithm
Algorithms are designed using basic program constructs like sequence,
selection or branching, repetition, etc.
We need methods to separate bad algorithm from good ones and to
choose the most effective approach for our problem.
In analyzing an algorithm, the first approach is to check the
correctness of the algorithm.
Tracing the algorithm.
Checking the algorithm for logical correctness.
Implementing the algorithm.
Testing the algorithm, which can yield correct output for all possible
combinations of input values. This is called program proving or
program verification.
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 11 / 40
Analyzing an Algorithm
Another approach of analysis is to check the simplicity of the
algorithm.
The simplest way of solving the problem is not always the best one.
It is necessary to evaluate the complexity of the algorithm.
The complexity of the algorithm is determined in terms of time and
space.
These determine the amount of time and storage an algorithm may
require for execution.
Finally, predict the performance of the algorithm in the best, worst,
and average cases.
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 12 / 40
Outline
1 Algorithm Specification
Introduction to Algorithm
Algorithm Design and Data Structure
Analyzing an Algorithm
Algorithm Design Approaches
Pseudocode Conventions
2 Data Abstraction
Data Types
Abstract Data Types
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 13 / 40
Algorithm Design Approaches
Figure 1.1: Top-down and bottom-up design approaches
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 14 / 40
Algorithm Design Approaches
A complicated algorithm is split into small parts called modules, and
the process of splitting is known as modularization.
Modularization significantly reduces the complications of designing an
algorithm and make its process more easier to design and implement.
It is necessary to evaluate the complexity of the algorithm.
In the top-down approach, the complex module is divided into
submodules.
In bottom-up approach begins with elementary modules and then
combine them further.
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 15 / 40
Algorithm Design Approaches
Basis for
Comparison
Top-down
Approach
Bottom-up
Approach
Basic
- Break the massive
problem into multiple
subproblems.
- Solves the low-level
problems and integrate
them into a larger one.
Process
- Submodules are
solitarily analyzed.
- Examine what data is
to be encapsulated, and
implies the concept of
information hiding.
Communication
- The communications
is less among modules.
- In this, modules must
have communication.
Redundancy
- Contain redundant
information.
- Redundancy can be
eliminated.
Table 1.1: Comparison between top-down and bottom-up design approaches.
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 16 / 40
Algorithm Design Approaches
Algorithm 2 Top-down approach: nth number in the Fibonacci sequence
Require: non-negative integer n
Ensure: F(n); nth number in the Fibonacci sequence.
1: function F(n)
2: if n ≤ 1 then
3: return n
4: else
5: return F(n − 1) + F(n − 2)
6: end if
7: end function
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 17 / 40
Algorithm Design Approaches
Algorithm 3 Bottom-up approach: nth number in the Fibonacci sequence
Require: non-negative integer n
Ensure: nth number in the Fibonacci sequence.
1: if n = 0 or n = 1 then
2: print n
3: else
4: A ← 0
5: B ← 1
6: for I ← 2 to n do
7: Temp ← A + B
8: A ← B
9: B ← Temp
10: end for
11: write B
12: end if
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 18 / 40
Algorithm Design Approaches
Algorithm 4 Top-down approach: Multiplication of 1 to n numbers
Require: non-negative integer n
Ensure: PRODUCT(n); Multiplication of 1 to n numbers.
1: function PRODUCT(n)
2: if n = 1 then
3: return 1
4: else
5: return n ∗ PRODUCT(n − 1)
6: end if
7: end function
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 19 / 40
Algorithm Design Approaches
Algorithm 5 Bottom-up approach: Multiplication of 1 to n numbers
Require: non-negative integer n
Ensure: Multiplication of 1 to n numbers.
1: Result ← 1
2: for I ← 1 to n do
3: Result ← Result ∗ I
4: end for
5: write Result
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 20 / 40
Outline
1 Algorithm Specification
Introduction to Algorithm
Algorithm Design and Data Structure
Analyzing an Algorithm
Algorithm Design Approaches
Pseudocode Conventions
2 Data Abstraction
Data Types
Abstract Data Types
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 21 / 40
Pseudocode Conventions
Till now, we have described each step of an algorithm using natural
language like English. Here, we present most of our algorithms using
pseudocodes that resemble C and Pascal.
1 Comments begin with // and continue until the end of line.
2 Blocks are indicated with matching braces: { and }. The body of a
procedure also forms a block. Statements are delimited by ;
3 An identifier begins with a letter. The data types of variables are not
explicitly declared. The types will be clear from the context. Whether
it is global or local to a procedure will also be evident from context.
4 Compound data types can be formed with records.
node = record
{ datatype 1 data 1;
datatype 2 data 2;
node ∗ link; }
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 22 / 40
Pseudocode Conventions
5 link is a pointer to the record type node. For example, if p points to
a record of type node, p←data 1 stands for the value of the first
field. If q is a record of type node, q.data 1 denotes its first field.
6 Assignment of values to variables is done using the assignment
operator or leftwards arrow symbol.
variable := expression; or variable ← expression;
7 There are two boolean values true and false. In order to produce
these values, the logical operators and, or and not and the relational
operators <, ≤, =, ̸=, ≥ and > are provided.
8 Elements of multidimensional arrays are accessed using [ and ]. E.g.,
if A is a two dimensional array, the (i, j)th element of the array is
denoted as A[i, j]. Array indices start at zero.
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 23 / 40
Pseudocode Conventions
9 The following statements are employed for while, for and do...while.
while < condition > do
Statement 1
Statement 2
Statement n
end while
while < condition > do {
Statement 1;
Statement 2;
Statement n; }
10 The general form of a for loop is
for I ← l to m step s do
Statement 1
Statement 2
Statement n
end for
for I := l to m step s do {
Statement 1;
Statement 2;
Statement n; }
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 24 / 40
Pseudocode Conventions
11 The general form of a do...while loop is
do
Statement 1
Statement 2
Statement n
while< condition >
do{
Statement 1;
Statement 2;
Statement n; }
while< condition >
12 A conditional statement has the following forms.
if < condition > then < statement >
if < condition > then < statement1 > else < statement2 > =0
13 We also employ the following case statement.
case {
:< condition1 >: < statement 1 >
:< condition2 >: < statement 2 >
: else : < statement n > }
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 25 / 40
Pseudocode Conventions
14 Input and output are done using the instructions read and write.
15 There is only one type of procedure: Algorithm or Procedure. An
algorithm consists of a heading and a body. The heading takes the
form.
Algorithm Name (< P1, P2, ..., P3 >)
Algorithm 6 Maximum of n given numbers
1: procedure MAX(A, n)
2: Result ← A[1]
3: for i ← 2 to n do
4: if A[i] > Result then Result ← A[i]
5: end if
6: end for
7: return Result
8: end procedure
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 26 / 40
Pseudocode Conventions
Algorithm 7 Selection Sort
1: procedure SelectionSort(a, n)
2: //Sort the array a[1 : n] into ascending order.
3: for i ← 1 to n do
4: j ← i
5: for k ← i + 1 to n do
6: if a[k] < a[j] then j ← k
7: end if
8: end for
9: t ← a[i]
10: a[i] ← a[j]
11: a[j] ← t
12: end for
13: end procedure
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 27 / 40
Pseudocode Conventions
Algorithm 8 Towers of Hanoi
1: procedure TowerOfHanoi(n, x, y, z)
2: {
3: //Move the top n disks from tower x to tower y.
4: if n ≥ 1 then {
5: TOWEROFHANOI(n − 1, x, y, z);
6: write (”move top disk from tower”, x, ”to top of tower”, y);
7: TOWEROFHANOI(n − 1, z, y, x);
8: }
9: }
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 28 / 40
Outline
1 Algorithm Specification
Introduction to Algorithm
Algorithm Design and Data Structure
Analyzing an Algorithm
Algorithm Design Approaches
Pseudocode Conventions
2 Data Abstraction
Data Types
Abstract Data Types
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 29 / 40
Data Types
We are familiar with the basic data types of C. These include char,
int, float, and double. Some of these data types may be modified by
the keywords short, long, and unsigned.
In addition to these basic types, C helps us by providing two
mechanisms for grouping data together. These are the array and the
structure.
For example, An array is a collection of elements of the same basic
data type.
They are declared implicitly, for example, int list[5] defines a
five-element array of integers whose legitimate subscripts are in the
range 0 ... 4.
Structures are collections of elements whose data types need not be
the same.
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 30 / 40
Data Types
Data types are declarations for variables. This determines the type
and size of data associated with variables, and a set of operations
that act on those objects.
Whether your program is dealing with predefined data types or
user-defined data types, these two aspects must be considered:
objects and operations.
For example, the data type int consists of the objects {0, +1, -1,
+2, -2, ..., INT MAX, INT MIN}, where INT MAX and
INT MIN are the largest and smallest integers that can be
represented on your machine.
The operations on integers are many, and would certainly include the
arithmetic operators +, -, *, /, and %.
There is also testing for equality/inequality and the operation that
assigns an integer to a variable.
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 31 / 40
Data Types
Knowing all of the facts about the operations on a data type, we
might also want to know about how the objects of the data type are
represented.
E.g., a char is represented as a bit string occupying 1 byte of
memory on most computers, where as an int might occupy 2 or
possibly 4 bytes of memory. If 2 eight-bit bytes are used, then
INT MAX is 215 -1 = 32,767.
Knowing the representation of the objects of a data type can be
useful and dangerous.
It has been observed by many software designers that hiding the
representation of objects of a data type from its users is a good
design strategy.
In that case, the user is constrained to manipulate the objects solely
through the functions that are provided.
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 32 / 40
Outline
1 Algorithm Specification
Introduction to Algorithm
Algorithm Design and Data Structure
Analyzing an Algorithm
Algorithm Design Approaches
Pseudocode Conventions
2 Data Abstraction
Data Types
Abstract Data Types
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 33 / 40
Abstract Data Types
An Abstract Data Type (ADT) is a data type that is organized in
such a way that the specification of the objects and the operations
on the objects is separated from the representation of the objects
and the implementation of the operations.
The specification consists of the names of every function, the type
of its arguments and result.
There should also be a description of what the function does, but
without appealing to internal representation or implementation
details.
This requirement is quite important, and it implies that an abstract
data type is implementation-independent.
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 34 / 40
Abstract Data Types
ADT NaturalNumber is
Objects: An ordered sub-range of the integers starting at zero and ending
at the maximum integer (INT-MAX) on the computer.
Functions: for all x, y ∈ NaturalNumber; TRUE, FALSE ∈ Boolean,
where +, -, <, and == are the usual integer operations.
Functions Operations
NaturalNumber Zero() 0
Boolean IsZero(x)
if (x) return FALSE
else return TRUE
Boolean Equal(x, y)
if (x == y) return TRUE
else return FALSE
NaturalNumber Successor(x)
if (x == INT MAX) return x
else return x+1
Table 2.1: Abstract data type NautralNumber
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 35 / 40
Abstract Data Types
Functions Operations
NaturalNumber Add(x, y)
if (x+y <= INT MAX) return x+y
else return INT MAX
NaturalNumber Subtract(x, y)
if(x<y) return 0
else return x-y
Table 2.2: Abstract data type NautralNumber
The ADT definition begins with the name of the ADT. There are two
main sections in the definition: the objects and the functions.
The objects are defined in terms of the integers, but we make no
explicit reference to their representation.
For each function, we place the result type to the left of the function
name and a definition of the function to the right.
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 36 / 40
Abstract Data Types
ADT is a type (or class) for objects whose behavior is defined by a
set of value 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. The process of providing only the essentials and hiding the
details is known as abstraction.
A user only needs to know what a data type can do, but not how it
will be implemented. Think of ADT as a black box which hides the
inner structure and design of the data type.
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 37 / 40
Abstract Data Types
Figure 2.1: Abstract data types in data structures
If Stack, Queue and Linked List are the ADTs, then {push(),
pop(), peep()}, {enque(), dequeqe()}, {insert(), delete()} are
the few operations performed on these data structures.
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 38 / 40
Summary
Here, we have discussed
Algorithm and characteristics of an algorithm.
Rules to be followed for design and analysis of an algorithm.
The differentiation of data structures, file structures, and storage
structures.
Top-down and bottom-up design approaches through examples.
Rules to be followed while writing the pseudo code of an algorithm.
Abstract data type and its necessity in a program.
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 39 / 40
For Further Reading I
H. Sahni and A. Freed.
Fundamentals of Data Structures in C (2nd edition).
Universities Press, 2008.
A. K. Rath and A. K. Jagadev.
Data Structures Using C (2nd edition).
Scitech Publications, 2011.
Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 40 / 40

Algorithm Specification and Data Abstraction

  • 1.
    Algorithm Specification andData Abstraction Dr. Ashutosh Satapathy Assistant Professor, Department of CSE VR Siddhartha Engineering College Kanuru, Vijayawada September 22, 2022 Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 1 / 40
  • 2.
    Outline 1 Algorithm Specification Introductionto Algorithm Algorithm Design and Data Structure Analyzing an Algorithm Algorithm Design Approaches Pseudocode Conventions 2 Data Abstraction Data Types Abstract Data Types Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 2 / 40
  • 3.
    Outline 1 Algorithm Specification Introductionto Algorithm Algorithm Design and Data Structure Analyzing an Algorithm Algorithm Design Approaches Pseudocode Conventions 2 Data Abstraction Data Types Abstract Data Types Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 3 / 40
  • 4.
    Introduction to Algorithm Analgorithm is a well defined computational procedure that takes some value, or set of values, as input and produces some value, or set of values as output. It is thus a sequence of computational steps that transform the input into the output. The algorithm describes a computational procedure for achieving that input/output relationship. An algorithm is a finite set of instructions which accomplish a particular task. Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 4 / 40
  • 5.
    Introduction to Algorithm Everyalgorithm must satisfy the following criteria. 1 Input: There are some (possibly empty) input data which are externally supplied to the algorithm. 2 Output: There will be at least one output. 3 Definiteness: Each instruction/step of the algorithm must be clear and unambiguous. 4 Finiteness: If we trace out the instruction/step of an algorithm, then for all cases, the algorithm will terminate after a finite number of steps. 5 Effectiveness: The steps of an algorithm must be sufficiently basic that it can be carried out by a person mechanically using pen and paper. Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 5 / 40
  • 6.
    Introduction to Algorithm Algorithm1 Swap two numbers using third variable 1: start 2: read two values into two variables a, b 3: declare third variable, temp 4: temp = a 5: a = b 6: b = temp 7: write a, b ▷ Display a and b values 8: stop Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 6 / 40
  • 7.
    Outline 1 Algorithm Specification Introductionto Algorithm Algorithm Design and Data Structure Analyzing an Algorithm Algorithm Design Approaches Pseudocode Conventions 2 Data Abstraction Data Types Abstract Data Types Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 7 / 40
  • 8.
    Algorithm Design andData Structure To write a computer program for solving a problem we must start with the following four tasks: Identify the data items and their relationship. Find the operations that must be performed on these data items. Determine the best method to represent these data items in computer’s memory. Select a suitable programming language which can efficiently code the identified data representation. Note These data items can be arranged in different ways depending on the logical relationship between them. Arrangement of these data items is called data structures. E.g., arrays, records, lists, and files Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 8 / 40
  • 9.
    Algorithm Design andData Structure Important The storage of data in primary memory is called as data structure where as the storage of data in secondary memory is called file structure. The representation or the data structure in computer’s memory which is known as storage structure. Selection of a programming language best suited to represent the data structure. Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 9 / 40
  • 10.
    Outline 1 Algorithm Specification Introductionto Algorithm Algorithm Design and Data Structure Analyzing an Algorithm Algorithm Design Approaches Pseudocode Conventions 2 Data Abstraction Data Types Abstract Data Types Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 10 / 40
  • 11.
    Analyzing an Algorithm Algorithmsare designed using basic program constructs like sequence, selection or branching, repetition, etc. We need methods to separate bad algorithm from good ones and to choose the most effective approach for our problem. In analyzing an algorithm, the first approach is to check the correctness of the algorithm. Tracing the algorithm. Checking the algorithm for logical correctness. Implementing the algorithm. Testing the algorithm, which can yield correct output for all possible combinations of input values. This is called program proving or program verification. Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 11 / 40
  • 12.
    Analyzing an Algorithm Anotherapproach of analysis is to check the simplicity of the algorithm. The simplest way of solving the problem is not always the best one. It is necessary to evaluate the complexity of the algorithm. The complexity of the algorithm is determined in terms of time and space. These determine the amount of time and storage an algorithm may require for execution. Finally, predict the performance of the algorithm in the best, worst, and average cases. Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 12 / 40
  • 13.
    Outline 1 Algorithm Specification Introductionto Algorithm Algorithm Design and Data Structure Analyzing an Algorithm Algorithm Design Approaches Pseudocode Conventions 2 Data Abstraction Data Types Abstract Data Types Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 13 / 40
  • 14.
    Algorithm Design Approaches Figure1.1: Top-down and bottom-up design approaches Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 14 / 40
  • 15.
    Algorithm Design Approaches Acomplicated algorithm is split into small parts called modules, and the process of splitting is known as modularization. Modularization significantly reduces the complications of designing an algorithm and make its process more easier to design and implement. It is necessary to evaluate the complexity of the algorithm. In the top-down approach, the complex module is divided into submodules. In bottom-up approach begins with elementary modules and then combine them further. Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 15 / 40
  • 16.
    Algorithm Design Approaches Basisfor Comparison Top-down Approach Bottom-up Approach Basic - Break the massive problem into multiple subproblems. - Solves the low-level problems and integrate them into a larger one. Process - Submodules are solitarily analyzed. - Examine what data is to be encapsulated, and implies the concept of information hiding. Communication - The communications is less among modules. - In this, modules must have communication. Redundancy - Contain redundant information. - Redundancy can be eliminated. Table 1.1: Comparison between top-down and bottom-up design approaches. Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 16 / 40
  • 17.
    Algorithm Design Approaches Algorithm2 Top-down approach: nth number in the Fibonacci sequence Require: non-negative integer n Ensure: F(n); nth number in the Fibonacci sequence. 1: function F(n) 2: if n ≤ 1 then 3: return n 4: else 5: return F(n − 1) + F(n − 2) 6: end if 7: end function Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 17 / 40
  • 18.
    Algorithm Design Approaches Algorithm3 Bottom-up approach: nth number in the Fibonacci sequence Require: non-negative integer n Ensure: nth number in the Fibonacci sequence. 1: if n = 0 or n = 1 then 2: print n 3: else 4: A ← 0 5: B ← 1 6: for I ← 2 to n do 7: Temp ← A + B 8: A ← B 9: B ← Temp 10: end for 11: write B 12: end if Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 18 / 40
  • 19.
    Algorithm Design Approaches Algorithm4 Top-down approach: Multiplication of 1 to n numbers Require: non-negative integer n Ensure: PRODUCT(n); Multiplication of 1 to n numbers. 1: function PRODUCT(n) 2: if n = 1 then 3: return 1 4: else 5: return n ∗ PRODUCT(n − 1) 6: end if 7: end function Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 19 / 40
  • 20.
    Algorithm Design Approaches Algorithm5 Bottom-up approach: Multiplication of 1 to n numbers Require: non-negative integer n Ensure: Multiplication of 1 to n numbers. 1: Result ← 1 2: for I ← 1 to n do 3: Result ← Result ∗ I 4: end for 5: write Result Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 20 / 40
  • 21.
    Outline 1 Algorithm Specification Introductionto Algorithm Algorithm Design and Data Structure Analyzing an Algorithm Algorithm Design Approaches Pseudocode Conventions 2 Data Abstraction Data Types Abstract Data Types Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 21 / 40
  • 22.
    Pseudocode Conventions Till now,we have described each step of an algorithm using natural language like English. Here, we present most of our algorithms using pseudocodes that resemble C and Pascal. 1 Comments begin with // and continue until the end of line. 2 Blocks are indicated with matching braces: { and }. The body of a procedure also forms a block. Statements are delimited by ; 3 An identifier begins with a letter. The data types of variables are not explicitly declared. The types will be clear from the context. Whether it is global or local to a procedure will also be evident from context. 4 Compound data types can be formed with records. node = record { datatype 1 data 1; datatype 2 data 2; node ∗ link; } Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 22 / 40
  • 23.
    Pseudocode Conventions 5 linkis a pointer to the record type node. For example, if p points to a record of type node, p←data 1 stands for the value of the first field. If q is a record of type node, q.data 1 denotes its first field. 6 Assignment of values to variables is done using the assignment operator or leftwards arrow symbol. variable := expression; or variable ← expression; 7 There are two boolean values true and false. In order to produce these values, the logical operators and, or and not and the relational operators <, ≤, =, ̸=, ≥ and > are provided. 8 Elements of multidimensional arrays are accessed using [ and ]. E.g., if A is a two dimensional array, the (i, j)th element of the array is denoted as A[i, j]. Array indices start at zero. Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 23 / 40
  • 24.
    Pseudocode Conventions 9 Thefollowing statements are employed for while, for and do...while. while < condition > do Statement 1 Statement 2 Statement n end while while < condition > do { Statement 1; Statement 2; Statement n; } 10 The general form of a for loop is for I ← l to m step s do Statement 1 Statement 2 Statement n end for for I := l to m step s do { Statement 1; Statement 2; Statement n; } Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 24 / 40
  • 25.
    Pseudocode Conventions 11 Thegeneral form of a do...while loop is do Statement 1 Statement 2 Statement n while< condition > do{ Statement 1; Statement 2; Statement n; } while< condition > 12 A conditional statement has the following forms. if < condition > then < statement > if < condition > then < statement1 > else < statement2 > =0 13 We also employ the following case statement. case { :< condition1 >: < statement 1 > :< condition2 >: < statement 2 > : else : < statement n > } Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 25 / 40
  • 26.
    Pseudocode Conventions 14 Inputand output are done using the instructions read and write. 15 There is only one type of procedure: Algorithm or Procedure. An algorithm consists of a heading and a body. The heading takes the form. Algorithm Name (< P1, P2, ..., P3 >) Algorithm 6 Maximum of n given numbers 1: procedure MAX(A, n) 2: Result ← A[1] 3: for i ← 2 to n do 4: if A[i] > Result then Result ← A[i] 5: end if 6: end for 7: return Result 8: end procedure Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 26 / 40
  • 27.
    Pseudocode Conventions Algorithm 7Selection Sort 1: procedure SelectionSort(a, n) 2: //Sort the array a[1 : n] into ascending order. 3: for i ← 1 to n do 4: j ← i 5: for k ← i + 1 to n do 6: if a[k] < a[j] then j ← k 7: end if 8: end for 9: t ← a[i] 10: a[i] ← a[j] 11: a[j] ← t 12: end for 13: end procedure Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 27 / 40
  • 28.
    Pseudocode Conventions Algorithm 8Towers of Hanoi 1: procedure TowerOfHanoi(n, x, y, z) 2: { 3: //Move the top n disks from tower x to tower y. 4: if n ≥ 1 then { 5: TOWEROFHANOI(n − 1, x, y, z); 6: write (”move top disk from tower”, x, ”to top of tower”, y); 7: TOWEROFHANOI(n − 1, z, y, x); 8: } 9: } Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 28 / 40
  • 29.
    Outline 1 Algorithm Specification Introductionto Algorithm Algorithm Design and Data Structure Analyzing an Algorithm Algorithm Design Approaches Pseudocode Conventions 2 Data Abstraction Data Types Abstract Data Types Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 29 / 40
  • 30.
    Data Types We arefamiliar with the basic data types of C. These include char, int, float, and double. Some of these data types may be modified by the keywords short, long, and unsigned. In addition to these basic types, C helps us by providing two mechanisms for grouping data together. These are the array and the structure. For example, An array is a collection of elements of the same basic data type. They are declared implicitly, for example, int list[5] defines a five-element array of integers whose legitimate subscripts are in the range 0 ... 4. Structures are collections of elements whose data types need not be the same. Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 30 / 40
  • 31.
    Data Types Data typesare declarations for variables. This determines the type and size of data associated with variables, and a set of operations that act on those objects. Whether your program is dealing with predefined data types or user-defined data types, these two aspects must be considered: objects and operations. For example, the data type int consists of the objects {0, +1, -1, +2, -2, ..., INT MAX, INT MIN}, where INT MAX and INT MIN are the largest and smallest integers that can be represented on your machine. The operations on integers are many, and would certainly include the arithmetic operators +, -, *, /, and %. There is also testing for equality/inequality and the operation that assigns an integer to a variable. Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 31 / 40
  • 32.
    Data Types Knowing allof the facts about the operations on a data type, we might also want to know about how the objects of the data type are represented. E.g., a char is represented as a bit string occupying 1 byte of memory on most computers, where as an int might occupy 2 or possibly 4 bytes of memory. If 2 eight-bit bytes are used, then INT MAX is 215 -1 = 32,767. Knowing the representation of the objects of a data type can be useful and dangerous. It has been observed by many software designers that hiding the representation of objects of a data type from its users is a good design strategy. In that case, the user is constrained to manipulate the objects solely through the functions that are provided. Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 32 / 40
  • 33.
    Outline 1 Algorithm Specification Introductionto Algorithm Algorithm Design and Data Structure Analyzing an Algorithm Algorithm Design Approaches Pseudocode Conventions 2 Data Abstraction Data Types Abstract Data Types Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 33 / 40
  • 34.
    Abstract Data Types AnAbstract Data Type (ADT) is a data type that is organized in such a way that the specification of the objects and the operations on the objects is separated from the representation of the objects and the implementation of the operations. The specification consists of the names of every function, the type of its arguments and result. There should also be a description of what the function does, but without appealing to internal representation or implementation details. This requirement is quite important, and it implies that an abstract data type is implementation-independent. Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 34 / 40
  • 35.
    Abstract Data Types ADTNaturalNumber is Objects: An ordered sub-range of the integers starting at zero and ending at the maximum integer (INT-MAX) on the computer. Functions: for all x, y ∈ NaturalNumber; TRUE, FALSE ∈ Boolean, where +, -, <, and == are the usual integer operations. Functions Operations NaturalNumber Zero() 0 Boolean IsZero(x) if (x) return FALSE else return TRUE Boolean Equal(x, y) if (x == y) return TRUE else return FALSE NaturalNumber Successor(x) if (x == INT MAX) return x else return x+1 Table 2.1: Abstract data type NautralNumber Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 35 / 40
  • 36.
    Abstract Data Types FunctionsOperations NaturalNumber Add(x, y) if (x+y <= INT MAX) return x+y else return INT MAX NaturalNumber Subtract(x, y) if(x<y) return 0 else return x-y Table 2.2: Abstract data type NautralNumber The ADT definition begins with the name of the ADT. There are two main sections in the definition: the objects and the functions. The objects are defined in terms of the integers, but we make no explicit reference to their representation. For each function, we place the result type to the left of the function name and a definition of the function to the right. Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 36 / 40
  • 37.
    Abstract Data Types ADTis a type (or class) for objects whose behavior is defined by a set of value 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. The process of providing only the essentials and hiding the details is known as abstraction. A user only needs to know what a data type can do, but not how it will be implemented. Think of ADT as a black box which hides the inner structure and design of the data type. Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 37 / 40
  • 38.
    Abstract Data Types Figure2.1: Abstract data types in data structures If Stack, Queue and Linked List are the ADTs, then {push(), pop(), peep()}, {enque(), dequeqe()}, {insert(), delete()} are the few operations performed on these data structures. Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 38 / 40
  • 39.
    Summary Here, we havediscussed Algorithm and characteristics of an algorithm. Rules to be followed for design and analysis of an algorithm. The differentiation of data structures, file structures, and storage structures. Top-down and bottom-up design approaches through examples. Rules to be followed while writing the pseudo code of an algorithm. Abstract data type and its necessity in a program. Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 39 / 40
  • 40.
    For Further ReadingI H. Sahni and A. Freed. Fundamentals of Data Structures in C (2nd edition). Universities Press, 2008. A. K. Rath and A. K. Jagadev. Data Structures Using C (2nd edition). Scitech Publications, 2011. Dr. Ashutosh Satapathy Algorithm Specification and Data Abstraction September 22, 2022 40 / 40