图-有权图-最短路径算法

来源:互联网 发布:structure软件 使用 编辑:程序博客网 时间:2024/04/30 15:54
package k_graph.B_WeightedGraph.B_Path;
/**
 * 有向有权图
 * 最短路径算法 
 * @author Administrator
 *
 */
public class PathApp {
public static void main(String[] args) {
Graph theGraph = new Graph();
     theGraph.addVertex('A');     // 0  (start)
     theGraph.addVertex('B');     // 1
     theGraph.addVertex('C');     // 2
     theGraph.addVertex('D');     // 3
     theGraph.addVertex('E');     // 4


     theGraph.addEdge(0, 1, 50);  // AB 50
     theGraph.addEdge(0, 3, 80);  // AD 80
     theGraph.addEdge(1, 2, 60);  // BC 60
     theGraph.addEdge(1, 3, 90);  // BD 90
     theGraph.addEdge(2, 4, 40);  // CE 40
     theGraph.addEdge(3, 2, 20);  // DC 20
     theGraph.addEdge(3, 4, 70);  // DE 70
     theGraph.addEdge(4, 1, 50);  // EB 50


     System.out.println("Shortest paths:");
     theGraph.path();             // shortest paths
     System.out.println();


}


}package k_graph.B_WeightedGraph.B_Path;
/**
 * 寻找距离最近的顶点,利用最小更新路径数组中的父顶点和距离,循环。就都是最小路径了
 * @author Administrator
 *
 */
public class Graph {
private final int MAX_VERTS = 20;
private final int INFINITY = 1000000;
private Vertex vertexList[];
private int adjMat[][];
private int nVerts;
private int nTree;
private DistPar sPath[];// 最短路径数据数组
private int currentVert;// 现在的顶点
private int startToCurrent;// 现在顶点距离


public Graph() {
vertexList = new Vertex[MAX_VERTS];
adjMat = new int[MAX_VERTS][MAX_VERTS];
nVerts = 0;
nTree = 0;
sPath = new DistPar[MAX_VERTS];
for (int i = 0; i < MAX_VERTS; i++)
for (int j = 0; j < MAX_VERTS; j++)
adjMat[i][j] = INFINITY;
}


public void addVertex(char lab) {


vertexList[nVerts++] = new Vertex(lab);
}


public void addEdge(int start, int end, int weight) {


adjMat[start][end] = weight;
}


/**
* 找到所有最短路径
*/
public void path() {
int startTree = 0;//开始顶点在0
vertexList[startTree].isInTree = true;
nTree = 1;//放入树
//开始点到各顶点的所有距离,放入spath数组。
for (int j = 0; j < nVerts; j++) {
//获取距离
int tempDist = adjMat[startTree][j];
sPath[j] = new DistPar(startTree, tempDist);
}
while (nTree < nVerts) {
//获取最小距离的索引()索引对应顶点的索引
int indexMin = getMin();
//获取最小距离
int minDist = sPath[indexMin].distance;
//等于INFINITY说明,到所有顶点距离都遥不可及
if (minDist == INFINITY) {
System.out.println("有遥不可及的顶点");
break;

} else {
//现在顶点为最小距离索引
currentVert = indexMin;
//现在距离为最小距离
startToCurrent = sPath[indexMin].distance;

}
//加入树
vertexList[currentVert].isInTree = true;
nTree++;

adjust_sPath();
}
displayPaths();
nTree = 0;
for (int j = 0; j < nVerts; j++) {
vertexList[j].isInTree = false;
}
}

public void displayPaths() {
for (int i = 0; i < nVerts; i++) {
System.out.print(vertexList[i].label + "=");
if (sPath[i].distance == INFINITY)
System.out.print("inf");
else
System.out.print(sPath[i].distance);
char parent = vertexList[sPath[i].parentVert].label;
System.out.print("(" + parent + ") ");
}
System.out.println("");
}
/**
* 更新距离和顶点
*/
public void adjust_sPath() {

int column = 1;
while (column < nVerts) {
//跳过已经在树的节点
if (vertexList[column].isInTree) {
column++;
continue;
}
//获取现在节点到不在树内的其他顶点的距离
int currentToFringe = adjMat[currentVert][column];
//获取最新的距离
int startToFrige = startToCurrent + currentToFringe;
//取出数组之前的距离
int sPathDist = sPath[column].distance;
//比较
if (startToFrige < sPathDist) {
sPath[column].parentVert = currentVert;
sPath[column].distance = startToFrige;
}
column++;
}

}


/**
* 获取最小值索引

* @return
*/
public int getMin() {


int minDist = INFINITY;// 暂定最小值为INFINITY无穷大
int indexMin = 0;
for (int j = 1; j < nVerts; j++) {
if (!vertexList[j].isInTree && sPath[j].distance < minDist) {
minDist = sPath[j].distance;// 获取小值,赋值,下次拿这个小值和更小值比较,方便获取最小值索引
indexMin = j;
}
}
return indexMin;
}


}package k_graph.B_WeightedGraph.B_Path;


public class DistPar {// 距离父节点
public int distance;// 距离
public int parentVert;// 顶点父节点索引


public DistPar(int parentVert, int distance) {
this.parentVert = parentVert;
this.distance = distance;
}


@Override
public String toString() {
return "DistPar [distance=" + distance + ", parentVert=" + parentVert
+ "]";
}


}package k_graph.B_WeightedGraph.B_Path;


public class Vertex {
public char label;
public boolean isInTree;


public Vertex(char label) {
this.label = label;
this.isInTree = false;
}


}
0 0
原创粉丝点击