Ds Module 1 Notes
Ds Module 1 Notes
Ds Module 1 Notes
MODULE 1
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
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).
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,
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.
DATA DATA
Communication
FUNCTION FINCTION
DATA
FUNCTION
7
Federal institute of Science And Technology (FISAT) Department of Computer Science &Engineering
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.
4.Can be used to estimate the size of the largest problem that a program can solve.
Federal Institute of Science And Technology (FISAT)
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.
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.
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)
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)
f(n)
cg(n)
f(n) =n(g(n)
nÙ
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)
14