algorithms and their applications cs2004 (2012-2013) dr stephen swift 3.1 mathematical foundation

29
Algorithms and their Applications CS2004 (2012-2013) Dr Stephen Swift 3.1 Mathematical Foundation

Upload: leonard-wiggins

Post on 01-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Algorithms and their Applications

CS2004 (2012-2013)

Dr Stephen Swift

3.1 Mathematical Foundation

Mathematical Foundation Slide 2

Previously on CS2004…We looked at how to compare a number of algorithmsThe core topic of counting Primitive OperationsWe discussed why experimental studies could not always be used as a comparisonWe looked at Pseudo-Code

Mathematical Foundation Slide 3

MathematicsFor this module we are going to cover some mathematical topicsMathematics is a descriptive languageIt can be used to precisely describe how numerical items relate to each otherIt can be used to model the real worldWe will go over these slides rather quickly

They should be used as a reference

Mathematical Foundation Slide 4

Why?Why do we need mathematics in this module and the other modules?

Many of the techniques presented in this and other modules are based on a mathematical foundation

Nearly all computer science research papers will use mathematical notation

We can model the performance and efficiency of algorithms using mathematics

Mathematical Foundation Slide 5

What We are Going to CoverHow to interpret simple equations and implement them in JavaThe basics:

VariablesSetsEquationsFunctionsSubscriptsSummationProductsAnd a few other topics...

Mathematical Foundation Slide 6

What We are NOT Going to Cover

DifferentiationIntegrationSolving EquationsWriting mathematical proofsStatistics

But we will use simple summary statistics

Mathematical Foundation Slide 7

Variables – Part 1A variable is a symbol used to represent a mathematical constructE.g. numbers, sets, lists , vectors, matrices, …Often lower case letters or Greek letters are usedE.g. x, y, z, Ω, α, β, …

Mathematical Foundation Slide 8

Variables – Part 2Variables in Mathematics are treated the same as variables within a programming languageThey can be thought of as a box containing a value that can be read from or written to

Mathematical Foundation Slide 9

Basics – SetsA set is a finite or infinite collection of itemsA set has no orderA set only contains one copy of an itemWe write aÎA to say item a is a member of set AThe empty set: ϕ|A| is the cardinality of A, i.e. How many items in ASome well known sets:

R: Real numbers Z: Integers N: Natural numbers (integers ≥ 0) The alphabet

Mathematical Foundation Slide 10

Set Operators – Part 1Creating: A = {<variable> : <inclusion criteria>}A = {n2 : n is an integer and 0 ≤ n ≤ 5}A = {0, 1, 4, 9, 16, 25}

Union: A = B ∪ C, A = {a : aÎB OR aÎC}A contains all of B and C

Intersection: A = B ∩ C, A = {a : aÎB AND aÎC}A contains only what B and C have in common)

0 1 4 9 16 25

A

Mathematical Foundation Slide 11

Set Operators – Part 2Subset: If A is a set then B Í A (B is a subset of A) if every element of B is also in A, i.e. B is contained in A, e.g. N Í Z , i.e. all natural numbers are integersSuperset: In the example above A is a superset of B

Set Subtraction: A = C\B, A = {a : aÎC AND a B}A is B with all the elements of C removed

A

B

Mathematical Foundation Slide 12

Equations – Part 1

An equation uses mathematical operators to relate one set of variables or numbers to another set of variables or numbers

2+2 = 4 (a very simple equation)y = mx +c (a straight line)ax2+bx+c=0 (a quadratic equation)(x-a)2+(y-b)2=r2

(a circle of radius r with centre a,b)

Mathematical Foundation Slide 13

Equations – Part 2Often we have to simplify an equationThis often means adding up similar terms and ordering the powersE.g.:

(n-1)(n-2)(n-3)=(n2-2n-n+2)(n-3)=(n2-3n+2)(n-3)=n3-3n2-3n2+9n+2n-6

=n3-6n2+11n-6

In a previous years exam 15% of the students did the following:

n×n×n=3n!!!

Mathematical Foundation Slide 14

Functions – Part 1A function is a relation that uniquely associates members of one set with members of another setFunctions can take parameters,

E.g. f(x,y)= 2x+y Functions can be many to one E.g. f(x) = 7 Functions can be one to one E.g. f(x) = 7x + 3.21 Functions can be many to one E.g. f(x) = x2

