Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Algorithms & Complexity

Algorithms & Complexity

Frank Kair

Cheesecake Labs

April 05, 2018
Tweet

More Decks by Cheesecake Labs

Other Decks in Programming

Transcript

  1. Complexity Analysis We need a way to define the runtime

    of an algorithm regardless of the machine it’s currently running on.
  2. Big O Big O is a mathematical notation that describes

    the limiting behaviour of a function when the argument tends towards a particular value or infinity.
  3. The letter O is used because the growth rate of

    a function is also referred to as the order of the function. [...] an upper bound on the growth rate of the function. Big O
  4. Big O Length Iteration worst case 1 1 10 10

    100 100 1000 1000 10000 10000 … ...
  5. Merge Sort Recurrence relation T(n) = c if n ==

    1 = 2T(n/2) + n T(n) = 2T(n/2) + n
  6. Merge Sort Recurrence relation T(n) = c if n ==

    1 = 2T(n/2) + n T(n) = 2T(n/2) + n = 2 [ 2T(n/4) + n/2 ] + n
  7. Merge Sort Recurrence relation T(n) = c if n ==

    1 = 2T(n/2) + n T(n) = 2T(n/2) + n = 2 [ 2T(n/4) + n/2 ] + n = 4T(n/4) + 2n = 4 [ 2T(n/8) n/4 ] + n = 8T(n/8) + 3n = 16T(n/16) + 4n = ... = (2^k)T(n/(2^k)) + kn
  8. Recurrence relation T(n) = c if n == 1 =

    2T(n/2) + n T(n) = (2^k)T(n/(2^k)) + kn T(1) = c Merge Sort
  9. Recurrence relation T(n) = c if n == 1 =

    2T(n/2) + n T(n) = (2^k)T(n/(2^k)) + kn T(1) = c n/(2^k) = 1 Merge Sort
  10. Recurrence relation T(n) = c if n == 1 =

    2T(n/2) + n T(n) = (2^k)T(n/(2^k)) + kn T(1) = c n/(2^k) = 1 2^k = n Merge Sort
  11. Recurrence relation T(n) = c if n == 1 =

    2T(n/2) + n T(n) = (2^k)T(n/(2^k)) + kn T(1) = c n/(2^k) = 1 2^k = n k = lg n Merge Sort
  12. Recurrence relation T(n) = c if n == 1 =

    2T(n/2) + n T(n) = (2^k)T(n/(2^k)) + kn k = lg n T(n) = Merge Sort
  13. Recurrence relation T(n) = c if n == 1 =

    2T(n/2) + n T(n) = (2^k)T(n/(2^k)) + kn k = lg n T(n) = (2^lg n)T(1) + n lg n Merge Sort
  14. Merge Sort Recurrence relation T(n) = c if n ==

    1 = 2T(n/2) + n T(n) = (2^k)T(n/(2^k)) + kn k = lg n T(n) = (2^lg n)T(1) + n lg n = cn + n lg n
  15. Merge Sort Recurrence relation T(n) = c if n ==

    1 = 2T(n/2) + n T(n) = (2^k)T(n/(2^k)) + kn k = lg n T(n) = (2^lg n)T(1) + n lg n = cn + n lg n O(n log n)