Ds Module 1 Notes

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Department of Computer Science &Engineering

Federal Institute of Science And Technology (FISAT)

MODULE 1

Introduction to programming methodologies


Programming methodology deals with the analysis, design and implementation of
programs.

Algorithm
Algorithm is astep-by-step finite sequence of instruction, to solve a well-defined
computational problem.
That is, in practice to solve any complex real life problems; first we have to
define the problems. Second step is todesign the algorithm to solve that problem.
Writing and executing programs and then optimizing them may be effective
for small programs. Optimization of a program is directly concerned with
algorithm design. But for a large program, each part of the program must be well
organized before writing the program. There are few steps of refinement involved
when a problem is converted to program; this method is called stepwise
refinement method. There are two approaches for algorithm design; they are
top-down and bottom-up algorithm design.
Stepwise Refinement Techniques
We can write an informal algorithm, if we have an appropriate mathematical
model for a problem. The initial version of the algorithm will contain general
statements, Le., informal instructions. Then we convert this informal algorithm to

formal algorithm, that is, more definite instructions by applying any programming
language syntax and semantics partially. Finally a program can be developed by
converting the formal algorithm by a programming language manual. From the
above discussion we have understood that there are several steps to reach a
program from a mathematical model. In every step there is a refinement (or
conversion).
That is to convert an informal algorithm to a program, we must go through several
stages of formalization until we arrive at a programwhose meaning is formally
defined by a programming larnguage manualis called stepwise refinement
techniques.
There are three steps in refinement process, which is illustrated in Figure
1
Federal Institute of Science And Technology (FISAT) Department of Computer Science &Engineering

Mathematical Farmal Data


Model Structures
Language
Informal Psedo-language CIC+
Algorithm Program Program

1. In the first stage, modeling, we try to represent the problem using an appropriate
mathematical model such as a graph, tree etc. At this stage, the solution to the
problem is an algorithm expressed very informally.
2. At the next stage, the algorithm is written in pseudo-language (or formal algorithm)
that is, a mixture of any programming language constructs and less formal English
statements. The operations to be performed on the various types of data become fixed.
3. In the final stage we choose an implementation for each abstract data type and write
the procedures for the various operations on that type. The remaining in formal
statements in the pseudo-language algorithm are replaced by (or any programming
language) C/C++ code.

Programming style
Following sections will discuss different programming methodologies to design a
program.
1. Procedural
2. Modular

3. Structured

4. Object oriented

1. Procedural Programming
Procedural programming is a paradigmn based on the concept of using
procedures. Procedure (sometimes also called subprogram, routine or method) is a
sequence of commands to be executed. Any procedure can be called from any point
within the general program, including other procedures or even itself (resulting in a
recursion).

Procedural programming is widely used in large-scale projects, when the following


benefits are important:
re-usability of pieces code designed as procedures
2
Departrment of Computer Science &Engineering
Federal Institute of Sclence And Technology (FISAT)

case of following the logic of program;


Maintainability of code.
Emphasis is on doing things (algorithms).
Most of the functions share global data.
function to function.
move openly around the system from
Data
another.
Functions transform data from one form to
programming is a sub-paradigm of imperative programming, since each step
Procedural procedures.
even if by the means of defining
of computation is described explicitly,
Medn
Loroeramtdata

Prooedure D Procedure X
Procedure A

Prooedure Y Prooedure Z

2. Modular Programming
modules.
program is progressively decomposed into smaller partition called
The
problem
modular form, thus allowing an overall
The program can easily be written in
individual sub programs. Thus we can consider, a
to be decomposed into a sequence of
module. Conversely, a number of
module decomposed into successively subordinate
superior module.
modules can combined together to form a
program and the superior module,
Asub-module, are located elsewhere in the
subordinate module and call for its execution.
whenever necessary make a reference to
is
is known as a calling, and this module
This activity on part of the superior module
referred as called module. The sub module
referred as calling module, and sub module
or procedures.
may be subprograms such as function
modular program
The following are thesteps needed to develop a
1. Define the problem
the goal
2. Outline the steps needed to achieve
into subtasks
3. Decompose the problem
each sub task
4. Prototype a subprogram for

