Module 2 - Algorithm and Features of Algorithms - Docx-1
Module 2 - Algorithm and Features of Algorithms - Docx-1
(Week 1)
Page 1 of 21
2.3 Characteristics of a good Algorithm
Some of the desirable characteristics of a well – written algorithms are:
(a) Integrity/Correctness: Integrity refers to the accuracy of a calculations
in an algorithm and is an absolutely necessity in any algorithm. All
algorithms’ enhancements are meaningless if calculations are not
correctly carried out.
(b) Clarity: Clarity refers to the overall readability of an algorithm with
particular emphasis on its underlying logic. If an algorithm is written
clearly, it should be possible for another programmer to follow the
algorithm logic without undue effort.
(c) Simplicity: clarity and accuracy of an algorithm can be enhanced by
making algorithm logic simple and consistent with the overall algorithm
objectives. This can be achieved by sacrificing certain amount of
computation efficiency in order to maintain a relatively simple and
straightforward algorithmic structure.
(d) Efficiency: Efficiency is concern with algorithm execution and efficient
memory utilization. This can be achieved by avoiding unstructured
statements in the algorithm. Efficiency should not be obtained at the
expense of clarity or simplicity.
(e) Modularity: An algorithm is said to be modular if it is decomposed into
a series of identifiable tasks called subalgorithms or procedures. Each of
these subtasks can be implemented as a separate algorithm module. The
use of a modular programming structure enhances the accuracy and
clarity of an algorithm. It also facilitates future algorithms alterations.
(f) Generality: An algorithm should be made to be general as possible,
within reasonable limit. Variables should be used to read in the values
of certain key parameters rather than placing constant (fixed) values into
the program. Considerable amount of generality can be obtained with
very little additional programming effort.
(g) Robustness: Robustness means that how an algorithm can clearly define
our problem.
(h) Maintainability: Here, maintainability means that the algorithm
should be designed in a very simple structured way so that when we
redefine the algorithm, no major change will be done in the algorithm.
(i) Functionality: It considers various logical steps to solve the real-world
problem.
(j) User-friendly: If the algorithm is not user-friendly, then the designer
will not be able to explain it to the programmer.
(k) Extensibility: If any other algorithm designer or programmer wants to
use your algorithm then it should be extensible
(l) Documentation: The programmer must integrate good documentation
in every algorithm, that is, statements that briefly explain what every
Page 2 of 21
part of the algorithm is doing. Documentation (or Comments) makes
algorithms easy to read, understand and modify when necessary.
Page 3 of 21
The process of algorithm development generally involves many steps. The
general steps involved in algorithm development are as follows:
▪ Problem Definition
▪ Problem Analysis
▪ Algorithm Design
▪ Flowchart/Pseudocode
▪ Implementation
▪ Testing
▪ Documentation
(e)Implementation
In this step, the algorithm design is translated into a specific computer
programming language. Write the code to implement the algorithm, taking
into consideration the syntax, data types, and control structures of the
programming language.
(e) Testing
Page 4 of 21
Test the algorithm by giving the test data and see if the desired output is
generated. Testing with various inputs to verify the correctness and
effectiveness of the algorithm. Identify and fix any bugs or errors that may
arise during testing. Testing may involve different types of inputs,
including edge cases, to ensure the algorithm performs correctly in
different scenarios.
(d) Documentation
Document the algorithm, including its design, implementation details, and
any assumptions or limitations. This documentation helps others understand
and use the algorithm effectively.
Page 5 of 21
suited to solve a broad range of diverse problems. No matter which
programming language that is been used, it is important to learn algorithm
design techniques in data structures in order to be able to build scalable
systems. Selecting a properly design technique for algorithms is a complex
but important task. Some of the main algorithm design techniques are:
(a) Brute-force or exhaustive search
(b) Divide and Conquer
(c) Greedy Algorithms
(d) Dynamic Programming
(e) Branch and Bound Algorithm
(f) Randomized Algorithm
(g) Backtracking
(h) Linear Programming
The divide and conquer approach involve the following steps at each level:
▪ Divide: The original problem is divided into sub-problems.
▪ Conquer: The sub-problems are solved recursively.
▪ Combine: The solutions of the sub-problems are combined together to
get the solution of the original problem.
Page 6 of 21
The divide and conquer approach are applied in algorithms such as Binary
search, Quick sort, Merge sort, Integer multiplication, Matrix inversion,
Matrix multiplication, etc.
Page 7 of 21
of dynamic programming. Matrix-chain Multiplication and Assembly Line
Scheduling are another example.
Page 8 of 21
2.7.7 Backtracking
Backtracking is an optimization technique to solve combinational problems.
It is applied to both programmatic and real-life problems. In backtracking, we
start with a possible solution, which satisfies all the required conditions. Then
we move to the next level and if that level does not produce a satisfactory
solution, we return one level back and start with a new option. Backtracking
Algorithm tries each possibility until they find the right one. It is a depth-first
search of the set of possible solution. Eight queen problem, Sudoku puzzle
and going through a maze are popular examples where backtracking
algorithm is used.
Step 1 − START
Step 2 − declare three integers a, b & c
Step 3 − define values of a & b
Step 4 − add values of a & b
Step 5 − store output of step 4 to c
Step 6 − print c
Step 7 − STOP
Algorithms tell the programmers how to code the program. Alternatively, the
algorithm can be written as −
Page 9 of 21
Step 1 − START ADD
Step 2 – Read values of a & b
Step 3 −c←a+b
Step 4 − display c
Step 5 − STOP
Page 10 of 21
2.10 Algorithm Format Conventions and Design Notations/Tools
The solution process for most problems basically involves making
assignments, comparisons and repeating some predefined simple steps.
Others may include performing some arithmetic operations like addition,
subtraction, multiplication and division. We shall adopt the following
notations for the purpose of designing algorithms.
Page 11 of 21
Examples:
X^2 means X2
X^Y means Xy
X + Y means X plus Y
X/Y means x divided by Y
X – Y means X minus Y
X + Y means X plus Y
Page 12 of 21
else
c = 40
Endif
(ii) Goto Statement: The GOTO statement is used to alter the normal
sequence of algorithm (i.e., Pseudocode) execution by transferring
control to some other part of the program. In its general form, the
GOTO statement is written as:
GOTO label
where label is an identifier/step that is used to label the target
statement to which control will be transferred. Control may be
transferred to any other statement within the algorithm. (To be more
precise, control may be transferred anywhere within the current
Algorithm. The target statement must be labeled, and the label must
be followed by a colon. Thus, the target statement will appear as:
label: statement
Each labeled statement within the program (more precisely, within the
current function) must have a unique label; i.e., no two statements can
have the same label. For example
Page 13 of 21
Step 3: Print “ Hello”
.
.
.
GOTO Step 3
Page 14 of 21
For the purpose of algorithm, the third kind of logic refers to either of two
types of structures involving loops. Each type begins with a Repeat statement
and is followed by a module/statements/group of statements called the body
of the loop. For clarity, we will indicate the end of the structure by the
statement
[End of loop]
The repeat-for loop uses an index variable, such as K, to control the loop. The
loop will usually have the form:
Here start-value is called the initial value, stop_value is the end value or test value,
and step-value is the increment. Loop_variable is the variable used to control the
loop.
For example,
Repeat for counter = 1 to 5 by 1
Print Counter
End of Loop
The second is the repeat-while loop and it uses a condition to control the loop.
The loop will usually have the form
For example,
Repeat while ( i < 10)
counter = counter + 1
Print Counter
End of Loop
Page 15 of 21
A procedure is a finite sequence of well-defined steps or operations used in
solving a problem. An algorithm represents logical structure of a program to
be coded. An important characteristic of a correctly formulated algorithm is
that it guaranteed to accomplish the task for which the algorithm was
designed.
An algorithm for a computer is the set of steps that explains how to begin with
the known information specified in a problem statement and manipulate that
information to arrive at a solution and is typically first written in a format that
is not specific to a particular computer or programming languages. This way,
the software engineer focuses on formulating a correct algorithm rather than
an expressing the algorithm using the commands of a computer programming
language. In the later phase of the software development process, the
algorithm is translated into instructions written in a computer programming
language so that the algorithm can be implemented by a computer.
Solution:
Program Specification/Glossary
Fnum = First Number
Page 16 of 21
Snum = Second Number
Sum = Sum of the Numbers
Algorithm
Step 1: START ADD
[Algorithm to Find the Sum of two Numbers]
Step 2: Input Fnum and Snum
Step 3: Sum ← Fnum + Snum
Step 4: Print “Sum = ”, Sum
Step 5: STOP
Solution
Program Specification/Glossary
d – diameter
t – wall thickness
density ρ = rho
Ro – Ro
Ri = Ri
Volume = V
Weight = W
Algorithm
Step 1: START HOLLOWSPHERE
[Algorithm to Compute the Weight of a Hollow Sphere]
Step 2: Read d, t and rho
Step 3: Ro := d/2, Ri = Ro – t
Step 4: V := 4 * 3.142 * (Ri ^3 – Ro ^ 3)/3
Step 5: W:= rho * V
Step 6: Print “Weight = ”, W
Step 7: STOP
Example 3:- Write an Algorithm to convert a temperature reading in degrees
Fahrenheit to degrees Celsius, using the formula C = (5/9 )x (F- 32) .
Solution
Program Specification/Glossary
Temperature in Celsius degrees - Celsius
Temperature in Fahrenheit degrees – Fahrenheit
Page 17 of 21
Algorithm
Step 1: START TEMPERATURECONVERSION
[Algorithm to convert Temperature Reading in degree Fahrenheit
to Celsius]
Step 2: Input Fahrenheit
Step 3: Celsius ← 5/9 * (Fahrenheit – 32)
Step 4: Print “Temperature in degree Celsius = ”, Celsius
Step 5: STOP
Example 4: Write an Algorithm that calculates the Energy (Q) needed to heat
water from an initial temperature to a final temperature. The formula for
computing the Energy is:
Q = M * (final temperature – initial temperature) * 4184
Where M is the weight of water in Kilograms, temperatures are in degrees
Celsius and Energy Q is measured in joules.
Solution
Step 1: START ENERGYNEEDED
[Algorithm to compute in Joules]
Step 2: Read Mass, InitialTemperature, FinalTemperature
Step 3: Energy := Mass * (InitialTemperature – FinalTemperature) * 4184
Step 4: Print “Energy in Joules = ”, Enegry
Step 5: STOP
Solution
Step 1: START TRIANGLE
[Algorithm to compute the Perimeter of a triangle]
Step 2: Read A, B, and C
Step 3: Perimeter ← (A + B + C)/2
Step 4: Print “Perimeter = ”, Perimeter
Step 5: STOP
Page 18 of 21
Step 4: Print Fnum, “ Is Larger”
Step 5: Else
Step 6: Print Snum, “ Is Larger”
Step 7: ENDIF
Step 8: STOP
Solution
Step 1: START RELATIONSHIP
[Algorithm to compute mathematical relationship]
Step 2: Input X
Step 3: IF (X > = 0) then
Step 4: Y← 4 *X ^ 2 - X^ 2 + 1
Step 5: Else
Step 6: Y← X ^2 + X – 1
Step 7: ENDIF
Step 8: Print Y
Step 9: STOP
Page 19 of 21
Solution
Program Specification/Algorithm
Number – Numbers to be added
N – No of terms
Sum – Sum of the Numbers
Average – Average of the Numbers
Algorithm
Step 1: START SUMMATION
[Algorithm to compute the Sum and Average of 50 numbers]
Step 2: Sum: = 0.0.
Step 3: Input N
Step 4: Repeat for counter = 1 to N by 1
Step 5: Input Number
Step 6: Sum := Sum + Number
Step 7: End of Loop
Step 8: Average := Sum/N
Step 9: Print Sum
Step 10: Print , Average
Step 11: STOP
Example 2: Design an algorithm to calculate the sum and average of the first
50 integer numbers and outputs the result.
Solution:
Step 1: START SUMMATION
[Algorithm to compute the Sum and Average of the first 50 Integer
numbers]
Step 1: Sum: = 0
Step 2: Input N
Step 3: Repeat for counter = 1 to N by 1
Step 4: Sum := Sum + Counter
Step 5: End of Loop
Step 6: Average:= Sum/N
Step 7: Print Sum
Step 8: Print Average
Ste9 9: STOP
Page 20 of 21
Solution
Program specification/Algorithm
Let measurement in inches = inches
Let measurement in centimeters = centimeters.
Algorithm
Step 1: START MEASUREMENT
[Algorithm to convert from Inches to Centimeters]
Step 2: Inches := 1
Step 3: Read n (i.e. No of terms)
Step 4: Repeat for inches = 1 to n by 0.5
Step 5: centimeters: = 2.54 * inches
Step 6: Print inches, centimeters.
Step 7: End Loop
Step 8: STOP
Page 21 of 21