You are on page 1of 14

Chapter 1.

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.

In computing, an algorithm represents a sequence of processes, based on which a computer


program is written. Contrasting the two:

• An algorithm is usually simpler to understand by human than the actual computer


program.
• An algorithm is usually written in a more standard formal language, while the computer
program can differ significantly depending on the programming language use to program.

Example 1
The following is an algorithm to subtract two real numbers:

Algorithm 1.
1. input 𝑥 and 𝑦
2. 𝑧 = 𝑥 − 𝑦
3. output 𝑧
Chapter 1. Algorithms

If Algorithm 1 in Example 1 is written in a programming language called Fortran1, we may


end up with the following implementation:

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

Implementation in Matlab2 may look like this:

xinput = 'What is the value of x? ';


x = input(xinput);
yinput = 'What is the value of y? ';
y = input(yinput);
z = x-y;
disp(['x - y = ',num2str(z)]);

There is a long list of programming languages to translate an algorithm to a computer program.


Besides Fortran and Matlab, the list also includes Basic, C, C++, Python, Pascal, and Java.

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.2. Control Structures


In Example 1, the algorithm is executed step-by-step, starting from the first step down to the last.
Sometimes, some steps have to be executed more than once before continuing with the next steps
(think about a morning exercise program that consists of 10 push-ups and 20 sit-ups), and,
sometimes, we execute some steps only when a condition is met (like a diet program: if you do
not have chicken at lunch, then you can have dinner with 100 gram of beef). In these two
illustrations, we need to control the process in order to repeat or to execute.

There are two types of control structures:

1. conditional control
2. loop control

1.2.1. Conditional control


Conditional control is used to determine under which condition steps are executed (or not
executed). Conditional control can be performed by using:

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 𝑥 ≤ 𝑦.

1.2.2. Loop control


Loop control is used to execute a set of steps more than one time. The process of repeated
execution is referred to as ``loop’’. A loop ends when the expected number of repetitions has
been reached or some condition is met. (Just like, do push-ups 100 times or until you feel tired;
Chapter 1. Algorithms

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).

There are 3 types of loop controls:

• for-do
• while-do
• repeat-until

In for-do, the counter is built-in and automatically counts the repetition.

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.

Algorithm 4. Sum of the first 𝑛 positive whole numbers


1. input 𝑛
2. 𝑠𝑢𝑚 = 0
3. for 𝑖 = 1 to 𝑛 do
3.1. 𝑠𝑢𝑚 ← 𝑠𝑢𝑚 + 𝑖
4. output 𝑠𝑢𝑚

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.

An alternative to Algorithm 4 using a while-do structure is presented in Algorithm 5.

Algorithm 5. Sum of the first 𝑛 positive whole numbers


1. input 𝑛
2. 𝑠𝑢𝑚 = 0
3. 𝑖 = 0
4. while 𝑖 < 𝑛 do
4.1. 𝑖 ← 𝑖 + 1
4.2. 𝑠𝑢𝑚 ← 𝑠𝑢𝑚 + 𝑖
5. output 𝑠𝑢𝑚

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.

Algorithm 6. Sum of the first 𝑛 positive whole numbers


1. input n
2. 𝑠𝑢𝑚 = 0
3. 𝑖 = 0
4. repeat
4.1. 𝑖 ← 𝑖 + 1
4.2. 𝑠𝑢𝑚 ← 𝑠𝑢𝑚 + 𝑖
until 𝑖 = 𝑛
5. output 𝑠𝑢𝑚

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

Since 𝑥 > 𝑦 does not hold, Step 2.1 is not executed.


Chapter 1. Algorithms

Example 5.
Trace Algorithm 4 in Example 3, to compute ∑𝑛𝑖=1 𝑖 with the input 𝑛 = 6.

Step 𝑛 𝑠𝑢𝑚 𝑖 output Remark


1. 6 - - -
2. - 0 - -
3. - - 1 -
3.1. - 0+1=1 - -
3. - - 2 -
3.1. - 1+2=3 - - Loop is
3. - - 3 - performed with
3.1. - 3+3=6 - - the counter 𝑖
3 - - 4 - until it reaches
3.1. - 6 + 4 = 10 - - 𝑛=6
3. - - 5 -
3.1. - 10 + 5 = 15 - -
3. - - 6 -
3.1. - 15 + 6 = 21 - -
4. - - - 21

Notice that the loop is performed 6 times (equal to 𝑛).


Chapter 1. Algorithms

