[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.

...全文
378 4 打赏 收藏 转发到动态 举报

4 条回复

mtbullt123 2016-03-15
• 打赏
• 举报

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

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
} else if(adjacencyMatrix[j][i] > 0) {  //if randomGraph[j][i] existence, then randomGraph[i][j] = randomGraph[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];
}
}
}
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.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 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;
}
}
}

}

``````

50,350

javaspring bootspring cloud 技术论坛（原bbs）

• 近7日
• 近30日
• 至今