Slide 1

Slide 1 text

Beyond the language An introduc2on to algorithms Simone Carle7 / / @weppos RubyDay Italy 2015

Slide 2

Slide 2 text

Problem • There is a coffee table with a lot of bignè • You want to eat some bignè • One bignè is poisoned and it weighs a liKle bit more than the others • The only way for you to find the poisoned one is to weigh the bignè using a scale (or with your hands) • Each 2me you weigh the bignè you waste precious 2me

Slide 3

Slide 3 text

Goal • You want a way
 to eat as much bignè as you can, ideally without ea2ng the poisoned one and before all the good bignè are gone

Slide 4

Slide 4 text

Goal • You want an algorithm
 to eat as much bignè as you can, ideally without ea2ng the poisoned one and before all the good bignè are gone

Slide 5

Slide 5 text

Simone Carle7 @weppos

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

algorithm |ˈalgərɪð(ə)m| a process or set of rules to be followed in calculations or other problem-solving operations, especially by a computer.

Slide 9

Slide 9 text

algorithm |ˈalgərɪð(ə)m| a word used by programmers when they don't want to explain what they did. https://twitter.com/kellabyte/status/557736373319643137

Slide 10

Slide 10 text

An Algorithm is not a Program A program can be viewed as an elaborate algorithm.

Slide 11

Slide 11 text

An Algorithm is not a Program Computer programs contain algorithms that detail the specific instruc2ons a computer should perform, in a specific order, to execute a specific task.

Slide 12

Slide 12 text

As a programmer, you use a programming language to implement algorithms to write programs.

Slide 13

Slide 13 text

http://programmers.stackexchange.com/q/18406/3820

Slide 14

Slide 14 text

Back to the problem…

Slide 15

Slide 15 text

Problem • There is a coffee table with a lot of bignè • You want to eat some bignè • One bignè is poisoned and it weighs a liKle bit more than the others • The only way for you to find the poisoned one is to weigh the bignè using a scale (or with your hands) • Each 2me you weigh the bignè you waste precious 2me • You want to eat as much bignè as you can, ideally without ea2ng the poisoned one and before all the good bignè are gone

Slide 16

Slide 16 text

Problem Instance Computa;on
 model Find the heaviest item among n items. n items including the item we want to find. Scale. Each weight taken has a fixed cost. Algorithm Weighing strategy. Goal We want an algorithm that finds the heaviest item in the collec2on. The algorithm should be correct and efficient.

Slide 17

Slide 17 text

The simplest algorithm Take the first item and compare it to all the other items in the collec2on.

Slide 18

Slide 18 text

The simplest algorithm

Slide 19

Slide 19 text

The simplest algorithm

Slide 20

Slide 20 text

The simplest algorithm 1

Slide 21

Slide 21 text

The simplest algorithm 2

Slide 22

Slide 22 text

The simplest algorithm 3

Slide 23

Slide 23 text

The simplest algorithm 4

Slide 24

Slide 24 text

The simplest algorithm 5

Slide 25

Slide 25 text

The simplest algorithm 6

Slide 26

Slide 26 text

The simplest algorithm Correct: YES Efficient:

Slide 27

Slide 27 text

Cost of an algorithm

Slide 28

Slide 28 text

For simplicity, let's define the cost of an algorithm as an es;ma;on of the resources needed by an algorithm to solve a computa;on problem.

Slide 29

Slide 29 text

Resources can be CPU-;me, storage, …

Slide 30

Slide 30 text

The lower the cost, The higher the efficiency of the algorithm.

Slide 31

Slide 31 text

In computer science, the Big-O nota;on is used to classify the cost of an algorithm rela;ve to changes in input size.

Slide 32

Slide 32 text

Big-O nota;on is a way to measure the complexity of the algorithm in the worst-case scenario.

Slide 33

Slide 33 text

Complexity of an algorithm We need some basic Math to understand the

Slide 34

Slide 34 text

Big-O n = 2 n = 10 n = 20 constant O(1) 1 1 1 linear O(n) 2 10 20 logarithmic O(log2n) 1 ~ 3,3 ~ 4,3 quadra;c O(n2) 4 100 400 exponen;al O(2n) 4 1.024 1.048.576

Slide 35

Slide 35 text

