Slide 1

Slide 1 text

Look ma’, I know my algorithms!

Slide 2

Slide 2 text

Lucia Escanellas raviolicode

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

Speed Speed

Slide 5

Slide 5 text

Zen Elegance Elegance

Slide 6

Slide 6 text

Toolbox

Slide 7

Slide 7 text

Theory Theory

Slide 8

Slide 8 text

This example Not so common

Slide 9

Slide 9 text

FROM >30HS TO 18 S

Slide 10

Slide 10 text

WHY USE ORDERS? ALGORITHMS ARE POWERFUL AVOID TRAPS IN RUBY

Slide 11

Slide 11 text

WHY USE ORDERS? ALGORITHMS ARE POWERFUL AVOID TRAPS IN RUBY

Slide 12

Slide 12 text

WHY USING ORDERS? ALGORITHMS ARE POWERFUL AVOID TRAPS IN RUBY

Slide 13

Slide 13 text

Let’s have a look at the PROBLEM

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

Array: [-100, 1, 100]

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

10K takes 10s BUT 100M takes 30hs

Slide 24

Slide 24 text

Time to buy a NEW LAPTOP!

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

Graphical Math Properties Order Mathematical Properties

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

! 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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

DON’T JUST BELIEVE ME fooplot.com

Slide 35

Slide 35 text

BUT raviolicode, I’m getting BORED

Slide 36

Slide 36 text

I WANT CONCURRENCY I WANT CONCURRENCY

Slide 37

Slide 37 text

wait… was it Concurrency? or Parallelism?

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

NOT what I was expecting

Slide 46

Slide 46 text

Parallelism... Parallelism

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

THINKING algorithms is as IMPORTANT as ANY OTHER technique

Slide 51

Slide 51 text

BYE.

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

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)

Slide 56

Slide 56 text

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)

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

O(N) hidden! O(N)!

Slide 59

Slide 59 text

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)!

Slide 60

Slide 60 text

Leaky abstractions LEAKY ABSTRACTIONS

Slide 61

Slide 61 text

All Non-trivial abstractions are LEAKY to some degree

Slide 62

Slide 62 text

ABSTRACTIONS DO NOT really SIMPLIFY as they were meant to

Slide 63

Slide 63 text

Knowing THE ALGORITHMS Behind everyday methods PAYS OFF

Slide 64

Slide 64 text

Thanks :) Thanks :)