3
Federal Institute of Science And Technology (FISAT) Department of Cormputer Science &Engneerng

5. Repeat step 3 and 4 for each subprogram until further decomposition seems
counter productive

program
main progtan
datt

module module
datd +datd, data +datd,

procedu te, ptocoduta procedurc,

Modular Programming is heavily procedural. The focus is


entirely on writing code
(functions). Data is passive in Modular Programming. Modular
Programming
discourages the use of control variables and flags in parameters; their
to
presence tends
indicate that the caller needs to know too much about how the
function is
implemented.

Two methods may be used for modular


programming. They are known as top
down and bottom-up. Regardless of whether the top-down or
bottom-up method is
used, the end result is a modular program. This end result is
important, because not
all errors may be detected at the time of the initial
testing. It is possible that there are
still bugs in the program. If an error is discovered after
the program supposedly has
been fully tested, then the modules concerned can be
isolated and retested by them.
Regardless the design method used, if a program has been written in modular form,
of
it is easier to detect the source of the error and to
test it in isolation, than if the
program were written as one function.
Advantages of modular programming
1. Reduce the complexity of the entire problem
2. Avoid the duplication of code
3. debugging program is easier and reliable
4. Improves the performance
5. Modular program hides the use of data structure

4
6. Federal
at theprimitivesprogramming
complicated modules
modules
intrinsically Top-down
amodular 9. 8. 7.
style procedures
Bottom-Up
modular methods. branches whichsimpler which
generate
or main Reusability-modules
Modular
It Global
thedesign reduces Institute
top.
of
Bottom-up control and Top-down
a module The data
modules to hierarchy accordingprinciples
processes of
with programming
of program Science
The features, the higher simpler understood the also
is and
bottom-upspecific
programming algorithm that passed algorithm
development hidden
And
of until to its imprOves Technology
levels pieces, DownTop of
modules until where are modules and softwareprogramming
related
top-down can
the in
the of
based downward design be (FISAT)
method design the each cannot work the module
all an module modules. used
and language,
of on
structure. having is is
Module
4
engineering design portability
application is used, a be in
isbuld the functionality, through technique MOcule
other
the further
consists
widelyapplication a Eachdictate
5 and opposite and Module
them That single 5 Module and program
of
used constructing is the breaking for subdivided. only module that
1
program
into Department
constructed usually is entry programming
for more has
of structure organizing
top-down
Medule
6
Mcdule
3 of should a without
top-down and elementary
program of
testing, been the Computer
complex gradually in a
written. the specification
single and also rewriting
startingdesign. programmingwithout style. should Science
because form coding be
structures, exit process
That more of Thedivided be and Engineering
&
with It unconditional point,
each is, and refers
functions programs
down division intodivided retesting
tends that
starting existing
ending of
and
more to into are sub into
the or to in in of
a
Federa instute of Soence And Technology (FISAT) Departnent of Computer Soence &Engineering

lowest-level functions is written and tested first. This testing is done by special test
functions that call the low-level functions, providing them with different pararmeters
and examining the results for correctness. Once lowest-level functions have been tested
and verified to be correct, the next level of functions may be tested. Since the
lowest
level functions already have been tested, any detected errors are probably due to the
higher-level functions. This process continues, moving up the levels, until finally the
main function is tested.

Mcdule
1

Module Moduie
3

Bottom
Module Module Module
Up 4 5 6