http://bigocheatsheet.com/

Slide 36

Slide 36 text

Let's analyze the 1st algorithm

Slide 37

Slide 37 text

1st algorithm: "one vs all" Take the first item and compare it to all the other items in the collec2on.

Slide 38

Slide 38 text

1st algoritm Correct: YES Cost: n
 O(n) Efficient: It depends…

Slide 39

Slide 39 text

2nd algorithm: "each pair" Compare a pair of items at 2me.

Slide 40

Slide 40 text

2nd algorithm

Slide 41

Slide 41 text

2nd algorithm 1

Slide 42

Slide 42 text

2nd algorithm 2

Slide 43

Slide 43 text

2nd algorithm 3

Slide 44

Slide 44 text

2nd algorithm 4

Slide 45

Slide 45 text

2nd algoritm Correct: YES Cost: n/2 O(n) Efficient: It depends…

Slide 46

Slide 46 text

3rd algorithm: "divide et pesa" Split the collec2on of items in two sets with the same number of items, and weigh them. Keep the heaviest set and discard eat the other. (*) Split the new collec2on of items in two sets and iterate. (*) If the # of items is odd, take out the last element. If the two sets have the same weight, the last element is the result. Otherwise discard the element along with the non-relevant set.

Slide 47

Slide 47 text

3rd algorithm

Slide 48

Slide 48 text

3rd algorithm 1

Slide 49

Slide 49 text

3rd algorithm 2

Slide 50

Slide 50 text

3rd algorithm 3

Slide 51

Slide 51 text

3rd algoritm Correct: YES Cost: log2 n
 O(log n) Efficient: It depends…

Slide 52

Slide 52 text

4th algorithm: "divide /3 et pesa" Split the collec2on of items in 3 sets, and weigh the ones with the same number of elements. If the two sets have the same weight, discard both and con2nue recursively on the third. Otherwise, keep the heaviest and con2nue recursively.

Slide 53

Slide 53 text

4th algorithm

Slide 54

Slide 54 text

4th algorithm 1

Slide 55

Slide 55 text

4th algorithm 2

Slide 56

Slide 56 text

4th algoritm Correct: YES Cost: log3 n
 O(log n) Efficient:

Slide 57

Slide 57 text

n 10 100 1.000 10.000 1st 9m 1h 39m 16h 6d 2nd 5m 50m 8h 3,5d 3rd 3m 6m 9m 13m 4th 3m 5m 7m 9m

Slide 58

Slide 58 text

Language vs Algorithm Shouldn't I simply use a faster language?

Slide 59

Slide 59 text

Data Structures Ruby Hash Table

Slide 60

Slide 60 text

rubyists = {} # insert rubyists[:weppos] = "Simone Carletti" rubyists[:jodosha] = "Luca Guidi" rubyists[:john] = "John Doe" # delete rubyists.delete(:john) # search rubyists[:weppos] # => "Simone Carletti"

Slide 61

Slide 61 text

all opera;ons should cost O(1) In an ideal world

Slide 62

Slide 62 text

key1: "value1" key2: "value2" key3: "value3" Linked list

Slide 63

Slide 63 text

key1: "value1" key2: "value2" key3: "value3" The search costs O(n)! Linked list

Slide 64

Slide 64 text

search (ruby 2.2) 10.000 itera;ons First Key Last Key 4 0,00305 0,00282 8 0,00417 0,00318 16 0,00391 0,00359 32 0,00476 0,00399 64 0,00335 0,00275 128 0,00447 0,00366 256 0,00435 0,00342 512 0,00325 0,00439 1024 0,00317 0,00300 2048 0,00413 0,00334 4096 0,00490 0,00370 8192 0,00342 0,00452 16384 0,00522 0,00302 32768 0,00367 0,00408 0,01 0,1 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768

Slide 65

Slide 65 text

Hash Table in Ruby 16 #define ST_DEFAULT_MAX_DENSITY 5 #define ST_DEFAULT_INIT_TABLE_SIZE 16 #define ST_DEFAULT_PACKED_TABLE_SIZE 18 num_bins

Slide 66

Slide 66 text

Hash Table in Ruby 16 whatever[:key1] = "value"

Slide 67

Slide 67 text

