Professional Documents
Culture Documents
Algorithms
Chapter 1
Algorithms
1.1. Definition
A central theme in computing is the design of a process for carrying out a task. A task can be
inputting personal data (name, date of birth, etc.), to sorting a set of data, recognizing a pattern
in a data set, finding the cheapest way to link a set of computing sites into a network, determining
the shortest path to be followed by a robotic arm, scoring a goal in FIFA21, or docking a
spacecraft in the International Space Station. To perform a task, one usually follows certain step-
by-step process called algorithm.
Definition 1.1.
An algorithm is a finite sequence of steps for performing a task, such that:
• each step is a clear and unambiguous instruction that can be executed in a finite time;
• the sequence in which the steps are to be executed is clearly defined;
• the process is guaranteed to stop after a finite number of steps have been executed.
To perform a task, we usually need to start with a set of inputs. Once the task is completed, we
typically want to know the result, which can be presented as a set of outputs.
Example 1
The following is an algorithm to subtract two real numbers:
Algorithm 1.
1. input 𝑥 and 𝑦
2. 𝑧 = 𝑥 − 𝑦
3. output 𝑧
Chapter 1. Algorithms
real*8 x, y, z
write(*,*) ‘What is the value of x?’
read(*,*) x
write(*,*) ‘What is the value of y?’
read(*,*) y
z = x - y
write(*,*) ‘z = ‘,z
The resulting computer programs as shown above are called code. Since in a code, we can still
see (even though most of the time, difficult to see) the structure of the underlying algorithm, this
way of writing an algorithm as in Example 1 is called as pseudocode.
1. conditional control
2. loop control
1
Fortran stands for Formula Translation, a popular programming language in the scientific computing community.
It was developed by IBM in the 1950s and has since been evolved into Fortran 2018, which parallel computing
supports.
2
Matlab stands for Matrix Laboratory. It was first developed as a programming tool to help students in a
programming course at the University of Arizona. The first commercial version was released in 1984. It is now one
of the successful and powerful commercial programming languages.
Chapter 1. Algorithms
• if-then
• if-then-else
In if-then, the steps are only executed if the conditions stated in the if-part is satisfied. Otherwise,
the steps are skipped.
Example 2.
Consider the following algorithm:
Algorithm 2.
1. input 𝑥 and 𝑦
2. if 𝑥 > 𝑦 then
2.1. 𝑧 = 𝑥 − 𝑦
3. if 𝑥 ≤ 𝑦 then
3.1. 𝑧 = 𝑦 − 𝑥
4. output 𝑧;
Here, Step 2.1. is only executed if the input 𝑥 is greater than 𝑦. If 𝑥 is not greater than 𝑦, then
Step 2.1 is skipped. For instance, for 𝑥 = 5 and 𝑦 = 3, 𝑥 > 𝑦; thus Step 2.1 is executed, giving
𝑧 = 𝑥 − 𝑦 = 5 − 3 = 2. Since 𝑥 > 𝑦, the condition in the if-part of Step 3 is not satisfied. The
algorithm then skips Step 3.1. In Step 4, the output shows 𝑧 = 2.
If 𝑥 = 5 and 𝑦 = 10, condition in the if-part of Step 2 is not satisfied, so Step 2.1 is not
executed. Since now 𝑥 ≤ 𝑦, the condition in Step 3 is satisfied, requiring the algorithm to
execute Step 3.1, giving 𝑧 = 𝑦 − 𝑥 = 10 − 5 = 5. Next, in Step 4, the output shows 𝑧 = 5.
Comparing to Algorithm 1, the way the two numbers are subtracted in Algorithm 2 depends on
some conditions. These conditions are set to guarantee that the output of Algorithm 2 is always
a nonnegative number.
The two if-then structures in Algorithm 2 can be combined into one if-then-else structure as
follows:
Algorithm 3.
1. input 𝑥 and 𝑦
2. if 𝑥 > 𝑦 then
2.1. 𝑧 = 𝑥 − 𝑦
else
2.2. 𝑧 = 𝑦 − 𝑥
3. output 𝑧;
Clearly the else part represents the condition when 𝑥 > 𝑦 is not met, which is equivalent to
when 𝑥 ≤ 𝑦.
it is possible that you stop the push-up before 100, because you are tired already.) Since the
process must end after the required repetitions are completed, a counter is needed (just like
counting each push-up).
• for-do
• while-do
• repeat-until
In the next example, we sum the first n positive whole numbers: 1, 2, 3, … , 𝑛. In mathematics,
we denote this sum by ∑𝑛𝑖=1 𝑖.
Example 3.
In Algorithm 4, 𝑖 serves as a counter, starting from 1 until 𝑛. The loop is terminated when 𝑖 = 𝑛.
The variable 𝑠𝑢𝑚 is a dummy variable. The ← is an assignment symbol. In this case, starting
with 𝑠𝑢𝑚 = 0, at the counter 𝑖 = 1, the new value 𝑠𝑢𝑚 + 𝑖 = 0 + 1 = 1 is assigned to 𝑠𝑢𝑚.
Thus, after the completion of the first loop (𝑖 = 1), we have 𝑠𝑢𝑚 = 1. At the counter 𝑖 = 2 (the
second loop), a new value is assigned to 𝑠𝑢𝑚, which is 𝑠𝑢𝑚 + 𝑖 = 1 + 2 = 3; So now, 𝑠𝑢𝑚 =
3, etc.
In while-do, a condition is required to repeat an execution. If the condition is not satisfied, the
loop will then be terminated. The condition is set in the while-part of the structure. This structure
however does not have a counter. Thus, a counter must be set, if required.
Since we only need to sum the first 𝑛 positive whole number, we must stop adding when the 𝑛th
whole number has been added. Step 4.1 does the counting and will allow the program to check
whether the 𝑛th whole number has been added, thus providing a control mechanism.
Chapter 1. Algorithms
The repeat-until structure is similar to while-do, in the sense that the steps will be executed
repeatedly until a condition is met. The condition is described in the until-part of the process.
Since the repeat-until process does not have a built-in counter, a counter may be invoked, if
required.
Below is an algorithm for performing the same task as Algorithm 4, but with repeat-until.
In Algorithm 4, Step 4.1 again serves a counter (as we need it). Once it reaches 𝑖 = 𝑛, the loop
is terminated, and the process proceeds with Step 5.
1.3. Tracing
Tracing is used to check if an algorithm does the task correctly. The term tracing is similar to
debugging in computer programming. In tracing, the process is checked step-by-step, usually
based on the known output so that the output of the algorithm can be compared. One hard part
of programming is when you have to fix a code that “works” but delivers wrong output.
Example 4.
Trace Algorithm 3, with input 𝑥 = 20 and 𝑦 = 25.
Step 𝑥 𝑦 𝑧 Output
1. 20 25 - -
2. - - - -
2.1. - - - -
2.2. - - 5 -
3. - - - 5
Example 5.
Trace Algorithm 4 in Example 3, to compute ∑𝑛𝑖=1 𝑖 with the input 𝑛 = 6.
Example 6.
Trace Algorithm 5, to compute ∑𝑛𝑖=1 𝑖 with the input 𝑛 = 6.
Example 7.
The following algorithm finds the smallest number from a given list of 𝑛 numbers:
𝑥1 , 𝑥2 , … , 𝑥𝑛 . It is based on looking at each number, keeping track at each step of the smallest
number found so far.
Consider the list of numbers: 10, 8, 12, 6, 100. Here, the inputs are 𝑛 = 5, and 𝑥1 = 10,
𝑥2 = 8, 𝑥3 = 12, 𝑥4 = 6, 𝑥5 = 100. If the algorithm is right, then it must produce 𝑚𝑖𝑛 = 6
as the output.
Tracing of the algorithm may be presented like this; the input steps are skipped.
Algorithm 8. Compute 𝑥 𝑛
1. input 𝑛
2. input 𝑥
3. 𝑎𝑛𝑠𝑤𝑒𝑟 ← 𝑥
4. for 𝑖 = 1 to 𝑛 − 1 do
4.1. 𝑎𝑛𝑠𝑤𝑒𝑟 ← 𝑎𝑛𝑠𝑤𝑒𝑟 × 𝑥
5. output 𝑎𝑛𝑠𝑤𝑒𝑟
Exercise 1.3. Trace Algorithm 8, with the input 𝑥 = 3 and 𝑛 = 8. What is the output? How
many times does the algorithm perform the for loop? If we add to the algorithm a 6th step
like this,
6. output i
what is the output in Step 6?
Exercise 1.4. Write a version of Algorithm 8 using the while-do structure.
1.4.2. Computing 𝑛!
𝑛! (read: 𝑛 factorial) denotes the product of the first 𝑛 positive whole numbers. Thus,
𝑛! = 1 × 2 × 3 × … × (𝑛 − 1) × 𝑛.
Algorithm 9. Compute 𝑛!
1. input 𝑛
2. 𝑎𝑛𝑠𝑤𝑒𝑟 = 1
3. for 𝑖 = 2 to 𝑛
3.1. 𝑎𝑛𝑠𝑤𝑒𝑟 ← 𝑎𝑛𝑠𝑤𝑒𝑟 × 𝑖
4. output 𝑎𝑛𝑠𝑤𝑒𝑟
Chapter 1. Algorithms
Exercise 1.6. Trace Algorithm 9, with the input 𝑛 = 8. What is the output? How many times
does the algorithm perform the for-do loop?
Exercise 1.7. Write a version of Algorithm 8 using the while-do structure.
∑ 𝑖 2 = 12 + 22 + 32 + ⋯ + 𝑛2 .
𝑖=1
For instance, when 𝑛 = 4,
4
∑ 𝑖 2 = 12 + 22 + 32 + 42 = 1 + 4 + 9 + 16 = 30.
𝑖=1
Described below is an algorithm for computing the sum of the squares, using while-do structure.
Exercise 1.8. Trace Algorithm 10, with the input 𝑛 = 7. What is the output? How many
times does the algorithm perform the while-do loop?
Exercise 1.9. Write a version of Algorithm 8 using the for-do structure.
The same task can also be written using the for-do structure. Since the for-do structure does not
include a condition other than the maximum counter, to terminate a for-do loop under additional
condition will require additional process. One instance is by using return. The process return
terminates the loop, gives the output, and stops the program.
Using the for-do structure, the task of finding a character can be performed by the following
algorithm.
In Algorithm 11 and 12, the loop process is terminated when the sought character 𝑠 has been
found.
We can modify Algorithm 11 to find out how many character 𝑠 is contained in the string. Again,
we will use a repeat-until structure to perform the task.
Exercise 1.10. Trace Algorithm 11, to find 8 in 0416391871630654. What is the output?
How many times does the algorithm perform the repeat-until loop?
Exercise 1.11. What is the maximum number of the repeat-until loop needed to search a
character in 0416391871630654? Under which scenario does this maximum number of loops
need be performed?
Exercise 1.12. Write a version of Algorithm 11 using the while-do structure.
Exercise 1.13. Trace Algorithm 13, to find 1 in 0416391871630654. What is the output?
How many times does the algorithm perform the repeat-until loop?
Exercise 1.14. For Algorithm 13, with 0416391871630654 and 𝑠 = 2, what is the output?
Exercise 1.15. Write a version of Algorithm 13 using the for-do structure.
Suppose we are given two strings of size 𝑛: 𝑥1 𝑥2 … 𝑥𝑛 and 𝑦1 𝑦2 … 𝑦𝑛 . We want to find if the
two strings have some common characters. Algorithm 14 does this job.
Exercise 1.17. Two strings: 01263791785 and 53418902715 are inputted in Algorithm 14.
a) What is the output?
b) How many times does the algorithm perform Step 4.1?
c) How many times does the algorithm perform Step 4?
d) Does the algorithm execute Step 5?
1 0 8
[ ].
−3 4 7
Numbers in a matrix represents its entries. The horizontal direction of the entries is called “row”
of the matrix, and the vertical direction of the entries is called “column” of the matrix. The above
Chapter 1. Algorithms
matrix has 2 rows and 3 columns. To identify an entry in a matrix, we can use a position index.
For the above matrix, we can set up position indices as follows (shown in blue):
1 2 3
1 1 0 8
[ ]
2 −3 4 7
Using the above indexing, 0 for instance is the (1,2)-entry of the matrix. Similarly, 7 is the (2,3)-
entry of the matrix. Notice that the row index is stated first, followed by the column index. In
general, if 𝐴 denotes a matrix with 𝑚 rows and 𝑛 columns, we can use 𝑎𝑖,𝑗 to denote the (𝑖, 𝑗)-
entry of 𝐴. Thus, if
11 −6 8
𝐴=[5 10 7 ],
3 −4 −2
then 𝑎1,1 = 11, 𝑎1,2 = −6, 𝑎2,2 = 10, 𝑎3,2 = −4, and so on.
Suppose 𝐴 and 𝐵 are two matrices with the same number of rows, 𝑚, and the same number of
columns, 𝑛. The sum of the two matrices is the matrix
𝐶 = 𝐴 + 𝐵,
where
𝑐𝑖,𝑗 = 𝑎𝑖,𝑗 + 𝑏𝑖,𝑗 .
For instance, if 𝐴 is as given above and
−1 2 4
𝐵=[ 7 8 0],
10 −12 9
then
11 − 1 −6 + 2 8+4 10 −4 12
𝐶 =[5+7 10 + 8 7 + 0 ] = [12 18 7 ].
3 + 10 −4 − 12 −2 + 9 13 −16 7
The calculation of the sum of two matrices can be performed by using the following algorithm.
Exercise 1.20. If 𝐴 and 𝐵 have 𝑚 rows and 𝑛 columns, how many times does Algorithm 14
perform the loop in Step 3 and the loop in Step 3.1?