Computer science is often treated as an engineering discipline, and a central goal of engineering is solving problems. This chapter introduces algorithms, which are systematic methods for solving problems. It also introduces pseudocode, a useful tool for describing and analysing algorithms, together with number representations used in computer science.

Numbers

Every reader could be quite surprised when seeing the title for this section. Yes, numbers, we have known what is number since the very beginning when we get to learn math as toddlers. In this section, we will explain the system of number, not only will we figure out how numbers and their operations are defined, but how they are categorized.

Typology of Numbers

This part recalls the type of numbers we’ve learned since primary school and their set notations.

  1. Natural Numbers

    • Definition: Natural numbers are the set of positive integers used for counting and ordering, which do not include zero or negative numbers.

    • Set Notation: N={1,2,3,}\mathbb{N} = \{1, 2, 3, \ldots\}

  2. Integers

    • Definition: Integers are all the whole numbers including positive natural numbers, their negatives, and zero.

    • Set Notation: Z={,3,2,1,0,1,2,3,}\mathbb{Z} = \{\ldots, -3, -2, -1, 0, 1, 2, 3, \ldots\}

  3. Rational Numbers

    • Definition: Rational numbers are numbers that can be expressed as the quotient of two integers, a fraction ab\frac{a}{b}, where aa and bb are integers and b0b \neq 0. The set includes all integers and fractions.

    • Set Notation: Q={aba,bZ,b0}\mathbb{Q} = \left\{\frac{a}{b} \mid a, b \in \mathbb{Z}, b \neq 0\right\}

Remark

For numbers that are written in finite decimal places, if there is a looping part in the decimal places, it is still recognized as rational numbers. For instance, 13\frac{1}{3} could be represented as 0.333330.33333\dots. Usually, we use upperline to mark the repeating part, in this case it is0.30.\overline{3}. But for those who have infinitely non-repeating decimal places, such as π=3.1415926535\pi=3.1415926535\dots, we categorize it as irrational number, as they cannot be written in the ab\frac{a}{b} form.

  1. Irrational Numbers

    • Definition: Irrational numbers are real numbers that cannot be expressed as a ratio of two integers. The decimal expansion of irrational numbers is non-terminating and non-repeating. Examples include π\pi and 2\sqrt{2}.

    • Set Notation: I={xRxQ}\mathbb{I} = \{x \in \mathbb{R} \mid x \notin \mathbb{Q}\} (Note: I\mathbb{I} is used here for illustrative purposes and is not a standard symbol.)

  2. Real Numbers

    • Definition: The real numbers include both rational and irrational numbers, encompassing all points on an infinitely extended number line. The set of real numbers is continuous and is composed of all limits of sequences of rational numbers.

    • Set Notation: R={xx is a limit of a sequence of rational numbers}\mathbb{R} = \{x \mid x \text{ is a limit of a sequence of rational numbers}\}

  3. Prime Numbers

    • Definition: A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In other words, pp is prime if p>1p > 1 and if pp is divisible only by 1 and pp.

    • Set Notation: P={pNp>1 and p has no divisors other than 1 and p}\mathbb{P} = \{p \in \mathbb{N} \mid p > 1 \text{ and } p \text{ has no divisors other than } 1 \text{ and } p\}

    • Examples: The first few prime numbers are: 2,3,5,7,11,13,17,2, 3, 5, 7, 11, 13, 17, \ldots

    The following Venn diagram shows the relationship between different number sets.

Figure pending: Venn Diagram of Number Sets

Remark

Complex number is currently not a something necessary, as our discussion so far only falls in the real number set.

The Real Number System

Considering the real numbers are the only system involved so far this book, here, we provide a new mathematical perspective that is different what we were taught, to understand the real number system. We will introduce three axioms that real number holds.

Definition