3. Structured Programming
It is a
programming style; and this style of programming is known by several names:
Procedural decomposition, Structured programming, etc. Structured
not programming with structures but by using
programming is
following types of code structures to
write programs:
1. Sequence of sequentially executed statements
2. Conditional execution of statements (ie., "if
statements)
3. Looping or iteration (ie., for, do...while, and while" statements)
4. Structured subroutine calls (ie., functions)
In particular, the following language usage is forbidden:
" "GoTo statements
" "Break" or continue"out of the middle of loops
" Multiple exit points to a function/ procedure
/subroutine (Le., multiple return"
statemnents)
" Multiple entry points to a
function/procedure/subroutine/method
In this style of programming there is a great risk that
implementation details of
many data structures have to be shared between functions, and thus
globally exposed.
Federal Instute of Science And Technology (FISAT) Department of Computer Science &Engineering

This in turn tempts other functions to use these implementation details; there by
creating unwanted dependencies between difflerent parts of the program. The main
disadvantage is that all decisions made from the start of thc project depend directly or
indirectly on the high-level specification of the application. It is a well known fact that
this specification tends to change over a time. When that happens, there is agreat risk
that large parts of the application need to be rewritten.

Advantages of structured programming


to their control
1. clarity: structured programming has a clarity and logical pattern
structure and due to this tremendous increase in programming productivity
has a single entry
2. another key to structured programming is that each block of code
sequence of code into modules
point and single exit point. So we can break up long
programming is of great
3. Maintenance: the clarity and modularity inherent in structured
code.
help in finding an error and redesigning the required section of

4. Object oriented programming


approach is to
The major motivating factor in the invention of object-oriented
modular approach. 00P
remove some of the flaws encountered in the procedural or
and does not allow it to
treats data as a critical element in the program development
function that operate on
flow freely around the system. It ties data more closely to the
it, and protects it from accidental modification from outside function. OOP allows
and then builds
decomposition of a problem into a number of entities called objects
data and function in
data and function around these objects. The organization of
accessed only by
object-oriented programs is shown in fig. The data of an object can be
can access the
the function associated with that object. However, function of one object
function of other objects.
Urganization of data and hunction in 00P
Object A Object B

DATA DATA
Communication
FUNCTION FINCTION

DATA

FUNCTION

7
Federal institute of Science And Technology (FISAT) Department of Computer Science &Engineering

Some of the features of object oriented programming are:


" Emphasis is on data rather than procedure.
" Programs are divided into what are known as objects.
" Data structures are designed such that they characterize the objects.
" Functions that operate on the data of an object are ties together in the data
structure.
" Data is hidden and cannot be accessed by
external function.
" Objects may communicate with each other through
function.
" New data and functions can be easily
added whenever necessary.
" Follows bottom up
approach in program design.
Analysis of Algorithm
After designing an algorithm, it has to be checked and its correctness needs to
be predicted; this is done by analyzing the algorithm. The
algorithm can be analyzed by
tracing all step-by-step instructions, reading the algorithmn for logical correctness, and
testing it on some data using mathematical techniques to prove it correct. Another type
of analysis is to analyze thesimplicity of the algorithm. That is, design the
algorithm in
a simple way so that it becomes easier to be implemented. However, the
simplest and
most Straight forward way of solving a problem may not be sometimes the
best one.
Moreover there may be more than one algorithm to solve a problem. The choice of a
particular algorithm depends on following performance analysis and measurements:
1. Space complexity
2. Time complexity
Space Complexity
Analysis of space complexity of an algorithm or program is the amount of memory it
needs to run to completion. Some of the reasons for studying space complexity are:

1. If the program is to run on multi user system, it may be required to specify the
amount of memory to be allocated to the program.

2. We may be interested to know in advance that whether sufficient memory is


available to run the program.

3. There may be several possible solutions with different space requirements.

4.Can be used to estimate the size of the largest problem that a program can solve.
Federal Institute of Science And Technology (FISAT)

The space needed bya program consists of following comnponents.


"Instruction space: Space needed to store the executable version of the prograrn and it
is fixed.
values and has further two
" Data space : Space needed to store all constants, variable
components:
This space is fixed.
(a) Space needed by constants and simple variables.
such as arraysand structures.
(b) Space needed by fixed sized structural variables.
() Dynamically allocated space. This space usually varies.
stack space: This space is needed to store the information to
"Environment
suspended (partially completed) functions. Each timne a function is invoked
resume the
environment stack:
the following data is saved on the
of the Called
it has to resume after completion
(a) Return address : Le., from where
function.
values of formal parameters in the function
(b) Values of all lead variables and the
being invoked.
function is called the recursion stack
The amount of space needed by recursive
depends on the space needed by the
space. For each recursive function, this space
addition, this space depends on the
local variables and the formal parameter. In
of nested recursive calls.
maximum depth of the recursion ie., maximum number

Time Complexity
amount of time it needs
The time complexity of an algorithm or a program is the
the implementation of the
to run to completion. The exact time will depend on
used, the
algorithm, programming language, optimizing the capabilities of the compiler
measure the
CPU speed, other hardware characteristics/ specifications and soon. To
time complexity accurately, we have to count all sorts of operations performed in an
algorithm. If we know the time for each one of the primitive operations performed in a
given computer, we can easily compute the time taken by an algorithm to complete its
execution. This time will vary from machine to machine. By analyzing an algorithm, it
is hard tocome out with an exact time required. To find out exact time complexity, we
need to know the exact instructions executed by the hardware and the time required
Federal Institute of Science And Technology (FlSAT) Department of Computer Science &Engineering

for the instruction. The time complexity also depends on the amount of data inputted
to an algorithm. But we can calculate the order of magnitude for the time required.

The time complexity also depends on the amount of data input to an


algorithm,
but we can calculate the order of magnitude for the time
required. That is, our
intention is to estimate the execution time of an algorithm
irrespective of the computer
machine on which it will be used.
Some of the reasons for studying time
complexity are
a) We may be interest to know in
advance that whether an algorithm or program
will provide a satisfactory real time
response
b) There may be several possible
solutions with different timne requirements
Here, the more sophisticated method is to identify the key
operations and count
such operations performed till the program completes its
execution. A key operation in
our algorithm is an operation that takes
maximum time among all possible operations
in the algorithm. Such an abstract,
theoretical approach is not only useful for
discussing and comparing algorithms, but also it is useful to improve solutions to
practical problems. The time complexity can now be expressed as function of
number
of key operations performed. Before we go ahead with our
discussions, it is important
to understand the rate growth analysis of an algorithm, as
shown in Figure

