next up previous
Next: About this document ...

A Z Specification for a Priority Queue
Eric C. Cyr (ecyr@clemson.edu)

Let $ELEMENT$ be the set of all possible items you want in your priority queue. Boolean is a place holder for boolean values.
\begin{zed}[ELEMENT]\\
Boolean ::= True \vert False
\end{zed}

A generic way to specify a greater than function of an $ELEMENT$ type. This is to allow the queue to have a priority, for a more specific priority queue you would simply change this definition.
\begin{axdef}
GreaterThan : ELEMENT \cross ELEMENT \fun Boolean
\where
\forall x,y:ELEMENT @ GreaterThan(x,y) = True \lor GreaterThan(x,y) = False
\end{axdef}

This is the schema for PriorityQueue. All that PriorityQueue contains is a set of partial functione mapping the natural numbers to the items in the queue. The predicate gurantees that the doman of "items" is the emptyset or {1..n} where n is an integer greater then or equal to 1.
\begin{schema}{PriorityQueue}
items: \nat \pfun ELEMENT
\where
\forall x:\nat \vert x > 1 @ x \in \dom items \implies x-1 \in \dom items
\end{schema}

The initialize for a PriorityQueue, make sure it is empty to begin with.
\begin{schema}{InitPriorityQueue}
PriorityQueue'
\where
items' = \emptyset
\end{schema}

Return "True" if the priority queue is empty and "False" otherwise
\begin{schema}{IsEmpty}
\Xi PriorityQueue \\
result!: Boolean
\where
result!=True \iff \char93 items = 0 \\
result!=False \iff \char93 items > 0
\end{schema}

The join operation is the similar to what enqueue typically does, however in the case of the priority queue it needs to insert the item in the correct order. Notice this schema has two local variables, "smallest" representing the smallest index where the corresponding element is greater then the item to be added and "greater" which is the indexs of elements such that there corresponding element is greater then the one to be added.
\begin{schema}{Join}
\Delta PriorityQueue \\
theItem?: ELEMENT \\
smallest:...
...1 \mapsto items(x)~\} \cup \\
\t1 \{ smallest \mapsto theItem? \}
\end{schema}

The Leave operation is the same as dequeue, it simply returns the value with index 1 and shifts everything down.
\begin{schema}{Leave}
\Delta PriorityQueue \\
theItem!: ELEMENT
\where
item...
...vert x \mapsto y \in items \land x > 1 @ \\
\t1 x-1 \mapsto y ~\}
\end{schema}



next up previous
Next: About this document ...
Brian Malloy 2002-04-22