Download presentation
Presentation is loading. Please wait.
Published bySarah Nash Modified over 9 years ago
1
CS 23022 Discrete Mathematical Structures Mehdi Ghayoumi MSB rm 132 mghayoum@kent.edu Ofc hr: Thur, 9:30-11:30a
2
Announcements There is no Homework!!! Next session we have a quiz for Induction
3
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 Z : x < 0 } has no least element.
4
Mathematical Induction - why does it work? Is the set of non-negative reals (R) well ordered? No. { x R : x > 1 } has no least element.
5
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.
6
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.
7
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)
8
Strong Mathematical Induction How do we know such a line always exists? Consider the convex hull of the points: If there is an alternating pair of colors on the hull, we’re done! OK!! (by IH)
9
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. OK!! (by IH) Keep score of # of each color seen. Orange gets the early lead, and then comes from behind to tie at the end. There must be a tie along the way
10
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.
11
Strings and Inductive Definitions Countably infinite 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 *? No.
12
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 I point this out because the length of strings is something we might like to use for an inductive argument.
13
Strings and Inductive Definitions Inductive definition of the reversal of a string (the reversal of string w is written w R.): R = If x , and w *, then (wx) R = ? For example (abc) R = cbax(w) R For example (abc) R = c(ab) R = cb(a) R = cba( ) R = cba
14
Strings and Inductive Definitions A Theorem: x,y * (xy) R = y R x R Proof (by induction on |y|): Base Case (|y| = 0): If |y| = 0, y =, then (xy) R = (x ) R = x R = x R = y R x R. IH: If |y| n, then x *, (xy) R = y R x R. Prove: If |y| = n+1, then x *, (xy) R = y R x R.
15
Strings and Inductive Definitions IH: If |y| n, then x *, (xy) R = y R x R. Prove: If |y| = n+1, then x *, (xy) R = y R x R. 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 = au R x R by IH = (ua) R x R by inductive defn of reversal = y R x R by substitution
16
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.
17
Algorithms How many times does “ twiddle-thumbs ” happen? 1. for i = 1 to n 2. for j = 1 to m 3. twiddle-thumbs The “time complexity” of an algorithm is a measure of its running time. But different machines run at different speeds! So we give running times in terms of big-oh, since different machines affect run times by constant factors. Complexity is O(mn)
18
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 Inductive (Recursive) Definition But equivalently, we could define it like this: Recursive CaseBase Case
19
Inductive Definitions Another VERY common example: Fibonacci Numbers Recursive CaseBase Cases Is there a non-recursive definition for the Fibonacci Numbers?
20
Inductive Definitions Fibonacci Numbers double fib(int n) { double prev = -1; double result = 1; double sum; int i; for(i = 0;i <= n;++ i) { sum = result + prev; prev = result; result = sum; } return result; }
21
Algorithms Algorithm MAX Input: x 1, x 2, …, x n, an array of numbers Output: y, the maximum of x 1, x 2, …, x n 1. for j = 1 to n-1 2. if x j > x j+1 then 3. temp = x j+1 4. x j+1 = x j 5. x j = temp Complexity is O(n) varsx1x1 x2x2 x3x3 x4x4 input3241 j = 133 22 41 j = 2233 44 1 j = 32344 11 final2314
22
Algorithms Algorithm BUBBLE Input: x 1, x 2, …, x n, an array of numbers Output: ?? 1. for j = n downto 2 2. MAX(x 1,x 2,…,x j ) 3. output ?? A reasonable output for this function would be: A.x 1, the minimum element of the array B.x n, the max element of the array C.The entire array… it has now been sorted D.j the loop counter
23
Algorithms
25
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; }
26
Algorithms The running time of this algorithm is: A.O(n log n) B.O(n) C.O(n 2 ) D.None of the above.
27
Running times It’s fun to make comparisons about the running times of algorithms of various complexities. Inp size compxity 102030405060 n.00001s.00002s.00003s.00004s.00005s.00006s n2n2.0001s.0004s.0009s.0016s.0025s.0036s n5n5.1s3.2s24.3s1.7m5.2m13m 3n3n.059s58m6.5y3855c2x10 8 c 1.3x10 13 c But computers are getting faster! Maybe we can do better.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.