Question: [PYTHON 3] Implementing a bounded queue and a circular queue These are the cicular queue and bounded queue classes used in class: class CircularQueue: def

[PYTHON 3] Implementing a bounded queue and a circular queue

[PYTHON 3] Implementing a bounded queue and a circular queue These are

These are the cicular queue and bounded queue classes used in class:

class CircularQueue: def __init__(self, capacity): if type(capacity)!=int or capacity 0, ('Error: Illegal capacity: %d' % (capacity)) self.__items = [] self.__capacity = capacity def enqueue(self, item): if len(self.__items) == self.__capacity: raise Exception('Error: Queue is full') self.__items.append(item) def dequeue(self): if len(self.__items) == 0: raise Exception('Error: Queue is empty') return self.__items.pop(0) def peek(self): if len(self.__items) == 0: raise Exception('Error: Queue is empty') return self.__items[0] def isEmpty(self): return len(self.__items) == 0 def isFull(self): return len(self.__items) == self.__capacity def size(self): return len(self.__items) def capacity(self): return self.__capacity def clear(self): self.__items = [] def __str__(self): str_exp = "" for item in self.__items: str_exp += (str(item) + " ") return str_exp def __repr__(self): return str(self.items) + " Max=" + str(self.__capacity)

Q2 Implement the Bounded Queue and Circular Queue ADT as seen in class The python implementations of these data structures are in the lecture slides Make sure you raise exceptions when - peek)/dequeue() if the queue is empty - enqueue) if the queue is full Compare the runtime of dequeue) methods in Bounded Queue and Circular Queue. In bounded queue, once you dequeue an item, the remaining items in the list will be shifted left. This happen because we are using the pop(index) method in the list class to dequeue. Therefore, the dequeue in Bounded Queue is O(n) However in Circular Queue, the front most item is accessed in the list by using the subscription operator and the head is incremented by 1. Therefore, the dequeue in a Circular Queue is O(1) So in theory, dequeue in Circular Queue should be much faster than the dequeue in a Bounded Queue. Write your code to justify whether this hypothesis is true or false Here are the steps for this experiment 1. Create a Circular Queue object and enqueue 100000 items in it 2. Create a Bounded Queue object and enqueue 100000 items in it 3. Compute the time required to dequeue all items from the Circular Queue 4. Compute the time required to dequeue all items from the Bounded Queue Here is how to use the time module import time start time.time() # The statement (s) that you want to test end - time.time() time interval = end - start 5. Print the dequeue() runtime for each queue Please note : We need to enqueue many items in the Circular and Bounded Queue in order to get a reasonable run time for both queues Here is the sample output: For Bounded Queue, the total runtime of dequeing 100000 is: 2.2983570098876953 For Circular Queue, the total runtime of dequeing 100000 is: 0.11895871162414551 Q2 Implement the Bounded Queue and Circular Queue ADT as seen in class The python implementations of these data structures are in the lecture slides Make sure you raise exceptions when - peek)/dequeue() if the queue is empty - enqueue) if the queue is full Compare the runtime of dequeue) methods in Bounded Queue and Circular Queue. In bounded queue, once you dequeue an item, the remaining items in the list will be shifted left. This happen because we are using the pop(index) method in the list class to dequeue. Therefore, the dequeue in Bounded Queue is O(n) However in Circular Queue, the front most item is accessed in the list by using the subscription operator and the head is incremented by 1. Therefore, the dequeue in a Circular Queue is O(1) So in theory, dequeue in Circular Queue should be much faster than the dequeue in a Bounded Queue. Write your code to justify whether this hypothesis is true or false Here are the steps for this experiment 1. Create a Circular Queue object and enqueue 100000 items in it 2. Create a Bounded Queue object and enqueue 100000 items in it 3. Compute the time required to dequeue all items from the Circular Queue 4. Compute the time required to dequeue all items from the Bounded Queue Here is how to use the time module import time start time.time() # The statement (s) that you want to test end - time.time() time interval = end - start 5. Print the dequeue() runtime for each queue Please note : We need to enqueue many items in the Circular and Bounded Queue in order to get a reasonable run time for both queues Here is the sample output: For Bounded Queue, the total runtime of dequeing 100000 is: 2.2983570098876953 For Circular Queue, the total runtime of dequeing 100000 is: 0.11895871162414551

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!