英文:
How priority queue is used with heap to solve min distance
问题
请耐心一点,我对数据结构非常新。
我对如何使用优先队列解决最小距离感到困惑。例如,如果我有一个矩阵,并且想要从源点到目标点找到最小距离,我知道我会执行Dijkstra算法,在这个算法中,我可以使用队列轻松找到源点与矩阵中所有元素之间的距离。
然而,我对在这里如何使用堆 + 优先队列感到困惑。例如,假设我在一个网格上从(1,1)
开始,想要找到到(3,3)
的最小距离,我知道如何实现算法,即找到邻居,检查距离并标记为已访问。但我读到了关于优先队列和最小堆的内容,想要实现它。
目前,我唯一的理解是优先队列有一个键来定位元素。我的问题是,当我插入第一个邻居(1,0),(0,0),(2,1),(1,2)
时,它们是根据一个键(在这种情况下是距离)插入到优先队列中的。所以下一次搜索将是矩阵中具有最短距离的元素。但是使用优先队列时,如何使用堆处理超过2个邻居的情况呢?例如,(1,1)
的子节点就是上面提到的4个邻居。这将违反2*i
、2*i + 1
和i/2
的规则。
总之,我不明白如何使用最小堆 + 优先队列来找到类似距离的最小值。
0 1 2 3
_ _ _ _
0 - |2|1|3|2|
1 - |1|3|5|1|
2 - |5|2|1|4|
3 - |2|4|2|1|
英文:
Please bear with me I am very new to data structures.
I am getting confused how a priroity queue is used to solve min distance. For example if I have a matrix and want to find the min distance from the source to the destination, I know that I would perform Dijkstra algorithm in which with a queue I can easily find the distance between source and all elements in the matrix.
However, I am confused how a heap + priority queue is used here. For example say that I start at (1,1)
on a grid and want to find the min distance to (3,3)
I know how to implement the algorithm in the sense of finding the neighbours and checking the distances and marking as visited. But I have read about priority queues and min heaps and want to implement that.
Right now, my only understanding is a priority queue has a key to position elements. My issue is when I insert the first neighbours (1,0),(0,0),(2,1),(1,2)
they are inserted in the pq based on a key (which would be distance in this case). So then the next search would be the element in the matrix with the shortest distance. But with the pq, how can a heap be used here with more then 2 neighbours? For example the children of (1,1)
are the 4 neighbours stated above. This would go against the 2*i
and 2*i + 1
and i/2
In conclusion, I don't understand how a min heap + priority queue works with finding the min of something like distance.
0 1 2 3
_ _ _ _
0 - |2|1|3|2|
1 - |1|3|5|1|
2 - |5|2|1|4|
3 - |2|4|2|1|
答案1
得分: 1
你需要使用优先队列来获取每次移动中的最小权重,因此最小优先队列(MinPQ)将适用于此情况。
最小优先队列(MinPQ)在内部使用堆的技术来进行元素的插入以及诸如sink()
和swim()
等操作,以使元素保持正确的位置。
因此,最小优先队列(MinPQ)是一种在内部使用堆技术的数据结构。
英文:
You need to use the priority queue to get the minimum weights in every move so the MinPQ will be fit for this.
MinPQ uses internally technique of heap to put the elements in the right position operations such as sink()
swim()
So the MinPQ is the data structure that uses heap technique internally
答案2
得分: 0
如果我正确地理解了您的问题,您在以下这点上遇到了困难:
> 但是对于优先队列(pq),在这种情况下如何使用堆处理超过2个邻居的情况?例如,(1,1)的子节点是上面提到的4个邻居。这似乎与2 * i和2 * i + 1以及i / 2的概念相冲突。
听起来让您困惑的是,这里有两个不同的概念,您可能将它们混合在一起了。首先,有一个概念是“网格中的两个位置可能相邻”。在这个世界中,每个位置最多有四个邻居。接下来,有二叉堆的形状,其中每个节点有两个子节点,其位置由对数组索引进行某些算术计算得出。这两者是完全独立的 - 二叉堆不知道它存储的项来自网格,网格也不知道有一个数组,其中每个节点都有两个子节点,存储在特定的位置。
例如,假设您想要在二叉堆中存储位置(0, 0)、(2, 0)、(-2, 0)和(0, 2),并且这些位置的权重分别为1、2、3和4。那么二叉堆的形状可能如下所示:
(0, 0)
权重 1
/ \
(2, 0) (0, 2)
权重 2 权重 4
/
(0, -2)
权重 3
这个树仍然给每个节点两个子节点;这些子节点只是不一定映射回网格中节点的相对位置。
更一般地说,把优先队列当作一个黑盒子。想象它只是一个可以“存储一些新内容”的魔法设备,以及“我可以为你提供到目前为止最便宜的东西”的魔法设备,就是这样。事实上,它在内部偶然地被实现为一个二叉堆,基本上是不相关的。
希望这能帮助到您!
英文:
If I'm interpreting your question correctly, you're getting stuck at this point:
> But with the pq, how can a heap be used here with more then 2 neighbours? For example the children of (1,1) are the 4 neighbours stated above. This would go against the 2i and 2i + 1 and i/2
It sounds like what's tripping you up is that there are two separate concepts here that you may be combining together. First, there's the notion of "two places in a grid might be next to one another." In that world, you have (up to) four neighbors for each location. Next, there's the shape of the binary heap, in which each node has two children whose locations are given by certain arithmetic computations on array indices. Those are completely independent of one another - the binary heap has no idea that the items its storing come from a grid, and the grid has no idea that there's an array where each node has two children stored at particular positions.
For example, suppose you want to store locations (0, 0), (2, 0), (-2, 0) and (0, 2) in a binary heap, and that the weights of those locations are 1, 2, 3, and 4, respectively. Then the shape of the binary heap might look like this:
(0, 0)
Weight 1
/ \
(2, 0) (0, 2)
Weight 2 Weight 4
/
(0, -2)
Weight 3
This tree still gives each node two children; those children just don't necessarily map back to the relative positions of nodes in the grid.
More generally, treat the priority queue as a black box. Imagine that it's just a magic device that says "you can give me some new thing to store" and "I can give you the cheapest thing you've given be so far" and that's it. The fact that, internally, it coincidentally happens to be implemented as a binary heap is essentially irrelevant.
Hope this helps!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论