Set4 - Computer Scienceweb.cse.ohio-state.edu/~kiel.32/09B.Set4.pdf · 2016. 9. 15. · Documenting...

23
Set4 15 September 2016 OSU CSE 1

Transcript of Set4 - Computer Scienceweb.cse.ohio-state.edu/~kiel.32/09B.Set4.pdf · 2016. 9. 15. · Documenting...

Page 1: Set4 - Computer Scienceweb.cse.ohio-state.edu/~kiel.32/09B.Set4.pdf · 2016. 9. 15. · Documenting Set4(and Map4) • By now you understand Hashing and its benefits. • But the

Set4

15 September 2016 OSU CSE 1

Page 2: Set4 - Computer Scienceweb.cse.ohio-state.edu/~kiel.32/09B.Set4.pdf · 2016. 9. 15. · Documenting Set4(and Map4) • By now you understand Hashing and its benefits. • But the

Documenting Set4 (and Map4)

• By now you understand Hashing and its benefits.

• But the algorithms we are using are not trivial, and make a lot of assumptions about the representation.

• How can we document those assumptions in our code?

15 September 2016 OSU CSE 2

Page 3: Set4 - Computer Scienceweb.cse.ohio-state.edu/~kiel.32/09B.Set4.pdf · 2016. 9. 15. · Documenting Set4(and Map4) • By now you understand Hashing and its benefits. • But the

The correspondence of Set4@correspondencethis =

union i: integer, s: finite set of T

where (0 <= i and i < |$this.hashTable.entries| and <s> = $this.hashTable.entries[i, i+1))

(s)

15 September 2016 OSU CSE 3

Page 4: Set4 - Computer Scienceweb.cse.ohio-state.edu/~kiel.32/09B.Set4.pdf · 2016. 9. 15. · Documenting Set4(and Map4) • By now you understand Hashing and its benefits. • But the

The correspondence of Set4@correspondencethis =

union i: integer, s: finite set of T

where (0 <= i and i < |$this.hashTable.entries| and <s> = $this.hashTable.entries[i, i+1))

(s)

15 September 2016 OSU CSE 4

The prefixed union introduces a universal

quantifier. This is the union over all possible values for

i and s

Page 5: Set4 - Computer Scienceweb.cse.ohio-state.edu/~kiel.32/09B.Set4.pdf · 2016. 9. 15. · Documenting Set4(and Map4) • By now you understand Hashing and its benefits. • But the

The correspondence of Set4@correspondencethis =

union i: integer, s: finite set of T

where (0 <= i and i < |$this.hashTable.entries| and <s> = $this.hashTable.entries[i, i+1))

(s)

15 September 2016 OSU CSE 5

What does the whereclause tells us about the

values of i and sthat are of interest to us?

Page 6: Set4 - Computer Scienceweb.cse.ohio-state.edu/~kiel.32/09B.Set4.pdf · 2016. 9. 15. · Documenting Set4(and Map4) • By now you understand Hashing and its benefits. • But the

The correspondence of Set4@correspondencethis =

union i: integer, s: finite set of T

where (0 <= i and i < |$this.hashTable.entries| and <s> = $this.hashTable.entries[i, i+1))

(s)

15 September 2016 OSU CSE 6

From the first two parts of the where clause we can

see that i has to be a valid index of our hash table

Page 7: Set4 - Computer Scienceweb.cse.ohio-state.edu/~kiel.32/09B.Set4.pdf · 2016. 9. 15. · Documenting Set4(and Map4) • By now you understand Hashing and its benefits. • But the

The correspondence of Set4@correspondencethis =

union i: integer, s: finite set of T

where (0 <= i and i < |$this.hashTable.entries| and <s> = $this.hashTable.entries[i, i+1))

(s)

15 September 2016 OSU CSE 7

The third part of the whereclause tells is that s has to be the set at position i of

our hash table.

Page 8: Set4 - Computer Scienceweb.cse.ohio-state.edu/~kiel.32/09B.Set4.pdf · 2016. 9. 15. · Documenting Set4(and Map4) • By now you understand Hashing and its benefits. • But the

The correspondence of Set4@correspondencethis =

union i: integer, s: finite set of T

where (0 <= i and i < |$this.hashTable.entries| and <s> = $this.hashTable.entries[i, i+1))

(s)

15 September 2016 OSU CSE 8

And the abstract value of this is the union of all

such sets s.

Page 9: Set4 - Computer Scienceweb.cse.ohio-state.edu/~kiel.32/09B.Set4.pdf · 2016. 9. 15. · Documenting Set4(and Map4) • By now you understand Hashing and its benefits. • But the

