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 {

/**

* 图的顶点

*/

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:图的起点

* @return

*/

public static Map dijkstra(Graph graph, int startIndex){

//创建距离表,存放起点到每一个点的距离,默认值为无穷大

Map distanceMap = new HashMap<>();

//记录已经遍历过的顶点

Set 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 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 distanceMap = dijkstra(graph, 0);

int distance = distanceMap.get(6);

System.out.println(distance);

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值