迪杰斯特拉算法完整代码(Java)

package com.rao.graph;

import java.util.*;

/**
* @author Srao
* @className Dijkstra
* @date 2019/12/10 22:15
* @package com.rao.graph
* @Description 迪杰斯特拉算法
*/
public class Dijkstra {

/**
* 图的顶点
*/
private static class Vertex{
String data;
Vertex(String data){
this.data = data;
}
}

/**
* 图的边
*/
private static class Edge{
//从adj[i]到index
int index;
//到index的距离
int weight;
public Edge(int index, int weight) {
this.index = index;
this.weight = weight;
}
}

/**
* 图(邻接矩阵)
*/
private static class Graph{
private Vertex[] vertices;
private LinkedList<Edge>[] adj;
Graph(int size){
vertices = new Vertex[size];
adj = new LinkedList[size];
for (int i = 0; i < adj.length; i++) {
adj[i] = new LinkedList<>();
}
}
}

/**
* 初始化图
* @param graph
*/
private static void initGraph(Graph graph){
graph.vertices[0] = new Vertex("A");
graph.vertices[1] = new Vertex("B");
graph.vertices[2] = new Vertex("C");
graph.vertices[3] = new Vertex("D");
graph.vertices[4] = new Vertex("E");
graph.vertices[5] = new Vertex("F");
graph.vertices[6] = new Vertex("G");

graph.adj[0].add(new Edge(1, 5));
graph.adj[0].add(new Edge(2, 2));
graph.adj[1].add(new Edge(0, 5));
graph.adj[1].add(new Edge(3, 1));
graph.adj[1].add(new Edge(4, 6));
graph.adj[2].add(new Edge(0, 2));
graph.adj[2].add(new Edge(3, 6));
graph.adj[2].add(new Edge(5, 8));
graph.adj[3].add(new Edge(1, 1));
graph.adj[3].add(new Edge(2, 6));
graph.adj[3].add(new Edge(4, 1));
graph.adj[3].add(new Edge(5, 2));
graph.adj[4].add(new Edge(1, 6));
graph.adj[4].add(new Edge(3, 1));
graph.adj[4].add(new Edge(6, 7));
graph.adj[5].add(new Edge(2, 8));
graph.adj[5].add(new Edge(3, 2));
graph.adj[5].add(new Edge(6, 3));
graph.adj[6].add(new Edge(4, 7));
graph.adj[6].add(new Edge(5, 3));
}

/**
* 迪杰斯特拉算法
* @param graph:图
* @param startIndex:图的起点
* @return
*/
public static Map<Integer, Integer> dijkstra(Graph graph, int startIndex){
//创建距离表,存放起点到每一个点的距离,默认值为无穷大
Map<Integer, Integer> distanceMap = new HashMap<>();
//记录已经遍历过的顶点
Set<Integer> accessedSet = new HashSet<>();
//图的顶点数量
int size = graph.vertices.length;
//初始化距离表
for (int i = 1; i < size; i++) {
distanceMap.put(i, Integer.MAX_VALUE);
}
//遍历起点,刷新距离表
accessedSet.add(0);
List<Edge> edgesFromStart = graph.adj[startIndex];
for (Edge edge : edgesFromStart) {
distanceMap.put(edge.index, edge.weight);
}
//循环遍历所有的点,并且刷新距离表
for (int i = 1; i < size; i++) {
//寻找到顶点最短的距离的点
int minDistanceFromStart = Integer.MAX_VALUE;
int minDistanceIndex = -1;
for (int j = 1; j < size; j++) {
if (!accessedSet.contains(j) && distanceMap.get(j) < minDistanceFromStart){
minDistanceFromStart = distanceMap.get(j);
minDistanceIndex = j;
}
}
if (minDistanceIndex == -1){
break;
}
//遍历这个最小距离的顶点
accessedSet.add(minDistanceIndex);
for (Edge edge : graph.adj[minDistanceIndex]) {
if (accessedSet.contains(edge.index)){
continue;
}
int weight = edge.weight;
int preDistance = distanceMap.get(edge.index);
if (weight != Integer.MAX_VALUE && (minDistanceFromStart + weight) < preDistance){
distanceMap.put(edge.index, minDistanceFromStart + weight);
}
}
}
return distanceMap;
}

public static void main(String[] args) {
Graph graph = new Graph(7);
initGraph(graph);
Map<Integer, Integer> distanceMap = dijkstra(graph, 0);
int distance = distanceMap.get(6);
System.out.println(distance);
}
}
原文地址:https://www.cnblogs.com/rao11/p/12024051.html