The correspondence of Set4@correspondencethis =

union i: integer, s: finite set of T

where (0 <= i and i < |$this.hashTable.entries| and <s> = $this.hashTable.entries[i, i+1))

(s)

15 September 2016 OSU CSE 9

Thus the value of this is the union of all the sets in

our hash table.

Page 10: Set4 - Computer Scienceweb.cse.ohio-state.edu/~kiel.32/09B.Set4.pdf · 2016. 9. 15. · Documenting Set4(and Map4) • By now you understand Hashing and its benefits. • But the

The convention of Set4@convention|$this.hashTable.entries| > 0 andfor all i: integer, s: finite set of T, x: T

where (0 <= i and i < |$this.hashTable.entries| and<s> = $this.hashTable.entries[i, i+1)] andx is in s)

([computed result of x.hashCode()] mod|$this.hashTable.entries| = i)) and

|$this.hashTable.examinableIndices| =|$this.hashTable.entries| and

$this.size = sum i: integer, s: finite set of Twhere (0 <= i and i < |$this.hashTable.entries| and

<s> = $this.hashTable.entries[i, i+1))(|s|)

15 September 2016 OSU CSE 10

Page 11: Set4 - Computer Scienceweb.cse.ohio-state.edu/~kiel.32/09B.Set4.pdf · 2016. 9. 15. · Documenting Set4(and Map4) • By now you understand Hashing and its benefits. • But the

The convention of Set4@convention|$this.hashTable.entries| > 0 andfor all i: integer, s: finite set of T, x: T

where (0 <= i and i < |$this.hashTable.entries| and<s> = $this.hashTable.entries[i, i+1)] andx is in s)

([computed result of x.hashCode()] mod|$this.hashTable.entries| = i)) and

|$this.hashTable.examinableIndices| =|$this.hashTable.entries| and

$this.size = sum i: integer, s: finite set of Twhere (0 <= i and i < |$this.hashTable.entries| and

<s> = $this.hashTable.entries[i, i+1))(|s|)

15 September 2016 OSU CSE 11

Perhaps it will be better if we break this into parts…

Page 12: Set4 - Computer Scienceweb.cse.ohio-state.edu/~kiel.32/09B.Set4.pdf · 2016. 9. 15. · Documenting Set4(and Map4) • By now you understand Hashing and its benefits. • But the

The convention of Set4@convention|$this.hashTable.entries| > 0 andfor all i: integer, s: finite set of T, x: T

where (0 <= i and i < |$this.hashTable.entries| and<s> = $this.hashTable.entries[i, i+1)] andx is in s)

([computed result of x.hashCode()] mod|$this.hashTable.entries| = i)) and

|$this.hashTable.examinableIndices| =|$this.hashTable.entries| and

$this.size = sum i: integer, s: finite set of Twhere (0 <= i and i < |$this.hashTable.entries| and

<s> = $this.hashTable.entries[i, i+1))(|s|)

15 September 2016 OSU CSE 12

Page 13: Set4 - Computer Scienceweb.cse.ohio-state.edu/~kiel.32/09B.Set4.pdf · 2016. 9. 15. · Documenting Set4(and Map4) • By now you understand Hashing and its benefits. • But the

Set4’s convention (1 of 4)|$this.hashTable.entries| > 0 and …

15 September 2016 OSU CSE 13

Surprisingly Arrays can have zero elements in

Java, but we want to have buckets in our hash

table…

Page 14: Set4 - Computer Scienceweb.cse.ohio-state.edu/~kiel.32/09B.Set4.pdf · 2016. 9. 15. · Documenting Set4(and Map4) • By now you understand Hashing and its benefits. • But the

Set4’s convention (2 of 4)… andfor all i: integer,

s: finite set of T,

x: T

where (0 <= i andi < |$this.hashTable.entries| and<s> = $this.hashTable.entries[i, i+1)]and x is in s)([computed result of x.hashCode()] mod

|$this.hashTable.entries| = i)) and …

15 September 2016 OSU CSE 14

The first three parts of the where clause look

familiar… What values of i and s are we interested in?

Page 15: Set4 - Computer Scienceweb.cse.ohio-state.edu/~kiel.32/09B.Set4.pdf · 2016. 9. 15. · Documenting Set4(and Map4) • By now you understand Hashing and its benefits. • But the

Set4’s convention (2 of 4)… andfor all i: integer,

s: finite set of T,

x: T

where (0 <= i andi < |$this.hashTable.entries| and<s> = $this.hashTable.entries[i, i+1)]and x is in s)([computed result of x.hashCode()] mod

|$this.hashTable.entries| = i)) and …