nlog, a

log, a

10
FederalInstute of Soience And Technology (FISAT) Department of Computer Science &Engineering

The function that involves n' as an exponent, i.e., 2", n", n ! are called
exponential functions, which is too slow except for small size input function where

growth is less than or equal to nC, (where Cis a constant) i.e.; n, n, n log2n, n, log2 n
reasonable sized
are said to be polynomial. Algorithms with polynomial time can solve
problems if the constant in the exponent is small.
are three cases:
When we analyze an algorithm it depends on the input data, there
1. Best case

2. Average case
3. Worst case
program might be expected to take on best
In the best case, the amount of time a
possible input data.
might be expected to take on typical
In the average case, the amount of time a program
(or average) input data.

would take on the worst possible


In the worst case, the amount of time a program
input configuration.
Frequency Count
that every
Frequency count method can be used to analyze a program. Here we assume
execution. Hence the
statement takes the same constant amount of time for its
determination of time complexity of a given program is the just matter of summing the
frequency counts of all the statements of that program Consider the following examples
for(i-0; I, n;it+) for(i-0;i<n;it+)
X++; for)j-0;j<nj++)
X++; xtt;

(a) (b) (c)

In the example (a) the statement x++ is not contained within any loop either explicit or
implicit. Then its frequency count is just one. In example (b) same element will be
executedn times and in example (3) it is executed by n. From this frequency count we
can analyze program

11
Federal Institute of Science And Technology Department of Computer Science &Engineering
(FISAT)

Growth of Functions and Asymptotic