A set SS with operations ++ and \cdot and distinguished elements 0 and 1 with 010 \neq 1 is a field if the following properties hold for all x,y,zSx, y, z \in S:

  • x+ySx + y \in S Closure

  • (x+y)+z=x+(y+z)(x + y) + z = x + (y + z) Associativity

  • x+y=y+xx + y = y + x Commutativity

  • x+0=xx + 0 = x Identity

  • given xx, there is a wSw \in S such that x+w=0x + w = 0 Inverse

  • xySx \cdot y \in S Closure

  • (xy)z=x(yz)(x \cdot y) \cdot z = x \cdot (y \cdot z) Associativity

  • xy=yxx \cdot y = y \cdot x Commutativity

  • x1=xx \cdot 1 = x Identity

  • for x0x \neq 0, there is a wSw \in S such that xw=1x \cdot w = 1 Inverse

  • x(y+z)=xy+xzx \cdot (y + z) = x \cdot y + x \cdot z Distributive Law

The operations ++ and \cdot are called addition and multiplication. The elements 0 and 1 are the additive identity element and the multiplicative identity element, respectively.

It follows from these axioms that the additive inverse and multiplicative inverse (of a nonzero xx) are unique. The additive inverse of xx is the negative of xx, written as x-x. To define subtraction of yy from xx, we let xy=x+(y)x - y = x + (-y). The multiplicative inverse of xx is the reciprocal of xx, written as x1x^{-1}. The element 0 has no reciprocal. To define division of xx by yy when y0y \neq 0, we let xy=x(y1)\frac{x}{y} = x \cdot (y^{-1}). We write xyx \cdot y as xyxy and xxx \cdot x as x2x^2. We use parentheses where helpful to clarify the order of operations.

Definition

A positive set in a field FF is a set PFP \subset F such that for x,yFx, y \in F,

  • x,yPx, y \in P implies x+yPx + y \in P Closure under Addition

  • x,yPx, y \in P implies xyPxy \in P Closure under Multiplication

  • xFx \in F implies exactly one of x=0x = 0, xPx \in P, xP-x \in P Trichotomy

An ordered field is a field with a positive set PP. In an ordered field, we define x<yx < y to mean yxPy - x \in P. The relations ,>,\leq, >, and \geq have analogous definitions in terms of PP.

Note that P={xF:x>0}P = \{ x \in F : x > 0\}. Another phrasing of trichotomy is that each ordered pair (x,y)(x, y) satisfies exactly one of x<yx < y, x=yx = y, x>yx > y. If SFS \subseteq F, then βF\beta \in F is an upper bound for SS if xβx \leq \beta for all xSx \in S.

Definition

An ordered field FF is complete if every nonempty subset of FF that has an upper bound in FF has a least upper bound in FF.

This theorem ensures the square roots of positive real numbers.

Axiom axi

and axi
imply many familiar property of arithmetic:

Proposition

Each of N\mathbb{N}, Z\mathbb{Z}, and Q\mathbb{Q} is closed under addition and multiplication, Z\mathbb{Z} and Q\mathbb{Q} are closed under subtraction, and the set of nonzero numbers in Q\mathbb{Q} is closed under division.

The next four propositions state properties of an ordered field FF. All statements apply for each choice of x,y,z,u,vFx, y, z, u, v \in F.

Proposition

Elementary consequences of the field axioms. a)x+z=y+z implies x=yb)x0=0c)(x)y=(xy)d)x=(1)xe)(x)(y)=xyf)xz=yz and z0 imply x=yg)xy=0 implies x=0 or y=0\begin{aligned} \text{a)} &\quad x + z = y + z \text{ implies } x = y \\ \text{b)} &\quad x \cdot 0 = 0 \\ \text{c)} &\quad (-x)y = -(xy) \\ \text{d)} &\quad -x = (-1)x \\ \text{e)} &\quad (-x)(-y) = xy \\ \text{f)} &\quad xz = yz \text{ and } z \neq 0 \text{ imply } x = y \\ \text{g)} &\quad xy = 0 \text{ implies } x = 0 \text{ or } y = 0 \end{aligned}

Proposition

O1:xxReflexive PropertyO2:x<y and y<x imply x=yAntisymmetric PropertyO3:x<y and y<z imply x<zTransitive PropertyO4:At least one of x<y and y<x holdsTotal Ordering Property\begin{aligned} \text{O1:} &\quad x \leq x && \text{Reflexive Property} \\ \text{O2:} &\quad x < y \text{ and } y < x \text{ imply } x = y && \text{Antisymmetric Property} \\ \text{O3:} &\quad x < y \text{ and } y < z \text{ imply } x < z && \text{Transitive Property} \\ \text{O4:} &\quad \text{At least one of } x < y \text{ and } y < x \text{ holds} && \text{Total Ordering Property} \end{aligned}