Hash Table in Ruby 16 whatever[:key1] = "value" :key1.hash # => 707933195402600884 707933195402600884 % 16 # => 4

Slide 68

Slide 68 text

Hash Table in Ruby 16 whatever[:key1] = "value" key1

Slide 69

Slide 69 text

Hash Table in Ruby 16 whatever[:key1] = "value" whatever[:key2] = "value" key1 key2

Slide 70

Slide 70 text

Hash Table in Ruby 16 whatever[:key1] = "value" whatever[:key2] = "value" whatever[:key3] = "value" key3 key2 key1

Slide 71

Slide 71 text

Hash Table in Ruby 16 whatever[:key1] = "value" whatever[:key2] = "value" whatever[:key3] = "value" whatever[:key4] = "value" whatever[:key5] = "value" whatever[:key6] = "value" whatever[:key7] = "value" whatever[:key8] = "value" whatever[:key9] = "value" key9 key2 key3 key1 key5 key4 key8 key6 key7

Slide 72

Slide 72 text

Hash Table in Ruby 16 whatever[:key1] = "value" whatever[:key2] = "value" whatever[:key3] = "value" whatever[:key4] = "value" whatever[:key5] = "value" whatever[:key6] = "value" whatever[:key7] = "value" ... whatever[:keyN] = "value" #define ST_DEFAULT_MAX_DENSITY 5 #define ST_DEFAULT_INIT_TABLE_SIZE 16 #define ST_DEFAULT_PACKED_TABLE_SIZE 18

Slide 73

Slide 73 text

Hash Table in Ruby 17

Slide 74

Slide 74 text

Hash Table in Ruby 17 static void rehash(register st_table *table) { ... }

Slide 75

Slide 75 text

Hash Table in Ruby 17

Slide 76

Slide 76 text

Hash Table in Ruby 17 keyN # search whatever[:keyN]

Slide 77

Slide 77 text

Hash Table in Ruby 17 keyN # search whatever[:keyN] :keyN.hash # => 2149668665177381990 2149668665177381990 % 17 # => 5

Slide 78

Slide 78 text

Hash Table in Ruby 17 keyN # search whatever[:keyN] :keyN.hash # => 2149668665177381990 2149668665177381990 % 17 # => 5 5

Slide 79

Slide 79 text

Hash Table in Ruby 17 # search whatever[:keyN] :keyN.hash # => 2149668665177381990 2149668665177381990 % 17 # => 5 5 keyN

Slide 80

Slide 80 text

Object#hash class BadKey def hash 3 end end class GoodKey end

Slide 81

Slide 81 text

Lookup time with BadKey 10.000 itera;ons First Key Last Key 4 0,00362 0,00580 8 0,00587 0,00397 16 0,00851 0,00442 32 0,0182 0,00300 64 0,02397 0,00295 128 0,05332 0,00348 256 0,09378 0,00476 512 0,2033 0,00294 1024 0,4146 0,00297 2048 0,92197 0,00453 4096 1,58056 0,00289 8192 3,323 0,00306 16384 6,66864 0,00414 32768 14,44931 0,00324 Lookup time with GoodKey 10.000 itera;ons First Key Last Key 4 0,00305 0,00282 8 0,00417 0,00318 16 0,00391 0,00359 32 0,00476 0,00399 64 0,00335 0,00275 128 0,00447 0,00366 256 0,00435 0,00342 512 0,00325 0,00439 1024 0,00317 0,00300 2048 0,00413 0,00334 4096 0,00490 0,00370 8192 0,00342 0,00452 16384 0,00522 0,00302 32768 0,00367 0,00408 0,01 0,1 1 10 100 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 Bad key Good Key

Slide 82

Slide 82 text

a good

Slide 83

Slide 83 text

http://amzn.to/1LI4VAj http://amzn.to/1LI5bPC

Slide 84

Slide 84 text

Thanks to Prof. Luciano Gualà Department of Mathema2cs
 of the University of Rome "Tor Vergata". Pat Shaughnessy Author of Ruby Under a Microscope

Slide 85

Slide 85 text

Photos hKps:/ /www.flickr.com/photos/stevefrazier/21397889609/ hKps:/ /www.flickr.com/photos/chanmelmel/15546712170/

Slide 86

Slide 86 text

Thanks! Simone Carle7 @weppos