[JAVA] 最小生成树解决TSP

Neptune.WQ 2015-07-19 10:59:12
问题背景:
TSP Problem:

Input: a weighted complete undirected graph G with vertex set
G.V={ v_0,v_(1,)…,v_(n-1)}. The weight of the edge from v_i and v_j is denoted as G.w(i,j). It is assumed that the weights of the edges are non-negative. In other words, the weights satisfy the following constraints:

{ (G.w(i,j)=G.w(j,i); G.w(i,j)>0 if i≠ j; G.w(j,i)=0 if i=j) }

Output: a TSP tour on G which is a Hamiltonian cycle that cycle that starts from v_0, visit each node in G only once and return back to starting vertex〖 v〗_0.

Objective: Minimize the total weight of the edges in the TSP tour.

Heuristics

Minimum Spanning Tree (MST) Based Heuristic

Construct a MST, T, for the vertices in G from starting point v_0;

Traverse around T to get the initial TSP tour for G;

Exploit the triangular inequality and remove unnecessary visits in the TSP tour.

Compute the length of the tour.

Nearest Neighbour (NN) Heuristic

current vertex ← v0;

Loop for n-1 steps

At each step, choose to visit next the vertex that is closest to the current vertex;

Update the current vertex;

Including the closing edge (back to v0) in the tour;

Compute the length of the tour.

求以上两种算法的JAVA代码思路
补充: 1. 自动生成一个随机的N顶点图片的代码已经附上, 问题是如何实现以上两种算法呢. 拜托
...全文
378 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
mtbullt123 2016-03-15
  • 打赏
  • 举报
回复
你好,你有这个算法的实现代码吗? 有的话,发一份给我邮箱吧,谢谢! mtdoorlh@163.com
Neptune.WQ 2015-07-19
  • 打赏
  • 举报
回复


import java.util.Vector;

public class GraphServiceImpl implements GraphService {

	public Graph generateRandomGraph(int n) {
		
		//Firstly: to create a empty graph model
		Graph graph = new Graph();
		
		//Secondly, to confirm the list of vertex based on the parameter 'n'
		String[] vertexArray = new String[n];
		for(int i = 0; i < vertexArray.length; i++) {
			vertexArray[i] = "V" + i;
		}
		graph.setVertex(vertexArray);
		
		Vector<Edge> edges = new Vector<Edge>();
		
		//Finally, to confirm the adjacency Matrix
		int[][] adjacencyMatrix = new int[vertexArray.length][vertexArray.length];
		
		String vextexFrom;
		String vextexTo;
		int weight;
		for(int i = 0; i < vertexArray.length; i++) {
			for(int j = 0; j < vertexArray.length; j++) {
				if(i == j) {  //G.w(j,i) = 0 if i == j
					adjacencyMatrix[i][j] = 0;
				} else if(adjacencyMatrix[j][i] > 0) {  //if randomGraph[j][i] existence, then randomGraph[i][j] = randomGraph[j][i] 
					adjacencyMatrix[i][j] = adjacencyMatrix[j][i];  //G.w(i,j) = G.w(j,i)
				} else { 
					//randomly set the city distance between 1 and 1000
					adjacencyMatrix[i][j] = 1+((int)(Math.random()*1000));   //G.w(i,j) > 0 if i != j
					vextexFrom = vertexArray[i];
					vextexTo = vertexArray[j];
					weight = adjacencyMatrix[i][j];
					edges.addElement(new Edge(vextexFrom, vextexTo, weight));
				}
			}
		}
		graph.setAdjacencyMatrix(adjacencyMatrix);
		graph.setEdges(edges);
		return graph;
	}

	public void printGraphMatrix(Graph graph) {
		for(int i = 0; i < graph.getVertex().length; i++) {
			System.out.print("\t" + graph.getVertex()[i]);
		}
		
		System.out.println();
		
		for(int i = 0; i < graph.getVertex().length; i++) {
			System.out.print(graph.getVertex()[i]);
			
			for(int j = 0; j < graph.getVertex().length; j++) {
				System.out.print("\t" + graph.getAdjacencyMatrix()[i][j]);
			}
			
			System.out.println();
		}
		
	}

	public void printGraphEdges(Graph graph) {
		Vector<Edge> edges = graph.getEdges();
		
		for(Edge edge : edges) {
			System.out.println(edge.getVertexFrom() + "\t" + edge.getVertexTo() + "\t" + edge.getWeight());
		}
	}

}
Neptune.WQ 2015-07-19
  • 打赏
  • 举报
回复

public class Edge {

	private String vertexFrom;
	private String vertexTo;
	private int weight;
	
	public Edge(String vertexFrom, String vertexTo, int weight) {
		this.vertexFrom = vertexFrom;
		this.vertexTo = vertexTo;
		this.weight = weight;
	}
	
	public int compareToEdgeWeigt(Edge edge) {
		return this.weight - edge.weight;
	}
	
	public String getVertexFrom() {
		return vertexFrom;
	}

	public void setVertexFrom(String vertexFrom) {
		this.vertexFrom = vertexFrom;
	}

	public String getVertexTo() {
		return vertexTo;
	}

	public void setVertexTo(String vertexTo) {
		this.vertexTo = vertexTo;
	}

	public int getWeight() {
		return weight;
	}

	public void setWeight(int weight) {
		this.weight = weight;
	}
}
Neptune.WQ 2015-07-19
  • 打赏
  • 举报
回复

import java.util.Vector;

public class Graph {

	private String[] vertexArray;
	private int[][] adjacencyMatrix;
	private Vector<Edge> edges;
	
	public Vector<Edge> getEdges() {
		return edges;
	}
	public void setEdges(Vector<Edge> edges) {
		this.edges = edges;
	}
	public String[] getVertex() {
		return vertexArray;
	}
	public void setVertex(String[] vertexArray) {
		this.vertexArray = vertexArray;
	}
	public int[][] getAdjacencyMatrix() {
		return adjacencyMatrix;
	}
	public void setAdjacencyMatrix(int[][] adjacencyMatrix) {
		this.adjacencyMatrix = adjacencyMatrix;
	}
	
}

50,350

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