Proposition

F1:xy implies x+zy+zAdditive Order LawF2:x<y and 0<z imply xz<yzMultiplicative Order LawF3:x<y and w<v imply x+w<y+vAddition of InequalitiesF4:0x,0w imply 0xwMultiplication of Inequalities\begin{aligned} \text{F1:} &\quad x \leq y \text{ implies } x + z \leq y + z && \text{Additive Order Law} \\ \text{F2:} &\quad x < y \text{ and } 0 < z \text{ imply } xz < yz && \text{Multiplicative Order Law} \\ \text{F3:} &\quad x < y \text{ and } w < v \text{ imply } x + w < y + v && \text{Addition of Inequalities} \\ \text{F4:} &\quad 0 \leq x, 0 \leq w \text{ imply } 0 \leq xw && \text{Multiplication of Inequalities} \end{aligned}

Proposition

(a)x<y implies y<x(b)xy and z0 imply yzxz(c)0x and 0y imply 0xy(d)0x2(e)0<1(f)0<x implies 0<x1(g)0<x<y implies 0<y1<x1\begin{aligned} \text{(a)} &\quad x < y \text{ implies } -y < -x \\ \text{(b)} &\quad x \leq y \text{ and } z \leq 0 \text{ imply } yz \leq xz \\ \text{(c)} &\quad 0 \leq x \text{ and } 0 \leq y \text{ imply } 0 \leq xy \\ \text{(d)} &\quad 0 \leq x^2 \\ \text{(e)} &\quad 0 < 1 \\ \text{(f)} &\quad 0 < x \text{ implies } 0 < x^{-1} \\ \text{(g)} &\quad 0 < x < y \text{ implies } 0 < y^{-1} < x^{-1} \end{aligned}

Floor, Ceiling, and Remainder

This Section discusses more form numbers that may not be as familiar as real numbers. We first introduce Integer Function:

Definition

If xx is any real number, we write \lfloor x \rfloor = \text{the greatest integer less than or equal to \( x ) (the floor of \( x ))} \lceil x \rceil = \text{the least integer greater than or equal to \( x ) (the ceiling of \( x ))}

Note that the xx could be not just a variable, but a mathematical expression.

Example

2=1,2=2,12=1,12=1,12=0 (not negative);\lfloor \sqrt{2} \rfloor = 1, \quad \lceil \sqrt{2} \rceil = 2, \quad \left\lceil \frac{1}{2} \right\rceil = 1, \quad \left\lfloor -\frac{1}{2} \right\rfloor = -1, \quad \left\lceil -\frac{1}{2} \right\rceil = 0 \ (\text{not negative});

Properties of Integer Function

We will look into the properties of integer function with its graph.

Figure pending: Visualization of ⌈x⌉ and ⌊x⌋

Theorem

Keep in mind the following important properties of integer function, which is often used in algorithm analysis or other mathematical proof.

  1. xxx\lfloor x \rfloor \leq x \leq \lceil x \rceil

  2. xZx=x\forall x\in \mathbb{Z} \text{, } \lceil x \rceil = \lfloor x \rfloor

  3. xZ    x=x+1x \notin \mathbb{Z} \iff \lceil x \rceil = \lfloor x \rfloor+ 1

  4. x=x\lfloor -x \rfloor = -\lceil x \rceil; x=x\lceil-x \rceil = - \lfloor x \rfloor

  5. x1<xxx<x+1x - 1 < \lfloor x \rfloor \leq x \leq \lceil x \rceil < x + 1

The proofs to these conclusions are quite basic, and is therefore not provided here.

Remainder and Integer Function

We introduce a new operation that we have learned before in this section with its notation.

Notation

The modulo operation, denoted as amodna \bmod n, finds the remainder when one integer aa is divided by another integer nn. If aa divided by nn gives a quotient qq with remainder rr, then a=nq+ra = nq + r, and rr would be the result of amodna \bmod n.

Moving back to the integer function, we have:

Theorem

x,yR,xmody=xyxy,if y0;xmod0=x\forall x, y \in \mathbb{R}, x \bmod y = x - y\left\lfloor \frac{x}{y} \right\rfloor, \quad \text{if } y \neq 0; \quad x \bmod 0 = x

