优先队列的实现

优先队列的实现是基于最大/最小堆的,队列的特性是先入先出(FIFO)。

最大优先队列指的是不管入队顺序如何,队列中的最大元素先出列。

最小优先队列指的是不管入队顺序如何,队列中的最小元素先出列。

//以最大优先队列为例
public class PriorityQueue {
    private int[] array;
    private int size;

    public PriorityQueue(int capacity) {
        this.array = new int[capacity];
        this.size = 0;
    }

    public static void main(String[] args) throws Exception {
        //最大优先队列
        PriorityQueue priorityQueue = new PriorityQueue(6);
        priorityQueue.enQueue(5);
        priorityQueue.enQueue(1);
        priorityQueue.enQueue(2);
        priorityQueue.enQueue(6);
        priorityQueue.enQueue(9);
        priorityQueue.enQueue(3);

        System.out.print(priorityQueue.deQueue() + " ");
        System.out.print(priorityQueue.deQueue() + " ");
        System.out.print(priorityQueue.deQueue() + " ");
        System.out.print(priorityQueue.deQueue() + " ");
        System.out.print(priorityQueue.deQueue() + " ");
        System.out.print(priorityQueue.deQueue() + " ");
    }

    public void enQueue(int element) {
        if (size >= array.length) {
            resize();
        }

        array[size++] = element;
        //最大堆的插入操作
        upAdjust();
    }

    //通过不断‘上升’的方式构造二叉堆
    public void upAdjust() {
        int childIndex = size - 1;
        int parentIndex = (childIndex - 1) / 2;
        int temp = array[childIndex];
        while (childIndex > 0 && temp > array[parentIndex]) {
            array[childIndex] = array[parentIndex];
            childIndex = parentIndex;
            parentIndex = (childIndex - 1) / 2;
        }
        array[childIndex] = temp;
    }

    public int deQueue() throws Exception {
        if (size <= 0) {
            throw new Exception("queue is empty");
        }

        int element = array[0];
        array[0] = array[--size];
        //最大堆的删除操作
        downAdjust();
        return element;
    }

    public void downAdjust() {
        int parentIndex = 0;
        int childIndex = 1;
        int temp = array[parentIndex];

        while (childIndex < size) {
            if (childIndex + 1 < size && array[childIndex] < array[childIndex + 1]) {
                childIndex++;
            }

            if (temp >= array[childIndex]) {
                break;
            }

            array[parentIndex] = array[childIndex];
            parentIndex = childIndex;
            childIndex = 2 * parentIndex + 1;
        }
        array[parentIndex] = temp;
    }

    public void resize() {
        int[] newArray = new int[array.length * 2];
        System.arraycopy(array, 0, newArray, 0, array.length);
        array = newArray;
    }
}

 

优先队列实现Prim算法的思想是每次选择权重最小的边来构建最小生成树。下面是使用优先队列实现Prim算法的步骤: 1. 创建一个优先队列,并定义一个自定义的比较器,用于比较边的权重。比较器应该使得权重最小的边在队列的前面。 2. 初始化一个布尔数组vIsChoose,用于标记顶点是否已经被选择。 3. 随机选择一个顶点作为初始节点,并将其标记为已选择。 4. 将初始节点的所有相邻边加入优先队列。 5. 进入循环,直到优先队列为空或者已选择的顶点数等于图的顶点数减一(最小生成树的边数等于顶点数减一)为止。 6. 从优先队列中取出权重最小的边,如果该边连接的顶点未被选择,则将该边加入最小生成树,并将该顶点标记为已选择。 7. 将新选择的顶点的所有相邻边加入优先队列。 8. 重复步骤6和步骤7,直到循环结束。 9. 最终得到的最小生成树即为Prim算法的结果。 下面是一个使用优先队列实现Prim算法的Python代码示例: ```python import heapq def prim(graph): n = len(graph) # 图的顶点数 visited = [False] * n # 记录顶点是否已经被选择 min_span_tree = [] # 最小生成树的边集合 pq = [] # 优先队列,用于存储边 # 选择初始节点 start_node = 0 visited[start_node] = True # 将初始节点的所有相邻边加入优先队列 for neighbor, weight in graph[start_node]: heapq.heappush(pq, (weight, start_node, neighbor)) # 循环直到优先队列为空或者已选择的顶点数等于图的顶点数减一 while pq and len(min_span_tree) < n - 1: weight, u, v = heapq.heappop(pq) # 如果边连接的顶点未被选择,则将该边加入最小生成树 if not visited[v]: visited[v] = True min_span_tree.append((u, v, weight)) # 将新选择的顶点的所有相邻边加入优先队列 for neighbor, weight in graph[v]: heapq.heappush(pq, (weight, v, neighbor)) return min_span_tree # 示例图的邻接表表示 graph = [ [(1, 2), (2, 3)], # 顶点0的相邻边:(1, 2)和(2, 3) [(0, 2), (2, 4), (3, 3)], # 顶点1的相邻边:(0, 2)、(2, 4)和(3, 3) [(0, 3), (1, 4), (3, 5)], # 顶点2的相邻边:(0, 3)、(1, 4)和(3, 5) [(1, 3), (2, 5)] # 顶点3的相邻边:(1, 3)和(2, 5) ] min_span_tree = prim(graph) print(min_span_tree) # 输出最小生成树的边集合 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值