More Mathematical Techniques

Slides:



Advertisements
Similar presentations
Factoring Polynomials
Advertisements

Study Guides Quantitative - Arithmetic - Numbers, Divisibility Test, HCF and LCM Mycatstudy.com.
ACM Workshop Number Theory.
Types of Number
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
1 Dynamic Programming Jose Rolim University of Geneva.
Recursion. n! (n factorial)  The number of ways n objects can be permuted (arranged).  For example, consider 3 things, A, B, and C.  3! = 6 1.ABC 2.ACB.
Dynamic Programming Technique. D.P.2 The term Dynamic Programming comes from Control Theory, not computer science. Programming refers to the use of tables.
Discrete Mathematics Recursion and Sequences
Set, Combinatorics, Probability, and Number Theory Mathematical Structures for Computer Science Chapter 3 Copyright © 2006 W.H. Freeman & Co.MSCS Slides.
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
- ABHRA DASGUPTA Solving Adhoc and Math related problems.
Mathematics of Cryptography Part I: Modular Arithmetic, Congruence,
Applied Discrete Mathematics Week 9: Relations
CSCI 1900 Discrete Structures
Quantitative - Arithmetic - Numbers, Divisibility Test, HCF and LCM
Factoring Polynomials
More Counting Lecture 16: Nov 9 A B …… f. This Lecture We will study how to define mappings to count. There will be many examples shown. Bijection rule.
Fall 2002CMSC Discrete Structures1 One, two, three, we’re… Counting.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2008 Dynamic programming  top-down vs. bottom-up  divide & conquer vs. dynamic programming  examples:
March 10, 2015Applied Discrete Mathematics Week 6: Counting 1 Permutations and Combinations How many different sets of 3 people can we pick from a group.
CSC401: Analysis of Algorithms CSC401 – Analysis of Algorithms Chapter Dynamic Programming Objectives: Present the Dynamic Programming paradigm.
CS 8833 Algorithms Algorithms Dynamic Programming.
Discrete Mathematics Recurrence Relations Chapter 5 R. Johnsonbaugh
Modular (Remainder) Arithmetic n = qk + r (for some k; r < k) eg 37 = (2)(17) + 3 Divisibility notation: 17 | n mod k = r 37 mod 17 = 3.
4/3/2003CSE More Math CSE Algorithms Euclidean Algorithm Divide and Conquer.
A binomial is a polynomial with two terms such as x + a. Often we need to raise a binomial to a power. In this section we'll explore a way to do just.
2/24/20161 One, two, three, we’re… Counting. 2/24/20162 Basic Counting Principles Counting problems are of the following kind: “How many different 8-letter.
Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall.
Copyright © 2011 Pearson Education, Inc. Factoring CHAPTER 6.1Greatest Common Factor and Factoring by Grouping 6.2Factoring Trinomials of the Form x 2.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Problem Solving: Brute Force Approaches
CMSC Discrete Structures
L14: Permutations, Combinations and Some Review
The Pigeonhole Principle
Polynomials & Factoring
Modeling with Recurrence Relations
COCS DISCRETE STRUCTURES
Objective The student will be able to:
Welcome to Interactive Chalkboard
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Number Theory (Chapter 7)
Data Structures and Algorithms
Chapter 5 Induction and Recursion
a*(variable)2 + b*(variable) + c
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Problem Solving: Brute Force Approaches
Copyright © Cengage Learning. All rights reserved.
Dynamic Programming.
More Mathematical Techniques
a*(variable)2 + b*(variable) + c
Mathematics Basic Math and Combinatorics
Basic Counting.
Kevin Mason Michael Suggs
The Binomial Theorem.
a*(variable)2 + b*(variable) + c
Combinatorics.
More Counting A B … f Lecture 16: Nov 9.
Witzzle Pro The Rules of the Game.
Dynamic Programming Dynamic Programming 1/18/ :45 AM
Counting Discrete Mathematics.
CMSC Discrete Structures
Permutations and Combinations
Counting II: Recurring Problems And Correspondences
Minimax strategies, alpha beta pruning
CMSC Discrete Structures
Recurrence Relations Discrete Structures.
Recursive Algorithms 1 Building a Ruler: drawRuler()
More Mathematical Techniques
Minimax strategies, alpha beta pruning
Presentation transcript:

More Mathematical Techniques