Example 6.
Trace Algorithm 5, to compute ∑𝑛𝑖=1 𝑖 with the input 𝑛 = 6.

Step 𝑛 𝑠𝑢𝑚 𝑖 output Remark


1. 6 - - -
2. - 0 - -
3. - - 0 - Initiate the counter
4. - - - - 𝑖 < 𝑛 is satisfied
4.1. - - 0+1=1 -
4.2. - 0+1=1 - -
4. - - - - 𝑖 < 𝑛 is satisfied
4.1. - - 1+1=2 -
4.2. - 1+2=3 - -
4. - - - - 𝑖 < 𝑛 is satisfied
4.1. - - 2+1=3 -
4.2. - 3+3=6 - -
4. - - - - 𝑖 < 𝑛 is satisfied
4.1. - - 3+1=4 -
4.2. - 6 + 4 = 10 - -
4. - - - - 𝑖 < 𝑛 is satisfied
4.1. - - 4+1=5 -
4.2. - 10 + 5 = 15 - -
4. - - - - 𝑖 < 𝑛 is satisfied
4.1. - - 5+1=6 -
4.2. - 15 + 6 = 21 - -
4. - - - - 𝑖 < 𝑛 is NOT satisfied. Terminate
the loop
5. - - - 21

Notice that the loop is performed 6 times.


Chapter 1. Algorithms

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.

Algorithm 7. Find the smallest number.


1. input the number of values n
2. input the list of numbers 𝑥1 , 𝑥2 , . . . , 𝑥𝑛
3. 𝑚𝑖𝑛 ← 𝑥1
4. for 𝑖 = 2 to 𝑛 do
4.1. if 𝑥𝑖 < 𝑚𝑖𝑛 then
4.1.1. 𝑚𝑖𝑛 ← 𝑥𝑖
5. output 𝑚𝑖𝑛

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.

Step 𝑛 𝑖 𝑥𝑖 𝑚𝑖𝑛 Output Remark


3. - - - 10 -
4. - 2 - - -
4.1. - - 8 - - 𝑥2 < 𝑚𝑖𝑛 is
satisfied
4.1.1. - - - 8 -
4. - 3 - - -
4.1. - - 12 - - 𝑥2 < 𝑚𝑖𝑛 is
not satisfied
4. - 4 - - -
4.1. - - 6 - - 𝑥2 < 𝑚𝑖𝑛 is
satisfied
4.1.1. - - - 6 -
4. - 5 - - -
4.1. - - 100 - - 𝑥2 < 𝑚𝑖𝑛 is
not satisfied
5. - - - - 6

How many times is Step 4 performed?


Chapter 1. Algorithms

Exercise 1.1. Trace Algorithm 6, with the input 𝑛 = 10.


Exercise 1.2. Trace Algorithm 7 in Example 7, with the input list: 17, 20, 16, 40, 8, 20, 7, 13.

1.4. More Examples


1.4.1. Computing 𝑥 𝑛
The following algorithm takes a positive whole number 𝑛 and a real number 𝑥 as inputs, and
computes 𝑥 𝑛 .

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.

Exercise 1.5. Write a version of Algorithm 8 using the repeat-until structure.

1.4.2. Computing 𝑛!
𝑛! (read: 𝑛 factorial) denotes the product of the first 𝑛 positive whole numbers. Thus,

𝑛! = 1 × 2 × 3 × … × (𝑛 − 1) × 𝑛.

For example, 5! = 1 × 2 × 3 × 4 × 5 = 120. We can perform the computation of 𝑛! on a


computer using the following algorithm.

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.

1.4.3. Computing ∑𝑛𝑖=1 𝑖 2


The sum of the square of the first 𝑛 positive whole numbers is the denoted by the notation ∑𝑛𝑖=1 𝑖 2 .
Thus,
𝑛

∑ 𝑖 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.

Algorithm 10. Compute the sum of squares ∑𝑛𝑖=1 𝑖 2 .


1. input 𝑛
2. 𝑠𝑢𝑚 = 0
3. 𝑖 = 0
4. while 𝑖 < 𝑛 do
4.1 𝑖 ← 𝑖 + 1
4.2 𝑠𝑢𝑚 ← 𝑠𝑢𝑚 + 𝑖 2
5. output 𝑠𝑢𝑚

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.

1.4.4. Finding a character in an 𝑛-character-long string


Suppose you are given a string like this: 134906745962. Does the string have a ``6’’? The answer
is yes. On a computer, a program can be written to perform this task, based on the following
algorithm, with 𝑠 be the character to find, using repeat-until structure.

