Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Data Structures and Algorithms Discrete Math Review Chris Brooks Department of Computer Science University of San Francisco Department of Computer Science — University of San Francisco – p.1/32 2-0: Discrete Math Review Sets Logarithms Summations Recursion Proof Techniques Department of Computer Science — University of San Francisco – p.2/32 2-1: Sets A set is a collection of distinguishable objects (usually called members or elements) {1,2,3,4} {red, green, blue} {1,2,3,4,5,6,...} is in set indicates that object denotes the cardinality (size) of set is the set with no elements (the “empty set”) . Department of Computer Science — University of San Francisco – p.3/32 : All the objects in A or B All the objects in A and B All the objects that are in A, but not : A is a subset of B (or B is a superset of A) Set Difference: in B. Intersection: Set Operations Union: 2-2: Department of Computer Science — University of San Francisco – p.4/32 2-3: Logarithms (written as In many places, logs use base 10 or base is equivalent to: Think of it as ’the power you’d need to raise b to to yield y.’ Logarithms (or ’logs’) are most easily thought of as the inverse of exponentiation. ) In computer science, we typically use base 2 This is a result of using binary arithmetic. Department of Computer Science — University of San Francisco – p.5/32 2-4: Logarithm examples How many bits does it take to encode 1,000,000 different values? Department of Computer Science — University of San Francisco – p.6/32 Logarithm examples 2-5: How many bits does it take to encode 1,000,000 different values? Each bit can take on one of two values (1 or 0) Therefore, bits can represent values. So, encoding 1,000,000 values will require = 20 bits Department of Computer Science — University of San Francisco – p.7/32 2-6: Logarithm examples If we have an alphabetically sorted list of 128 names, how many records do we need to look at to find a given individual? Department of Computer Science — University of San Francisco – p.8/32 Logarithm examples 2-7: If we have an alphabetically sorted list of 128 names, how many records do we need to look at to find a given individual? Since the list is sorted, we can use binary search. Look at the middle element: if it’s after than the name we’re looking for, search the first half of the list. If it’s before the name we’re looking for, look at the second half of the list. Each check cuts the size of the list in half; how many times can we do this? If we think backwards, in terms of doubling the list, we’ll need doublings to generate a list of length . If , then Department of Computer Science — University of San Francisco – p.9/32 Logarithmic Operators 2-8: This is used to change bases. Department of Computer Science — University of San Francisco – p.10/32 Logarithm examples Evaluate 2-9: Department of Computer Science — University of San Francisco – p.11/32 Logarithm examples , since By rule 1: Evaluate 2-10: Department of Computer Science — University of San Francisco – p.12/32 Logarithm examples Evaluate 2-11: But many calculators don’t do base 2! Department of Computer Science — University of San Francisco – p.13/32 Logarithm examples 2-12: Department of Computer Science — University of San Francisco – p.14/32 2-13: Logarithm examples Logarithms can also be very useful for comparing very large or small numbers. For example: is this true? Neither number can be calculated directly without risking overflow. Department of Computer Science — University of San Francisco – p.15/32 2-14: Logarithm examples Since the logarithm function is monotonically increasing, if , then Department of Computer Science — University of San Francisco – p.16/32 Summations 2-15: Often, in analyzing a program’s performance, we’ll need to add up the number of times an operation is taken. This is known as a summation. Summations are typically written as: Which is equivalent to (1) Department of Computer Science — University of San Francisco – p.17/32 2-16: Summations Typically, you’ll want to replace a summation with a single expression, known as a closed-form solution. Some common summations and their closed-form solutions: (3) (4) (2) Department of Computer Science — University of San Francisco – p.18/32 2-17: Summations (5) Some common summations and their closed-form solutions: (7) (6) There are more on page 28 of your text. Department of Computer Science — University of San Francisco – p.19/32 2-18: Recursion Recursion is a fundamental problem-solving technique Involves decomposing a problem into: A base case that can be solved directly A recursive step that indicates how to handle more complex cases. A common recursive example is factorial: long factorial(int input) if (input == 1) return 1; else return input * factorial(input - 1); Department of Computer Science — University of San Francisco – p.20/32 2-19: Recursion A more interesting example is the Towers of Hanoi. It’s hard to write an iterative program to solve this, but the recursive version is startlingly simple: void towers(int ndisks, Tower startTower, Tower goalTower, Tower tempT if (ndisks == 0) return; else towers(ndisks - 1, startTower, tempTower, goalTower); moveDisk(startTower, goalTower); towers(ndisks - 1, tempTower, goalTower, startTower); Department of Computer Science — University of San Francisco – p.21/32 2-20: Mathematical Proof Often, we’ll find ourself needing to mathematically prove something, such as that a particular algorithm is optimal, that a certain set of outcomes is impossible, or that a solution is guaranteed to be correct. There are lots of invalid proof techniques: Proof by assertion Proof by example (this can be valid for existence proofs) Proof by appeal to authority Proof by obfuscation etc ... Department of Computer Science — University of San Francisco – p.22/32 2-21: Mathematical Proof The three most common valid forms of proof are: Direct proof (sometimes called a constructive proof) Indirect proof, or proof by contradiction Inductive Proof Department of Computer Science — University of San Francisco – p.23/32 2-22: Direct Proof With direct proof, you set out to directly show how the hypothesis holds. Direct proofs are typically very short and direct. Example: Hypothesis: Every odd integer is the difference of two perfect squares. (A perfect square is a number , where is an integer, such as 1,4,9,16,25, ...) Department of Computer Science — University of San Francisco – p.24/32 2-23: Direct Proof is an , where We can write an odd number as integer. Hypothesis: Every odd integer is the difference of two perfect squares. . These are both perfect squares. Direct proof is great, when you can do it. Department of Computer Science — University of San Francisco – p.25/32 2-24: Proof by Contradiction Proof by contradiction works as follows: To show that is true, start by assuming is false. Use this to derive a logical contradiction. Since being false results in a contradiction, must be true. Note that this assumes all hypotheses are either true or false. Department of Computer Science — University of San Francisco – p.26/32 Proof by Contradiction Example 2-25: Hypothesis: There are infinitely many prime numbers. Proof: Assume not. Let be the largest prime, and be the product of all primes from 2, ..., . Let = . For any prime , will have a remainder of 1. (by construction) Since no prime number divides , no number less (except 1) divides than Therefore is prime. This contradicts our assumption that is the largest prime; therefore our assumption is false, and our hypothesis proved. Department of Computer Science — University of San Francisco – p.27/32 2-26: Inductive Proof Inductive proofs are very similar to recursion. You establish a base case that is proved directly This is followed by an inductive step, which shows how the hypothesis holds for larger cases. Department of Computer Science — University of San Francisco – p.28/32 2-27: Inductive Proof Typically, we’ll want to prove that something holds over a range of values. The base case will prove the theorem for the initial values. The indictive step will show that, if the theorem holds for , then it holds for . This provides the machinery needed to prove the theorem for any particular value of . Department of Computer Science — University of San Francisco – p.29/32 Inductive Proof Example . Previously, we saw that 2-28: . Base case: Let Let’s prove this. (8) Inductive step. First, we state the inductive hypothesis that: Department of Computer Science — University of San Francisco – p.30/32 2-29: Inductive Proof Example Assuming this is true, adding the th element yields: (9) Therefore, we have proved our theorem. Department of Computer Science — University of San Francisco – p.31/32 2-30: Summary Hopefully this material is looking familiar to you. If not: Read Chapter 2 closely Rediscover your Discrete Math text Come talk to me or the TA in office hours. You also have a homework to help ’jog your memory.’ Department of Computer Science — University of San Francisco – p.32/32