Special Number Sequences Fibonacci numbers Fib(0) = 0, Fib(1) = 1, Fib(n) = Fib(n-1)+Fib(n-2) Dynamic Programming is usually used But, there are faster methods – matrix powers Sometimes require Java Bigints to use Zeckendorf’s Theorem: Every positive integer can be written as a sum of Fibonacci numbers, no two of which are consecutive. Can determine this greedily – choosing largest possible at each step Pisano Period Last digit/Last 2 digits/Last 3 digits/Last 4 digits repeat with period 60/300/1500/15000

Special Number Sequences Binomial Coefficients Coefficients of powers of a binomial (x+y)n kth coefficient is C(n,k) – i.e. n choose k – which is n!/[(n-k)!k!] Notice C(n,k) = C(n,n-k) Can find with Pascal’s Triangle Row 1 is 1 Row 2 is 1 1 Row 3 is 1 2 1 Row 4 is 1 3 3 1 Each row is 1 longer. End values are 1, those in middle are sum of 2 above it Good if need all numbers 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 numbers.

Special Number Sequences Catalan Numbers Cat(n) = C(2n,n)/(n+1) Cat(0) = 1 Cat(n+1) = Cat(n) * [(2n+2)(2n+1)]/[(n+2)(n+1)] Useful for several problems: Number of distinct binary trees with n vertices Number of expressions containing n pairs of parentheses that are correctly matched Number of ways n+1 factors can be parenthesized fully Number of ways a convex polygon with n+2 sides can be triangulated Number of monotonic paths on an nxn grid that don’t pass diagonal

Prime Numbers To test if a number is prime Could check divisibility by all numbers < sqrt(n) Could check divisibility by all ODD numbers < sqrt(n) Could check divisibility by all PRIME numbers < sqrt(n) To generate primes, could repeatedly test, then add on to list as new ones are found Useful if you just are testing one number, and it’s large. Or, use Sieve of Erastothenes to find all primes in some range Mark all numbers as primes to start; start pointer at 1 Repeat: Pick next prime number (on first iteration, this will be 2, then 3, then 5) Mark all multiples of that number as not prime (increment by that amount, marking not prime)

GCD and LCM GCD: gcd(a,b) LCM: lcm(a,b) Use Euclid’s algorithm If b == 0, return a, otherwise: return gcd(b, a%b) Notice that gcd(a,b,c) = gcd(a,gcd(b,c)), etc. LCM: lcm(a,b) calculate lcm(a,b) = a*(b/gcd(a,b)) Notice you wan to divide by gcd first, to avoid intermediate overflow

Extended Euclid Algorithm Can tell x, y in a Diophantine equation: ax + by = c d = gcd(a,b). d must divide c First solution via extended Euclid algorithm Euclid(a,b): If b == 0, x=1, y=0, d = a extendedEuclid(b, a%b) x1 = y y1 = x – (a/b)*y x=x1 y=y1 That gives only the first (of an infinite number) valid solution, x0, y0 Others are x = x0 + (b/d) n; y =y0 + (a/d) n

Combinatorics See the discussion earlier about binomial coefficients Choosing k objects from a set of n: C(n, k) Number of ways to permute 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!] Choose k objects from n, but allow k to be chosen more than once C(n+k-1, k)

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 monotonic 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)

Finding Repeating Cycles Sometimes you have a sequence in which the sequence begins repeating. Want to find point at which the repeating sequences start: This is m Want to find the length of the sequence. This is l Can store each value computed. Then, when new value is generated, see if you already found it. If you need the actual value when first found, store in a map. Value gives m, can subtract from current value to get l If you just need to know if a repetition exists, store in a set.

Floyd’s Cycle-Finding Notice that if l is the cycle length, then for i > m, xi = xi+kl Just let kl = i. SO, xi = x2i Can iterate two pointers: a lower one that increases by 1 per step, and an upper one that increases by 2 per step When the two values match, you have i, 2i. So kl = i. To find m, start one pointer at i, and one at beginning. Then, increment them both by 1. When they match, the first is at m. Finally, you can get l by setting one pointer to m and one to m+1, then increment the bigger pointer until it matches m. That difference is l.

Game Playing Can determine who will win a (simple) game by exploring the game space. Use a minimax tree. Generate possible moves at each level Player 1 will pick the best from available choices (win if possible) Player 2 will pick the worst move from player 1’s perspective (i.e. make player 1 lose if possible) If each plays optimally, who will win? Can determine if you explore entire tree, or enough to ensure a choice is known at any level In real games (probably not contest), you can use alpha-beta pruning to speed things up.