矩阵中两个节点之间的最短距离是多少?
我有一个 5x5 矩阵(25 个节点)。有没有一个公式可以找到矩阵中两个节点 i 和 j 之间的最短距离?
注:1 个节点与其邻居之间的距离为 1 个单位。
=================
根据我的观察,这两个节点 i 和 j 之间有许多距离相同的路径 所以我不确定是否有一个公式可以计算最短的?如果有人可以提供帮助,我将不胜感激。谢谢。
例如:
* * * i *
* * * * *
* * * * *
* * * * *
* j * * *
i 和 j 之间的最短距离是 6 个单位。
I have a matrix 5x5 (25 nodes). Is there a formula that I can find the shortest distance between 2 node i and j in the matrix ?
Note: distance between 1 node and its neighbor is 1 unit.
=================
In my observation, there are many paths with the same distance between those 2 nodes i and j
so i'm not sure if there is a formula to calculate the shortest one? I appreciate if anybody can help. Thanks.
Ex:
* * * i *
* * * * *
* * * * *
* * * * *
* j * * *
Shortest distance between i and j is 6 units.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信您需要的是
L1
距离,也称为 曼哈顿距离。因此,如果您的两个节点具有矩阵索引(i1,j1)
和(i2,j2)
,那么它们之间的最短距离为|i1-i2|+ |j1-j2|
。当然,这是假设您不能沿对角线移动。
I believe what you need is the
L1
distance, also called the Manhattan distance. So if your two nodes have matrix indices(i1,j1)
and(i2,j2)
, then the shortest distance between them is|i1-i2|+|j1-j2|
.This is of course, assuming you can't move diagonally.
我认为正常的勾股定理就可以了。获取您所在位置与您想去的位置之间的 X、Y 差异;这会给你一个负值或正值。由此,您应该能够根据需要向上/向下左/右移动,直到位于同一行/列。无法弄清楚如何获取上标;但这会起作用。
I would think that the normal Pythagorean theorem would work just fine. Get the X,Y difference between where you are and where you want to go; this will give you a negative or positive value. From this you should be able to move up/down left/right as needed until you are in the same row/column. Couldn't figure out how to get superscript; but this will work.
看看度量空间
Take a look at Metric Space