121 Flashcards
(130 cards)
What is predicate logic?
encompasses all of propositional logic + expands on its limitations - accounts for parts of propositions, links between parts of propositions + quantifiers
split into -
syntax - notation for concepts + operators used to create formulae
semantics - meaning behind formulae
Explain concepts:
terms - similar to subjects, expressed through nouns/pronouns, written as the lower case of the 1st letter
- argument of the predicate so
written as P(a)
predicates - properties/relations among terms, expressed through verbs, written as the upper case of the 1st letter
properties = “is property”
relations = “is the relation of”/”are related”
What are the types of operators in predicate logic?
connectives + quantifiers
What is an atomic formula?
consists of 1 predicate and 1<= terms
functions that take 1+ arguments (constants/variables) and returns true or false (Boolean)
What is a variable? What is a constant?
var = represents general terms (individuals of the same type or a class rather than specific individuals), written with lower case letters from end of alphabet
takes values from the universe of discourse - set of all entities that can replace a variable in an atomic formula
constant = a term desc. specific individual entities
Atomic formulae can be…
closed/ground formulae - predicate’s arguments are only constants and so are propositions with truth values
OR
open/unground formulae - predicate’s arguments consist of at least 1 variable and so are not propositions as they have no truth value
AND
have arity in sense of no. of vars (unary, binary, n-ary)
for 0 arity = proposition, 1 arity = properties, 2+ arity = relationships
What are the 2 quantifiers in predicate logic?
universal - every/”for all” (equivalent to a conjunction with all the variables) - denoted by upside down A
negation = not all
existential - at least one/”there exists” (equivalent to a disjunction with all the variables) - denoted by a backwards E
negation = none
What are the rules of inference?
all of propositional plus…
- universal/existential instantiation
- universal/existential generalisation
What is universal + existential instantiation?
universal - if it is true for a variable it is true for a constant of that variable
existential - if it is true for a constant it is true for a constant of that variable
What is universal + existential generalisation?
universal - if it is true for any arbitrary x it is true for every x
existential - if it is true for a constant it is true for a constant of that variable
What is the difference between restricted + unrestricted quantifiers?
unrestricted quantifiers - all elements in the Universe of Discourse - use conjunction
e.g “Every man has 2 legs”
Ax (M(x) -> L(x))
restricted quantifiers - some elements in the Universe of Discourse (a subset) - use implication
e.g “There is a man who has 2 legs”
Ex (M(x) AND L(x))
What functions + properties are needed for a queue?
head + tail pointers
enqueue(item) - if head null makes item head, else goes till tmp.next == null then makes item next
dequeue - return head and move rest up
isEmpty()
isFull() - if bounded
int data
element next
Pros + Cons of 2D arrays
+ random access - very fast
- fixed size (can only grow by copying elements - costly)
- insertion + deletion is costly
What properties + functions does a stack need?
top pointer
isEmpty()
isFull() if bounded
push(stack, x) - makes x top if stack isEmpty, else makes x top and current top x.prev
pop(stack) - check isEmpty, if not return top + make top stack.top.prev
element prev
int data
iteration vs recursion
iteration :
+ memory usage is controlled explicitly by the programmer - stack overflow is less likely
+ can execute more quickly - no overhead from stack frame creation/destruction
- naturally recursive functions can be harder to understand in iterative form
recursion :
+ naturally recursive functions are much more concise when recursively expressed
+ languages which support tail recursion can eliminate some of the extra costs to performance + stack overflow
What is indexed retrieval?
using an identifier (unique object attribute) to store + retrieve each object/record
- useful as allows us to perform binary search (objects sorted by key)
- can make searches up to 26x faster (when ordered alphabetically) but if all objects in 1 section then usual TC
What is a problem with storing objects? And how can we solve this?
collisions
solved by indexed retrieval (recomputes index array + shifts to ensure room) + chains (recomputation + dynamic use of pointers)
Efficiency of chains:
much faster (where objects distributed across many chains)
- same improvement as w/ using arrays
worst case - every object is in the same chain so O(N)
Explain hash tables:
data struct. that converts a key (unique identifier) to an int, using has function,
which becomes the index for storing the key-value pair
designed to be fast to work w/
- when few collisions = O(1)
ℎ:𝑈 →{0,1,2,…,𝑚 −1} - h = hash function, U = universe of keys, m = array size
What makes a hash function good?
- produces a wide range of index values (helps minimise collisions)
- unform hashing - each key is equally likely to map to an array location
What operations are helpful for hashing?
- multiplication/division = helps create a wider range of indexes
- MOD - can % by array size to ensure index is within array bounds (helpful after multiplication)
- logical shifts + OR operations - can replace multiplication/division for better speeds
hash table as an ADT:
- stores <k,v> pairs (where keys are unique + a handle to the associated value
- put (t, <k,v>) - to store <k, v> in table t
- get (t, k) - returns <k,v> if k is in t
- delete (t, k)
Explain rehashing:
used to make the table efficient again once chains get too long
create a new hash table at least double the current size
move all the key-value pairs over
though invisible to the ADT user, it can be an expensive operation
Define an algorithm:
set of computational steps that transform the input → the output
can be represented by code, flow charts, sentences, punch cards + pseudocode