无向图的最短路径算法JAVA实现

时间:2023-03-08 19:00:36

一,问题描述

给出一个无向图,指定无向图中某个顶点作为源点。求出图中所有顶点到源点的最短路径。

无向图的最短路径其实是源点到该顶点的最少边的数目。

本文假设图的信息保存在文件中,通过读取文件来构造图。文件内容的格式参考这篇文章第一部分

二,算法实现思路

无向图的最短路径实现相对于带权的有向图最短路径实现要简单得多。

源点的最短路径距离为0,从源点开始,采用广度优先的顺序,首先将与源点邻接的顶点的路径求出,然后再依次求解图中其他顶点的最短路径。

由于顶点的最短路径的求解顺序 是一个 广度优先的顺序,因此需要一个辅助队列。初始时,将源点的最短路径距离设置为0,将源点入队列。

然后,在一个while循环中,从队列中弹出顶点,遍历该顶点的邻接点,若该邻接点的距离未被更新过(表示该邻接点未被访问过),更新邻接点的最短路径距离为 该顶点的距离加上1,并将所有的邻接点入队列。

三,最短路径算法的实现

感觉该算法的实现与 二叉树的层序遍历,有向图的拓扑排序算法实现都非常的相似。他们都采用了广度的思想在里面。

广度优先的思想就是:处理完某个顶点后,去处理该顶点的所有邻接点,处理完它的邻接点后,再去处理更远(更外层)的顶点。

算法的代码如下:

     /*
* 计算源点s到无向图中各个顶点的最短路径
* 需要一个队列来保存图中的顶点,初始时,源点入队列,然后以广度的形式向外扩散求解其他顶点的最短路径
*/
private void unweightedShortestPath(Vertex s){
//初始化
Queue<Vertex> queue = new LinkedList<>();
s.dist = 0;
queue.offer(s);//将源点dist设置为0并入队列 while(!queue.isEmpty()){
Vertex v = queue.poll();
for (Edge e : v.adjEdges) {//扫描v的邻接边(点)
if(e.endVertex.dist == Integer.MAX_VALUE){//如果这个顶点(e.endVertex)未被访问(每个顶点只会入队列一次)
e.endVertex.dist = v.dist + 1;//更新该顶点到源点的距离
queue.offer(e.endVertex);
e.endVertex.preNode = v;//设置该顶点的前驱顶点
}//end if
}//end for
}//end while
}

第11行while循环,每个顶点出队列一次,第13行for循环,表示每条边被处理一次,故算法的时间复杂度为O(V+E)

第14行if语句表明,图中每个顶点只会入队列一次。因为,顶点入队列后,该顶点的 dist 设置为 v.dist+1,不再是 Integer.MAX_VALUE

四,完整代码实现