Below is the proof to the first conclusion, where the properties of integer function are applied.

Proof

By the Division Algorithm, for any integer xx and any positive integer yy, there exist unique integers qq and rr such that x=qy+rx = qy + r and 0r<y0 \leq r < |y|, where qq is the quotient and rr is the remainder. The floor function xy\left\lfloor \frac{x}{y} \right\rfloor yields the largest integer less than or equal to xy\frac{x}{y}, which by definition is the quotient qq. Thus, we have: xy=q\left\lfloor \frac{x}{y} \right\rfloor = q and therefore: yxy=yqy\left\lfloor \frac{x}{y} \right\rfloor = yq Subtracting this from xx gives: xyxy=xyq=rx - y\left\lfloor \frac{x}{y} \right\rfloor = x - yq = r The uniqueness of the quotient and remainder in the Division Algorithm ensures that this value of rr is the remainder from the modulus operation. Therefore, we have: xmody=xyxyx \mod y = x - y\left\lfloor \frac{x}{y} \right\rfloor which is the remainder when xx is divided by yy, completing the proof.

Also, from definition, by dividing yy on both sides of the equation: 0xyxy=xmodyy<10\leq \frac{x}{y} - \lfloor\frac{x}{y}\rfloor = \frac{x \bmod y}{y} < 1 Among which, xmody<yx \bmod y <y.

Proof

By the definition of the modulo operation, xmodyx \mod y can be written as xyxyx - y\left\lfloor \frac{x}{y} \right\rfloor. Since xy\left\lfloor \frac{x}{y} \right\rfloor is the greatest integer less than or equal to xy\frac{x}{y}, we have: xyxyxyxy=xx=0.x - y\left\lfloor \frac{x}{y} \right\rfloor \geq x - y \cdot \frac{x}{y} = x - x = 0. Furthermore, because xy\left \lfloor \frac{x}{y} \right\rfloor is less than xy\frac{x}{y}, it follows that: xyxy<xy(xy1)=y.x - y\left\lfloor \frac{x}{y} \right\rfloor < x - y \cdot \left(\frac{x}{y} - 1\right) = y. Hence, 0xmody<y0 \leq x \mod y < y.

And we have:

Corollary

By above-mentioned conclusions:

  • If y>0, then 0xmody<yy > 0, \text{ then } 0 \leq x \bmod y < y.

  • If y<0, then0xmody>yy < 0, \text{ then} 0 \geq x \bmod y > y.

  • x(xmody) is an integral multiple of yx - (x \bmod y) \text{ is an integral multiple of } y.

We call xmodyx \bmod y the remainder when xx is divided by yy. We call xy\lfloor \frac{x}{y} \rfloor the quotient. We have xmody=0x \bmod y = 0 if and only if xx is a multiple of yy, that is, if and only if xx is divisible by yy. The notation yxy \mid x, read ”yy divides xx”, means that yy is a positive integer and xmody=0x \bmod y = 0.

exercises

Exercise

Let FF be a field consisting of exactly three elements 0,1,x0, 1, x. Prove that x+x=1x + x = 1 and that xx=1x \cdot x = 1. Obtain the addition and multiplication tables for FF.

Hint: Think on the property of filed: inverse of addition and multiplication.

Proof

Since FF is a field, it has the properties of both a group under addition and a group under multiplication (excluding 00 for the latter).

Part 1: Proof that x+x=1x + x = 1

  1. In a group, every element has an additive inverse. In FF, the additive inverse of 00 is 00 itself, since 0+0=00 + 0 = 0.

  2. The additive inverse of 11 cannot be 11 itself because 1+1=11 + 1 = 1 would imply 1=01 = 0, which is a contradiction. Therefore, 11‘s additive inverse must be some other element of FF, which can only be xx. Hence, 1+x=01 + x = 0.

  3. The element xx must also have an additive inverse in FF, which cannot be 00 (as 00‘s inverse is 00) and cannot be 11 (as 11‘s inverse is xx). The only option left is xx itself. Thus, x+x=0x + x = 0.

  4. Given x+x=0x + x = 0 and 1+x=01 + x = 0, by the cancellation law, it must be that x=1x = 1. However, this contradicts the assumption that xx is distinct from 11. Therefore, our assumption that x+x=0x + x = 0 is incorrect.

  5. The only remaining possibility is x+x=1x + x = 1.

