Upload
pedram-shirinbolaghi
View
220
Download
1
Embed Size (px)
DESCRIPTION
Logic Lecture nr 8
Citation preview
Semester 2
Logic, Algorithms, Data structures
Hash tables for efficient Sets and Maps
Module 8
Ch 4.5 hashing
(Ch. 1.2 - 1.4 (logic revisited))
Ch 9.2
Logic Revisited Deduction proofs and
Predicate logic
&
Motivation for this Hash tables
I wonder how could find our cute little whoopie so fast.
Let’s ask Eddie, he’s a software engineer.
The Hash-table is a
big-impact-innovation that
has altered our society.
Database > Spells > Hashing Hashing Rank 24
Reduces time for table lookup from linear to constant time.
If software development was WoW,
then Hashing would have been a spell:
Understanding of Hash-tables is a key
skill in making programs more efficient,
and gaining rank as software developer.
Simpler Case We want a fast phone dictionary.
Should we use
a Linked List?
key = ”Bates”
phone = ”5444”
nxt =
key = ”Batson”
phone = ”3072”
nxt =
Lookup time is O(n)
...
Should we use a Search Tree?
...
key = ”Mildred”
phone = ”7651”
left = right=
key = ”Silberman”
phone = ”4664”
left = right=
key = ”Henry”
phone = ”4664”
left = right=
... ...
Lookup time is O(log(n))
Which datastructure
should we use?
. . .
”a”1
Should we use precede lists with an array
(or array of arrays, or array of arrays of arrays?)
”b”b
”z”26
. . .
”a”1
”b”2
”z”26
”a”1
”b”2
”z”26
”t”20
...
...
This would mean a lot of
memory overhead.
28
28
28
...
. . .
key = ”Bates...”
phone = ”5444”
nxt =
key = ”Batson
phone = ”3072”
nxt =
Suggestion from past course
Yet another idea
1.Translate each key into a unique number.
2. Use the key as an array index.
0
1
”batson” 360
... ...
...
”bates” 149 key = ”bates”
phone = ”5444”
key = ”batson
phone = ”3072”
Coding Functions How do we translate strings to integers codes?
Idea: multiply each character code (where a has code 1) by
20 for the righmost position
21 for the next to rightmost position
22 for the next to next to rightmost position, etc.
and take the sum.
Example
f (”bates”) = 242 + 231 + 2220 + 215 + 2019
= 162 + 81 + 420 + 25 + 119 = 149
coding
function
a b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
f (”batson”) = ? = 322 + 161 + 820 + 419 + 215 + 114
= 360
f (”paraskevopoulos”) = 21416 + ... = 262 144 + ...
That’s a large array
Compression Functions
[0,1, 2, ... , 30]
A big interval, e.g.,
may be chopped up (or hashed) using a compression function, e.g.
c(x) = 0 if x = 0, 10, 20, 30
1 if x = 1, 11, 21
9 if x = 9, 19,29
x mod 10
Ex.
c(f (”bates”)) = c (149) = 149 mod 10 = 9
c(f (”batson”)) = c (360) = 360 mod 10 = 0
0 30
0 9
compression
function
Hash functions
A hash function is what we get by composing
a coding function and a compression function
h(x) = c(f(x))
f c
h
x
The Hash table
0
1
2
3
4
5
6
7
8
9
key = ”bates”
phone = ”5444”
key = ”batson”
phone = ”3072”
c(f (”bates”)) = 9
c(f (”batson”)) = 0
What about Collisions?
Suppose f (”paraskevopoulos”) = 456 789
Slots
Collision handling with lists
360
149 456 789
key = ”Bates”
phone = ”5444”
key = ”Batson”
phone = ”3072”
key = “paraskevopoulos”
phone = ”6941”
h(f (”bates”)) = 9
h(f (”batson”)) = 0
h(f (”paraskevopoulos”)) = 9
149
456 789
360
This is the most common collision handling technique.
Collision handling with linear probing
0
1
2
3
4
5
6
7
8
9
key = ”bates”
phone = ”5444”
key = ”batson”
phone = ”3072”
h(f (”bates”)) = 9
h(f (”batson”)) = 0
149
456 789
360
h(f (”paraskevopoulos”)) = 9
key = “paraskevopoulos”
phone = ”6941”
When the desired place is occupied,
then place the binding in the next free slot.
Load Factor
The load factor () of a hash table is defined as
no of elements
no of slots
i.e. the average length of the collision resolution lists.
High - table behaviour more of list and less of array
Low - wastes memory
=
= 16/ 5 = 3.2
= 2/10 = 0.2
Experiements suggest that we should choose N large enough to maintain
< 0.9 when lists are used for collision resoltution
< 0.5 when linear probing is used.
Rehashing 0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
Fresh map After 2 insertions After 8 insertions
= 0 = 0.2 = 0.8
0 1 2 3 4 5 6 7 8 9
10
0 1 2 3 4 5 6 7 8 9
After 9 insertions
= 0.45
Rehashing
Qualities of Hash Functions
0,1,2, ..., N
h : S T
We want keys to be evenly distributed over 0 .. N
We want equality among keys to be preserved.
S T
Examples of poor hash function:
- Use lowermost bits of memory address
of object in heap.
- Take the length of a string as the hash code
Hash tables in Java
Hash Map
Set Map
HashSet
java.util.HashMap
class HashMap<K,V>
{
V put(K key, V value); \\ Binds value to key (returns old binding for convenience.)
V get(K key); \\ Returns value bound to key or null if unbound.
}
Coding functions in Java Each Object in Java has a Coding Function built-in:
You may create your own coding function! by overriding the method hashCode()!
Compression functions in Java Java.util.HashMap defines
int hash(Object key)
{
return Math.abs( key.hashCode() % buckets.length )
}
”Mod”
Slot Handling in Java Java.util.HashMap definies
public HashMap( int initialCapacity, float loadFactorBound)
public HashMap() { this(11, 0.75) }
Initial
slot count If exceeds
then
new slotcount = 2*old slotcount +1
+ re-hashing
Logic revisited
• Inference rules and Deduction proofs
• Predicate Logic
Copyright 2007, Lars Pareto
Proving Logical Facts
There are a number of alternatives:
• Construct a truth table. (You did this in assignment 1.)
• Rewrite logical formula using equivalence laws. (We
looked at this briefly in lecture 1.)
• Construct a deduction proof. (New for today.)
Copyright 2007, Lars Pareto
Copyright 2007, Lars Pareto
Some logical equivalences A B B A
A B B A
A ( B C ) (A B) C
A ( B C ) (A B) C
A ( B C ) (A B) ( A C )
A ( B C ) (A B ) (A C)
A F A
A T A
A T T
A F F
A A' T
A A' F
(A´)´ A
A A A
A A A
(A B )´ A ' B '
(A B)´ A ' B '
Copyright 2007, Lars Pareto
Law for the implication operator
A B A' B
Proof: A B A B A' B
F F T T
F T T T
T F F F
T T T T
Copyright 2007, Lars Pareto
Using truth tables to prove laws A B B A
A B B A
A ( B C ) (A B) C
A ( B C ) (A B) C
A ( B C ) (A B) ( A C )
A ( B C ) (A B ) (A C)
A F A
A T A
A T T
A F F
A A' T
A A' F
(A´)´ A
A A A
A A A
(A B )´ A ' B '
(A B)´ A ' B '
A B C A(BC) AB AC (AB)(AC)
F F F
F F T
F T F
F T T
T F F
T F T
T T F
T T T T
T
T
T
T
F
F
F
T
T
T
T
T
T
F
F T
T
T
T
T
T
F
F
T
T
T
T
T
F
F
F
Copyright 2007, Lars Pareto
Another example of rewriting
A (A' B)
{ Law: P ( Q R ) (P Q ) (P R) } (A A') (A B)
{ Law: P P' T} T (A B)
{ Law: P Q Q P } (A B) T
{ Law: P T P } A B
Rather than inserting values in A (A' B) to compute it,
let’s first simplify it as much as we can:
Copyright 2007, Lars Pareto
Names of logical laws
When calculating and explaing calculations, it’s quite handy
to have names for out laws.
A B B A
A B B A A T T
A F F
The commutative laws
Example
A ( B C ) (A B) C
A ( B C ) (A B) C
The associative laws
A ( B C ) (A B) ( A C )
A ( B C ) (A B ) (A C)
The distributive laws
A F A
A T A
(A B ) ' A ' B '
(A B) ' A ' B '
A A' T
A A' F
(A´)´ A
A A A
A A A
The identity laws
The dominance laws
Double negation
The idempotence laws
De Morgan’s laws
Copyright 2007, Lars Pareto
Applying laws to sub-expressions When calculting, we often apply laws to rewrite a subexpression.
Example
We want to simplify A (A' B ') '
A (A' B')' { De morgan }
A ((A')' (B')') { Associativity }
(A (A')') (B')' {Double negation }
(A A) (B')' { Idempotence }
A (B')'
{ Double negation }
A B
Deduction Proofs
• You have a number of premises and want to prove that
these imply a certain conclusion.
P1 P2 P3 …Q
• You can use equivalences to rewrite formulas or sub-
formulas.
• You can also use inference rules to create new facts from
already known facts.
• Finally you deduce the conclusion.
Copyright 2007, Lars Pareto
Copyright 2007, Lars Pareto
Structure of a deductive proof
1. P1
2. P2
3. P3
33.D1 B1
34.D2 B2
35.D3 B3
73.Q
Hypoteses
Derived
statements
Conclusion
Explanations
why the derived
statements hold
Inference rules
• Inference rules have a set of premises which must all
already be deduced facts (or premises) and a conclusion
which becomes the new deduced facts.
• Inference rules can only be applied in one direction.
Copyright 2007, Lars Pareto
Premises Conclusion Inference rule name
A B, A B Modus ponens
A B, B ' A' Modus tollens
A, B A B Conjunction
A B A Simplification
A A B Addition
Copyright 2007, Lars Pareto
Logical Inference rules The following laws, which one can easily prove (try!), are useful:
( A B) AB
Modus ponens
( A B) B ' A'
Modus tollens
Examples It Rains Calvin gets wet.
It rains.
Can we conclude that Calving gets wet?
1.
It Rains Calvin gets wet.
Calvin gets wet.
Can we conclude that it rains?
2.
It Rains Calvin gets wet.
Calvin is not wet.
Can we conclude that it does not rain?
3.
It Rains Calvin gets wet.
It does not rain.
Calvin is not wet.
4.
Yes, by
MP
No.
Yes, by
MT
No
Copyright 2007, Lars Pareto
Predicate logic
Copyright 2007, Lars Pareto
Another Example
Statements
– All men owns a tie.
– Some men owns a bowtie.
Question
Is there a man that owns both a tie and a
bowtie?
Conclusion: Yes
Logical
thinking, I Propositional logic
does not support
all and some.
Copyright 2007, Lars Pareto
Predicate
A predicate is a statement of one or several variables.
We have seen predicates in the Sets modules.
Example:
” x is a man” is a predicate.
” x owns a tie” is a predicate
We often introduce predicate symbols as shorthands
Example
We let Man(x) stands for ”x is a man”, and
Tie(x) stand for ”x is a tie”, and
Owns(x,y) stand for ”x owns y”
This let’s us write
Man(joe) Tie(armani3758) Owns(joe, armani3758)
Copyright 2007, Lars Pareto
Objects
Variables are placeholders for objects
Objects may be just anything: numbers, strings, booleans, french
cheeses, people, animals, ...
Copyright 2007, Lars Pareto
Universal and Existental quantifiers
x is read ”for all x”
x is read ”for some x” (or, ”there exists x”)
Quantifers are used to specify how many objects that has a certain
property.
Examples:
( x) ” x is a man”
( x) ” x is a man” ”x owns a tie”
Copyright 2007, Lars Pareto
Nested quantifiers
(x) ( Animal(x) ( y) (Smells(x,y) Cheese(y) Hungry (x)) )
A riddle!
”There is an object x such that x is an anmial,
and such that for all objects y such that x smells y , and
such that y is a cheese implies that x is hungry.”
In other words
”There is an animal that gets hungry whenever it smells cheese. ”
Copyright 2007, Lars Pareto
Scope
The scope of a quantifier is the expression right of it
Example
( x) ( Man(x) ( y) (Smells(x,y) Cheeset(y) Hungry (x)) )
Proofs in Predicate Logic
• Since the set of individual is unknown and can be infinite
constructing truth tables is not an option.
• Therefore to construct a proof for a formula containing
quantifiers we must resort to logical reasoning (rewriting
or deduction)
Copyright 2007, Lars Pareto
Copyright 2007, Lars Pareto
Back to our challenge Predicates
(x)(Man(x) Hastie(x)
( x) (Man(x) Hasbow(x))
( x) (Man(x)
Hastie(x) Hasbow(x))
Statements
– All men owns a tie.
– Some men owns a bowtie.
Question
Is there a man that owns both a tie
and a bowtie?
Copyright 2007, Lars Pareto
Warning:
Why and not
Statements
(x)(Man(x) Hastie(x)
( x) (Man(x) Hasbow(x))
Statements
(x)(Man(x) Hastie(x)
( x) (Man(x) Hasbow(x))
Because
( x) (Man(x) Hasbow(x))
does not capture what we mean.
Why not?
Copyright 2007, Lars Pareto
( x) ( Man(x) Hasbow(x) ) is read
”There is an object such that
if the object is a man,
then that person has a bowtie”
Observ that
Man(x) Hasbow(x)
is true if
Man(x) is false!
A B A B
f f t
f t t
t f f
t t t
Law
followed by followed by
Copyright 2007, Lars Pareto
Proof attempt
1. (x)(Man(x) Hastie(x) ) Hyp1
2. ( x) (Man(x) Hasbow(x)) Hyp2
99 ( x) (Man(x) Hastie(x) Hasbow(x)) Q:
P:
We need rules to take us from
1,2 to 99
Copyright 2007, Lars Pareto
Rules for predicate logic
Law Side condition Name
( x) P(x) P(t) t a variable or a constant
t must not become bound
Universal
Instantiation
( x) P(x) P(a)
a is a constant
a is not used before
Existential
Instantiation
P(x) ( x) P(x) x must not ocurr in the proof premises Universal
Generalisation
P(t) ( x) P(x) t is a variable or a constant
if t is a konstant, then
x must not already occur in P(t)
Existential
Generalisation
Difficult? Don’t worry: this rules are indeed difficult.
Copyright 2007, Lars Pareto
Proof
1. (x)(Man(x) Hastie(x) ) P1
2. ( x) (Man(x) Hasbow(x)) P2
3. Man(MrX) Hasbow(MrX) 2,Exist.Inst.
4. Man(MrX) Hastie(MrX) 1,Univ.Inst.
5. Man(MrX) 3, Simplif.
6. Hastie(MrX) 5,4, MP
7. Hastie(MrX)Man(MrX)Hasbow(MrX) 6,3, Conjunction
8. ( x)(Hastie(x)Man(x)Hasbow(x)) 7, Exist. Gen. Q
P
We check to see that side conditions for 3,4,7 hold.
Copyright 2007, Lars Pareto
More Laws
The first two equivalences express commutativity
between existential quantifier and disjunction, and
universal quantifier and conjunction, respectively.
The two last equivalences are the de Morgan’s laws
correspondence for quantifiers.