Mathematical Foundation Slide 15

Functions – Part 2There is a special function that we will be using in some of our workThis is the exponential functionf(x) = exp(x) = ex

where e = 2.718…

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 20

1

2

3

4

5

6

7

8

x

exp(x

)

Mathematical Foundation Slide 16

Subscripts – Part 1A subscript is a natural number that indexes a list of variables. For example:

Let X be the list [x1,...,xn], then to access any element we use the notation xi, where 1≤i ≤n

We use the notation |X| to refer to the number of elements in the list X, which is n in this case

Mathematical Foundation Slide 17

Subscripts – Part 2For example:

X = [5,2,-8,3.6,88,2000.003]

Then x1=5, x2 = 2, x3 = -8 etc…Note also that |X|=6Note also that we use double subscripts for arrays (matrices)

Mathematical Foundation Slide 18

Summation – Part 1

Let X be the list [x1,...,xn], then if we want sum all of the elements up, we would use the notation:

n

n

ii

xxxxs

xs

...321

1

Note that we are arbitrarily assigning the result to s. If we wanted to sum the squares:

223

22

21

1

2

... n

n

ii

xxxxs

xs

Mathematical Foundation Slide 19

Summation – Part 2Like with a for loop, the bottom value is the starting value, and the top value is the end valueNote that i can be replaced by any other variable name, as long as you are consistent. By convention the variables i,j and k are often used for subscripts

Mathematical Foundation Slide 20

Products

Let X be the list [x1,...,xn], then if we want multiple together all of the elements, we would use the notation:

n

n

ii

xxxxs

xs

...321

1

Note that we are arbitrarily assigning the result to s. If we wanted to multiply the squares:

223

22

21

1

2

... n

n

ii

xxxxs

xs

Mathematical Foundation Slide 21

FactorialThe notation n! (n factorial) is defined as multiply all of the integers between 1 and n together

n

i

inn1

...3.2.1!

E.g. 6! = 1.2.3.4.5.6 = 720n! also gives the number of possible arrangements of n items

Note that 0! = 1

Mathematical Foundation Slide 22

Permutations

The number of ways that r ordered items can be picked from n items is defined as:

122

4.3.2.1

!2

!4P

)!(

!P

42

rn

nnr

Mathematical Foundation Slide 23

Combinations

The number of ways that r unordered items can be picked from n items is defined as:

62.2

4.3.2.1

!2!2

!4

2

4

)!(!

!C

rnr

n

r

nnr

Mathematical Foundation Slide 24

Permutations and CombinationsIf the order is important then we use permutations otherwise we use combinations For example if the key code to a lock is 745 then 574 would not open it!

Permutations, there are ways of choosing a 3 digit key code from the 10 digits

If we like a fruit salad containing banana, pear and grapes, then the order doesn’t matter!

Combinations, there are ways of choosing three fruits from ten fruits

720P103

120C103

Mathematical Foundation Slide 25

Floor and Ceiling FunctionsFloor

x is the largest integer less than or equal to xx x17.3 = 17

Ceiling x is the smallest integer greater than or equal to xx x17.3 =18

Mathematical Foundation Slide 26

Logarithms – Part 1Logb(x) = y means that x = by

Logb(b)=1b1=b for all b0

Logb(1)=0b0=1 for all b0

Logb(xy)= Logb(x)+ Logb(y)Logb(x/y)= Logb(x)- Logb(y)Logb(xy)= yLogb(x)Logb(x)= Loga(x)/ Loga(b)Logb(x) where x 0 is undefined

Mathematical Foundation Slide 27

Logarithms – Part 2For example:

Log10(1000) = 3Log2(8) = 3

Note that:If y = exp(x) then Ln(y) = xThis is known as the natural logarithm

If y = axbz thenLn(y) = Ln(ax)+Ln(bz) = xLn(a)+zLn(b)

Mathematical Foundation Slide 28

Other Topics You May NeedProbability

Generating 1 in n chancesCovered in level 1

Summing seriesAdding up 1+2+3+4+....+nCovered in level 1

Summary statisticsMean, median, variance, etc...Covered in level 1

Mathematical Foundation Slide 29

Next Topic

We will next look in more detail at Time Complexity and Asymptotic NotationThe laboratory will be a Mathematical CAA Test