The document discusses several algorithms:
1. Program verification - A technique to mathematically prove that a program's output matches its specifications for any input.
2. Algorithm efficiency - Designing algorithms that are economical with resources like time and memory.
3. Analysis of algorithms - Qualities like correctness, understandability and efficiency.
It then provides examples of algorithms for:
1. Swapping variable values
2. Counting elements that meet a condition in a set
3. Summing a set of numbers
4. Computing factorials
5. Evaluating trigonometric functions like sine
2. PROGRAM VERIFICATION
• The application of mathematical proof techniques to establish that the results
obtained by the execution of a program with arbitrary inputs are in accord with
formally defined output specifications.
1. Computer model for program execution.
2. Input assertion specify any constraints that have been placed on the values of the input variables
used by the program.
3. Output assertion specify symbolically the results that the program is expected to produce for input
data that satisfies the input assertion.
4. Implications and symbolic execution.
5. Verification of straight-line program segments.
6. Verification of program segments with branches / loops / arrays.
7. Proof of termination.
2
3. THE EFFICIENCY OF ALGORITHMS
• Design algorithms that are economical in the use of CPU time and memory because of
high cost of computing resources.
• Suggestions that are useful in designing efficient algorithms.
1. Redundant computations.
2. Referencing array elements.
3. Inefficiency due to late termination.
4. Early detection of desired output conditions.
5. Trading storage for efficiency gains.
3
5. FUNDAMENTAL ALGORITHMS
EXCHANGING THE VALUES OF TWO VARIABLES
Problem Statement:
Given two variables a and b, interchange the values of the variables.
5
• Algorithm development
The problem of interchanging the data associated with two variables involves a
very fundamental mechanism that occurs in many sorting and data manipulation
algorithm.
Ex: a b
4 5
Here a has 4 and b has 5. our aim is to replace the variable of a with 4 and b with 5.
6. • So the target solution may look as
a b
5 4
• Here we can make use of the assignment operator a = b; b = a;
• where the b value is assigned to a. the value has been changed, and the changed
a value has been assigned to b.
a = 5, b = 5
• So we make use of the temporary variable.
t = a;
a = b;
b = t;
• Now,
t = 4, a = 5, b = 4
• Our target solution has been achieved.
7. Algorithm Description:
• 1. Save the original value of a in t.
• 2. Assign to a the original value of b.
• 3. Assign to b the original value of a that is stored in t.
APPLICATION
• This kind of swapping technique is applicable for all kinds of sorting algorithm.
Notes on design
1. The use of an intermediate temporary variable allows the exchange of two variables to proceed
correctly.
2. This example emphasized that at any stage in a computation a variable always assumes the
value dictated by the most recent assignment made to that variable
3. Working through the mechanism with a particular example can be a useful way of detecting
design faults
4. A more common application of exchange involves two array elements (eg. a[i] and a[j]). The
steps for such an exchange are:
t:= a[i];
a[i] := a[j];
a[j] := t
8. COUNTING
Problem Statement:
Given a set of n students examination marks (in the range 0 to 100), make a count of
number of students passed the examination. A pass is awarded for all marks of 50 and
above.
8
• Algorithm development
Count must be made of number of items in a set which possess some particular property
or which satisfy some particular condition or conditions. This class of problems is by
“examination marks”.
As a starting point for developing a computer algorithm for this problem we can consider
solving by hand.
Suppose that the set of marks are
55,42,77,63,29,57,89
Check is first mark >=50, so one student has passed so far. The second mark is then
examined and no change to the count. The process continues until all marks are
examined.
9. • In more detail:
Therefore, number of students passed = 5
Order in which
marks are
examined
Marks Counting details for passes
55 Previous count = 0 Current count = 1
42 Previous count = 1 Current count = 1
77 Previous count = 1 Current count = 2
63 Previous count = 2 Current count = 3
29 Previous count = 3 Current count = 3
57 Previous count = 3 Current count = 4
89 Previous count = 4 Current count = 5
10. Algorithm description:
1. Prompt and read the number of marks to be processed.
2. Initialize count to zero.
3. While there are still marks to be processed repeatedly do
a. Read next mark.
b. If it is a pass then add one to count.
4. Print total number of passed students.
Notes on design
1. Initially, and each time through the loop, the variable count represents the number of passes so far
encountered. On the termination (when i=n) count represents the total number of passes in the set.
Because I is incremented by 1 with each iteration, eventually the condition i<n will be violated and the
loop will terminate.
2. It was possible to use substitution to improve on the original solution to the problem. The simplest and
most efficient solution to a problem is usually the best all-round solution.
3. An end-of-line test is included to handle multiple lines of data.
Applications
All forms of counting
10
COUNTING
11. SUMMATION OF A SET OF N NUMBERS
Problem Statement:
Given a set of n numbers, design an algorithm that adds these numbers and returns the resultant sum.
Assume n is >=0.
11
Algorithm development
⁻Consider the addition of 421, 583 and 714
⁻In designing an algorithm to add a set of numbers a primary concern is the mechanism for the
addition process.
⁻Simplest way
s := a+b+c
⁻Make general enough that will successfully handle a wide variety of input conditions.
s = (a1+a2+a3+a4+…+an) which is equivalent to
s= 𝑖=1
𝑛
𝑎𝑖
12. Steps followed by a set of n iterative steps
1. Compute first sum (s=0) as special case
2. Build each of the n remaining sums from its predecessor by an iterative process
3. Write out the sum of n numbers
ALGORITHM:
1. Prompt and read total numbers to sum.
2. Initialize sum for zero numbers.
3. While less than n numbers have been summed repeatedly do
a. Read next mark.
b. Compute current sum by addin
4. Print total number of passed students.
Applications:
Average calculations, variance and least square calculations.
13. FACTORIAL COMPUTATION
Problem Statement:
Given a number n, compute n factorial (written as n!) where n>=0.
13
• Algorithm development
n! can be done as n!= 1x2x3x…..x(n-1)xn for n>=1
as 0! =1
We can generalize this by observing that n! can always be obtained from (n-1)! by simply
multiplying it by n (for n>=1)
n! = nx(n-1)! For n>=1
Using this:
1! = 1x0!
2!= 2x1!
3!= 3x2!
.
.
.
14. ALGORITHM:
1. Establish n, the factorial required where n>=0.
2. Set product p for 0! Also set product count to zero.
3. While less than n products have been calculated repeatedly do,
a. Increment product count.
b. Compute the ith product p by multiplying I by the most recent product.
4. Return the result n!.
Applications:
Probability, Statistical and mathematical computations.
14
15. SINE FUNCTION COMPUTATION
Problem Statement:
Design an algorithm to evaluate the function sin(x) as defined by the infinite series expansion
sin 𝑥 =
𝑥
1!
−
𝑥3
3!
+
𝑥5
5!
−
𝑥7
7!
15
16. Algorithm development
• The function we need to compute will involve the following steps:
fp := 1;
j := 0;
While j<i do
begin
j := j+1;
fp := fp*x/j
end
• This algorithm can be completed by implementing the addition and subtractions and making the appropriate termination.
• To generate consecutive terms of the sine series we can use
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 𝑖𝑡ℎ 𝑡𝑒𝑟𝑚 =
𝑥2
𝑖(𝑖 − 1)
𝑋 𝑝𝑟𝑒𝑣𝑖𝑜𝑢𝑠 𝑡𝑒𝑟𝑚
• To get alternate terms, sign := -sign
• Will generate alternate positive and negative terms
• The initial conditions are
i := i+2;
term := -term*x*x/(i*(i-1));
tsin = tsin + term
- How to terminate the algorithm
- Fix an acceptable error level and generate successive terms until the contribution of the current term is less than the acceptable error
17. ALGORITHM:
1. Set up initial conditions for the first term that cannot be computed iteratively.
2. While the absolute value of current term is greater than the acceptable error do
a. Identify the current ith term.
b. generate current term from its predecessor.
c. add current term with the appropriate sign to the accumulated sum for the sine function.
Applications:
Mathematical and Statistical computations.
17
18. REVERSING THE DIGITS OF AN INTEGER
Problem Statement:
Design an algorithm that accepts a positive integer and reverses the order of its digits.
18
Algorithm development
Digit reversal is a technique used to remove bias from a set of numbers. Eg. Input:
27953
Output: 35972
If we apply the following two steps
r := n mod 10
n := n div 10
The central steps in algorithm are:
1. While there are still digits in the number being reversed do
a) Extract the rightmost digit from the number being reversed and append the digit to the
right-hand end of the current reversed number representation;
b) Remove the rightmost digit from the number being reversed
19. ALGORITHM:
1. Establish n, the positive integer to be reversed.
2. Set the initial condition for the reversed integer dreverse.
3. While the integer being reversed is greater than zero do,
a. Use the remainder function to extract the rightmost digit of the number being
reversed.
b. Increase the previous reversed integer representation dreverse by a factor of 10
and add to it the most recently extracted digit to give the current dreverse value.
c. Use integer division by 10 to remove the rightmost digit from the number being
reversed.
dreverse := (previous value of dreverse)*10 + (most recently extracted rightmost digit)
Applications:
Hashing and information retrieval, database applications.
19
21. ALOGORITHM:
1. Establish the newbase and initialize the quotient q to the decimal number to be converted.
2. Set the new digit count ndigit to zero.
3. Repeatedly,
A. Compute the next most significant digit i.e octal from the current quotient q as the remainder
r after division by newbase.
B. Convert r to appropriate ascii value.
C. increment new digit count ndigit and store r in output array newrep.
D. Compute the next quotient q from its predecessor using integer division by newbase until the
quotient is zero.
Applications:
Interpretation of stored computer data and instructions.
21
22. CHARACTER TO NUMBER CONVERSION
Problem Statement:
Given the character representation of an integer convert it to its conventional decimal
format.
22
Example:
Character representation ‘125’ when converted to decimal format results in
125.
23. Algorithm:
1. Establish the character string for conversion to decimal and its length n.
2. Initialize decimal value to zero.
3. Set base0 value to the ascii or ordinal value of ‘0’.
4. While less than n characters have been examined do
a. Convert next character to corresponding decimal digit.
b. Shift current decimal value to the left one digit and add in digit for current
character.
5. Return decimal integer corresponding to input character representation.
Applications:
Business applications, tape processing.
23