scientific programming part b
play

Scientific Programming: Part B Lecture 5 Luca Bianco - Academic - PowerPoint PPT Presentation

Scientific Programming: Part B Lecture 5 Luca Bianco - Academic Year 2019-20 luca.bianco@fmach.it [credits: thanks to Prof. Alberto Montresor] Dictionary: ADT Possible implementations of a dictionary Hash table: definitions Key Function


  1. Scientific Programming: Part B Lecture 5 Luca Bianco - Academic Year 2019-20 luca.bianco@fmach.it [credits: thanks to Prof. Alberto Montresor]

  2. Dictionary: ADT

  3. Possible implementations of a dictionary

  4. Hash table: definitions Key Function Hash table 0 1 3 Luca Bianco ... David Leoni 60 ... Massimiliano Luca 116 ... m-1

  5. Hash table: collisions Key Function Hash table 0 collision 1 3 Luca Bianco ... There are several David Leoni 60 ways to deal ... Massimiliano Luca with these... 116 Andrea Passerini ... m-1

  6. Direct access tables Example: days of the year

  7. Perfect hash function

  8. Hash functions we will have to deal with collisions anyway. More on this later...

  9. Hash functions

  10. Hash functions: possible implementations

  11. Hash functions: possible implementations (the code) ord → ascii representation of a character Replace the b that stands for binary!

  12. Hash function implementation Luca 1,282,761,569 mod 383 Index: 351 David 293,692,926,308 mod 383 Index: 345 Massimiliano 23,948,156,761,864,131,868,341,923,439 mod 383 Index: 208 Andrea 71,942,387,426,657 mod 383 Index: 111 Alberto 18,415,043,350,787,183 mod 383 Index: 221 Alan Turing 39,545,995,566,905,718,680,940,135 mod 383 Index: 314

  13. Conflicts: separate chaining

  14. Separate chaining: complexity

  15. Separate chaining: complexity

  16. Separate chaining: complexity

  17. Hash table: rules for hashing objects [https://www.asmeurer.com/blog/posts/what-happens-when-you-mess-with-hashing-in-python/]

  18. Hash table: sample code (m = 11) [[('Andrea', 15)], [ ('Luca', 27), ('David', 5), ('Alberto', 12) ], [], [], [('Alan', 1)], [], [('Massimiliano', 12)], [], [], [], []] pair to deal with collisions Luca -> 27 Thomas -> None [[('Andrea', 15)], [ ('David', 5), ('Alberto', 12) ], [], [], [('Alan', 1)], [], [('Massimiliano', 12)], [], [], [], []] SOME CONFLICTS!

  19. Hash table: sample code (m = 17) [[], [], [], [], [], [], [('Alan', 1)], [], [], [('Andrea', 15)], [], [], [('David', 5)], [('Massimiliano', 12)], [], [('Luca', 27)], [('Alberto', 12)]] Luca -> 27 Thomas -> None [[], [], [], [], [], [], [('Alan', 1)], [], [], [('Andrea', 15)], [], [], [('David', 5)], [('Massimiliano', 12)], [], [], [('Alberto', 12)]] NO CONFLICTS!

  20. In python...

  21. Python built-in: set

  22. Python built-in: dictionary

  23. Stack: Last in, first out queue

  24. Stack: Last in, first out queue

  25. Stack: Last in, first out queue

  26. Stack: Last in, first out queue my_func(80)

  27. Stack: Last in, first out queue my_func(20) my_func(80)

  28. Stack: Last in, first out queue my_func(5) my_func(20) my_func(80)

  29. Stack: Last in, first out queue my_func(1) my_func(5) my_func(20) my_func(80)

  30. Stack: Last in, first out queue 1 my_func(1) my_func(5) my_func(20) my_func(80)

  31. Stack: Last in, first out queue 6 my_func(5) my_func(20) my_func(80)

  32. Stack: Last in, first out queue my_func(20) 26 my_func(80)

  33. Stack: Last in, first out queue 106 my_func(80)

  34. Stack: Last in, first out queue 106

  35. Stack: Last in, first out queue Note : the stack has finite size!

  36. Stack: implementation could have used a deque, linked list,...

  37. Stack: uses

  38. Stack: exercise Ideas on how to implement par_checker using a Stack? Simplifying assumption: only characters allowed in input are ”{ [ ( ) ] }” Possible solution: Loop through the input string and... ● push opening parenthesis to stack ● when analyzing a closing parenthesis, Desired output pop one element from the stack and {{([][])}()} balanced: True compare: if matching keep going, else [{()] balanced: False return False {[(())][{[]}]} balanced: True {[(())][{[]}] balanced: False

  39. Stack: exercise Desired output {{([][])}()} balanced: True [{()] balanced: False {[(())][{[]}]} balanced: True {[(())][{[]}] balanced: False

  40. Queue: First in, first out queue (FIFO)

  41. Queue: example

  42. Queue: uses and implementation

  43. Queue: as a list (with deque) Not very interesting implementation. Just pay attention to the case when the Queue is empty in top and dequeue Makes use of efficient deque object that provides ~ O(1) push/pop https://docs.python.org/3.7/library/collections.html#collections.deque

  44. Queue as a circular list tail tail tail tail

  45. Queue as a circular list: example

  46. Queue as a circular list: example

  47. Queue as a circular list: example

  48. Queue as a circular list: example

  49. Queue as a circular list: example

  50. Queue as a circular list: example

  51. Queue as a circular list: example skipping a few typing steps...

  52. Queue as a circular list: example skipping a few typing/reading steps...

  53. Queue as a circular list: exercise Implement the CircularQueue data structure (without going to the next slide…)

  54. Queue as a circular list: the code

  55. Exercise 1 Consider the following code (where s is a list of n elements). What is its complexity?

  56. Exercise 1 Consider the following code (where s is a list of n elements). What is its complexity? strings are immutable!

  57. Exercise 2 Consider the following code (where s is a list of n elements). What is its complexity?

  58. Exercise 2 Consider the following code (where s is a list of n elements). What is its complexity?

  59. Exercise 3 Consider the following code (where s is a list of n elements). What is its complexity?

  60. Exercise 3 Consider the following code (where s is a list of n elements). What is its complexity? Note that: “”.join(res) has complexity O(n)

  61. Exercise 4 Consider the following code (where L is a list of n elements). What is its complexity?

  62. Exercise 4 Consider the following code (where L is a list of n elements). What is its complexity?

  63. Exercise 5 Consider the following code (where L is a list of n elements). What is its complexity?

  64. Exercise 5 Consider the following code (where L is a list of n elements). What is its complexity?

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend