java迪杰斯特拉算法_迪杰斯特拉算法完整代码(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 {
/**
* 圖的頂點(diǎn)
*/
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[] 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:圖的起點(diǎn)
* @return
*/
public static Map dijkstra(Graph graph, int startIndex){
//創(chuàng)建距離表,存放起點(diǎn)到每一個(gè)點(diǎn)的距離,默認(rèn)值為無窮大
Map distanceMap = new HashMap<>();
//記錄已經(jīng)遍歷過的頂點(diǎn)
Set accessedSet = new HashSet<>();
//圖的頂點(diǎn)數(shù)量
int size = graph.vertices.length;
//初始化距離表
for (int i = 1; i < size; i++) {
distanceMap.put(i, Integer.MAX_VALUE);
}
//遍歷起點(diǎn),刷新距離表
accessedSet.add(0);
List edgesFromStart = graph.adj[startIndex];
for (Edge edge : edgesFromStart) {
distanceMap.put(edge.index, edge.weight);
}
//循環(huán)遍歷所有的點(diǎn),并且刷新距離表
for (int i = 1; i < size; i++) {
//尋找到頂點(diǎn)最短的距離的點(diǎn)
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;
}
//遍歷這個(gè)最小距離的頂點(diǎn)
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 distanceMap = dijkstra(graph, 0);
int distance = distanceMap.get(6);
System.out.println(distance);
}
}
總結(jié)
以上是生活随笔為你收集整理的java迪杰斯特拉算法_迪杰斯特拉算法完整代码(Java)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 死磕jdk源码之如何注释
- 下一篇: 树控件,多条件组合查询与混合数据源