弗洛伊德算法
参考 http://www.cnblogs.com/skywang12345/p/3711532.html
import java.util.ArrayList;
import java.util.List;
public class FloydInGraph {
private static int INF = Integer.MAX_VALUE;
private int[][] dist;
//顶点i 到 j的最短路径长度,初值是i到j的边的权重
private int[][] path;
private List<Integer> result = new ArrayList<Integer>();
public static void main(String[] args) {
FloydInGraph graph = new FloydInGraph(5);
int[][] matrix = {
{INF, 30, INF, 10, 50},
{INF, INF, 60, INF, INF},
{INF, INF, INF, INF, INF},
{INF, INF, INF, INF, 30},
{50, INF, 40, INF, INF},
};
int begin=0;
int end=4;
graph.findCheapestPath(begin,end,matrix);
List<Integer> list=graph.result;
System.out.println(begin+" to "+end+",the cheapest path is:");
System.out.println(list.toString());
System.out.println(graph.dist[begin][end]);
}
public void findCheapestPath(int begin,int end,int[][] matrix){
floyd(matrix);
result.add(begin);
findPath(begin,end);
result.add(end);
}
public void findPath(int i,int j){
// 找到路由节点
int k=path[i][j];
if(k==-1)
return;
// 从i到路由节点进行递归寻找中间节点
findPath(i,k);
result.add(k);
// 从j到路由节点进行递归寻找中间节点
findPath(k,j);
}
public void floyd(int[][] matrix){
int size=matrix.length;
for(int i=0;i< size;i++){
for(int j=0;j< size;j++){
path[i][j]=-1;
dist[i][j]=matrix[i][j];
}
}
for(int k=0;k< size;k++){
for(int i=0;i< size;i++){
for(int j=0;j< size;j++){
if(dist[i][k]!=INF&&
dist[k][j]!=INF&&
dist[i][k]+dist[k][j]< dist[i][j]){
// 更新i和j两点间的距离
dist[i][j]=dist[i][k]+dist[k][j];
// 更新i和j两点间的路由信息
path[i][j]=k;
}
}
}
}
}
public FloydInGraph(int size){
this.path=new int[size][size];
this.dist=new int[size][size];
}
}
计算最短路径版本
import java.util.ArrayList;
import java.util.List;
public class FloydInGraph {
private static int INF = Integer.MAX_VALUE;
private int[][] dist;
//顶点i 到 j的最短路径长度,初值是i到j的边的权重
private int[][] path;
private List<Integer> result = new ArrayList<Integer>();
public static void main(String[] args) {
FloydInGraph graph = new FloydInGraph(17);
int[][] matrix = {
{INF, 30, INF, 10, 50},
{INF, INF, 60, INF, INF},
{INF, INF, INF, INF, INF},
{INF, INF, INF, INF, 30},
{50, INF, 40, INF, INF},
};
int begin=0;
int end=14;
System.out.println(graph.floyd(matrix, begin, end));
}
public int floyd(int[][] matrix, int begin, int end){
int size=matrix.length;
for(int i=0;i< size;i++){
for(int j=0;j< size;j++){
dist[i][j]=matrix[i][j];
}
}
for(int k=0;k< size;k++){
for(int i=0;i< size;i++){
for(int j=0;j< size;j++){
if(dist[i][k]!=INF&&
dist[k][j]!=INF&&
dist[i][k]+dist[k][j]< dist[i][j]){
// 更新i和j两点间的距离
dist[i][j]=dist[i][k]+dist[k][j];
}
}
}
}
return dist[begin][end];
}
public FloydInGraph(int size){
this.path=new int[size][size];
this.dist=new int[size][size];
}
}
结合guava table使用
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import java.util.*;
public class FloydInGraph {
private static int INF = Integer.MAX_VALUE;
private int[][] dist;
private int[][] path;
public static void main(String[] args) {
int begin=0;
int end=2;
FloydInGraph graph = new FloydInGraph(5);
HashBasedTable<String, String, Map<String, Integer>> table = HashBasedTable.create();
HashMap<String, Integer> hm1 = new HashMap<>();
hm1.put("1", 30);
HashMap<String, Integer> hm2 = new HashMap<>();
hm2.put("1", 10);
HashMap<String, Integer> hm3 = new HashMap<>();
hm3.put("1", 50);
HashMap<String, Integer> hm4 = new HashMap<>();
hm4.put("1", 60);
HashMap<String, Integer> hm5 = new HashMap<>();
hm5.put("1", 30);
HashMap<String, Integer> hm6 = new HashMap<>();
hm6.put("1", 50);
HashMap<String, Integer> hm7 = new HashMap<>();
hm7.put("1", 40);
table.put("S0","S1", hm1);
table.put("S0","S3", hm2);
table.put("S0","S4", hm3);
table.put("S1","S2", hm4);
table.put("S3","S4", hm5);
table.put("S4","S0", hm6);
table.put("S4","S2", hm7);
int size = table.columnKeySet().size() > table.rowKeySet().size() ? table.columnKeySet().size():table.rowKeySet().size();
int[][] matrix = new int[size][size];
for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[0].length;j++){
matrix[i][j] = INF;
}
}
Set<Table.Cell<String, String, Map<String, Integer>>> cells = table.cellSet();
for (Table.Cell<String, String, Map<String, Integer>> cell : cells) {
int rowValue = Integer.valueOf(cell.getRowKey().substring(1));
int columnValue = Integer.valueOf(cell.getColumnKey().substring(1));
int val = 0;
Map<String,Integer> hm = cell.getValue();
for(Map.Entry<String,Integer> map:hm.entrySet()){
val = map.getValue();
}
matrix[rowValue][columnValue] = val;
}
for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[0].length;j++){
System.out.print(matrix[i][j]+"\t");
}
System.out.println();
}
// int[][] matrix = {
// {INF, 30, INF, 10, 50},
// {INF, INF, 60, INF, INF},
// {INF, INF, INF, INF, INF},
// {INF, INF, INF, INF, 30},
// {50, INF, 40, INF, INF},
// };
System.out.println(graph.floyd(matrix, begin, end));
}
public int floyd(int[][] matrix, int begin, int end){
int size=matrix.length;
for(int i=0;i< size;i++){
for(int j=0;j< size;j++){
dist[i][j]=matrix[i][j];
}
}
for(int k=0;k< size;k++){
for(int i=0;i< size;i++){
for(int j=0;j< size;j++){
if(dist[i][k]!=INF&&
dist[k][j]!=INF&&
dist[i][k]+dist[k][j]< dist[i][j]){
// 更新i和j两点间的距离
dist[i][j]=dist[i][k]+dist[k][j];
}
}
}
}
return dist[begin][end];
}
public FloydInGraph(int size){
this.path=new int[size][size];
this.dist=new int[size][size];
}
}