NonDirectedGraph.java构造图并实现最短路径算法

 import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue; /*
* 求解无向图的单源最短路径
*/
public class NonDirectedGraph {
private class Vertex{
private String vertexLabel;//顶点标识
private List<Edge> adjEdges;//与该顶点邻接的边(点)
private int dist;//顶点距离(该顶点到起始顶点的距离)
private Vertex preNode; public Vertex(String vertexLabel) {
this.vertexLabel = vertexLabel;
adjEdges = new LinkedList<>();
dist = Integer.MAX_VALUE;
preNode = null;
}
}
private class Edge{
private Vertex endVertex;
public Edge(Vertex endVertex) {
this.endVertex = endVertex;
}
} private Map<String, Vertex> nonDirectedGraph;//保存了图中所有的顶点,边的关系以List形式保存在Vertex类中
private Vertex startVertex;//图的起始顶点 public NonDirectedGraph(String graphContent) {
nonDirectedGraph = new LinkedHashMap<>();
buildGraph(graphContent);
} private void buildGraph(String graphContent){
String[] lines = graphContent.split("\n"); String startNodeLabel, endNodeLabel;
Vertex startNode, endNode;
for(int i = 0; i < lines.length; i++){
String[] nodesInfo = lines[i].split(",");
startNodeLabel = nodesInfo[1];
endNodeLabel = nodesInfo[2]; endNode = nonDirectedGraph.get(endNodeLabel);
if(endNode == null){
endNode = new Vertex(endNodeLabel);
nonDirectedGraph.put(endNodeLabel, endNode);
} startNode = nonDirectedGraph.get(startNodeLabel);
if(startNode == null){
startNode = new Vertex(startNodeLabel);
nonDirectedGraph.put(startNodeLabel, startNode);
}
Edge e = new Edge(endNode);
//对于无向图而言,起点和终点都要添加边
endNode.adjEdges.add(e);
startNode.adjEdges.add(e);
}
startVertex = nonDirectedGraph.get(lines[0].split(",")[1]);//总是以文件中第一行第二列的那个标识顶点作为源点
} public void unweightedShortestPath(){
unweightedShortestPath(startVertex);
} /*
* 计算源点s到无向图中各个顶点的最短路径
* 需要一个队列来保存图中的顶点,初始时,源点入队列,然后以广度的形式向外扩散求解其他顶点的最短路径
*/
private void unweightedShortestPath(Vertex s){
//初始化
Queue<Vertex> queue = new LinkedList<>();
s.dist = 0;
queue.offer(s);//将源点dist设置为0并入队列 while(!queue.isEmpty()){
Vertex v = queue.poll();
for (Edge e : v.adjEdges) {//扫描v的邻接边(点)
if(e.endVertex.dist == Integer.MAX_VALUE){//如果这个顶点(e.endVertex)未被访问(每个顶点只会入队列一次)
e.endVertex.dist = v.dist + 1;//更新该顶点到源点的距离
queue.offer(e.endVertex);
e.endVertex.preNode = v;//设置该顶点的前驱顶点
}//end if
}//end for
}//end while
} //打印图中所有顶点到源点的距离及路径
public void showDistance(){
Collection<Vertex> vertexs = nonDirectedGraph.values();
for (Vertex vertex : vertexs) {
System.out.print(vertex.vertexLabel + "<--");
Vertex tmpPreNode = vertex.preNode;
while(tmpPreNode != null){
System.out.print(tmpPreNode.vertexLabel + "<--");
tmpPreNode = tmpPreNode.preNode;
}
System.out.println("distance=" + vertex.dist);
}
}
}

打印路径也可以使用递归来实现:

     public void showDistanceRecursive(Vertex v){
if(v.preNode != null){
showDistanceRecursive(v.preNode);
}
System.out.print(v.vertexLabel + " ");
}

打印顶点 v 的路径,第三行 先打印 v 的前驱顶点的路径,然后再在第5行打印 v 。

第5行的打印输出语句在第三行的递归调用语句之后,故最里层的递归调用最先被打印出来,最里层的递归调用即源点,因为只有源点的 preNode == null。

当所有的里层递归调用返回后,最终执行到最外层的递归调用处,执行第5行打印 顶点 v 后,整个递归结束。

TestShortestPath.java是个测试类,用来测试结果。

 public class TestShortestPath {//hapjin test
public static void main(String[] args) {
String graphFilePath;
if(args.length == 0)
graphFilePath = "F:\\xxx";
else
graphFilePath = args[0]; String graphContent = FileUtil.read(graphFilePath, null);
NonDirectedGraph graph = new NonDirectedGraph(graphContent);
graph.unweightedShortestPath();
graph.showDistance();
}
}

FileUtil.java负责读取存储图信息的文件。具体参考有向图的拓扑排序算法JAVA实现

保存图的 文件内容如下:

0,0,1,4
1,0,2,7
2,0,3,3
3,1,2,3
4,1,4,2
5,3,4,3
6,2,5,2
7,4,5,2

测试输出结果如下:

无向图的最短路径算法JAVA实现

源点标识是 0,

0 号顶点到 1 号顶点的最短距离为1,路径为:0-->1

0 号顶点到 5 号顶点的最短距离为2,路径为:0-->2-->5

.....

....