1-CSC3011-Algorithms and Data Structures-Mathematical Background
1-CSC3011-Algorithms and Data Structures-Mathematical Background
Algorithms
Lecture 1: Mathematics Background
Mathematical Background
Outline
This topic reviews the basic mathematics required
for this course:
Indices and Logarithms
Arithmetic and Geometric series
Modular Arithmetic
Proving Statements
Proof by Mathematical Induction, Proof by Contradiction, Proof
by counter example
Recursion
Recurrence relations
MAW DSA – Chapter 1 up to section 1.3
Mathematical Background
Exponents
Recall the following identities
anxam = an+m
an/am = an-m
(an)m = anm
More Generally
Logarithms
Theorems
Arithmetic Series
Each term in an arithmetic series is increased by a
constant value (usually 1) :
n
nn 1
1 2 3 n i
i 1 2
Mathematical Background
Arithmetic Series
Proof 1 (straight forward):
1 + 2 + 3 + ... + n – 2 + n – 1 + n
+ n + n – 1 + n – 2 + ... + 3 + 2 + 1
n + 1 + n + 1 + n + 1 + ... + n + 1 + n + 1 + n + 1
= n (n + 1)
Since we added the series twice, we must divide the
result by 2
Mathematical Background
Other Series
We could repeat this process, after all:
n 2 n 12
n
n n 12n 1
n
i2
6
i 0
3
i
4
i 0
n n 12n 1 n 3
n
n
nn 1 n 2
i 0
i
2
2
i 0
i 2
6
3
n 2 n 1
n 2
n4
i 0
3
i
4
4
Mathematical Background
Other Series
We can generalize this formula
n
n k 1
i 0
k
i
k 1
Demonstrating with k = 3 and k = 4:
Mathematical Background
Geometric Series
The next series we will look at is the geometric
series with common ratio r:
n n 1
1 r
i 0
ri
1 r
Proving Statements
There are two common ways of proving that a
mathematical statement is true
Proof by principle of Mathematical Induction
Proof by Contradiction
Proof by Contraposition
i 0
i 0 1 1
2
2
k k 1
k
i 0
i
2
Mathematical Background
Arithmetic Series
Step III: Using the assumption that
k k 1
k
i 0
i
2
k 1
k 1k 2
i 0
i
2
Mathematical Background
Arithmetic Series
Then, for n = k + 1, we have:
k 1 k
i k 1 i
i 0 i 0
Geometric Series
Proof by induction:
The formula is correct for n = 0:
0
1 r 01
r
i 0
i
1 and
1 r
1
k 1 k
1 r k 1 (1 r )r k 1 1 r k 1
r
i 0
i
r k 1
r r
i 0
i k 1
1 r
1 r
r k 1 r k 2 1 r k 1 1 r k 2 1 r ( k 1) 1
1 r 1 r 1 r
Proof by Contradiction
Assume that the theory is not true
Proof by Contraposition
A P be the statement of the form “p implies q”
The contraposition of P is “not q implies not p”
Prove the contraposition of the P
Modular Arithmetic
If two numbers A and B give the same
remainder when divided by N, we say they are
congruent modulo N
This is denoted as follows
A B(mod N)
E.g. 2383(mod 5)
Mathematical Background
Modular Arithmetic
There are three important facts from modular
arithmetic
Mathematical Background
Modular Arithmetic
If a b(mod n) and c
Then
n|a–b
n | c(a – b)
n | ac – bc
ac bc(mod n) by definition
Or
a – b = nr for some r since n|a – b
So ac – bc = c(a – b) = cnr which is divisible by
n
Therefore ac bc(mod n)
Mathematical Background
Recursion
A recursive method is a method that directly or
indirectly makes a call to itself
For recursion to work, the following rules have to be
adhered to:
1. Base case
Always have a base case for termination – no recursion
2. Make progress
Recursive calls make progress towards the base case
3. Design rule
Assume the recursive calls work
Example: Fibonacci numbers
4. Compound interest
Never duplicate work
Mathematical Background
Design Rule
Example:
Algorithms fib(n)
If n==0||n==1 then
Return 1
Else
Return fib(n-2) + fib(n-1)
End if
End.
……….
Check fib(4) evaluation
Mathematical Background
Applications of Recursion
Recursion has applications in the following
areas
Numerical Applications
Divide and Conquer Algorithms
Divide: smaller problems are solved recursively
Conquer: The solution is then formed from
solutions to sub problems
Dynamic programming
Solves subproblems non-recursively by recording
answers in a table
Backtracking Algorithms
Use recursion to try all possibilities
Mathematical Background
Numerical Applications
Solving Modular Exponentiation of the form
XN(mod P)
E.g. Suppose you want to know the last digit
of
4545
323
Numerical applications
Greatest common divisor
The greatest common divisor of two
nonnegative integers A and B, denoted by
gcd(A,B), is the largest integer D that divides
both A and B
E.g. gcd(70,25) = 5
A computationally efficient implementation
uses modular arithmetic
gcd (a, b) = gcd(b, a(mod b))
Mathematical Background
Dynamic Programming
Some recursive algorithms may not perform
efficiently because they put a lot of load on
the compiler.
Dynamic Programming
Example:
Algorithms fib(n)
X = new array of size n
X[0] = X[1] = 1
If(X[n] = 0)
X[n] = fib(n-1) + fib(n-2)
Else
Return X[n]
End.
Mathematical Background
Recurrence Relations
A recurrence relationship is a means of defining a
sequence based on previous values in the sequence
Recurrence Relations
Given the two recurrence relations
xn = x n – 1 + 2 xn = 2xn – 1 + n
and the initial condition x1 = 1 we would like
to find explicit formulae for the sequences
Summary
In this topic, we have discussed:
A heuristic background as to why we must use
mathematics
together with a mathematical review of:
Logarithms
Modular Arithmetic
Arithmetic and other series
Mathematical induction
Geometric series
Recurrence relations
Mathematical Background
Tutorial Sheet 1
MAW: Chapter 1 Exercises, p26.
1.1 - 2,
1.5 - 6,
1.7
1.8 a, b, c
1.9.
1.10
1.12
Refer to the supplement for more questions