chapter 5
play

Chapter 5: Algorithms and Heuristics CS105: Great Insights in - PowerPoint PPT Presentation

Chapter 5: Algorithms and Heuristics CS105: Great Insights in Computer Science Fact Cheat Sheet! Fact 1: Average number of trials until an event happens if that event has probability p is 1/p Example: If the probability of winning the


  1. Chapter 5: Algorithms and Heuristics CS105: Great Insights in Computer Science

  2. Fact Cheat Sheet! • Fact 1: Average number of trials until an event happens if that event has probability p is 1/p • Example: If the probability of winning the lottery is 1/1000, then it will take, on average 1000 plays to win • Fact 2: The sum of all of the numbers from 1 to n is simply n(n+1)/2, which is equal to (n 2 +n)/2 • The sum of the numbers from 1 to 100 is 100*101/2 = 5050

  3. Fact Cheat Sheet cont... • Fact 5: A logarithm of base 10, lg , is approximately the number of times you can divide the number by 10. A logarithm of base two is approximately the number of time you can divide by 2. • Example: log 10 (1000) = 3 and log 2 (1024) = 10 • Fact 4: The number of syllables for any number, n , is approximately lg(n) if the number is read one digit at a time • Example: The number of syllables in 1000 (read as one-zero-zero-zero) is lg(1000) = 3

  4. Fact Cheat Sheet cont... • Fact 5: Average number of peeks into a list to find a number, if the list is in random order, is n/2 • Example: If I have a list of 20 numbers in random order and I would like to find if 5 is on the list. The average number of times I need to look at elements of the list is 20/2 = 10.

  5. Song Growth • Let’s talk about how many syllables we sing given a song of a certain type as the number of verses grows. • In general, we’re interested in the number of syllables as a function of n , the number of verses.

  6. Clementine In a cavern, in a canyon, Drove she ducklings to the water How I missed her! How I missed her, Verse 1 Verse 3 Verse 5 Excavating for a mine, Ev'ry morning just at nine, How I missed my Clementine, Dwelt a miner forty niner, Hit her foot against a splinter, But I kissed her little sister, And his daughter Clementine. Fell into the foaming brine. I forgot my Clementine. Oh my darling, oh my darling, Oh my darling, oh my darling, Oh my darling, oh my darling, Oh my darling, Clementine! Oh my darling, Clementine! Oh my darling, Clementine! Thou art lost and gone forever Thou art lost and gone forever Thou art lost and gone forever Dreadful sorry, Clementine. Dreadful sorry, Clementine. Dreadful sorry, Clementine. Light she was and like a fairy, Ruby lips above the water, Verse 2 Verse 4 And her shoes were number nine, Blowing bubbles, soft and fine, Herring boxes, without topses, But, alas, I was no swimmer, Sandals were for Clementine. So I lost my Clementine. Oh my darling, oh my darling, Oh my darling, oh my darling, Oh my darling, Clementine! Oh my darling, Clementine! Thou art lost and gone forever Thou art lost and gone forever Dreadful sorry, Clementine Dreadful sorry, Clementine.

  7. Counting Syllables verses syllables verses syllables 1 55 5 271 2 109 6 323 3 161 7 375 4 219 8 432

  8. Plotting Syllables 500 375 syllables 250 125 0 1 2 3 4 5 6 7 8 verses: n • Total syllables roughly, T( n ) = 54 n .

  9. Old MacDonald Verse 1 Old Macdonald had a farm, E-I-E-I-O Old Macdonald had a farm, E-I-E-I-O And on his farm he had a cow, E-I-E-I-O And on his farm he had a chick, E-I-E-I-O With a "moo-moo" here and a "moo-moo" there With a "cluck, cluck" here and a "cluck, cluck" there Here a "moo" there a "moo" Here a "cluck" there a "cluck" Everywhere a "moo-moo" Verse 4 Everywhere a "cluck-cluck" Old Macdonald had a farm, E-I-E-I-O With a "neigh, neigh" here and a "neigh, neigh" there Here a "neigh" there a "neigh" Old Macdonald had a farm, E-I-E-I-O Everywhere a "neigh-neigh" And on his farm he had a pig, E-I-E-I-O With a (snort) here and a (snort) there Verse 2 With a (snort) here and a (snort) there Here a (snort) there a (snort) Here a (snort) there a (snort) Everywhere a (snort-snort) Everywhere a (snort-snort) With a "moo-moo" here and a "moo-moo" there With a "moo-moo" here and a "moo-moo" there Here a "moo" there a "moo" Here a "moo" there a "moo" Everywhere a "moo-moo" Everywhere a "moo-moo" Old Macdonald had a farm, E-I-E-I-O Old Macdonald had a farm, E-I-E-I-O Old Macdonald had a farm, E-I-E-I-O And on his farm he had a horse, E-I-E-I-O With a "neigh, neigh" here and a "neigh, neigh" there Verse 3 Here a "neigh" there a "neigh" Everywhere a "neigh-neigh" With a (snort) here and a (snort) there Here a (snort) there a (snort) Everywhere a (snort-snort) With a "moo-moo" here and a "moo-moo" there Here a "moo" there a "moo" Everywhere a "moo-moo" Old Macdonald had a farm, E-I-E-I-O

  10. Counting Syllables verses syllables verses syllables 1 59 5 515 2 140 6 684 3 243 7 875 4 368 8 1088

  11. Plotting Syllables 1500 1125 syllables 750 375 0 1 2 3 4 5 6 7 8 verses: n

  12. Plotting More Syllables 5000 3750 syllables 2500 1250 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 verses: n

  13. N Bottles of Beer Verse 1 Verse 2643 One Bottles of Beer on the wall, ... One bottles of beer, If one of those bottles should happen to fall, Two Six Four Three Bottles of Beer on the wall, Two bottles of beer on the wall, Two Six Four Three bottles of beer, If one of those bottles should happen to fall, Two Six Four Three bottles of beer on the wall, Verse 2 Two Bottles of Beer on the wall, Two bottles of beer, ... If one of those bottles should happen to fall, Verse 10741 Three bottles of beer on the wall, One Zero Seven Four One Bottles of Beer on the wall, One Zero Seven Four One bottles of beer, Verse 3 Three Bottles of Beer on the wall, If one of those bottles should happen to fall Three bottles of beer, One Zero Seven Four Two bottles of beer on If one of those bottles should happen to fall, the wall Four bottles of beer on the wall, Verse 4 Four Bottles of Beer on the wall, Four bottles of beer, If one of those bottles should happen to fall, Five bottles of beer on the wall,

  14. Counting Syllables verses syllables verses syllables 1 30 10 328 2 60 20 628 3 90 100 3560 4 120 1000 38400

  15. Plotting Syllables 400 300 syllables 200 100 0 1 2 3 4 5 6 7 8 9 10 verses: n • Total syllables?

  16. Plotting More Syllables 700 525 syllables 350 175 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 verses: n

  17. Different Growth Rates • With these constants, 0.572 n 2 lg n n 2 lg n has fastest growth, then 0.4 n 2 (Quadratic) quadratic, then n lg n , then linear. 2.86 n lg n • For big n , always the 2 n (Linear) same order regardless of the constants! • Leads to the notion of “Big O”.

  18. Big O • Formally, big O is a • In practice, however, it notation that denotes a gives us a way of class of functions all of ignoring constants and which are upper low-order terms to bounded cluster together asymptotically. functions that behave similarly. O( n ) 17 n + 91.2 lg( n )+ n 1/2

  19. Big O • Formally, big O is a • In practice, however, it notation that denotes a gives us a way of class of functions all of ignoring constants and which are upper low-order terms to bounded cluster together asymptotically. functions that behave similarly. O( n ) 17 n + 91.2 lg( n )+ n 1/2

  20. Big O • Formally, big O is a • In practice, however, it notation that denotes a gives us a way of class of functions all of ignoring constants and which are upper low-order terms to bounded cluster together asymptotically. functions that behave similarly. O( n ) 17 n + 91.2 lg( n )+ n 1/2

  21. Common Growth Classes • Linear: O( n ) - O( n log n ) - Dreidel - N Bottles of Beer - Clementine - N Little Monkeys • Quadratic: O( n 2 ) - O( n 2 log n ) - An Old Lady - N Days of Christmas - Old Macdonald - Who Knows N ? - There Was a Tree

  22. Common Growth Classes • Linear: O( n ) - O( n log n ) - Dreidel - N Bottles of Beer constant size verse - Clementine - N Little Monkeys • Quadratic: O( n 2 ) - O( n 2 log n ) - An Old Lady - N Days of Christmas - Old Macdonald - Who Knows N ? - There Was a Tree

  23. Common Growth Classes • Linear: O( n ) - O( n log n ) each verse - Dreidel - N Bottles of Beer contains constant the next size verse higher - Clementine - N Little Monkeys number • Quadratic: O( n 2 ) - O( n 2 log n ) - An Old Lady - N Days of Christmas - Old Macdonald - Who Knows N ? - There Was a Tree

  24. Common Growth Classes • Linear: O( n ) - O( n log n ) each verse - Dreidel - N Bottles of Beer contains constant the next size verse higher - Clementine - N Little Monkeys number • Quadratic: O( n 2 ) - O( n 2 log n ) - An Old Lady - N Days of each verse a constant Christmas size larger - Old Macdonald than the previous - Who Knows N ? - There Was a Tree

  25. Common Growth Classes • Linear: O( n ) - O( n log n ) each verse - Dreidel - N Bottles of Beer contains constant the next size verse higher - Clementine - N Little Monkeys number • Quadratic: O( n 2 ) - O( n 2 log n ) - An Old Lady - N Days of each verse each verse lists one a constant more Christmas size larger number - Old Macdonald than the than the previous previous - Who Knows N ? - There Was a Tree

Recommend


More recommend