博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
图的应用:哈密尔顿路径
阅读量:4449 次
发布时间:2019-06-07

本文共 3532 字,大约阅读时间需要 11 分钟。

1       public List
getHamiltonianPath(int v) { 2 // A path starts from v. (i, next[i]) represents an edge in 3 // the path. isVisited[i] tracks whether i is currently in the 4 // path. 5 int[] next = new int[getSize()]; 6 for (int i = 0; i < next.length; i++) 7 next[i] = -1; // Indicate no subpath from i is found yet 8 9 boolean[] isVisited = new boolean[getSize()]; 10 11 // The vertices in the Hamiltionian path are stored in result12 List
result = null;13 14 // To speed up search, reorder the adjacency list for each 15 // vertex so that the vertices in the list are in increasing 16 // order of their degrees17 for (int i = 0; i < getSize(); i++)18 reorderNeigborsBasedOnDegree(neighbors.get(i));19 20 if (getHamiltonianPath(v, next, isVisited)) {21 result = new ArrayList
(); // Create a list for path 22 int vertex = v; // Starting from v23 while (vertex != -1) {24 result.add(vertex); // Add vertex to the result list25 vertex = next[vertex]; // Get the next vertex in the path26 }27 }28 29 return result; // return null if no Hamiltionian path is found30 }31 32 /** Reorder the adjacency list in increasing order of degrees */33 private void reorderNeigborsBasedOnDegree(List
list) {34 for (int i = list.size() - 1; i >= 1; i--) {35 // Find the maximum in the list[0..i]36 int currentMaxDegree = getDegree(list.get(0));37 int currentMaxIndex = 0;38 39 for (int j = 1; j <= i; j++) {40 if (currentMaxDegree < getDegree(list.get(j))) { 41 currentMaxDegree = getDegree(list.get(j));42 currentMaxIndex = j;43 }44 }45 46 // Swap list[i] with list[currentMaxIndex] if necessary;47 if (currentMaxIndex != i) {48 int temp = list.get(currentMaxIndex);49 list.set(currentMaxIndex, list.get(i));50 list.set(i, temp);51 }52 }53 }54 55 /** Return true if all elements in array isVisited are true */56 private boolean allVisited(boolean[] isVisited) {57 boolean result = true;58 59 for (int i = 0; i < getSize(); i++) 60 result = result && isVisited[i];61 62 return result;63 }64 65 /** Search for a Hamiltonian path from v */66 private boolean getHamiltonianPath(int v, int[] next,67 boolean[] isVisited) {68 isVisited[v] = true; // Mark vertex v visited69 70 if (allVisited(isVisited)) 71 return true; // The path now includes all vertices, thus found72 73 for (int i = 0; i < neighbors.get(v).size(); i++) {74 int u = neighbors.get(v).get(i);75 if (!isVisited[u] && 76 getHamiltonianPath(u, next, isVisited)) {77 next[v] = u; // Edge (v, u) is in the path78 return true; 79 }80 }81 82 isVisited[v] = false; // Backtrack, v is marked unvisited now83 return false; // No Hamiltonian path exists from vertex v84 }

 对于一个给定的网络,确定起点后,如果存在一条路径,最后又回到原出发点,且满足每个顶点只被访问一次的情况下能穿过这个网络,我们就说这个网络存在哈密顿路径。

转载于:https://www.cnblogs.com/wanghui390/p/3436798.html

你可能感兴趣的文章
类和对象-2
查看>>
使用UltraEdit+BCC5.5搭建C语言学习环境(转)
查看>>
485收发控制器:
查看>>
mssql死锁
查看>>
读取iOS plist文件 (其实类似读取xml文件)
查看>>
wps的几个优点
查看>>
Swift 可选链
查看>>
Servlet的入门案例
查看>>
获取请求参数乱码的问题
查看>>
代码实现:判断E盘目录下是否有后缀名为.jpg的文件,如果有,就输出该文件名称...
查看>>
Android客户端测试点
查看>>
Jquery:怎样让子窗体的div显示在父窗体之上
查看>>
01概率
查看>>
.NET LINQ 元素操作
查看>>
51nod 1020
查看>>
Shell脚本
查看>>
MatLab Load cv::Mat 导入数据
查看>>
html+css相关笔记(一)
查看>>
基于块流协议保证音频优先发送
查看>>
关于互联网的一些数据
查看>>