在应用 Dijkstra 算法之前根据需要修剪图形是否值得?

发布于 2024-09-15 20:12:29 字数 684 浏览 1 评论 0原文

我在程序中使用 Dijkstra 算法。假设我有一个带有顶点和边的图。如果我们想象从源顶点 "a" 开始的所有边如下

a-->b        
a-->c   and  
a-->d  

,以顶点 "f" 结束的所有边是:

b-->f
m-->f
e-->f
w-->f

我从一开始就需要知道的就是,我希望边缘 a-->b 作为我的起始边缘(假设 "a" 作为起始点),因此不需要搜索其他邻居"a"(a-->c 和 a-->d)

另外我只想要以 m--> 结尾的路径;f (假设 "f" 作为目的地)即我不希望路径包含 b-->f,m-->f,e-- >f,w-->f

那么,修剪我的初始图形使其不包含这些边,然后对其应用 Dijkstra 是个好主意吗?

实际上找到这些边缘需要一些搜索。我想知道是否值得(考虑时间或 CPU 使用情况)进行搜索和修剪我的图表,或者有更好的方法吗?

I am using Dijkstra algorithm in a program. Suppose I have a graph with vertices and edges. If we imagine all the edges starting from source vertex "a" are as below

a-->b        
a-->c   and  
a-->d  

and all the edges ending to vertex "f" are:

b-->f
m-->f
e-->f
w-->f

what I need to know from the beginning is that, I want the edge a-->b as my starting edge (assume "a" as start point) so do not need to search for the other neighbors of "a" i.e. (a-->c and a-->d)

Also I only want the paths which end to m-->f (assume "f" as the destination) i.e. I do not want the path containing b-->f,m-->f,e-->f,w-->f

So is it a good idea to trim my initial graph as such it doesn't contain these edges and then apply Dijkstra on that?

Actually finding these edges needs some searches. I wonder if it is worth (considering time or CPU usage) doing searches and trimming my graph or there is a better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪花飘飘的天空 2024-09-22 20:12:29

为什么不直接搜索从 bm 的路径,然后添加您想要的边呢?如果您确实需要它,您可以添加一个特殊情况来排除包含 af 的边被添加到堆栈中 - 您必须检查是否存在使其整体速度更快,我敢打赌,它会在小图上出现,但不会在真正巨大的图上出现(无论如何,它只会以恒定因子改变速度)。

Why not just search for a path from b to m then and add the edges you want after that? If you really need it, you may add a special case to exclude edges containing a and f from ever being added to the stack -- you'd have to check if that makes it faster overall, my bet is that it will on small graphs but not on really huge ones (it only changes speed by a constant factor anyways).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文