Algorithm 11. Finding a character 𝑠


1. input 𝑛
2. input the n-character long string 𝑥1 𝑥2 𝑥3 ⋯ 𝑥𝑛
3. input 𝑠
4. 𝑖 = 0
5. 𝑠𝑒𝑎𝑟𝑐ℎ_𝑓𝑙𝑎𝑔 = 𝑓𝑎𝑙𝑠𝑒
6. repeat
6.1. 𝑖 ← 𝑖 + 1
6.2. if 𝑥𝑖 = 𝑠 then
Chapter 1. Algorithms

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.

Algorithm 12. Finding a character 𝑠


1. input 𝑛
2. input the 𝑛-character long string 𝑥1 𝑥2 𝑥3 ⋯ 𝑥𝑛
3. input 𝑠
4. for 𝑖 = 1 to 𝑛 do
4.1. if 𝑥𝑖 = 𝑠 then
4.1.1. return ‘The string contains the character 𝑠’
5. output ‘The string does not contain the character 𝑠’

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.

Algorithm 13. Finding how many character 𝑠


1. input 𝑛
2. input the 𝑛-character long string 𝑥1 𝑥2 𝑥3 ⋯ 𝑥𝑛
3. input 𝑠
4. 𝑖 = 0
5. 𝑐ℎ𝑎𝑟𝑎𝑐𝑡𝑒𝑟_𝑐𝑜𝑢𝑛𝑡 = 0
6. repeat
6.1. 𝑖 ← 𝑖 + 1
6.2. if 𝑥𝑖 = 𝑠 then
6.2.1. 𝑐ℎ𝑎𝑟𝑎𝑐𝑡𝑒𝑟_𝑐𝑜𝑢𝑛𝑡 ← 𝑐ℎ𝑎𝑟𝑎𝑐𝑡𝑒𝑟_𝑐𝑜𝑢𝑛𝑡 + 1
until 𝑖 = 𝑛
7. if 𝑐ℎ𝑎𝑟𝑎𝑐𝑡𝑒𝑟_𝑐𝑜𝑢𝑛𝑡 ≠ 0 then
7.1. output ‘The string contains 𝑐ℎ𝑎𝑟𝑎𝑐𝑡𝑒𝑟_𝑐𝑜𝑢𝑛𝑡 character 𝑠’
else
7.2. output ‘The string does not contain the character 𝑠’
Chapter 1. Algorithms

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.

Exercise 1.16. Write a version of Algorithm 13 using the while-do structure.

1.4.5. Identifying same characters in two strings.

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.

Algorithm 14. Finding a common character


1. input 𝑛
2. input the 𝑛-character long string 𝑥1 𝑥2 𝑥3 ⋯ 𝑥𝑛
3. input the 𝑛-character long string 𝑦1 𝑦2 𝑦3 ⋯ 𝑦𝑛
4. for 𝑖 = 1 to 𝑛 do
4.1. for 𝑗 = 1 to 𝑛 do
4.1.1. if 𝑥𝑖 = 𝑦𝑗 then
4.1.1.1. return “A common character has been found; it is 𝑥𝑖 "
5. return “There is no common character in the given two strings"

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.4.6. Sum of matrices (Optional)

A matrix can be loosely defined as an array of numbers. An example is

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.

Algorithm 15. Sum of two matrices 𝐴 and 𝐵


1. input 𝑎𝑖,𝑗 , 𝑖 = 1, … , 𝑚, and 𝑗 = 1, … , 𝑛.
2. input 𝑏𝑖,𝑗 , 𝑖 = 1, … , 𝑚, and 𝑗 = 1, … , 𝑛.
3. for 𝑖 = 1 to 𝑚 do
3.1. for 𝑗 = 1 to 𝑛 do
3.1.1. 𝑐𝑖,𝑗 = 𝑎𝑖,𝑗 + 𝑏𝑖,𝑗
4. output 𝑐𝑖,𝑗 , 𝑖 = 1, … , 𝑚, and 𝑗 = 1, … , 𝑛.
Chapter 1. Algorithms

Exercise 1.18. Let


20 −10 −4 7
𝐴=[8 12 ] and 𝐵 = [ 10 5].
15 −5 −5 8
Compute 𝐴 + 𝐵.
Exercise 1.19. For the matrices 𝐴 and 𝐵 given in Exercise 1.17, how many times does
Algorithm 14 perform the for-do loop in Step 3? How many times does Algorithm 14
perform the for-do loop in Step 3.1?

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?

You might also like