Data Structures - Lab 07

Tuesday, October 5th

Implementing and using a queue

Objectives:


Part A: Big-Oh notation of different implementations of Stack

This week's lab should look very familiar to you as it starts with same structure we used in last week's lab.

Yesterday we started considering queues, and we wrote two different implementations of a queue that were built on top of Python's underlying list data structure. 

As we discussed in lecture yesterday, there are some distinct reasons why we might use one over the other with these two implementations.

Of course, like we did last week with stacks, I also would like you to consider the two implemenations that your author discusses int he chapter - the ArrayQueue and LinkedQueue.

All four of these implementations are contained in a file called queues.py which is contained in the following zip file:

 

Getting Started

 

FrontZeroQueue

Let's begin by considering a Queue where the front of the Queue is at zero (FrontZeroQueue.py). 

This means that the data looks something like this if we begin with an empty Queue

0 1 2 3 4 ...
           

And then enqueue(10)

0 1 2 3 4 ...
10          

And then enqueue(4)

0 1 2 3 4 ...
10 4        

And then enqueue(22)

0 1 2 3 4 ...
10 4 22      

And then enqueue(36)

0 1 2 3 4 ...
10 4 22 36    

And finally dequeue()

0 1 2 3 4 ...
4 22 36      

 

[Q1]  Using what you know about  Queues and assuming this python-list based implementation illustrated above, predict the Big-Oh notation for this queue implementation.  [Note : I realize that this is a prediction and that you have the "right" to be wrong here, but last week some of you were WAY off with your predictions for this type of table.  Take the time to really look at the code and consider the big-oh complexity for each of these methods.]

  enqueue(item) dequeue() peek() len() isEmpty()
Big-Oh          

 

BackZeroQueue

Next, let's consider a queue where the tail of the Queue is at zero (BackZeroQueue.py). 

We begin with an empty queue

0 1 2 3 4 ...
           

And then enqueue(10)

0 1 2 3 4 ...
10          

And then enqueue(4)

0 1 2 3 4 ...
4 10        

And then enqueue(22)

0 1 2 3 4 ...
22 4 10      

And then enquque(36)

0 1 2 3 4 ...
36 22 4 10    

And finally dequeue()

0 1 2 3 4 ...
36 22 4      

 

[Q2]  Using what you know about  Queues and assuming this python-list based implementation illustrated above, predict the Big-Oh notation for this queue implementation. 

  enqueue(item) dequeue() peek() len() isEmpty()
Big-Oh          

 

ArrayQueue

Next, let's consider the code called ArrayQueue which used the Array class that we worked with in chapter 13.  Take some time to look at this code and see how it works

[Q3]  Compare ArrayQueue to to FrontZeroQueue and BackZeroQueue.  Which of the fisrt two queues is Array Queue the most like?  Why?

[Q4]   Using what you know about  Stacks and using the code/implementation provided in stacks.py, predict the Big-Oh notation for this Stack implementation.  In other words, will push take longer if there are 10 items on the list versus if there are 1000 items on the list?  What about the other methods?

 

  enqueue(item) dequeue() peek() len() isEmpty()
Big-Oh          

 

LinkedQueue

Next, let's consider the code called LinkedQueue which used the Node class from chapter 13 to create a stack using a linked list.  Take some time to look at this code and see how it works

[Q5]  Using the vocabulary of the underlying linked list, where is the front of the queue with this implementation?

[Q6]   Using what you know about  Stacks and using the code/implementation provided in stacks.py, predict the Big-Oh notation for this Stack implementation.  In other words, will push take longer if there are 10 items on the list versus if there are 1000 items on the list?  What about the other methods?

 

  enqueue(item) dequeue() peek() len() isEmpty()
Big-Oh          

 

Benchmarking the runtime of each of these implementations

Let's see if you were correct in your answers to Q1, Q2, Q4 and Q6.

Open the file named timeQueueTest.py.  Currently, this piece of code should:

[Q7] Run several variations of this test to complete the table below:

  FrontZeroQueue BackZeroQueue ArrayQueue LinkedQueue

Starts with

10,000 20,000 Big-Oh 10,000 20,000 Big-Oh 10,000 20,000 Big-Oh 10,000 20,000 Big-Oh
enqueue() *5000                        
dequeue() *5000                        
peek() *5000                        
len() *5000                        
isEmpty() *5000                        

 

[Q8] Compare the big-oh complexity you observed in Q7 with your predictions in Q1, 2, 4, and 6.  Where were you incorrect?  Explain why what you observed would be correct.

 

 


Part B: Using a Queue

Josephus Flavius was a famous Jewish historian of the first century at the time of the Second Temple destruction. During the Jewish-Roman war he got trapped in a cave with a group of 40 soldiers surrounded by Romans. The legend has it that the soldiers decided that they did not want to "surrender" to the Romans.  However, Jewish law made suicide a non-option.  Instead, they decided to form a circle and play a "warped" version of "Eanie, Meanie, Minie, Mo".  They would start at the beginning and count off.  Every third person that they counted would be killed.  When they got back to the beginning they would keep going,  proceeding around until no one was left.  Josephus, not keen to die, quickly found the safe spot in the circle and thus stayed alive.

Your job in this part of the lab is to use a Queue to figure out in which position Josephus would need to stand.

How to do this:

Configure your code so that you can easily modify N (the number of people in the original group - our example assumes 40) and M (the number of people to count when we say "kill every Mth person" - or example assumes 3)

[Q9]  What position (number) would Josephus have wanted to occupy if he wanted to be the last person alive.

[SIG1]  Demo this code for Dr. Schafer when you have it all worked out.


To get full credit for this lab you must complete all questions and obtain all signatures by the START of class on Wednesday.