Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Algorithms and their Applications CS2004 (2012-2013) Dr Stephen Swift 3.1 Mathematical Foundation."— Presentation transcript:

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

2 Mathematical FoundationSlide 2 Previously on CS2004… We looked at how to compare a number of algorithms Primitive Operations The core topic of counting Primitive Operations We discussed why experimental studies could not always be used as a comparison We looked at Pseudo-Code

3 Mathematical FoundationSlide 3 Mathematics For this module we are going to cover some mathematical topics Mathematics is a descriptive language It can be used to precisely describe how numerical items relate to each other It can be used to model the real world We will go over these slides rather quickly They should be used as a reference

4 Mathematical FoundationSlide 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

5 Mathematical FoundationSlide 5 What We are Going to Cover How to interpret simple equations and implement them in Java The basics: Variables Sets Equations Functions Subscripts Summation Products And a few other topics...

6 Mathematical FoundationSlide 6 What We are NOT Going to Cover Differentiation Integration Solving Equations Writing mathematical proofs Statistics But we will use simple summary statistics

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

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

9 Mathematical FoundationSlide 9 Basics – Sets A set is a finite or infinite collection of items A set has no order A set only contains one copy of an item We write a  to say item a is a member of set A The empty set: ϕ |A| is the cardinality of A, i.e. How many items in A Some well known sets: R : Real numbers Z : Integers N : Natural numbers (integers ≥ 0) The alphabet

10 Mathematical FoundationSlide 10 Set Operators – Part 1 Creating: A = { : } A = { n 2 : 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

11 Mathematical FoundationSlide 11 Set Operators – Part 2 Subset: 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 integers Superset: 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

12 Mathematical FoundationSlide 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) ax 2 +bx+c=0 (a quadratic equation) (x-a) 2 +(y-b) 2 =r 2 (a circle of radius r with centre a,b )

13 Mathematical FoundationSlide 13 Equations – Part 2 Often we have to simplify an equation This often means adding up similar terms and ordering the powers E.g.: (n-1)(n-2)(n-3)=(n 2 -2n-n+2)(n-3)=(n 2 -3n+2)(n-3)=n 3 -3n 2 -3n 2 +9n+2n-6 =n 3 -6n 2 +11n-6 In a previous years exam 15% of the students did the following: n×n×n=3n!!!

14 Mathematical FoundationSlide 14 Functions – Part 1 A function is a relation that uniquely associates members of one set with members of another set Functions 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) = x 2

15 Mathematical FoundationSlide 15 Functions – Part 2 There is a special function that we will be using in some of our work This is the exponential function f(x) = exp(x) = e x where e = 2.718…

16 Mathematical FoundationSlide 16 Subscripts – Part 1 A subscript is a natural number that indexes a list of variables. For example: Let X be the list [ x 1,...,x n ], then to access any element we use the notation x i, 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

17 Mathematical FoundationSlide 17 Subscripts – Part 2 For example: X = [5,2,-8,3.6,88,2000.003] Then x 1 =5, x 2 = 2, x 3 = -8 etc… Note also that |X|=6 Note also that we use double subscripts for arrays (matrices)

18 Mathematical FoundationSlide 18 Summation – Part 1 Let X be the list [ x 1,...,x n ], then if we want sum all of the elements up, we would use the notation: Note that we are arbitrarily assigning the result to s. If we wanted to sum the squares:

19 Mathematical FoundationSlide 19 Summation – Part 2 Like with a for loop, the bottom value is the starting value, and the top value is the end value Note 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

20 Mathematical FoundationSlide 20 Products Let X be the list [ x 1,...,x n ], then if we want multiple together all of the elements, we would use the notation: Note that we are arbitrarily assigning the result to s. If we wanted to multiply the squares:

21 Mathematical FoundationSlide 21 Factorial The notation n! ( n factorial) is defined as multiply all of the integers between 1 and n together E.g. 6! = 1.2.3.4.5.6 = 720 n ! also gives the number of possible arrangements of n items Note that 0! = 1

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

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

24 Mathematical FoundationSlide 24 Permutations and Combinations If 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

25 Mathematical FoundationSlide 25 Floor and Ceiling Functions Floor  x  is the largest integer less than or equal to x  x   x  17.3  = 17 Ceiling  x  is the smallest integer greater than or equal to x  x   x  17.3  =18

26 Mathematical FoundationSlide 26 Logarithms – Part 1 Log b ( x ) = y means that x = b y Log b ( b )=1 b 1 = b for all b  0 Log b (1)=0 b 0 =1 for all b  0 Log b ( xy )= Log b ( x )+ Log b ( y ) Log b ( x/y )= Log b ( x )- Log b ( y ) Log b ( x y )= y Log b ( x ) Log b ( x )= Log a ( x )/ Log a ( b ) Log b ( x ) where x  0 is undefined

27 Mathematical FoundationSlide 27 Logarithms – Part 2 For example: Log 10 (1000) = 3 Log 2 (8) = 3 Note that: If y = exp(x) then Ln(y) = x natural logarithm This is known as the natural logarithm If y = a x b z then Ln(y) = Ln(a x )+Ln(b z ) = xLn(a)+zLn(b)

28 Mathematical FoundationSlide 28 Other Topics You May Need Probability Generating 1 in n chances Covered in level 1 Summing series Adding up 1+2+3+4+....+ n Covered in level 1 Summary statistics Mean, median, variance, etc... Covered in level 1

29 Mathematical FoundationSlide 29 Next Topic We will next look in more detail at Time Complexity and Asymptotic Notation The laboratory will be a Mathematical CAA Test


Download ppt "Algorithms and their Applications CS2004 (2012-2013) Dr Stephen Swift 3.1 Mathematical Foundation."

Similar presentations


Ads by Google