Download Paine Algorithm, Priority Queues, Heaps, and Data Structures Analysis and more Lecture notes Computer Science in PDF only on Docsity!
Drunken Paine, Dictionary, Jumble,
Priority Queues, Heaps
Paine Algorithm
Input: A list of people, people
Output: A Tom if there is one, NULL if not
findTom(people):
if length(people) is 1: return people[0] //base case
if people[0] knows people[1]: //eliminate one
erson
not_tom = remove people[0] from people
else:
not_tom = remove people[1] from people
candidate = findTom(people) //recursive call (solve smaller
roblem)
if candidate is NULL: return NULL //verify candidate
if not_tom knows candidate & candidate doesn’t know not_tom:
return candidate
else:
return NULL
Inductive proof of correctness
Base case: Our algorithm is correct for a group of size 1
True. We designed our algorithm’s base case according to the definition of a Tom in a group of size 1
Assume our algorithm is correct for a group of size k-
Done.
Show that if our algorithm is correct for a group of size k-1, then it
is correct for a group of size k.
We have an input of size k and we remove one non Tom from the group, then make a recursive call to find a Tom in a group of size k- This recursive call returns the correct answer (the Tom of the group of size k-1 or NULL)by our previous assumption
continued…
If the recursive call returns NULL:
There is no Tom in the group of size k because we eliminated one non Tom and there’s no Tom in the other k-1 people Our algorithm returns NULL in this case, so it’s correct.
If the recursive call returns a Tom (called candidate):
candidate is the only possible Tom of the group of size k candidate is not the Tom of the group of size k if the non Tom we eliminated doesn’t know candidate or if candidate knows the non Tom. In this case, there is no Tom in the group of size k, so we return NULL Otherwise, candidateis a Tom in the group of size k In this case, we return candidate
In every possible case, our algorithm returns the correct Tom if
there is one for the group of size k.
…given that our algorithm is correct for a group of size k-
The SET Abstract Data Type
S=createSet (n): creates a new empty set structure,
initially empty but capable of holding up to n elements.
isEmpty (S): checks whether the set S is empty.
size (S): returns the number of elements in S.
elementOf (x,S): checks whether the value x is in the
set S.
enumerate (S): yields the elements of S in some
arbitrary order.
add (S,x): adds the element x to S, if it is not there
already.
delete (S,x): removes the element x from S, if it is
there.
Implementing sets
Can use hashtable:
“create”, “isEmpty”, and “size” are trivial
“enumerate”: take all elements in all buckets
“add” is just “insert”; “delete” is “delete”
isElement is just “find”
Implementing a dictionary
Create(n)
Build an array of prime size a little more than n,
each entry an empty list
Pick k numbers, mod n, to handle keys of length
k
Insert(key, value)
Let u = (a 1 key 1 + … + a (^) k keyk ) mod n
Insert (key, value) into array[u]
Find(key)
Let u = (a 1 key 1 + … + a (^) k keyk ) mod n
Search for (key, *) in array[u]
If you find (key, val), return val
Else return None
(Modify as appropriate to return list of vals)
Example Application: JUMBLE!
JUMBLE
Input: list of all 5-letter words in English
Each word represented as an array of five
characters
Output: all words for which no other
permutation is a word
Priority Queue Definition
A priority queue stores a collection of items
An item is a pair
(k, element)
Main methods of the Priority Queue ADT
insert(k, o)
inserts an item with key k and element o
o = removeMin()
removes the item with smallest key and returns its element
Implementation
insert running time? removeMin running time?
insert 7 removeMin^ removeMin