Heaps and PQs(The Priority Queue) | CS 61B Data Structures, Spring 2019

公开课 CS61B Data Structures, UCB CS61B Data Structures, UCB(1) Primitive Types Each Java type has a different way to interpret the bits: eg. 72 stored as 01001000 Letter H stored as 01001000 primitive types in Java byte short int lo... 阅读详情

Heaps and PQs(The Priority Queue)

The Priority Queue Interface

If we want to cared more about quickly finding the smallest or largest element instead of quickly searching

Now we come to the Abstract Data Type of a Priority Queue. To understand this ADT, consider a bag of items. You can add items to this bag, you can remove items from this bag, etc. The one caveat is that you can only interact with the smallest items of this bag.

/** (Min) Priority Queue: Allowing tracking and removal of the
  * smallest item in a priority queue. */
public interface MinPQ<Item> {
	/** Adds the item to the priority queue. */
	public void add(Item x);
	/** Returns the smallest item in the priority queue. */
	public Item getSmallest();
	/** Removes the smallest item from the priority queue. */
	public Item removeSmallest();
	/** Returns the size of the priority queue. */
	public int size();
}

Summary

  • Priority Queue is an Abstract Data Type that optimizes for handling minimum or maximum elements.
  • There can be space/memory benefits to using this specialized data structure.
  • Implementations for ADTs that we currently know don’t give us efficient runtimes for PQ operations(优先队列).

举例子,对于查找队列中最小的值。最大的值等等:

Ordered Array:
1. add : Θ(N)
2. getSmallest : Θ(1)
3. removeSmallest : Θ(N)
Bushy BST :
1. add : Θ(logN)
2. getSmallest : Θ(logN)
3. removeSmallest : Θ(logN)
HashTable
1. add : Θ(1)
2. getSmallest : Θ(N)
3. removeSmallest : Θ(N)

  • A binary search tree among the other structures is the most efficient

Heap Structure

We will define our binary min-heap as being complete and obeying min-heap property:

  • Min-heap: Every node is less than or equal to both of its children
  • Complete: Missing items only at the bottom level (if any), all nodes are as far left as possible.

在这里插入图片描述

在这里插入图片描述

Heap Operations

The three methods we care about for the PriorityQueue ADT are add , getSmallest , and removeSmallest . We will start by conceptually describing how these methods can be implemented given our given schema of a heap.

  1. add : Add to the end of heap temporarily. Swim up the hierarchy to the proper place. Swimming involves swapping nodes if child < parent

  2. getSmallest : Return the root of the heap (This is guaranteed to be the minimum by our min-heap property)

  3. removeSmallest : Swap the last item in the heap into the root. Sink down the hierarchy to the proper place. Sinking involves swapping nodes if parent > child. Swap with the smallest child to preserve min-heap property.

动画演示

Tree Representation

Approach 1a, 1b, and 1c

We will create mappings between nodes and their children

  • In approach Tree1A, we consider creating pointers to our children and storing the value inside of the node object. These are hardwired links that give us fixed-width nodes. We can observe the code:
public class Tree1A<Key> {
   Key k; // e.g. 0
   Tree1A left;
   Tree1A middle;
   Tree1A right;
   ...

在这里插入图片描述

  • Alternatively, in Tree1B, we explore the use of arrays as representing the mapping between children and nodes. This would give us variable-width nodes, but also awkward traversals and performance will be worse.
public class Tree1B<Key> {
   Key k; // e.g. 0
   Tree1B[] children;
   ...

  • Lastly, we can use the approach for Tree1C. This will be slightly different from the usual approaches that we’ve seen. Instead of only representing a node’s children, we say that nodes can also maintain a reference to their siblings
public class Tree1C<Key> {
   Key k; // e.g. 0
   Tree1C favoredChild;
   Tree1C sibling;
   ...

在这里插入图片描述

在这里插入图片描述

Approach 2

Recall the Disjoint Sets ADT. The way that we represented this Weighted Quick Union structure was through arrays. For representing a tree, we can store the keys array as well as a parents array. The keys array represent which index maps to which key, and the parents array represents which key is a child of another key.

public class Tree2<Key> {
   Key[] keys;
   int[] parents;
   ...

在这里插入图片描述
It’s time to make a very important observation! Based on the structure of the tree and the relationship between the array representations and the diagram of the tree, we can see:

  1. The tree is complete. This is a property we have defined earlier.
  2. The parents array has a sort of redundant pattern where elements are just doubled.
  3. Reading the level-order of the tree, we see that it matches exactly the order of the keys in the keys array.

: Store keys in an array. Don’t store structure anywhere.

