Paine Algorithm, Priority Queues, Heaps, and Data Structures Analysis, Lecture notes of Computer Science

The paine algorithm for finding a 'tom' in a group of people, an inductive proof of its running time and correctness, and the use of heaps and priority queues in data structures. It also explains the implementation of sets and dictionaries, and provides an example application of a jumble algorithm.

Typology: Lecture notes

2012/2013

Uploaded on 03/23/2013

dhruv
dhruv 🇮🇳

4.3

(12)

194 documents

1 / 48

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Drunken Paine, Dictionary, Jumble,
Priority Queues, Heaps
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30

Partial preview of the text

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