Part 2: Proof that xx=1x \cdot x = 1

  1. Similarly, in a multiplicative group (excluding 00), every non-zero element has a multiplicative inverse. For 11, the multiplicative inverse is 11 itself since 11=11 \cdot 1 = 1.

  2. The element xx must have a multiplicative inverse. It cannot be 00 since 00 is not invertible, and it cannot be 11 since 11 is already serving as its own inverse.

  3. The only remaining option for the multiplicative inverse of xx is xx itself. Hence, xx=1x \cdot x = 1.

Now we construct the addition and multiplication tables for FF:

Addition Table: +01x001x11x0xx01\begin{array}{c|ccc} + & 0 & 1 & x \\ \hline 0 & 0 & 1 & x \\ 1 & 1 & x & 0 \\ x & x & 0 & 1 \\ \end{array}

Multiplication Table: ×01x0000101xx0x1\begin{array}{c|ccc} \times & 0 & 1 & x \\ \hline 0 & 0 & 0 & 0 \\ 1 & 0 & 1 & x \\ x & 0 & x & 1 \\ \end{array}

Problem

Is there a field with exactly four elements? Is there a field with exactly six elements?

Exercise

Let nn be an integer, and let xx be a real number. Prove that:

  1. x<n\lfloor x \rfloor < n if and only if x<nx < n;

  2. nxn \leq \lfloor x \rfloor if and only if nxn \leq x;

  3. xn\lfloor x \rfloor \leq n if and only if x<n+1x < n+1;

  4. n<xn < \lfloor x \rfloor if and only if n+1xn+1 \leq x;

  5. x=n\lfloor x \rfloor = n if and only if nx<n+1n \leq x < n+1;

  6. x=n\lceil x \rceil = n if and only if n1<xnn-1 < x \leq n.

Proof

The defining inequality is

xx<x+1.\lfloor x\rfloor \leq x < \lfloor x\rfloor+1.

Because nn is an integer, each statement follows by comparing xx with the adjacent integers nn and n+1n+1. In particular, x=n\lfloor x\rfloor=n exactly when nx<n+1n\leq x<n+1, and x=n\lceil x\rceil=n exactly when n1<xnn-1<x\leq n.

Exercise

Using the previous exercise, prove that x=x\lfloor -x \rfloor = -\lceil x \rceil.

Proof

Let m=xm=\lceil x\rceil. By the definition of the ceiling function, m1<xm.m-1<x\leq m. Multiplying by 1-1 reverses the inequalities: mx<(m1)=m+1.-m\leq -x<-(m-1)=-m+1. Thus m-m is the greatest integer less than or equal to x-x, so x=m=x\lfloor -x\rfloor=-m=-\lceil x\rceil.

Exercise

Prove that

k12=k2,k12=k21\left\lceil\frac{k-1}{2}\right\rceil=\left\lfloor\frac{k}{2}\right\rfloor, \qquad \left\lfloor\frac{k-1}{2}\right\rfloor=\left\lceil\frac{k}{2}\right\rceil-1

for every kZk\in\mathbb{Z}.

Proof

We will prove the statement by cases.

When kk is even, let k=2nk=2n. (2n1)/2=n12=n,and2n/2=n\lceil (2n-1)/2\rceil = \lceil n - \frac{1}{2}\rceil=n, and \lfloor 2n/2\rfloor = n.

When kk is odd, let k=2n+1k=2n+1. 2n/2=n\lceil 2n/2 \rceil = n, and 2n/2=n\lfloor 2n/2\rfloor=n.

For the second identity, if k=2nk=2n then (k1)/2=n1=k/21\lfloor(k-1)/2\rfloor=n-1=\lceil k/2\rceil-1, while if k=2n+1k=2n+1 then (k1)/2=n=k/21\lfloor(k-1)/2\rfloor=n=\lceil k/2\rceil-1.

Algorithm and Algorithm Analysis

This Section discusses what is algorithm, and more importantly, how algorithms are assessed.

Algorithm

What is an Algorithm?

Definition

