Java 在图中建立随机哈密顿路径
在我的代码中,我在图中找到哈密顿路径来解决另一个问题。Java 在图中建立随机哈密顿路径,java,algorithm,graph,Java,Algorithm,Graph,在我的代码中,我在图中找到哈密顿路径来解决另一个问题。 我现在正在测试我的代码,我想得到一个没有边的一般图,并在其中构造一个哈密顿路径。在这个图上(现在有形成哈密顿路径的边),我将根据图中的参数添加随机边。 通过这种方式,我可以看到我的代码处理具有不同数量边的图形的速度有多快。 我如何处理有效的图形: 对于矩阵中的每个单元,我添加一个顶点 如果顶点u和v在矩阵中相邻,则可以连接它们。 我的目标是生成一个带有哈密顿路径的随机有效图。 问题是,我无法找到一种有效的方法来构造哈密顿路径,而不重复所
我现在正在测试我的代码,我想得到一个没有边的一般图,并在其中构造一个哈密顿路径。在这个图上(现在有形成哈密顿路径的边),我将根据图中的参数添加随机边。 通过这种方式,我可以看到我的代码处理具有不同数量边的图形的速度有多快。
我如何处理有效的图形:
我的目标是生成一个带有哈密顿路径的随机有效图。
例如:
The matrix: Possible path: Not possible:
-------------
| 1 | 2 | 3 | 1 - 2 - 3 1 - 2 - 3 _
------------- | |
| 4 | 5 | 6 | 4 - 5 - 6 4 - 5 - 6 |
------------- | | |
| 7 | 8 | 9 | 7 - 8 - 9 7 - 8 - 9_/
-------------
第二条路径不可能,因为3和9在矩阵中不相邻。有没有一种方法可以在线性时间内单独给定矩阵来构造哈密顿路径?
包哈密顿路径;
package hamiltonian_path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
public class Main {
int[] solution;
HashMap<Integer, List<Integer>> candidates;
public static void main(String args[]) {
Main main = new Main();
main.solution = new int[10];//stores the solution; index 0 is not used, i will use indexes from 1 to 9
main.candidates = new HashMap<Integer, List<Integer>>();//for each position (1 to 9) in the solution, stores a list of candidate elements for that position
List<Integer> oneToNine = new LinkedList<Integer>(Arrays.asList(1,2,3,4,5,6,7,8,9));
/*
* because no solution can start from matrix elements 2,4,6 or 8,
* for the sake of optimization, the above list can be written as
* Arrays.asList(1,3,5,7,9)
* the way it is right now is useful to follow the way the program
* does the backtracking, when it accidentally starts with either 2,4,6 or 8
*/
Collections.shuffle(oneToNine);//a random permutation of the list
main.candidates.put(1, oneToNine);
main.buildSol(1);
}
//backtracking
public void buildSol(int k)
{
if(k==10)
{
System.out.print("the solution is ");
printSolution();
return;
}
List<Integer> candList = candidates.get(k);
if(candList.isEmpty())
{
cleanSolution(k);
buildSol(k-1); //if no candidates for current step, go one step back
}
else
{
int firstCandidate = candList.get(0);
candList.remove(0);
candidates.put(k, candList);
solution[k] = firstCandidate;//for the position k in the solution, pick the first element in the candidates list
List<Integer> neighbors = getNeighbors(solution[k]);
List<Integer> prevElems = getPreviousElementsInSolution(k);
candidates.put(k+1, generateCandidates(neighbors, prevElems));//while being at step k, generate candidate elements for step k+1
//these candidates are the neighbors (in the matrix) of the current element (solution[k]),
//which are not already part of the solution at an earlier position
System.out.println("step "+k);
System.out.print("partial solution: ");
printSolution();
System.out.println();
buildSol(k+1);//go to next step
}
}
//candidates are those elements which are neighbors, and have not been visited before
public List<Integer> generateCandidates(List<Integer> neighbors, List<Integer> previousElements)
{
List<Integer> cnd = new ArrayList<Integer>();
for(int i=0;i<neighbors.size();i++)
if(!previousElements.contains(neighbors.get(i)))
cnd.add(neighbors.get(i));
return cnd;
}
//get the set of previous elements in the solution, up to solution[k]
public List<Integer> getPreviousElementsInSolution(int step)
{
List<Integer> previousElements = new ArrayList<Integer>();
for(int i=1; i<=step-1;i++)
previousElements.add(solution[i]);
return previousElements;
}
//get neighbors of the matrix element which corresponds to solution[k]
public List<Integer> getNeighbors(int element) {
List<Integer> neighboursList = new ArrayList<Integer>();
switch (element) {
case 1: neighboursList = Arrays.asList(2, 4);
break;
case 2: neighboursList = Arrays.asList(1, 3, 5);
break;
case 3: neighboursList = Arrays.asList(2, 6);
break;
case 4: neighboursList = Arrays.asList(1, 5, 7);
break;
case 5: neighboursList = Arrays.asList(2, 4, 6, 8);
break;
case 6: neighboursList = Arrays.asList(3, 5, 9);
break;
case 7: neighboursList = Arrays.asList(4, 8);
break;
case 8: neighboursList = Arrays.asList(5, 7, 9);
break;
case 9: neighboursList = Arrays.asList(6, 8);
break;
default: neighboursList = new ArrayList<Integer>();
break;
}
return neighboursList;
}
public void printSolution()
{
for(int i=1;i<solution.length;i++)
System.out.print(solution[i]+" ");
}
public void cleanSolution(int k)
{
for(int i=k;i<solution.length;i++)
solution[i]=0;
}
}
导入java.util.ArrayList;
导入java.util.array;
导入java.util.Collections;
导入java.util.HashMap;
导入java.util.LinkedList;
导入java.util.List;
公共班机{
int[]溶液;
HashMap候选;
公共静态void main(字符串参数[]){
Main Main=新Main();
main.solution=new int[10];//存储解决方案;不使用索引0,我将使用从1到9的索引
main.candidates=new HashMap();//对于解决方案中的每个位置(1到9),存储该位置的候选元素列表
List-oneToNine=newlinkedlist(Arrays.asList(1,2,3,4,5,6,7,8,9));
/*
*因为没有解决方案可以从矩阵元素2、4、6或8开始,
*为了优化,上面的列表可以写成
*数组.asList(1,3,5,7,9)
*它现在的方式对于遵循程序的方式很有用
*当它意外地以2、4、6或8开始时,是否进行回溯
*/
Collections.shuffle(oneToNine);//列表的随机排列
主。候选。put(1,oneToNine);
main.buildSol(1);
}
//回溯
公共建筑(int k)
{
如果(k==10)
{
系统输出打印(“解决方案是”);
打印解决方案();
返回;
}
List=candList=candidates.get(k);
if(candList.isEmpty())
{
清洁溶液(k);
buildSol(k-1);//如果当前步骤没有候选项,请后退一步
}
其他的
{
int firstCandidate=candList.get(0);
candList.remove(0);
候选人。put(k,candList);
解决方案[k]=firstCandidate;//对于解决方案中的位置k,选择候选列表中的第一个元素
List neighbories=getneighbories(解决方案[k]);
List prevElems=getPreviousElementsInSolution(k);
put(k+1,generateCandidates(neights,prevelem));//在步骤k,为步骤k+1生成候选元素
//这些候选元素是当前元素(解[k])的邻域(在矩阵中),
//在以前的位置上,它们还不是解决方案的一部分
System.out.println(“步骤”+k);
系统输出打印(“部分解决方案:”);
打印解决方案();
System.out.println();
buildSol(k+1);//转到下一步
}
}
//候选者是那些邻居,以前从未访问过的人
公共列表生成条件(列出邻居,列出以前的元素)
{
List cnd=new ArrayList();
对于(int i=0;i可能会有帮助)不清楚您的矩阵是如何表示图形的。我已经添加了一些关于图形外观以及如何构造图形的详细信息。@Henry建议删除“在该图形上,我将添加…”开头的句子,因为它描述的内容与您要解决的直接问题无关(对吗?),因此有点让人困惑。考虑到接下来要用图做什么,为什么最小生成树不能完成这项工作?因此,如果不重复所有可能的解决方案,就没有办法做到这一点?如果你仔细看,在我找到解决方案的那一刻(测试长度是否为10),我打印它并调用return,它退出该方法。这段代码根本不迭代所有可能的解决方案;它迭代大量的解决方案;它试图构建一个解决方案——如果它可以直接构建——它会;否则,如果它到达死胡同,它会后退,并尝试另一个分支。如果您查看print语句,它会当程序后退很多的时候,就是当它意外地以2,4,6,8开始时,这导致没有解决方案。正如我在评论中所说的,为了优化,将oneToNine
list修改为newlinkedlist(Arrays.asList(1,3,5,7,9));
。打印语句将反映出解决方案的生成要简单得多