Notation
Wien we study algorithms, we are interested in characterizing them according to their
emciency. We are usually interesting in the order of growth of the running time of an
algorithm, not in the exact running time. This is also referred to as the asymptotuc
running time. We need to develop away to talk about rate of growth of functions so that
We can compare algorithms. Asymptotic notation gives us a method for
classitying
functions according to their rate of growth.

Big-0 Notation
Definition:
f(n) = Olg(n)) if there are two positive constants c and n0 such that |f(n)|sc
g(n)|for all
n 2 n0. If f(n) is non negative, we can simplify the last condition to 0 s f(n) s c g(n)
for
all n n0. Then we say that "f(n) is big-0 of g(n)." As n
increases, f(n) grows no
faster than g(n). In other words, gln) is an asymptotic upper bound on f(n).

cgn)
f(n)

f(n) =0(8n)

Example: n2+n= O{n)


Proof: "Here, we have f(n) = n2 + n, and gln) = nó
"Notice that if n2 1, ns ný is clear.
"Also, notice that ifn 2 1, n2 s n3 is clear.
"In general, if asb, then n as nD whenever n 1. This fact is used often in these
types of proofs.
Therefore, n2 + ns n3 + n3= 2n3
"We have just shown that n +ns 2n3 for all n 1
12
jeral Institute of Science
And Technology
(FISAT) Department of Computer Science &Engineering

"Thus, we have shown that n2 + n = Oln3) (by


definition of Big 0, with
n0 = 1, and c = 2.)
Big-2 notation
Definition:
I\n) = S2|g(n)) iff there are two positive constants cand no such that |f(n)| 2c gn)T tor
all n 2no. If f(n) is nonnegative, we can simplify the last condition to 0 s cg(n) sf(n) for
all n 2 n0" then we say that f[n) is omega of g(n)."" As n increases, f(n) grows no
slower than g(n). In other words, g(n) is an asymptotic lower bound on t(n)

f(n)

cg(n)

f(n) =n(g(n)

Example: n3 +4n2 =Qn2)


Proof:"Here, we have f(n) = n + 4n2, and g(n) = n2
"It is not too hard to see that if n20, n s nÏ+ 4n2
"We have already seen that if n 1, n2 sn3
"Thus when n 1, n2 s n3 s n3 + 4n2
"Therefore, ln2 s nß + 4n2 for all n 1
"Thus, we have shown that ný + 4n2 = ln) (by definition of Big-S.,
with no = 1, and c =1.)
Big-0 notation
Definition:
f(n) = egln) iff there are three positive constants c1, C2 and no such that cl|g(n)| s
| fip)| s c2|g(n)| for alln 2nÍ. If f(n) is nonnegative, we can simplify the last condition
to 0s cl g(n) s f(n) s c2 g(n) for all n no. Then we say that f\n) is theta of g(n)."
13
Federal Institute of Soence And Technology (F1SAT) Departrment od Conputer enra h Engnosrng

As n increases, f(n) grows at the same rate as gln), In other words, gn) is an
asymptotically tight bound on f(n).

Bn) fn)

Example: n2 +5n +7=(n2)


Proof:When n 1, n2 +5n +7s n2 +5n2 + 7n2 s 13n2
" When n 0, n2 s n2 + 5n +7
Thus, when n 1

In2 sn2+ 5n +7s 13n2


Thus, we have shown that n² +5n +7 =0(n2) (by definition of Big-e, with
no = 1, cl = 1,and c2 = 13.)
Comparison of different Algorithmn

Algorithm Best case Average case Worst case


Quick sort O(n log n) O(n log n) o(n2)
Merge sort O(n log n) O(n log n) O(n log n)
Heap sort O(n log n) O(n log n) O(n log n)
Bubble sort O(n) Ofn2) O(n2)
Selection Sort O(n2) O(n2) O(n)
Insertion sort O(n) Ofn2) O(n)
Binary search O(1) O(log n) O(log n)
Linear search O(1) O(n) O(n)

14

You might also like