Java优先级队列6,优先级队列 - Java实现

package lhz.algorithm.chapter.six;

/**

* "优先级队列",《算法导论》6.5章节

* 原文摘要:

* A priority queue is a data structure for maintaining a set S of elements, each with an

* associated value called a key. A max-priority queue supports the following operations.

* ・ INSERT(S, x) inserts the element x into the set S. This operation could be written as S← S{x}.

* ・ MAXIMUM(S) returns the element of S with the largest key.

* ・ EXTRACT-MAX(S) removes and returns the element of S with the largest key.

* ・ INCREASE-KEY(S, x, k) increases the value of element x‘s key to the new value k,

* which is assumed to be at least as large as x‘s current key value.

* 堆的一种实际应用,可用于任务调度队列等。包含四个操作方法。

* @author lihzh(OneCoder)

* @blog http://www.coderli.com

*/

public class PriorityQueue {

private static final int DEFAULT_CAPACITY_VALUE = 16;

//初始化一个长度为16的队列(可作为构造参数),此处选择16参考HashMap的初始化值

private int[] queue;

//堆大小

private int heapSize = 0;

public static void main(String[] args) {

PriorityQueue q = new PriorityQueue();

q.insert(2);

q.insert(6);

q.insert(3);

q.insert(8);

q.insert(7);

q.insert(9);

q.insert(1);

q.insert(10);

q.insert(9);

System.out.println(q.extractMax());

System.out.println(q.extractMax());

q.insert(9);

q.insert(1);

q.insert(10);

System.out.println(q.extractMax());

System.out.println(q.extractMax());

System.out.println(q.extractMax());

System.out.println(q.extractMax());

System.out.println(q.extractMax());

System.out.println(q.extractMax());

System.out.println(q.extractMax());

}

public PriorityQueue(int capacity) {

this.queue = new int[capacity];

}

public PriorityQueue() {

this(DEFAULT_CAPACITY_VALUE);

}

/**

* 返回当前最大值

* @return

*/

public int maximum() {

return queue[0];

}

/**

* 往优先级队列出,插入一个元素

* 利用INCREASE-Key方法,从堆的最后增加元素

* 伪代码:

* MAX-HEAP-INSERT(A, key)

* 1 heap-size[A] ← heap-size[A] + 1

* 2 A[heap-size[A]] ← -∞

* 3 HEAP-INCREASE-KEY(A, heap-size[A], key)

* 时间复杂度:O(lg n)

* @param value 待插入元素

*/

public void insert(int value) {

//注意堆容量和数组索引的错位 1

queue[heapSize] = value;

heapSize++;

increaceKey(heapSize, value);

}

/**

* 增加给定索引位元素的值,并重新构成MaxHeap。

* 新值必须大于等于原有值

* 伪代码:

* HEAP-INCREASE-KEY(A, i, key)

* 1 if key < A[i]

* 2 then error "new key is smaller than current key"

* 3 A[i] ← key

* 4 while i > 1 and A[PARENT(i)] < A[i]

* 5 do exchange A[i] ↔ A[PARENT(i)]

* 6 i ← PARENT(i)

* 时间复杂度:O(lg n)

* @param index 索引位

* @param newValue 新值

*/

public void increaceKey (int heapIndex, int newValue) {

if (newValue < queue[heapIndex-1]) {

System.err.println("错误:新值小于原有值!");

return;

}

queue[heapIndex-1] = newValue;

int parentIndex = heapIndex / 2;

while (parentIndex > 0 && queue[parentIndex-1] < newValue ) {

int temp = queue[parentIndex-1];

queue[parentIndex-1] = newValue;

queue[heapIndex-1] = temp;

heapIndex = parentIndex;

parentIndex = parentIndex / 2;

}

}

/**

* 返回堆顶元素(最大值),并且将堆顶元素移除

* 伪代码:

* HEAP-EXTRACT-MAX(A)

* 1 if heap-size[A] < 1

* 2 then error "heap underflow"

* 3 max ← A[1]

* 4 A[1] ← A[heap-size[A]]

* 5 heap-size[A] ← heap-size[A] - 1

* 6 MAX-HEAPIFY(A, 1)

* 7 return max

* 时间复杂度:O(lg n),

* @return

*/

public int extractMax() {

if (heapSize < 1) {

System.err.println("堆中已经没有元素!");

return -1;

}

int max = queue[0];

queue[0] = queue[heapSize-1];

heapSize--;

maxHeapify(queue, 1);

return max;

}

/**

* 之前介绍的保持最大堆的算法

* @param array

* @param index

*/

private void maxHeapify(int[] array, int index) {

int l = index * 2;

int r = l + 1;

int largest;

//如果左叶子节点索引小于堆大小,比较当前值和左叶子节点的值,取值大的索引值

if (l <= heapSize && array[l-1] > array[index-1]) {

largest = l;

} else {

largest = index;

}

//如果右叶子节点索引小于堆大小,比较右叶子节点和之前比较得出的较大值,取大的索引值

if (r <= heapSize && array[r-1] > array[largest-1]) {

largest = r;

}

//交换位置,并继续递归调用该方法调整位置。

if (largest != index) {

int temp = array[index-1];

array[index-1] = array[largest-1];

array[largest-1] = temp;

maxHeapify(array, largest);

}

}

public int[] getQueue() {

return queue;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目标检测(Object Detection)是计算机视觉领域的一个核心问题,其主要任务是找出图像中所有感兴趣的目标(物体),并确定它们的类别和位置。以下是对目标检测的详细阐述: 一、基本概念 目标检测的任务是解决“在哪里?是什么?”的问题,即定位出图像中目标的位置并识别出目标的类别。由于各类物体具有不同的外观、形状和姿态,加上成像时光照、遮挡等因素的干扰,目标检测一直是计算机视觉领域最具挑战性的任务之一。 二、核心问题 目标检测涉及以下几个核心问题: 分类问题:判断图像中的目标属于哪个类别。 定位问题:确定目标在图像中的具体位置。 大小问题:目标可能具有不同的大小。 形状问题:目标可能具有不同的形状。 三、算法分类 基于深度学习的目标检测算法主要分为两大类: Two-stage算法:先进行区域生成(Region Proposal),生成有可能包含待检物体的预选框(Region Proposal),再通过卷积神经网络进行样本分类。常见的Two-stage算法包括R-CNN、Fast R-CNN、Faster R-CNN等。 One-stage算法:不用生成区域提议,直接在网络中提取特征来预测物体分类和位置。常见的One-stage算法包括YOLO系列(YOLOv1、YOLOv2、YOLOv3、YOLOv4、YOLOv5等)、SSD和RetinaNet等。 四、算法原理 以YOLO系列为例,YOLO将目标检测视为回归问题,将输入图像一次性划分为多个区域,直接在输出层预测边界框和类别概率。YOLO采用卷积网络来提取特征,使用全连接层来得到预测值。其网络结构通常包含多个卷积层和全连接层,通过卷积层提取图像特征,通过全连接层输出预测结果。 五、应用领域 目标检测技术已经广泛应用于各个领域,为人们的生活带来了极大的便利。以下是一些主要的应用领域: 安全监控:在商场、银行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值