  • To interpret array: Simply assume tree is complete.
  • Obviously only works for “complete” trees.
    在这里插入图片描述
Challenge: Write the parent(k) method for approach 3.

在这里插入图片描述

public int parent(int k) {
    return (k - 1) / 2;
}
Approach 3bApproach 3

Comparing to alternative implementations for The Priority Queue

在这里插入图片描述

  • Heap operations are amortized analysis, since the array will have to resize (not a bigdeal)
  • BST’s can have constant time getSmallest if pointer is stored to smallest element
  • Array-based heaps take around 1/3rd the memory it takes to represent a heap using approach 1A (direct pointers to children)

Code for MinPQ interface

/** (Min) Priority Queue: Allowing tracking and removal of the
  * smallest item in a priority queue. */
public interface MinPQ<Item> {
	/** Adds the item to the priority queue. */
	public void add(Item x);
	/** Returns the smallest item in the priority queue. */
	public Item getSmallest();
	/** Removes the smallest item from the priority queue. */
	public Item removeSmallest();
	/** Returns the size of the priority queue. */
	public int size();
}

B-Trees|CS 61B Data Structures, Spring 2019 B-Trees 阅读详情

相关推荐

大型国有银行应用系统全链路性能劣化智能根因定位与自愈Agent平台详细设计方案.docx

大型国有银行应用系统全链路性能劣化智能根因定位与自愈Agent平台详细设计方案.docx

Red Black Trees | CS 61B Data Structures, Spring 2019

Red Black Trees | CS 61B Data Structures, Spring 2019 reading:11.4, 11.5, Algs 424-431, 432-448 (extra) Why we need Red black Trees Wonderfully balanced as they are, B-Trees are really difficult to im...

yaoxinJJJ的博客 857

母婴行业智能体开发平台大模型应用方案.docx

母婴行业智能体开发平台大模型应用方案.docx

Hashing | CS 61B Data Structures, Spring 2019

Hashing reading: 12.1, 12.2, 12.3, 12.4, 12.5, Algs 458-468, 478-479, 468-475 (extra) Lecture 19: Hashing Set Implementations, DataIndexedIntegerSet Integer Representations of Strings, Integer Overfl...

yaoxinJJJ的博客 688

钢铁行业可信数据空间AI大模型服务平台建设方案.docx

钢铁行业可信数据空间AI大模型服务平台建设方案.docx

【深度学习开发环境配置】基于PyTorch的计算机视觉项目依赖管理:requirements.txt文件解析与版本控制方案

内容概要:该文件为一个Python项目的依赖包配置文件(requirements.txt),列出了项目运行所需的软件包及其最低版本要求。主要包括深度学习框架PyTorch及其视觉扩展 torchvision、科学计算库 numpy 和 scipy、数据处理工具 pandas、机器学习库 scikit-learn、图像处理库 opencv-python 和 Pillow,以及数据可视化工具 matplotlib 和 seaborn 等。该配置确保项目在不同环境中具备一致的运行条件。; 适合人群:从事深度学习、计算机视觉或数据科学相关工作的开发者与研究人员,熟悉Python环境管理的技术人员; 使用场景及目标:①用于搭建项目开发环境,确保依赖一致性;②支持模型训练、图像处理、数据分析等任务的环境准备;③便于项目共享与部署,降低环境配置成本; 阅读建议:在实际使用时,建议通过 pip install -r requirements.txt 安装依赖,并根据具体硬件环境适当调整版本兼容性,尤其注意 PyTorch 与 CUDA 版本的匹配。

Prefix Operations and Tries | CS 61B Data Structures, Spring 2019

Data Structures Summary The problem we are presented: Given a stream of data, retrieve(搜索) information of interest. All of the data structures we have discussed so far have been to solve the search pr...

yaoxinJJJ的博客 665

顶刊复现改进优化反演控制的水面舰艇自适应跟踪控制程序

内容概要:本文档介绍了名为“【顶刊复现】改进优化反演控制的水面舰艇自适应跟踪控制程序”的Matlab仿真资源,聚焦于水面舰艇与无人船在复杂海洋环境下的高精度路径跟踪与运动控制问题。该资源基于先进的非线性控制理论,如反演控制(Backstepping)、李雅普诺夫稳定性理论和自适应控制方法,构建了具备强鲁棒性和抗干扰能力的控制器,能够有效应对模型不确定性、外部扰动及环境变化。文档还列举了多项相关Matlab仿真项目,涵盖多智能体协同控制、事件触发通信、模糊PID控制、模型预测控制(NMPC)、滑模控制等多种前沿控制策略,充分体现了其在智能船舶控制、海洋工程自动化和无人系统领域的高水平学术价值与实际应用潜力。; 适合人群:具备自动控制理论基础和Matlab编程能力的科研人员、控制科学与工程及相关专业的研究生,以及从事无人船、水面航行器、水下机器人等智能航海装备控制系统开发的工程技术人员。; 使用场景及目标:① 复现顶级期刊(如SCI一区)中关于水面舰艇或无人船的先进控制算法,支撑高水平学术论文的研究与撰写;② 作为高校课程或科研实验的教学案例,开展非线性系统控制、自适应控制、轨迹跟踪等方向的教学与实践;③ 为智能船舶、海上无人集群系统等实际工程项目提供算法验证、性能测试与仿真优化的技术平台。; 阅读建议:此资源以Matlab代码为核心载体,建议使用者结合现代控制理论教材与原始参考文献,深入理解反演控制与自适应机制的设计逻辑与数学推导,并通过修改参数、调整初始条件和扰动设置等方式调试仿真程序,全面掌握算法的实现细节与性能边界,实现从理论分析到仿真实践的完整闭环。

clab8 总结|CS 61B Data Structures, Spring 2019

实战总结 Lab 8: HashMap /****************************************************************************** * Compilation: javac SeparateChainingHashST.java * Execution: java SeparateChainingHashST &l...

yaoxinJJJ的博客 639

电气数据大规模复杂配电网故障溯源数据集

内容概要:本文档介绍了《大规模复杂配电网故障溯源数据集》这一面向电力系统领域的专业技术资源,聚焦于现代配电网在高比例分布式电源接入、源荷不确定性等复杂运行条件下的故障诊断与溯源问题。资源提供了基于Matlab/Simulink平台构建的大规模、高复杂度仿真模型与真实或仿真数据集,涵盖IEEE标准测试系统(如IEEE 33、IEEE 9节点)及实际配网拓扑,支持对故障定位、异常检测、网络重构、韧性提升、应急电源调度等关键课题的深入研究。文档列举了多项配套技术案例,如配电网故障重构、状态估计、分布式电源承载力评估、应急移动电源动态调度等,突出了在新型电力系统背景下故障响应机制与智能决策算法的研发需求,适用于开展高水平科研与工程验证。; 适合人群:电气工程、自动化、能源系统及相关专业的高校研究生、博士生、科研机构研究人员,以及从事智能电网、配网自动化、故障诊断与电力系统仿真的工程技术人员。; 使用场景及目标:① 支撑复杂配电网中故障传播路径分析与根源定位算法的开发与验证;② 服务于配电网韧性评估、故障恢复策略优化、网络重构与应急调度等前沿课题的数据需求;③ 结合Matlab/Simulink环境进行电力系统建模、仿真与控制策略设计;④ 为高水平学术论文撰写、科研项目申报及实验平台搭建提供高质量数据支撑与案例参考。; 阅读建议:建议结合文档中提及的“荔枝科研社”资源平台获取完整的数据集与配套代码,根据具体研究方向筛选匹配的仿真模型与案例,优先关注与自身课题相关的故障场景与算法实现,并利用Matlab工具进行复现实验与算法性能对比分析。

实现labelme批量json_to_dataset方法

labelme可以帮助我们快速的实现Mask-RCNN中数据集json文件的生成,然而还需要我们进一步的将json转成dataset,可以直接在cmd中执行labelme_json_to_dataset.exe C:\Users\Administrator\Desktop\total\1.json(路径),但是这个过程需要我们一个json文件的生成,过程很慢。 一、打开abelm...

简简单单 5万+

Lecture 25: Shortest Paths| CS 61B Data Structures, Spring 2019

Categories Summary So Far: DFS vs. BFS Dijkstra’s Algorithm Dijkstra’s Correctness and Runtime A* A* Heuristics (188 preview) demo1 demo2 >As we discussed last time, BFS would not be a good ch...

yaoxinJJJ的博客 516

【仓颉 + 鸿蒙 + AI Agent】CangjieMagic框架(3):Agent请求和响应

CangjieMagic框架:使用华为仓颉编程语言编写,专门用于开发AI Agent,支持鸿蒙、Windows、macOS、Linux等系统。这篇文章剖析一下 CangjieMagic 框架中的 AgentRequest 和 AgentResponse。

九丘教育官方博客 1483

Lecture 26: Minimum Spanning Trees | cs61b 2019

Lecture 26: Minimum Spanning Trees MST, Cut Property, Generic MST Algorithm Prim’s Algorithm Kruskal’s Algorithm Spanning Trees MST vs. SPT A shortest paths tree depends on the start vertex: Becau...

yaoxinJJJ的博客 463

Sentinel-1 Burst数据处理避坑实录:从aria2c报错到wget脚本救场

本文详细介绍了Sentinel-1 Burst数据处理的完整解决方案,从下载困境到高效处理技巧。针对aria2c报错等常见问题,提供了增强版wget脚本和智能分组方法,显著提升下载成功率和处理效率,特别适合科研机构和企业级网络环境下的哨兵数据处理需求。

weixin_42641998的博客 287

从踩坑到填平:我在RHEL7上给Tesla A100装驱动的完整记录(含Fabric Manager配置)

本文详细记录了在RHEL7系统上为Tesla A100安装Nvidia驱动的完整过程,包括Fabric Manager配置的关键步骤。通过分析A100架构的特殊性,提供了从驱动安装到服务配置的详细指南,并分享了常见问题的排查方法和性能调优技巧,帮助用户高效部署高性能GPU计算环境。

weixin_29291863的博客 291

Lecture 23: Graphs and Traversals | cs61b 2019

Lecture 23: Graphs and Traversals Tree Traversals Graphs Depth First Search Breadth First Search Trees and Traversals Tree Definition A tree consists of: A set of nodes. A set of edges that connect...

yaoxinJJJ的博客 452

GB28181协议心跳机制:从标准解读到代码实现的保活策略

本文深入解析GB28181协议中的心跳机制,从标准解读到代码实现,详细介绍了保活策略的核心价值和底层逻辑。通过实战案例和技术要点,帮助开发者优化心跳间隔、超时次数等参数配置,提升安防监控系统的可靠性和性能。文章还提供了代码实现的防坑指南和特殊场景处理建议,适用于各类网络环境下的设备在线状态管理。

weixin_29611239的博客 114

Lecture 24: Graphs II: Graph Traversal Implementations |cs61b 2019

Lecture 24: Graphs II: Graph Traversal Implementations Tree and Graph Traversals Just as there are many tree traversals: Preorder: DBACFEG Inorder: ABCDEFG Postorder: ACBEGFD Level order: DBFACEG g...

yaoxinJJJ的博客 424

bash: huggingface-cli: command not found... 解决方案

本文主要介绍了bash: huggingface-cli: command not found… 解决方案,希望能对使用大模型的同学们有所帮助。 文章目录 1. 问题描述 2. 解决方案

weixin_43178406的博客 5万+

YOLOv11知识蒸馏实战:让小模型拥有大模型90%精度,速度提升3倍的终极教程

在self.alpha = alpha # 蒸馏损失权重self.temperature = temperature # 温度系数,控制分布平滑度# 输出分布蒸馏(KL散度损失)# 中间特征蒸馏(MSE损失,对齐中间层特征)# 总蒸馏损失知识蒸馏是YOLOv11在“小模型高性能部署”场景的“破局之法”——它让小模型能“站在大模型的肩膀上”,兼具速度与精度。从损失函数定义到多阶段训练,再到实战落地,本教程已为你铺就了一条清晰的路径。

m0_64661770的博客 804

Decomposition and Reductions|cs61b 2019 lec 27 |图总结

Graph Problems So Far DepthFirstPaths.java BreadthFirstPaths.java DijkstrasSP.java A*: Same as Dijkstra’s but with h(v, goal) added to priority of each vertex. LazyPrimMST.java PrimMST.java KruskalM...

yaoxinJJJ的博客 414

为YOLOv11引入加权双向特征金字塔(BiFPN)

本文介绍了将BiFPN应用于YOLOv11模型以提升边缘设备上小目标检测精度的实践。针对传统FPN在特征融合时的信息丢失问题,采用带可学习权重的BiFPN结构进行改造,实现了不同层级特征的动态融合。重点阐述了WeightedFeatureFusion模块的实现细节、neck结构改造方法及训练部署注意事项。实验表明,该改进使小目标AP提升3.1%,参数量仅增加5%,在精度和效率间取得良好平衡。文章还分享了权重初始化、归一化处理等实用技巧,并强调需根据数据特性评估改进效果,重新计算anchor等关键点。

as1225120794的博客 356

Range Searching and Multi-Dimensional Data|Quadtrees|K-d Trees|CS 61B Data Structures, Spring 2019

Multi-Dimensional Data Motivation: 2D Range Finding and Nearest Neighbors Suppose we want to perform operations on a set of Body objects in space? 2D Range Searching: How many objects are in the high...

yaoxinJJJ的博客 369

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

HW 3: Hashing| CS 61B Data Structures, Spring 2019

equals As mentioned in class, writing a proper equals method is a little tricker than it might sound at first blush. According to the Java language specification, your equals method should have the fo...

yaoxinJJJ的博客 308

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

上一篇: Hashing | CS 61B Data Structures, Spring 2019
下一篇: Prefix Operations and Tries | CS 61B Data Structures, Spring 2019
yaoxinJJJ
博客等级 码龄7年 5粉丝 35原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值