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

Beyond the language: the importance of algorith...

Beyond the language: the importance of algorithms (CodeMash 2017)

The importance of algorithms in programming

Mastering a programming language is not enough to write efficient code. Programmers often try to squeeze a programming language as much as they can in order to achieve the best performance, without realizing that the solution could be found in a more efficient algorithm or data structure. The talk is meant to inspire you to learn more about algorithm design by demonstrating, with real world examples, how a properly designed algorithm may drastically increase the efficiency of your code, regardless of the programming language you are using.

Simone Carletti

January 14, 2017
Tweet

More Decks by Simone Carletti

Other Decks in Programming

Transcript

  1. algorithm |ˈalgərɪð(ə)m| a process or set of rules to be

    followed in calculations or other problem-solving operations, especially by a computer.
  2. An Algorithm is not a Program A program can be

    viewed as an elaborate algorithm.
  3. 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.
  4. As a you use a to implement to write programmer,

    programming language algorithms programs.
  5. 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.
  6. 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
  7. 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.
  8. The simplest algorithm Take the first item and compare it

    to all the other items in the collec:on.
  9. 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.
  10. The Big-O nota:on is used to classify the cost of

    an algorithm rela:ve to changes in input size.
  11. Big-O nota:on is a way to measure the complexity of

    the algorithm in the worst-case scenario.
  12. 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
  13. 1st algorithm: "one vs all" Take the first item and

    compare it to all the other items in the collec:on.
  14. 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.
  15. 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.
  16. 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
  17. rubyists = {} # insert rubyists[:weppos] = "Simone Carletti" rubyists[:jodosha]

    = "Luca Guidi" rubyists[:john] = "John Doe" # delete rubyists.delete(:john) # search rubyists[:weppos] # => "Simone Carletti"
  18. 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
  19. 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
  20. Hash Table in Ruby 16 whatever[:key1] = "value" :key1.hash #

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

    "value" whatever[:key3] = "value" key3 key2 key1
  22. 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
  23. Hash Table in Ruby 17 # search whatever[:keyX] :keyX.hash #

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

    # => 2149668665177381993 2149668665177381993 % 17 # => 8 keyX
  25. 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
  26. Thanks to Prof. Luciano Gualà Department of Mathema:cs
 the University

    of Rome "Tor Vergata" Pat Shaughnessy Author of Ruby Under a Microscope