Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mathematics Basic Math and Combinatorics

Similar presentations


Presentation on theme: "Mathematics Basic Math and Combinatorics"— Presentation transcript:

1 Mathematics Basic Math and Combinatorics

2 Math in Problems Many problems can be simplified with some analysis ahead of time Doing some analysis of the problem ahead of time can make a seemingly complex or long-running problem go much faster Not always a simplification, but sometimes need to think mathematically. Brute Force is sometimes the right approach, but avoid unnecessary work E.g. read lots of numbers, how many are < k? Answer: do O(n) loop and count. Do NOT sort the numbers.

3 Finding Patterns Often there will be a pattern, but not always obvious
Can try to generate several elements of a sequence, to try to ID the pattern Takes some experience to recognize patterns Generate by hand first, before getting computer to do it Generate the first few elements by brute force, so that you can try to discern a pattern. Look for solutions that could use Dynamic Programming Look for well-known sequences

4 Common Sequences Arithmetic: a+bi Geometric: a*ri Other sequences:
Let an = a+(n-1)b Then the sum from a1 to an is n(a1+an)/2 e.g. n(n+1)/2 Geometric: a*ri Sum from a1 to an is a(1-rn)/(1-r) if r<1. Other sequences: Fibonacci Factorial Catalan, etc.

5 Logarithms Can simplify many operations
Convert multiplication sequence to addition, or exponentiation to multiplication Solve the easier problem, then take exponent (if needed) Can use to convert from one base to another

6 Java BigInteger Provides arbitrary precision integer math
When an unsigned long long is not large enough to hold Operates much slower than HW-supported integer operations Unfortunately, only available in Java So, if it’s needed, you need to code in Java Has other features Convert to Bases GCD Modulo arithmetic Probabilistic Prime Testing See book, section 5.3

7 Combinatorics - Permutations
Number of ways to permute n objects = n! Note: factorial can only be computed to ~20!, and that’s with long longs So, must cancel out factorials when possible. Number of permutations on n objects where there are more than one with same value: n!/[n1! n2! n3!...nk!]

8 Combinatorics – Combinations
Choosing k objects from a set of n: C(n, k) i.e. n choose k n!/[(n-k)!k!] Notice C(n,k) = C(n,n-k) Choose k objects from n, but allow values to be chosen more than once C(n+k-1, k) Can use a Dynamic Programming approach: C(n, k) = C(n-1,k-1) + C(n-1,k) Good if you need most but not all combinations.

9 Combinatorics examples
Given an nxm grid, how many rectangles can be made on the grid? Choose 2 vertical lines, choose 2 horizontal lines, then all combinations C(n,2)*C(m,2) Divide k balls into n boxes C(n+k-1, k) How many paths on a lattice from lower left to upper right, assuming monotonically increasing Use Dynamic Programming: 1 way to get to (0,0) and each (i,0) and (0, j) Then, paths to (i,j) = paths to (i-1, j) plus paths to (i, j-1)


Download ppt "Mathematics Basic Math and Combinatorics"

Similar presentations


Ads by Google