#include <iostream>
#include <vector>
using namespace std;
struct Task {
int id;//标识符 待输入
int urgency; // 紧急程度(优先级)
int processingTime; // 预计处理时间(秒)
Task() {}// 默认构造函数
Task(int _id, int _urgency, int _processingTime) : id(_id), urgency(_urgency), processingTime(_processingTime) {}// 带参构造函数,将参数赋值给成员变量,分别为id,urgency,processingTime
};
const double alpha = 2;// 加权函数参数
const double beta = 1;// 加权函数参数
struct CompareTasks {// 定义比较函数,用于堆的构建
bool operator()(const Task& t1, const Task& t2) {
double weight1 = alpha * (1.0 / t1.urgency) + beta * (1.0 / t1.processingTime);
double weight2 = alpha * (1.0 / t2.urgency) + beta * (1.0 / t2.processingTime);
if (weight1 == weight2) {
return t1.urgency > t2.urgency;
}
return weight1 < weight2;
}
};
class PriorityQueue {
private:
vector<Task> heap;
void heapifyUp(int index) {
int parent = (index - 1) / 2;
while (index > 0 && CompareTasks()(heap[index], heap[parent])) {
swap(heap[index], heap[parent]);
index = parent;
parent = (index - 1) / 2;
}
}
void heapifyDown(int index) {
int leftChild = 2 * index + 1;
int rightChild = 2 * index + 2;
int smallest = index;
if (leftChild < heap.size() && CompareTasks()(heap[leftChild], heap[smallest])) {
smallest = leftChild;
}
if (rightChild < heap.size() && CompareTasks()(heap[rightChild], heap[smallest])) {
smallest = rightChild;
}
if (smallest != index) {
swap(heap[index], heap[smallest]);
heapifyDown(smallest);
}
}
public:
void insert(Task task) {
heap.push_back(task);
heapifyUp(heap.size() - 1);
}
Task pop() {
Task minTask = heap.front();
swap(heap.front(), heap.back());
heap.pop_back();
heapifyDown(0);
return minTask;
}
bool isEmpty() {
return heap.empty();
}
};
int main() {
// 创建优先级队列
PriorityQueue pq;
pq.insert(Task(1, 2, 15));
pq.insert(Task(2, 3, 10));
pq.insert(Task(3, 8, 5));
pq.insert(Task(4, 1, 15));
pq.insert(Task(5, 4, 10));
pq.insert(Task(6, 9, 8));
pq.insert(Task(7, 6, 12));
pq.insert(Task(8, 5, 2));
pq.insert(Task(9, 7, 10));
pq.insert(Task(10, 10, 4));
pq.insert(Task(11, 11, 12));
pq.insert(Task(12, 13, 18));
pq.insert(Task(13, 15, 20));
pq.insert(Task(14, 12, 8));
pq.insert(Task(15, 14, 15));
while (!pq.isEmpty()) {
Task task = pq.pop();
double weight = alpha * (1.0 / task.urgency) + beta * (1.0 / task.processingTime);
cout << "Task ID: " << task.id << ", Urgency: " << task.urgency << ", Processing Time: " << task.processingTime << ", Weight: " << weight << endl;
}
return 0;
}
matplotlib绘图代码
import matplotlib.pyplot as plt
import numpy as np
# 任务数量
num_tasks = [1000, 10000, 50000, 100000, 200000, 300000, 400000, 500000, 600000, 700000, 800000, 900000, 910000, 920000, 930000, 940000, 950000, 960000, 970000, 980000, 990000, 1000000]
# 排序时间(秒)
sorting_time = [0.0007655, 0.0039999, 0.0313864, 0.0519806, 0.079248, 0.109885, 0.153507, 0.194126, 0.226293, 0.272727, 0.30183, 0.35148, 0.364976, 0.366271, 0.366893,0.371172, 0.373847, 0.386489, 0.397038, 0.407326, 0.419688, 0.429385]
# 对任务数量取对数
log_num_tasks = np.log10(num_tasks)
# 绘制图形
plt.figure(figsize=(8, 6))
plt.plot(log_num_tasks, sorting_time, marker='o', color='b', linestyle='-', linewidth=2)
# 添加标题和标签
plt.title('Priority Queue Sorting Time ')
plt.xlabel('Log(Number of Tasks)')
plt.ylabel('Sorting Time (seconds)')
# 显示网格线
plt.grid(True, which="both", ls="--")
# 显示图形
plt.show()