汉密尔顿路径(哈密顿路径)解析
转自:汉密尔顿路径(哈密顿路径)解析
汉密尔顿路径(哈密顿路径)
哈密顿路径也称作哈密顿链,指在一个图中沿边访问每个顶点恰好一次的路径。寻找这样的一个路径是一个典型的NP-完全(NP-complete)问题。后来人们也证明了,找一条哈密顿路的近似比为常数的近似算法也是NP完全的.
算法思路(寻找图中所有的哈密顿路)
- 首先用一个邻接矩阵存储图
- 将每一个顶点作为起点,查找哈密顿路
- 查找哈密顿路的思路:采用递归遍历的方式在递归的搜索的过程中同时需要不断的修改边可以链接的状态。0不可以链接 1 可以链接。
findHamiltonpath(int[][] M, int x, int y, int l) {int i;for (i = x; i < len; i++) { // Go through rowif (M[i][y] != 0) { // 2 point connectif (detect(path, i + 1))// if detect a point that already in the// path => duplicatecontinue;l++; // Increase path length due to 1 new point is connectedpath[l] = i + 1; // correspond to the array that start at 0,// graph that start at point 1if (l == len - 1) {// Except initial point already count// =>success connect all pointcount++;if (count == 1)System.out.println("Hamilton path of graph: ");display(path);l--;continue;}M[i][y] = M[y][i] = 0; // remove the path that has been get andfindHamiltonpath(M, 0, i, l); // recursively start to find new// path at new end pointl--; // reduce path length due to the failure to find new pathM[i][y] = M[y][i] = 1; // and tranform back to the inital form// of adjacent matrix(graph)}}path[l + 1] = 0; // disconnect two point correspond the failure to find// the..}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 找到图中所有的哈密顿路径并且打印。
算法Java源码
/*** @Author PaulMrzhang* @Version 2016年11月21日 下午6:59:19* @DESC 说明:这份代码是同时给我的,在这里感谢代码的原作者!*/
public class HamiltonPath {public static void main(String[] args) {HamiltonPath obj = new HamiltonPath();int[][] x = {{ 0, 1, 0, 1, 0 }, // Represent the graphs in the adjacent// matrix forms{ 1, 0, 0, 0, 1 }, { 0, 0, 0, 1, 0 }, { 1, 0, 1, 0, 1 },{ 0, 1, 0, 1, 0 } };int[][] y = { { 0, 1, 0, 0, 0, 1 }, { 1, 0, 1, 0, 0, 1 },{ 0, 1, 0, 1, 1, 0 }, { 0, 0, 1, 0, 0, 0 },{ 0, 0, 1, 0, 0, 1 }, { 1, 1, 0, 0, 1, 0 } };int[][] z = { { 0, 1, 1, 0, 0, 1 }, { 1, 0, 1, 0, 0, 0 },{ 1, 1, 0, 1, 0, 1 }, { 0, 0, 1, 0, 1, 0 },{ 0, 0, 0, 1, 0, 1 }, { 1, 0, 1, 0, 1, 0 } };obj.allHamiltonPath(x); // list all Hamiltonian paths of graph// obj.HamiltonPath(z,1); //list all Hamiltonian paths start at point 1}static int len;static int[] path;static int count = 0;public void allHamiltonPath(int[][] x) { // List all possible Hamilton path// in the graphlen = x.length;path = new int[len];int i;for (i = 0; i < len; i++) { // Go through column(of matrix)path[0] = i + 1;findHamiltonpath(x, 0, i, 0);}}// public void HamiltonPath(int[][] x, int start) { // List all possible
// // Hamilton path with
// // fixed starting point
// len = x.length;
// path = new int[len];
// int i;
// for (i = start - 1; i < start; i++) { // Go through row(with given
// // column)
// path[0] = i + 1;
// findHamiltonpath(x, 0, i, 0);
// }
// }private void findHamiltonpath(int[][] M, int x, int y, int l) {int i;for (i = x; i < len; i++) { // Go through rowif (M[i][y] != 0) { // 2 point connectif (detect(path, i + 1))// if detect a point that already in the// path => duplicatecontinue;l++; // Increase path length due to 1 new point is connectedpath[l] = i + 1; // correspond to the array that start at 0,// graph that start at point 1if (l == len - 1) {// Except initial point already count// =>success connect all pointcount++;if (count == 1)System.out.println("Hamilton path of graph: ");display(path);l--;continue;}M[i][y] = M[y][i] = 0; // remove the path that has been get andfindHamiltonpath(M, 0, i, l); // recursively start to find new// path at new end pointl--; // reduce path length due to the failure to find new pathM[i][y] = M[y][i] = 1; // and tranform back to the inital form// of adjacent matrix(graph)}}path[l + 1] = 0; // disconnect two point correspond the failure to find// the..} // possible hamilton path at new point(ignore newest point try another// one)public void display(int[] x) {System.out.print(count + " : ");for (int i : x) {System.out.print(i + " ");}System.out.println();}private boolean detect(int[] x, int target) { // Detect duplicate point in// Halmilton pathboolean t = false;for (int i : x) {if (i == target) {t = true;break;}}return t;}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
源码解析
采用递归遍历+极限穷举。
好的算法就是更好的对思路的实现,还要有更好的状态控制。
这里的
M[i][y] = M[y][i] = 0;//
M[i][y] = M[y][i] = 1; //
还有对函数的递归调用使用的非常棒!
汉密尔顿路径(哈密顿路径)解析相关推荐
- 两个输出文件名解析为同一输出路径_解析 crash log(一)
前言 在负责的产品中有最近一段时间有极个别用户老是反馈有偶尔闪退的情况,而且就这几个用户反复出现,其它用户,甚至就坐在他边上的用户进行了一样的操作都没有任何问题. 刚开始丢了个重现构建的新包给这几位用 ...
- linux shell 文件路径 分解 解析 切分 ${str:a:b} 用法
最近写脚本,需要对脚本中函数传递的路径参数进行截取,发现了以下比较好用的方法,记录下: file=/dir1/dir2/dir3/my.file.txt 我们可以用${ }分别替换获得不同的值: ${ ...
- Linux运维学习路径全解析
作为一个小白,想要入门Linux还是有一定难度的,想要将Linux作为谋生技能更是需要你付出更多的经历和努力,我分享一些我之前自学Linux的经验以及一些学习网站资源,希望能对你有所帮助. 先贴一张L ...
- 供应链金融业务实施路径深度解析
当前,在经济全球化.数字化.网络化发展越来越明显背景下,企业核心竞争已由个体上升至供应链乃至整个产业生态链的竞争,共建健康的产业生态已成全产业链发展的共识,同时也得到了各个国家的高度认同. 中国甚至提 ...
- 哈密尔顿路径问题解析
问题描述 哈密尔顿路径属于旅行商问题,是一个NP完全问题,即没有一个合适的算法来解决它,只能用朴素算法(也就是通常所说的暴力算法)去进行优化. 但这类问题的时间复杂度是极其可怕的O(n!),当n比较小 ...
- logback-spring.xml 文件路径 相对路径_Web前端必会知识点:VUE路径问题解析-Web前端教程...
Web前端,Web前端知识点,开课吧Web前端
- java servlet 获取路径问题_Java,JSP,Servlet获取当前工程路径(绝对路径)问题解析...
在jsp和class文件中调用的相对路径不同. 在jsp里,根目录是webroot 在class文件中,根目录是webroot/web-inf/classes 当然你也可以用system.getpro ...
- 基于Xml 的IOC 容器-解析配置文件路径
XmlBeanDefinitionReader 通过调用ClassPathXmlApplicationContext 的父类DefaultResourceLoader 的getResource()方法 ...
- Xamarin版的C# SVG路径解析器
Xamarin版的C# SVG路径解析器,对SVG的Path路径进行解析,其中包括: 主程序SvgPathParser.cs, 相关接口定义:ISourceFormatter.cs, 辅助类:Form ...
- linux 进入文件系统路径,Linux虚拟文件系统--文件路径名的解析(1)--整体过程
注意之前传递进来的dfd为AT_FDCWD,因此path_init中只有可能出现前两种情况:1.路径以绝对路径的方式给出 2.路径以相对路径的方式给出.此时nd中的path保存了查找的起始目录,对于第 ...
最新文章
- 第八章 用户方式中线程的同步(2)
- 应用程序池超出其作业限制设置_网站改版注意事项 - 蜘蛛池
- 遇到可爱女生如何搭讪?
- python反射、闭包、装饰器_python之闭包、装饰器、生成器、反射
- linux日志自动按天保存,linux实现按天生成日志文件并自动清理
- 在Java中编码为Base64
- 美国python网课免费-去不了USA?那又怎样?美国名校网课免费学!
- 史上最详细Git使用教程
- Mysql中的lpad,rpad函数
- 怎样让计算机加快速度,六大招教你把旧电脑恢复如新,速度提升N倍!-怎么让电脑速度变快...
- oracle归档默认路径,更改ORACLE归档路径及归档模式
- 视觉SLAM十四讲从理论到实践第二版源码调试笔记(理论基础1-6章)
- 如何搭建Telegram群机器人
- 1GB等于多少MB?
- Druid基本概念及架构介绍
- 计算机网络学习笔记:多路复用(频分多路复用、时分多路复用、波分多路复用、码分多路复用)
- explore_UserGuide
- ATH9K DRIVER LEARNING PART V: KFIFO
- bmcl java,BMCLV3客户端崩溃!
- C语言中 - 是什么意思?