1 / 19

CS 173: Discrete Mathematical Structures

CS 173: Discrete Mathematical Structures. Cinda Heeren heeren@cs.uiuc.edu Siebel Center, rm 2213 Office Hours: W 12:30-2:30. CS 173 Announcements. Section format has changed. Go. Exams returned in section this week. avg: 64.7 median: 65 std dev: 19.17 max: 100 (x5)

Download Presentation

CS 173: Discrete Mathematical Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 173:Discrete Mathematical Structures Cinda Heeren heeren@cs.uiuc.edu Siebel Center, rm 2213 Office Hours: W 12:30-2:30

  2. CS 173 Announcements • Section format has changed. Go. • Exams returned in section this week. • avg: 64.7 • median: 65 • std dev: 19.17 • max: 100 (x5) • Homework #6 due 10/16, 8a. Cs173 - Spring 2004

  3. 8 12 4 Both are O(n) CS173Algorithm Complexity How long does the “linear search algorithm” take? Suppose the data is (2, 8, 3, 9, 12, 1, 6, 4, 10, 7) and we’re looking for # The running time of the algorithm depends on the particular input to the problem. In this case we have two different complexity measures: • Worst case complexity - running time on worst input • Average case - average running time among all inputs Worst case for linear search is time n. Average case for linear search is time (1+2+…+n)/n = n(n+1)/2n = (n+1)/2. Cs173 - Spring 2004

  4. The main point of showing you this code is to remind you that the range is cut in half at every iteration. CS173Algorithm Complexity How long does the “binary search algorithm” take? Binary search Input: a sorted array of numbers a1, a2, … an and a number x Output: position i where ai = x, if x is in the list • i = 1, j = n; • while i < j • m = (i + j)/2; /* midpt of range (i,j)*/ • if x > am then i = m + 1 • else j = m • if x = ai then output “x is in position ai” • else output “x is not in list” Cs173 - Spring 2004

  5. If n is a power of 2, n = 2k, then k = lg n iterations occur. If n is not a power of 2, let k be the number so that 2k < n < 2k+1, and imagine that the array has 2k+1 elements. Then k+1 < lg n + 1 = O(log n) CS173Algorithm Complexity How long does the “binary search algorithm” take? Binary search Input: a sorted array of numbers a1, a2, … an and a number x Output: position i where ai = x, if x is in the list • i = 1, j = n; • while i < j • m = (i + j)/2; /* midpt of range (i,j)*/ • if x > am then i = m + 1 • else j = m • if x = ai then output “x is in position ai” • else output “x is not in list” Cs173 - Spring 2004

  6. But computers are getting faster! Maybe we can do better. CS173Running times It’s fun to make comparisons about the running times of algorithms of various complexities. Cs173 - Spring 2004

  7. CS173Running times Compare the sizes of problems solvable in 1 hour now, vs the size of problem we could solve if we had a 100 times faster machine. Cs173 - Spring 2004

  8. CS173A dangling complexity question. Why would we ever use “worst case complexity”? Advantages: • Easiest to analyze. • Bounds the worst possible thing that could happen (conservative). • Don’t have to decide or assume what typical inputs are (or find a distribution on inputs). Disadvantage: • Bizarre inputs could make worst-case running time seem horrible, when most inputs terminate in reasonable time. Cs173 - Spring 2004

  9. CS173On to recursive algorithms… Factorial (n) Input: an integer n > 0. Output: n! • If n = 1 then output 1 • else • output n x Factorial(n-1) Let T(n) denote the running time of the algorithm on input of size n. T(n) = C + T(n-1) T(1) = c T(n) = C + (C + T(n-2)) = C + (C + (C + T(n-3))) … = nC = O(n) Cs173 - Spring 2004

  10. CS173On to recursive algorithms… I need a volunteer. TH (A, B, C,n) • If n = 1 then Move(A,B) • else • TH(A,C,B,n-1) • Move(A,B) • TH(C,B,A,n-1) Cs173 - Spring 2004

  11. What is k when this bottoms out at 1? More general techniques investigated in cs273 Unroll a bit… CS173On to recursive algorithms… Now let’s analyze the running time. T(1) = C T(n) = 2 T(n-1) + C = 2 (2 T(n-2) + C) + C = 4 T(n-2) + 3C = 4 (2 T(n-3) + C) + 3C = 8 T(n-3) + 7C … = 2k T(n-k) + (2k-1)C = 2n-1 T(1) + (2n-1-1)C = (2n-1) C = O(2n) Cs173 - Spring 2004

  12. This doesn’t feel satisfying because we think about recursion inductively to begin with (as we should!). Yes CS173On to recursive algorithms… Is the algorithm correct? Does it do the right thing for 1 disk? Assume it does the right thing for n-1 disks. (IH) And finally, it DOES do the right thing for n disks: move n-1 out of the way, move the biggest disk, move n-1 back. Cs173 - Spring 2004

  13. CS173On to recursive algorithms… I need 8 volunteers. MergeSort (list) • If |list| = 1 then return list • else • MergeSort(left half of list) • MergeSort(right half of list) • Merge(left half of list,right half of list) Cs173 - Spring 2004

  14. CS173On to recursive algorithms… Write down the recurrence relation: T(n) = 2 T(n/2) + cn We’ll talk about techniques for solving it later. Cs173 - Spring 2004

  15. CS173On to recursive algorithms… An appropriate recurrence relation for binary search is: • T(n) = C + T(n-1) • T(n) = C + T(n/2) • T(n) = n + T(n/2) • Depends on the data. Cs173 - Spring 2004

  16. Product Rule CS173Counting Suppose you have 4 shirts, 3 pairs of pants, and 2 pairs of shoes. How many different outfits do you have? Cs173 - Spring 2004

  17. B 64 4 4 4 CS173Product Rule How many functions are there from set A to set B? A To define each function we have to make 3 choices, one for each element of A. How many ways can each choice be made? Cs173 - Spring 2004

  18. B 24 2 4 3 CS173Product Rule How many one-to-one functions are there from set A to set B? A To define each function we have to make 3 choices, one for each element of A. How many ways can each choice be made? Cs173 - Spring 2004

  19. Sum Rule Be careful not to overcount. CS173Counting A locker code is a sequence of 3 letters where the first letter is a consonant, the second letter is a vowel, and the third letter is Z, OR the first letter is a vowel, and the second and third letter together are a state abbreviation. Are there enough locker codes for everyone in this room? Cs173 - Spring 2004

More Related