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

Look ma' I know my algorithms!

Look ma' I know my algorithms!

RubyConf Argentina 2014

Lucia Escanellas

October 24, 2014
Tweet

Transcript

  1. Look ma’, I know
    my algorithms!

    View Slide

  2. Lucia Escanellas
    raviolicode

    View Slide

  3. Attributions
    https://flic.kr/p/6DDvQP https://flic.kr/p/qv5Zp
    https://flic.kr/p/6SaZsP https://flic.kr/p/edauSN
    https://flic.kr/p/4uNfK8 https://flic.kr/p/o9ggdk
    https://flic.kr/p/6kfuHz
    https://flic.kr/p/5kBtbS

    View Slide

  4. Speed
    Speed

    View Slide

  5. Zen Elegance
    Elegance

    View Slide

  6. Toolbox

    View Slide

  7. Theory
    Theory

    View Slide

  8. This example
    Not so common

    View Slide

  9. FROM >30HS
    TO 18 S

    View Slide

  10. WHY USE ORDERS?
    ALGORITHMS ARE POWERFUL
    AVOID TRAPS IN RUBY

    View Slide

  11. WHY USE ORDERS?
    ALGORITHMS ARE POWERFUL
    AVOID TRAPS IN RUBY

    View Slide

  12. WHY USING ORDERS?
    ALGORITHMS ARE POWERFUL
    AVOID TRAPS IN RUBY

    View Slide

  13. Let’s have a look at the
    PROBLEM

    View Slide

  14. Ordered array
    How many pairs (a,b) where
    a ≠ b
    -100 <= a + b <= 100

    View Slide

  15. Array: [-100, 1, 100]

    View Slide

  16. Array: [-100, 1, 100]
    (-100, 1), (-100, 100), (1, 100)

    View Slide

  17. Array: [-100, 1, 100]
    (-100, 1), (-100, 100), (1, 100)
    -100 + 1 = 99 YES

    View Slide

  18. Array: [-100, 1, 100]
    (-100, 1), (-100, 100), (1, 100)
    -100 + 100 = 0 YES

    View Slide

  19. Array: [-100, 1, 100]
    (-100, 1), (-100, 100), (1, 100)
    1 + 100 = 101 NO

    View Slide

  20. Array: [-100, 1, 100]
    (-100, 1), (-100, 100), (1, 100)
    Result: 2

    View Slide

  21. First solution
    Combinations of 2 elements
    Filter by: -100 <= a + b <= 100

    View Slide

  22. def count!
    combinations = @numbers.combination(2).to_a!
    !
    combinations!
    .map{ |a,b| a + b }!
    .select do |sum|!
    sum.abs <= THRESHOLD!
    end.size!
    end

    View Slide

  23. 10K takes
    10s
    BUT
    100M takes
    30hs

    View Slide

  24. Time to buy a
    NEW
    LAPTOP!

    View Slide

  25. Big O notation
    How WELL an algorithm
    SCALES
    as the
    DATA involved
    INCREASES

    View Slide

  26. Calc Array size (length=N)
    Count elements one by one: O(N)

    View Slide

  27. Calc Array size (length=N)
    Count elements one by one: O(N)
    Length stored in variable: O(1)

    View Slide

  28. Graphical Math Properties
    Order
    Mathematical
    Properties

    View Slide

  29. Remember:
    f < g => O(f + g) = O(g)
    O(K . f) = O(f)
    O(1) < O(ln N) < O(N) < O(N2) < O(eN)

    View Slide

  30. Ex: Binary Search
    Find 7 in [1, 2, 3, 4, 5, 6, 7, 8]
    1. element in the middle is 5
    2. 5 == 7 ? NO
    3. 5 < 7 ? YES => Find 7 in [6, 7, 8]
    Step 1

    View Slide

  31. !
    Find 7 in [0, 1, 2, 3, 4, 5, 6, 7, 8]
    1. element in the middle is 7
    2. 7 == 7 ? YES!
    FOUND IT!!
    Step 2

    View Slide

  32. Ex: Binary Search
    Worst case: ceil ( Log2 N ) 23 = 8
    ONLY 3 steps

    View Slide

  33. Typical examples
    Access to a Hash O(1)
    Binary search O(log N)
    Sequential search O(N)
    Traverse a matrix NxN O(N2)

    View Slide

  34. DON’T JUST BELIEVE ME
    fooplot.com

    View Slide

  35. BUT raviolicode,
    I’m getting BORED

    View Slide

  36. I WANT CONCURRENCY
    I WANT CONCURRENCY

    View Slide

  37. wait… was it
    Concurrency? or
    Parallelism?

    View Slide

  38. View Slide

  39. View Slide

  40. View Slide

  41. View Slide

  42. View Slide

  43. View Slide

  44. GIL+CPU-bound
    NO I/O OPERATIONS
    concurrency = OVERHEAD

    View Slide

  45. NOT what I
    was
    expecting

    View Slide

  46. Parallelism...
    Parallelism

    View Slide

  47. View Slide

  48. What do we
    REALLY get?
    O(N2
    / cores) = O(N
    2
    )
    jRubyGo
    Scala

    View Slide

  49. NO Spoilers
    O(N2) O(N.log(N)) O(N)

    View Slide

  50. THINKING algorithms
    is as IMPORTANT
    as ANY OTHER technique

    View Slide

  51. BYE.

    View Slide

  52. Wait! It's still slow.
    Wait! It’s still SLOW

    View Slide

  53. Given [1,2,3,4,5]
    Take 1, then print [5,4,3,2]
    Take 2, then print [5,4,3]
    and so on…

    View Slide

  54. What’s the ORDER of this code?
    @nums.each_with_index do |a,i|!
    ! puts @nums.slice(i+1,N).reverse!
    .inspect!
    end

    View Slide

  55. What’s the ORDER of this code?
    @nums.each_with_index do |a,i|!
    ! puts @nums.slice(i+1,N).reverse!
    .inspect!
    end
    Looks like O(N)

    View Slide

  56. What’s the ORDER of this code?
    @nums.each_with_index do |a,i|!
    ! puts @nums.slice(i+1,N).reverse!
    .inspect!
    end Behaves like O(N2)

    View Slide

  57. Let’s Look
    at the DOCS
    Ruby-Doc.org
    !
    #reverse

    View Slide

  58. O(N) hidden!
    O(N)!

    View Slide

  59. What’s the ORDER of this code?
    @nums.each_with_index do |a,i|!
    ! puts @nums.slice(i+1,N).reverse!
    .inspect!
    end
    O(N2)!

    View Slide

  60. Leaky abstractions
    LEAKY ABSTRACTIONS

    View Slide

  61. All Non-trivial
    abstractions
    are LEAKY to some degree

    View Slide

  62. ABSTRACTIONS
    DO NOT really
    SIMPLIFY
    as they were meant to

    View Slide

  63. Knowing
    THE
    ALGORITHMS
    Behind everyday methods
    PAYS OFF

    View Slide

  64. Thanks :)
    Thanks :)

    View Slide