Slide 1

Slide 1 text

Beyond the language the importance of algorithms in programming Simone Carle3 / / @weppos

Slide 2

Slide 2 text

Beyond the language An introduc:on to algorithms Simone Carle3 / / @weppos

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 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 6

Slide 6 text

algorithm |ˈalgərɪð(ə)m| a word used by programmers
 when they don't want to explain what they did.

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

bombardino

Slide 11

Slide 11 text

bombardino |\_(ツ)_/¯| The Bombardino is a popular winter drink in Italy, especially in the ski resorts. It is made with warm zabaglione (egg-based liqueur), brandy, and topped with a generous amount of whipped cream.

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

The Problem • Outside is very cold in Sandusky • You want to warm up with some Bombardino • One Bombardino is poisoned and it weighs a liFle bit more than the others • The only way for you to find the poisoned one is to weigh the Bombardino • Each :me you weigh a Bombardino you waste precious :me • You want to drink as much Bombardino as you can, ideally without drinking the poisoned one and before all the good Bombardino are gone

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

The simplest algorithm

Slide 17

Slide 17 text

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

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? Efficient?

Slide 27

Slide 27 text

The simplest algorithm Correct? Efficient? YES

Slide 28

Slide 28 text

The simplest algorithm Correct? Efficient? YES

Slide 29

Slide 29 text

Cost of an algorithm

Slide 30

Slide 30 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 31

Slide 31 text

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

Slide 32

Slide 32 text

The Big-O nota:on is used to classify the cost of an algorithm rela:ve to changes in input size.

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

1st algorithm Correct? Cost? Big-O? Efficient? YES n O(n) It depends…

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

2nd algorithm

Slide 39

Slide 39 text

2nd algorithm 1

Slide 40

Slide 40 text

2nd algorithm 2

Slide 41

Slide 41 text

2nd algorithm 3

Slide 42

Slide 42 text

2nd algorithm 4

Slide 43

Slide 43 text

2nd algorithm Correct? Cost? Big-O? Efficient? YES n/2 O(n) It depends…

Slide 44

Slide 44 text

3rd algorithm: "divide et pesa" Split the collec:on of items in two sets with the same number of items, and weigh them. Keep the heaviest set and discard drink the other.

Slide 45

Slide 45 text

3rd algorithm 1

Slide 46

Slide 46 text

3rd algorithm 2

Slide 47

Slide 47 text

3rd algorithm 3

Slide 48

Slide 48 text

3rd algorithm Correct? Cost? Big-O? Efficient? YES log2n O(log n) It depends…

Slide 49

Slide 49 text

4th algorithm: "divide /3 et pesa" Split the collec:on of items in 3 sets, and weigh the ones with the same number of elements.

Slide 50

Slide 50 text

4th algorithm 1

Slide 51

Slide 51 text

4th algorithm 1

Slide 52

Slide 52 text

4th algorithm 2

Slide 53

Slide 53 text

4th algorithm Correct? Cost? Big-O? Efficient? YES log3n O(log n) YES

Slide 54

Slide 54 text

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

Slide 55

Slide 55 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 56

Slide 56 text

Data Structures Ruby Hash Table

Slide 57

Slide 57 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 58

Slide 58 text

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

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

search in Ruby 2.3 0 0,002 0,003 0,005 0,006 4 16 64 256 1024 4096 16384 10k 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

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

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

Slide 67

Slide 67 text

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

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

Hash Table in Ruby 17 key3 key2 key1

Slide 70

Slide 70 text

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

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

Hash Table in Ruby 17 # search whatever[:keyX] :keyX.hash # => 2149668665177381993 2149668665177381993 % 17 # => 8 keyX

Slide 74

Slide 74 text

Hash Table in Ruby 8 17 # search whatever[:keyX] :keyX.hash # => 2149668665177381993 2149668665177381993 % 17 # => 8 keyX

Slide 75

Slide 75 text

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

Slide 76

Slide 76 text

Lookup :me with BadKey 10k itera?ons First Key Last Key 4 0,00362 0,00580 8 0,00587 0,00397 16 0,00851 0,00442 32 0,01820 0,00300 64 0,02397 0,00295 128 0,05332 0,00348 256 0,09378 0,00476 512 0,20330 0,00294 1024 0,41460 0,00297 2048 0,92197 0,00453 4096 1,58056 0,00289 8192 3,32300 0,00306 16384 6,66864 0,00414 32768 14,44931 0,00324 Lookup :me with GoodKey 10k 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 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 Good Key Bad Key

Slide 77

Slide 77 text

a good

Slide 78

Slide 78 text

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

Slide 79

Slide 79 text

Photos hFps:/ /www.flickr.com/photos/stevefrazier/21397889609/

Slide 80

Slide 80 text

Thanks! DNSimple dnsimple.com @dnsimple Simone Carleb simonecarle3.com @weppos hFps:/ /dnsimple.com/codemash