15 September 2016 OSU CSE 15

What about the values of x?

Page 16: Set4 - Computer Scienceweb.cse.ohio-state.edu/~kiel.32/09B.Set4.pdf · 2016. 9. 15. · Documenting Set4(and Map4) • By now you understand Hashing and its benefits. • But the

Set4’s convention (2 of 4)… andfor all i: integer,

s: finite set of T,

x: T

where (0 <= i andi < |$this.hashTable.entries| and<s> = $this.hashTable.entries[i, i+1)]and x is in s)([computed result of x.hashCode()] mod

|$this.hashTable.entries| = i)) and …

15 September 2016 OSU CSE 16

This tells us that if xis in a set in our

hash table, then that set is at a specific

position in our hash table...

Page 17: Set4 - Computer Scienceweb.cse.ohio-state.edu/~kiel.32/09B.Set4.pdf · 2016. 9. 15. · Documenting Set4(and Map4) • By now you understand Hashing and its benefits. • But the

Set4’s convention (2 of 4)… andfor all i: integer,

s: finite set of T,

x: T

where (0 <= i andi < |$this.hashTable.entries| and<s> = $this.hashTable.entries[i, i+1)]and x is in s)([computed result of x.hashCode()] mod

|$this.hashTable.entries| = i)) and …

15 September 2016 OSU CSE 17

In other words, this tells us that we are hashing the entries in our set!

Page 18: Set4 - Computer Scienceweb.cse.ohio-state.edu/~kiel.32/09B.Set4.pdf · 2016. 9. 15. · Documenting Set4(and Map4) • By now you understand Hashing and its benefits. • But the

Set4’s convention (3 of 4)… and|$this.hashTable.examinableIndices| =

|$this.hashTable.entries| and …

15 September 2016 OSU CSE 18

Page 19: Set4 - Computer Scienceweb.cse.ohio-state.edu/~kiel.32/09B.Set4.pdf · 2016. 9. 15. · Documenting Set4(and Map4) • By now you understand Hashing and its benefits. • But the

Set4’s convention (3 of 4)… and|$this.hashTable.examinableIndices| =

|$this.hashTable.entries| and …

15 September 2016 OSU CSE 19

If this is true, can there be a non-examinable

index in our hash table?

Page 20: Set4 - Computer Scienceweb.cse.ohio-state.edu/~kiel.32/09B.Set4.pdf · 2016. 9. 15. · Documenting Set4(and Map4) • By now you understand Hashing and its benefits. • But the

Set4’s convention (4 of 4)… and$this.size =

sum i: integer, s: finite set of Twhere (0 <= i and

i < |$this.hashTable.entries| and<s> = $this.hashTable.entries[i, i+1))

(|s|)

15 September 2016 OSU CSE 20

Page 21: Set4 - Computer Scienceweb.cse.ohio-state.edu/~kiel.32/09B.Set4.pdf · 2016. 9. 15. · Documenting Set4(and Map4) • By now you understand Hashing and its benefits. • But the

Set4’s convention (4 of 4)… and$this.size =

sum i: integer, s: finite set of Twhere (0 <= i and

i < |$this.hashTable.entries| and<s> = $this.hashTable.entries[i, i+1))

(|s|)

15 September 2016 OSU CSE 21

The prefixed sum introduces a universal

quantifier. This is the sum over all possible values for

i and s

Page 22: Set4 - Computer Scienceweb.cse.ohio-state.edu/~kiel.32/09B.Set4.pdf · 2016. 9. 15. · Documenting Set4(and Map4) • By now you understand Hashing and its benefits. • But the

Set4’s convention (4 of 4)… and$this.size =

sum i: integer, s: finite set of Twhere (0 <= i and

i < |$this.hashTable.entries| and<s> = $this.hashTable.entries[i, i+1))

(|s|)

15 September 2016 OSU CSE 22

The where clause looks familiar… What values of i and s are we interested

in?

Page 23: Set4 - Computer Scienceweb.cse.ohio-state.edu/~kiel.32/09B.Set4.pdf · 2016. 9. 15. · Documenting Set4(and Map4) • By now you understand Hashing and its benefits. • But the

Set4’s convention (4 of 4)… and$this.size =

sum i: integer, s: finite set of Twhere (0 <= i and

i < |$this.hashTable.entries| and<s> = $this.hashTable.entries[i, i+1))

(|s|)

15 September 2016 OSU CSE 23

What does this tell us about the value of sizewith respect to the value

of this?