0% found this document useful (0 votes)
4 views

1-CSC3011-Algorithms and Data Structures-Mathematical Background

Algorithm and data structure lecture slide

Uploaded by

Ben Aiah
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

1-CSC3011-Algorithms and Data Structures-Mathematical Background

Algorithm and data structure lecture slide

Uploaded by

Ben Aiah
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

CSC3011: Data Structures and

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

 an + an = 2an , for a = 2, 2n +2n = 2n+1


Mathematical Background

Indices and Logarithms


 Recall that if: y = ex then x = ln( y )

 More Generally

 With a being the base of the logarithm


Mathematical Background

Logarithms
 Theorems

 To convert the log from one base to another


Mathematical Background

Arithmetic Series
 Each term in an arithmetic series is increased by a
constant value (usually 1) :

n
nn  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  12
n
n n  12n  1
n

 i2 
6

i 0
3
i 
4
i 0

however, it is easier to see the pattern:

n n  12n  1 n 3
n
n
nn  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

and if |r| < 1 then it is also true that



1

i 0
i
r 
1 r
Mathematical Background

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

 To dispute a statement simply give an


occurrence in which the statement does not
hold: this is referred to as Proof by
counterexample
Mathematical Background

Principles of Mathematical Induction


Proceeds in a sequence of three steps
I. Test the base case:
 establish the theorem is true for some small
value(s)

II. Inductive hypothesis:


 Assume the theory is true for all cases up to n =
k

III. Proving for n = k + 1


 Use the inductive hypothesis to prove that the
theorem is true for n = k + 1
Mathematical Background

Example: Arithmetic series


Step I: The statement is true for n = 1:
1 2 n n  1
n

i 0
i 0  1 1 
2

2

Step II: Assume that the statement is true for n = k:

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

for n = k, we must show that

k 1
k  1k  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

By assumption, the second sum is known:


k k  1
k  1 
2

k  12  k  1k
2

k  1k  2 
2
Mathematical Background

Example: Arithmetic Series


 The statement is true for n = 1 and
the truth of the statement for n = k implies
the truth of the statement for n = k + 1.

 Therefore, by the process of mathematical


induction, the statement is true for all values
of n ≥ 1.
Mathematical Background

Geometric Series
 Proof by induction:
The formula is correct for n = 0:
0
1  r 01
r
i 0
i
1 and
1 r
1

For n = k, assume thatk 1  r k 1 ; then


 i 0
i
r 
1 r

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

and therefore, by the process of mathematical


induction, the statement is true for all n ≥ 0.
Mathematical Background

Proof by Contradiction
 Assume that the theory is not true

 Prove that this assumption leads to a breach


of well known theories and hence the
assumption was erroneous

 E.g. Prove that


 there are an infinite number of primes
 If a2 is divisible by 2 then a is divisible by 2
Mathematical Background

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

 E.g. Prove that


 If a2 is divisible by 2 then a is divisible by 2
Mathematical Background

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)

 This implies that N|A-B where | stands for


“divides” or A – B = Nk for k 

 E.g. 2383(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

 The first one can be solved without much ado


since powers of 45 will end with a five. You
can use modular arithmetic as well

 The second one could be challenging


Mathematical Background

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

Divide and Conquer Algorithms


 An important problem solving mechanism that
uses recursion and consists of two parts

 Divide: Smaller problems are solved recursively


( except the base case)
 Conquer The solution to the original problem is
then formed from the solutions to the subproblems

 They consist of at least two recursive calls


Mathematical Background

Dynamic Programming
 Some recursive algorithms may not perform
efficiently because they put a lot of load on
the compiler.

 In such cases these may be rewritten as


nonrecursive algorithm that systematically
records the results in a table,

 This technique is referred to as dynamic


programming

 E.g. An exponential algorithm to compute the


fibonacci numbers can be rewritten as a linear
Mathematical Background

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

 A sequence such as xn = 1/n gives an explicit formula for


each value in the sequence: 1, 1/2, 1/3, 1/4, ...

 A recurrence relation defines a sequence of values xn


where n is an integer by:
 Defining an initial value, e.g., x1 = 1
 Defining xn in terms of previous values
 For example,
xn = xn – 1 + 2
xn = 2xn – 1 + n
xn = xn – 1 + xn – 2
Mathematical Background

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

 In this case, we have


xn = 2n + 1 xn = 2 n + 1 – n – 2
respectively
Mathematical Background

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

You might also like