Question: Module 1 0 Homework - Priority Queue Implementation for Task Management Overview We will implement a priority queue to manage the scheduling of tasks in

Module 10 Homework - Priority Queue
Implementation for Task Management
Overview
We will implement a priority queue to manage the scheduling of tasks in an operating system. Tasks with
higher priority should be executed first. You are provided with a skeleton code for three classes Entry,
MaxHeap, and TaskManager. You will complete the implementation of the Entry class and MaxHeap class.
Requirements:
1. Entry Class Implementation:
Implement the comparison methods in the provided Entry class to compare entries based on
priority.
2. MaxHeap Class Implementation:
Implement methods to insert entries, remove the maximum priority entry, maintain the heap
property, and change the priority of existing tasks.
Implement heapifying methods to ensure the heap property is maintained after insertion,
removal, and priority change operations.
3. TaskManager Class Implementation:
The TaskManager class includes methods to add tasks, remove tasks with the highest priority.
Use the MaxHeap class methods to manage the task queue efficiently.
Examples
Several examples of behavior shown below.
# Creating entries
entry1= Entry(priority=5, process_id="Process 1")
entry2= Entry(priority=3, process_id="Process 2")
entry3= Entry(priority=7, process_id="Process 3")
entry4= Entry(priority=8, process_id="Process 4")
entry5= Entry(priority=2, process_id="Process 5")
entry6= Entry(priority=6, process_id="Process 6")
entry7= Entry(priority=4, process_id="Process 7")
# Initializing MaxHeap
max_heap = MaxHeap()
# Adding entries to the max heap
max_heap.put(entry1)
max_heap.put(entry2)
max_heap.put(entry3)
max_heap.put(entry4)
hw10.md 2024-04-11
2/2
max_heap.put(entry5)
max_heap.put(entry6)
max_heap.put(entry7)
# Changing priority of a process
max_heap.change_priority(process_id="Process 1", new_priority=10)
# Changing priority of a process
max_heap.change_priority(process_id="Process 3", new_priority=1)
# Removing processes until heap is empty
while max_heap:
print(f"Removing max priority process: {max_heap.remove_max()}")
# Expected output:
# Removing max priority process: Process 1
# Removing max priority process: Process 4
# Removing max priority process: Process 6
# Removing max priority process: Process 7
# Removing max priority process: Process 2
# Removing max priority process: Process 5
# Removing max priority process: Process 3
Starter Code:
class Entry:
"""Represents an entry in the priority queue."""
def __init__(self, priority, process_id):
"""Initializes an Entry object with a given priority and process ID."""
self.priority = priority
self.process_id = process_id
def __repr__(self):
"""Returns a string representation of the Entry object."""
return f"Entry(priority={self.priority}, process_id={self.process_id})"
####### Implement all Entry class methods under this line #######
def __gt__(self, other):
"""Compares the priority of this entry with another entry.
Returns:bool: True if this entry has higher priority than the other, False otherwise."""
def __eq__(self, other):
"""Checks if this entry is equal to another entry based on priority.
Returns:bool: True if the priorities are equal, False otherwise."""
class MaxHeap:
"""Represents a max heap data structure."""
def __init__(self):
"""Initializes a MaxHeap object."""
self._heap =[]
####### Implement all MaxHeap class methods under this line #######
def put(self, entry):
"""Inserts an entry into the max heap."""
def remove_max(self):
"""Removes and returns the entry with the maximum priority from the max heap.
Returns:The process ID that was removed from the queue. raise IndexError if the heap is empty"""
def change_priority(self, process_id, new_priority):
"""Changes the priority of a process in the max heap.
Returns:bool: True if the priority change was successful, False otherwise."""
def _upheap(self, index):
"""Performs up-heap operation to maintain heap property after insertion."""
def _downheap(self, index):
"""Performs down-heap operation to maintain heap property after removal."""
def __len__(self):
"""len is number of items in PQ"""
class TaskManager:
"""Manages the execution of processes using a priority queue."""
def __init__(self):
"""Initializes a TaskManager object."""
self.processor_queue = MaxHeap()
####### Implement all TaskManager class methods under this line #######
def add_process(self, entry):
"""Adds a process to the processor queue."""
def remove_process(self):
"""Removes and returns the process with the highest priority from the processor queue."""
Module 1 0 Homework - Priority Queue

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 Accounting Questions!