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

scientific programming part b
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Scientific Programming: Part B

Lecture 5

Luca Bianco - Academic Year 2019-20 luca.bianco@fmach.it [credits: thanks to Prof. Alberto Montresor]

slide-2
SLIDE 2

Dictionary: ADT

slide-3
SLIDE 3

Possible implementations of a dictionary

slide-4
SLIDE 4

Hash table: definitions

Luca Bianco David Leoni Massimiliano Luca

Key Function Hash table 1 3 60 ... ... 116 m-1 ...

slide-5
SLIDE 5

Hash table: collisions

Luca Bianco David Leoni Massimiliano Luca

Key Function Hash table 1 3 60 ... ... 116 m-1 ...

Andrea Passerini

collision There are several ways to deal with these...

slide-6
SLIDE 6

Direct access tables

Example: days of the year

slide-7
SLIDE 7

Perfect hash function

slide-8
SLIDE 8

Hash functions

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

slide-9
SLIDE 9

Hash functions

slide-10
SLIDE 10

Hash functions: possible implementations

slide-11
SLIDE 11

Hash functions: possible implementations (the code)

  • rd → ascii

representation of a character Replace the b that stands for binary!

slide-12
SLIDE 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

slide-13
SLIDE 13

Conflicts: separate chaining

slide-14
SLIDE 14

Separate chaining: complexity

slide-15
SLIDE 15

Separate chaining: complexity

slide-16
SLIDE 16

Separate chaining: complexity

slide-17
SLIDE 17

Hash table: rules for hashing objects

[https://www.asmeurer.com/blog/posts/what-happens-when-you-mess-with-hashing-in-python/]

slide-18
SLIDE 18

Hash table: sample code (m = 11)

[[('Andrea', 15)], [('Luca', 27), ('David', 5), ('Alberto', 12)], [], [], [('Alan', 1)], [], [('Massimiliano', 12)], [], [], [], []] Luca -> 27 Thomas -> None [[('Andrea', 15)], [('David', 5), ('Alberto', 12)], [], [], [('Alan', 1)], [], [('Massimiliano', 12)], [], [], [], []]

SOME CONFLICTS! pair to deal with collisions

slide-19
SLIDE 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!

slide-20
SLIDE 20

In python...

slide-21
SLIDE 21

Python built-in: set

slide-22
SLIDE 22

Python built-in: dictionary

slide-23
SLIDE 23

Stack: Last in, first out queue

slide-24
SLIDE 24

Stack: Last in, first out queue

slide-25
SLIDE 25

Stack: Last in, first out queue

slide-26
SLIDE 26

Stack: Last in, first out queue

my_func(80)

slide-27
SLIDE 27

Stack: Last in, first out queue

my_func(20) my_func(80)

slide-28
SLIDE 28

Stack: Last in, first out queue

my_func(20) my_func(80) my_func(5)

slide-29
SLIDE 29

Stack: Last in, first out queue

my_func(20) my_func(80) my_func(5) my_func(1)

slide-30
SLIDE 30

Stack: Last in, first out queue

my_func(20) my_func(80) my_func(5) my_func(1) 1

slide-31
SLIDE 31

Stack: Last in, first out queue

my_func(20) my_func(80) my_func(5) 6

slide-32
SLIDE 32

Stack: Last in, first out queue

my_func(20) my_func(80) 26

slide-33
SLIDE 33

Stack: Last in, first out queue

my_func(80) 106

slide-34
SLIDE 34

Stack: Last in, first out queue

106

slide-35
SLIDE 35

Stack: Last in, first out queue

Note: the stack has finite size!

slide-36
SLIDE 36

Stack: implementation

could have used a deque, linked list,...

slide-37
SLIDE 37

Stack: uses

slide-38
SLIDE 38

Stack: exercise

Desired output

{{([][])}()}

balanced: True

[{()]

balanced: False

{[(())][{[]}]}

balanced: True

{[(())][{[]}]

balanced: False

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,

pop one element from the stack and compare: if matching keep going, else return False

slide-39
SLIDE 39

Stack: exercise

Desired output

{{([][])}()}

balanced: True

[{()]

balanced: False

{[(())][{[]}]}

balanced: True

{[(())][{[]}]

balanced: False

slide-40
SLIDE 40

Queue: First in, first out queue (FIFO)

slide-41
SLIDE 41

Queue: example

slide-42
SLIDE 42

Queue: uses and implementation

slide-43
SLIDE 43

Queue: as a list (with deque)

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

slide-44
SLIDE 44

Queue as a circular list

tail tail tail tail

slide-45
SLIDE 45

Queue as a circular list: example

slide-46
SLIDE 46

Queue as a circular list: example

slide-47
SLIDE 47

Queue as a circular list: example

slide-48
SLIDE 48

Queue as a circular list: example

slide-49
SLIDE 49

Queue as a circular list: example

slide-50
SLIDE 50

Queue as a circular list: example

slide-51
SLIDE 51

Queue as a circular list: example

skipping a few typing steps...

slide-52
SLIDE 52

Queue as a circular list: example

skipping a few typing/reading steps...

slide-53
SLIDE 53

Queue as a circular list: exercise

Implement the CircularQueue data structure (without going to the next slide…)

slide-54
SLIDE 54

Queue as a circular list: the code

slide-55
SLIDE 55

Exercise 1

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

slide-56
SLIDE 56

Exercise 1

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

slide-57
SLIDE 57

Exercise 2

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

slide-58
SLIDE 58

Exercise 2

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

slide-59
SLIDE 59

Exercise 3

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

slide-60
SLIDE 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)

slide-61
SLIDE 61

Exercise 4

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

slide-62
SLIDE 62

Exercise 4

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

slide-63
SLIDE 63

Exercise 5

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

slide-64
SLIDE 64

Exercise 5

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