An algorithm is a well-defined, step-by-step procedure or sequence of instructions designed to solve a specific class of problems:

  • Each step in an algorithm must be clear and unambiguous.

  • Algorithms must be solvable, meaning they should be able to produce a correct solution for any valid input within a finite amount of time.

  • An algorithm must terminate, i.e., it should have a defined end, at which point the goal has been achieved and the final output is produced.

Here is an example of multiplication algorithm for better understanding of the concept.

Example

To find the product of integers MM and NN, both larger than one:

  1. Start two columns on a page, one labeled “A” and the other “B”; and put the value of MM under A and the value of NN under B.

  2. Repeat

  • calculate a new A-value by multiplying the old A-value by 2; and

  • calculate a new B-value by dividing the old B-value by 2 and reducing the result by a half if necessary to obtain an integer;

Until the B-value equals one.

  1. Go down the columns crossing out the A-value whenever the B-value is even.

  2. Add up the remaining A-values and “return” the sum.

To show how it works, assume A=73A=73 and B=41B=41.

A B


73 41 146 20 (2012\frac{1}{2} is reduced to 20) 292 10 584 5 1168 2 (212\frac{1}{2} is reduced to 2) 2336 1

: Execution of RPM

Sum of the remaining A-values: 2336+584+73=29932336+584+73=2993.

Let’s review this algorithm referring to definition def

.

  1. Clarity and Accuracy: All the instructions are clear and manipulable, nothing is ambiguous.

  2. Solvability: It is no doubt that for any two real numbers, we can find their product.

  3. Termination: For which ever numbers, we can always solve the problem in limited steps, as the terminate condition is when B=1B=1, while B is divided by 2 (integer division) repetitively.

Hence, the RPM is a good example of algorithm, and with this, we could tell whether something else is an algorithm or not.

Pseudocode

Pseudocode is a simplified, half-code, half-natural language script used by software developers and algorithm designers to outline the structure of a program or algorithm. It’s not executable code, but rather a high-level representation of the algorithm’s logic. The purpose of pseudo-code is to express the design of an algorithm in a form that can be easily translated into actual programming languages. It is written in a way that is understandable to people who do not necessarily know the syntax of programming languages. Pseudo-code allows the designer to focus on the core logic of the algorithm without getting bogged down with the syntactic details of a particular programming language. It often uses control structures like if-then-else, while, for, and others that are common to many high-level languages. Understanding pseudocode is quite easy as it quite close to natural languages.

Here’s the RPM transcribed in pseudocode:

$product \gets 0$ $product \gets product + A$ $A \gets A \times 2$ $B \gets B \div 2$ **return** $product$

To explicit:

  • procedure RPM(A, B): Defines a procedure or function named ‘RPM’ taking two parameters ‘A’ and ‘B’.

  • product \leftarrow 0: The assignment operator '\leftarrow' is used to assign the value on the right (0 here) to the variable on the left (‘product’).

  • while B >> 0 do: Begins a ‘while’ loop that continues as long as the condition ‘B >> 0’ is true. The ‘do’ indicates that the following block of code will execute if the condition is met.

  • if B is odd then: A conditional statement that checks if ‘B’ is odd. If it is, the subsequent statement is executed.

  • product \leftarrow product + A: An assignment operation that adds ‘A’ to ‘product’ and assigns the sum back to ‘product’.

  • end if: Marks the end of the ‘if’ statement.

  • A \leftarrow A ×\times 2: Multiplies the value of ‘A’ by 2 and then assigns the result back to ‘A’.

  • B \leftarrow B ÷\div 2: Divides the value of ‘B’ by 2 (integer division) and assigns the result back to ‘B’.

  • end while: Marks the end of the ‘while’ loop.

  • return product: The ‘return’ statement indicates the output or result of the procedure, which here is the value of the variable ‘product’.

  • end procedure: Marks the end of the ‘RPM’ procedure.

All later algorithms in this book will be presented using pseudocode.

Algorithm Analysis

Now let’s take a look at this algorithm from another aspects. Is this a good or a bad algorithm. People assess algorithms by examine its complexity, which could be either space complexity or time complexity. The former refers to the the relationship between the input and the space needed to execute the algorithm, the latter, similarly, refers to the time needed. Many tools are available to quantify complexity, for both space and time complexity, the big O notation is the most common measurement.

Definition

Big O notation is used to classify algorithms according to how their running time or space requirements grow as the input size grows. The notation describes an upper limit on the time an algorithm could possibly take to complete, given the size of the input. For a function f(n)f(n), where n is the scale of input for the algorithm, the Big O notation is formally defined as follows with cc as a positive constant: O(f(n))={g(n):Where c and n0 such that 0g(n)cf(n) for all nn0}O(f(n)) = \{ g(n) : \text{Where } c \text{ and } n_0 \text{ such that } 0 \leq g(n) \leq c \cdot f(n) \text{ for all } n \geq n_0 \}

The following table provides common time complexities using Big O notation:

f(n)f(n) Description


1 Constant logn\log n Logarithmic n Linear nlognn \log n Linearithmic n2n^2 Quadratic n3n^3 Cubic 2n2^n Exponential n!n! Factorial

: Common time complexities in Big O notation

We can visualize it using function graph

Figure pending: Time Complexity Visualization

So which space and time complexity does this algorithm fall in?

Time Complexity

To analyze the time complexity, we usually focus on the termination condition or the number of iteration for the algorithm. For the real number BB in RPM, each time it is divided by 2 until B=1B = 1. Therefore, the total number of iteration will be log2B\log_2 B, which is categorized in O(logn)O(\log n).

Remark

Actually, the time complexity of an algorithm could be shown by strict proof using MI. Here is the proof on the time complexity of RPM.

Proof: We will use mathematical induction to prove that the number of steps in the algorithm is proportional to log2(B)\log_2(B).

Base Case:\ When B=1B = 1, the algorithm requires only one step. This is consistent with log2(1)=0\log_2(1) = 0, which satisfies our complexity class O(logn)O(\log n).

Inductive Hypothesis:\ Assume that for a positive integer kk, when B=kB = k, the algorithm operates within log2(k)\log_2(k) steps.

Inductive Step:\ Consider B=2kB = 2k. In the first step of the algorithm, BB is halved to kk, and SS is doubled. From this point, based on our inductive hypothesis, reaching B=1B = 1 requires log2(k)\log_2(k) steps.

Hence, for A=2kA = 2k, the total number of steps is log2(k)+1\log_2(k) + 1. Using the properties of logarithms, we have: log2(k)+1=log2(k)+log2(2)=log2(2k)\begin{aligned} \log_2(k) + 1 &= \log_2(k) + \log_2(2) \\ &= \log_2(2k) \end{aligned}

Therefore, for any A=2kA = 2k, the total number of steps is also log2(2k)\log_2(2k), proving that for any positive integer AA, the time complexity of the Russian Peasant Multiplication is O(logA)O(\log A).

Space Complexity

The space complexity of the Russian Peasant Multiplication algorithm is determined by the amount of memory required to store the operands and the intermediate results. Initially, only two numbers need to be stored: the multiplicands. As the algorithm proceeds, we need additional space to keep track of the current product. Since the algorithm does not use any complex data structures and only requires a fixed number of variables, the space complexity is O(1)O(1), indicating constant space usage. It does not depend on the size of the input operands, as the memory required does not increase with larger numbers.

Exercises

Exercise

A non-recursive Square and Multiply Algorithm to calculate bnb^n.

Precondition: nn is a positive integer and bb is of any type that can be multiplied.

Postcondition: the value returned is equal (b)n(b)^n.

  1. Show that the algorithm terminates. Let aka_k denote the value of aa after the kkth iteration of the while-loop, and let s=lg(n)s = \lfloor \lg(n) \rfloor. Prove by Mathematical Induction on kk that For any nonnegative integer kk, after kk iterations of the while-loop: 2skak<2sk+12^{s-k} \le a_k < 2^{s-k+1}

  2. Proof of correctness. Use Mathematical Induction on kk to prove For any nonnegative integer kk, after kk iterations of the while-loop: (square)a×product=(b)n(\text{square})^a \times \text{product} = (b)^n

$product \gets 1$ $square \gets b$ $a \gets n$ $product \gets product \times square$ $square \gets square \times square$ $a \gets \lfloor a / 2 \rfloor$ $product \times square$
Proof

1. Base Case k=0k=0: For k=0k = 0, a0=na_0 = n, which is the initial value of aa. Since s=lg(n)s = \left\lfloor \lg(n) \right\rfloor, it is the greatest integer less than or equal to lg(n)\lg(n), therefore 2sn<2s+12^s \leq n < 2^{s+1}. So, for k=0k = 0, the predicate holds because: 2s20a0=n<2×2s20\frac{2^s}{2^0} \leq a_0 = n < 2 \times \frac{2^s}{2^0}

Inductive Step: Assume P(k)P(k) holds for some nonnegative integer kk. That is: 2s2kak<2×2s2k\frac{2^s}{2^k} \leq a_k < 2 \times \frac{2^s}{2^k} We need to show P(k+1)P(k+1) holds. During each iteration, aa is halved (integer division by 2), which gives us: ak+1=ak2a_{k+1} = \left\lfloor \frac{a_k}{2} \right\rfloor

Since aka_k is an integer, ak2\left\lfloor \frac{a_k}{2} \right\rfloor will either be ak2\frac{a_k}{2} or ak12\frac{a_k - 1}{2}, depending on whether aka_k is even or odd. Thus, we have: 2s2k+1ak2<2×2s2k+1\frac{2^s}{2^{k+1}} \leq \left\lfloor \frac{a_k}{2} \right\rfloor < 2 \times \frac{2^s}{2^{k+1}}

Since ak<2×2s2ka_k < 2 \times \frac{2^s}{2^k}, dividing by 2 gives ak2<2s2k\frac{a_k}{2} < \frac{2^s}{2^k}, and therefore: ak+1<2×2s2k+1a_{k+1} < 2 \times \frac{2^s}{2^{k+1}}

Similarly, 2s2kak\frac{2^s}{2^k} \leq a_k implies 2s2k+1ak2\frac{2^s}{2^{k+1}} \leq \frac{a_k}{2}, so we have: 2s2k+1ak+1\frac{2^s}{2^{k+1}} \leq a_{k+1} This completes the inductive step and thus, by induction, P(k)P(k) holds for all nonnegative integers kk.

Proof

2. We need to prove that after kk iterations of the while-loop, the invariant holds: (square)a×product=bn(\text{square})^a \times \text{product} = b^n

Base Case k=0k=0: Initially, product=1\text{product} = 1, square=b\text{square} = b, and a=na = n. So, (square)a×product=bn(\text{square})^a \times \text{product} = b^n is trivially true.

Inductive Step: Assume the invariant holds after kk iterations, i.e., (squarek)ak×productk=bn(\text{square}_k)^{a_k} \times \text{product}_k = b^n Now, consider the k+1k+1th iteration. There are two cases:

Case 1 (aka_k is odd): The product is updated by multiplying it with squarek\text{square}_k, and we have: productk+1=productk×squarek\text{product}_{k+1} = \text{product}_k \times \text{square}_k Since aka_k is odd, we can write ak=2m+1a_k = 2m + 1 for some integer mm, and after the iteration, aa becomes ak+1=ma_{k+1} = m. The invariant becomes: (squarek)2m+1×productk=(squarek)m×productk+1=bn(\text{square}_k)^{2m+1} \times \text{product}_k = (\text{square}_k)^m \times \text{product}_{k+1} = b^n

Case 2 (aka_k is even): The product remains the same, and aka_k can be written as 2m2m, so the invariant remains: (squarek)2m×productk=(squarek)m×productk=bn(\text{square}_k)^{2m} \times \text{product}_k = (\text{square}_k)^m \times \text{product}_k = b^n since squarek+1=(squarek)2\text{square}_{k+1} = (\text{square}_k)^2 and ak+1=ma_{k+1} = m.

In both cases, after the iteration, square\text{square} is squared, so we get: squarek+1=(squarek)2\text{square}_{k+1} = (\text{square}_k)^2 and therefore, the invariant still holds as: (squarek+1)ak+1×productk+1=bn(\text{square}_{k+1})^{a_{k+1}} \times \text{product}_{k+1} = b^n

Thus, by mathematical induction, the invariant holds true for every iteration of the loop, proving the correctness of the algorithm.

Recursion

Recursively Defined Algorithms and Structures

Structural Induction