计算二维网格中哈密顿路径总数的剪枝策略

发布于 2024-11-15 21:28:22 字数 516 浏览 4 评论 0原文

我最近试图计算出哈密顿路径的总数(基本上从起始顶点开始,访问每个节点一次并到达结束顶点)。蛮力 dfs 在 7x8 的中等大小的网格上进行长时间的行走。我想出了其中一些修剪策略。这些修剪技术的目标是不应该进一步扩展不能是哈密顿路径的部分路径。

DFS算法: 从起始顶点开始,访问其邻居节点并递归。记录访问的节点数,一旦到达结束顶点,检查访问的节点总数是否与网格中的节点总数相同。这将是指数复杂度,因为对于每个顶点,您可以朝 4 个方向移动,其复杂度为 O(4^n)。因此,重点应该是尽快拒绝一条路径,而不是等到到达终点顶点。

剪枝技巧:

1)对于给定的部分路径,检查剩余图是否连通。如果不是,那么该部分路径最终不能成为哈密顿路径。

2) 对于给定的部分路径,每个尚未访问的节点必须至少具有2度,使得一个邻居节点可以用于进入该节点,而其他邻居节点可以用于退出。

我想知道这些修剪技术可以节省多少时间。另外,我是否缺少一些非常重要的修剪技术,因为我的速度提升并不显着。

提前致谢 !!!

I was recently trying to come up with the total number of hamiltonian paths (basically starting for start vertex, visit each node exaactly once and reach the end vertex). Brute force dfs goes for a long walk on a moderate sized grid of 7x8. I came up with some of these prunning strategies. The goal of these pruning techniques is that a partial path which cannot be a hamiltonian path should not be extended further.

DFS Algorithm:
Start for the start vertex, visit its neighbor nodes and recurse. Keep a count of the number of nodes visited and once reaching the end vertex, check if the totla visited nodes is same as the total nodes in the grid. This is going to be exponential complexity as for each vertex you can go in 4 directions, which will be of O(4^n). So focus should be to reject a path as soon as possible and not wait until reaching the end vertex.

Pruning Techniques:

1) For a given partial path, check if the remaining graph is connected. If it is not, then this partial path cannot end up to be a hamiltonian path.

2) For a given partial path, each yet-to-be-visited node must have atleast 2 degree such that one neighbor node could be used to enter this node and other other neighbor is used to exit.

I was wondering how much these pruning techniques could save time. Also am I missing some very improtant pruning techinque because my speed up is not significant.

Thanks in Advance !!!

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

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

发布评论

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

评论(1

天冷不及心凉 2024-11-22 21:28:22

有一个适用于一般图的 O(n^2 * 2^n) 解决方案。该结构与此处描述的 O(2^n * n^2) 算法相同,

http:// /www.algorithmist.com/index.php/Traveling_Salesperson_Problem

除了记录最小距离之外,您还记录计数。

除此之外,你可以做的任何修剪仍然会有帮助。

There is an O(n^2 * 2^n) solution that works for general graphs. The structure is identical to the O(2^n * n^2) algorithm described here,

http://www.algorithmist.com/index.php/Traveling_Salesperson_Problem

Except rather than recording minimum distances, you are recording counts.

Any pruning you can do on top of that will still help.

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