280 likes | 457 Views
CS 23022 Discrete Mathematical Structures. Mehdi Ghayoumi MSB rm 132 mghayoum@kent.edu Ofc hr: Thur, 9:30-11:30a. Announcements. There is no Homework !!! Next session we have a quiz for Induction. No. { x Z : x < 0 } has no least element. Mathematical Induction - why does it work?.
E N D
CS 23022Discrete Mathematical Structures Mehdi Ghayoumi MSB rm 132 mghayoum@kent.edu Ofc hr: Thur, 9:30-11:30a
Announcements There is no Homework!!! Next session we have a quiz for Induction
No. { x Z : x < 0 } has no least element. Mathematical Induction - why does it work? Definition: A set S is “well-ordered” if every non-empty subset of S has a least element. Examples: the set of natural numbers (N) is well-ordered. Is the set of integers (Z) well ordered?
No. { x R : x > 1 } has no least element. Mathematical Induction - why does it work? Is the set of non-negative reals (R) well ordered?
Strong Mathematical Induction An example. Given n blue points and n orange points in a plane with no 3 collinear, prove there is a way to match them, blue to orange, so that none of the segments between the pairs intersect.
Strong Mathematical Induction Base case (n=1): Assume any matching problem of size less than (k+1) can be solved. Show that we can match (k+1) pairs.
Strong Mathematical Induction Show that we can match (k+1) pairs. Suppose there is a line partitioning the group into a smaller one of j blues and j oranges, and another smaller one of (k+1)-j blues and (k+1)-j oranges. OK!! (by IH) OK!! (by IH)
Strong Mathematical Induction How do we know such a line always exists? Consider the convex hull of the points: OK!! (by IH) If there is an alternating pair of colors on the hull, we’re done! OK!! (by IH)
There must be a tie along the way Strong Mathematical Induction If there is no alternating pair, all points on hull are the same color. Notice that any sweep of the hull hits an orange point first and also last. We sweep on some slope not given by a pair of points. Keep score of # of each color seen. Orange gets the early lead, and then comes from behind to tie at the end. OK!! (by IH) OK!! (by IH)
Strings and Inductive Definitions Let be a finite set called an alphabet. The set of strings on , denoted * is defined as: *, where denotes the null or empty string. If x , and w *, then wx*, where wx is the concatenation of string w with symbol x.
Countably infinite No. Strings and Inductive Definitions Example: Let = {a, b, c}. Then * = {, a, b, c, aa, ab, ac, ba, bb, bc, ca, cb, cc, aaa, aab,…} How big is *? Is there a largest string in *?
I point this out because the length of strings is something we might like to use for an inductive argument. Strings and Inductive Definitions Inductive definition of the length of strings (the length of string w is |w|.): || = 0 If x , and w *, then |wx| = |w| + 1
For example (abc)R = cba x(w)R Strings and Inductive Definitions Inductive definition of the reversal of a string (the reversal of string w is written wR.): R = If x , and w *, then (wx)R = ? For example (abc)R = c(ab)R = cb(a)R = cba()R = cba = cba
Strings and Inductive Definitions A Theorem: x,y * (xy)R = yRxR Proof (by induction on |y|): Base Case (|y| = 0): If |y| = 0, y = , then (xy)R = (x)R = xR = xR = yRxR. IH: If |y| n, then x *, (xy)R = yRxR. Prove: If |y| = n+1, then x *, (xy)R = yRxR.
Strings and Inductive Definitions IH: If |y| n, then x *, (xy)R = yRxR. Prove: If |y| = n+1, then x *, (xy)R = yRxR. If |y| = n+1, then a , u *, so that y = ua, and |u| = n. Then, (xy)R = (x(ua))R by substitution = ((xu)a)R by assoc. of concatenation = a(xu)R by inductive defn of reversal = auRxR by IH = (ua)RxR by inductive defn of reversal = yRxR by substitution
Algorithms An iterative algorithm is one that repeats the same sequence of steps a number of times. for loops while loops repeat loops goto?? The running time of an iterative algorithm depends on the number of times the loop is invoked.
But different machines run at different speeds! Complexity is O(mn) Algorithms How many times does “twiddle-thumbs” happen? for i = 1 to n for j = 1 to m twiddle-thumbs The “time complexity” of an algorithm is a measure of its running time. So we give running times in terms of big-oh, since different machines affect run times by constant factors.
But equivalently, we could define it like this: Inductive (Recursive) Definition Recursive Case Base Case Inductive Definitions We completely understand the function f(n) = n!, right? As a reminder, here’s the definition: n! = 1 · 2 · 3 · … · (n-1) · n, n 1
Recursive Case Base Cases Is there a non-recursive definition for the Fibonacci Numbers? Inductive Definitions Another VERY common example: Fibonacci Numbers
Inductive Definitions Fibonacci Numbers double fib(int n) { double prev = -1; double result = 1; double sum; inti; for(i = 0;i <= n;++ i) { sum = result + prev; prev = result; result = sum; } return result; }
Complexity is O(n) Algorithms Algorithm MAX Input: x1, x2, …, xn, an array of numbers Output: y, the maximum of x1, x2, …, xn for j = 1 to n-1 if xj > xj+1 then temp = xj+1 xj+1 = xj xj = temp
Algorithms Algorithm BUBBLE Input: x1, x2, …, xn, an array of numbers Output: ?? for j = n downto 2 MAX(x1,x2,…,xj) output ?? A reasonable output for this function would be: • x1, the minimum element of the array • xn, the max element of the array • The entire array… it has now been sorted • j the loop counter
Algorithms int main() { int array[BUBBLE], i, j,temp = 0; for (i = 0; i < BUBBLE; i ++) { cout<<“…”; for (j = 0; j < BUBBLE; j++); { temp = array[j+1]; array[j+1] = array[j]; array[j] = temp; } } return 0; }
Algorithms The running time of this algorithm is: • O(n log n) • O(n) • O(n2) • None of the above.
But computers are getting faster! Maybe we can do better. Running times It’s fun to make comparisons about the running times of algorithms of various complexities.