dijkstra 算法的java实现

本文详细介绍迪杰斯特拉算法的原理与Java实现,该算法用于寻找加权图中两点间的最短路径。通过将顶点集合分为已知最短路径集合S与待处理集合T,逐步扩展S并更新T中顶点的最短路径估值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

按路径长度递增次序产生算法,把顶点集合V分成两组:
(1)S:已求出的顶点的集合(初始时只含有源点V0)
(2)V-S=T:尚未确定的顶点集合
将T中顶点按递增的次序加入到S中,保证:
(1)从源点V0到S中其他各顶点的长度都不大于从V0到T中任何顶点的最短路径长度
(2)每个顶点对应一个距离值
S中顶点:从V0到此顶点的长度
T中顶点:从V0到此顶点的只包括S中顶点作中间顶点的最短路径长度
依据:可以证明V0到T中顶点Vk的,或是从V0到Vk的直接路径的权值;或是从V0经S中顶点到Vk的路径权值之和。
java算法实现:

package com.routeSearch.route;

public class Dijkstra {
    private int[] distance;
    private int[] route;
    private static int Max = 999;

    /**
     * 求顶点begin到顶点i的最小距离。
     * @param begin 开始顶点
     * @param costs 图的邻接矩阵
     */
    public void dijkstra(int begin,int[][] costs){
        if( costs == null || (begin<0 && begin>costs.length) ){
            throw new RuntimeException("传入的参数不合法...");
        }
        boolean[] flag = new boolean[costs.length];
        distance = new int[costs.length];
        route = new int[costs.length];
        //初始化
        for(int i=0;i<costs.length;i++){
            flag[i] = false;
            route[i] = 0;
            distance[i] = costs[begin][i];
        }
        //初始化开始顶点
        flag[begin] = true;
        distance[begin] = 0;

        int k = 0;
        for(int i=0;i<costs.length;i++){
            // 寻找当前最小的路径;
            // 即,在未获取最短路径的顶点中,找到离vs最近的顶点(k)。
            int min = Max;
            for(int j=0;j<costs.length;j++){
                if(flag[j] == false && distance[j] < min){
                    min = distance[j];
                    k = j;
                }
            }
            //标记"顶点k"为已经获取到最短路径
            flag[k] = true;
            // 修正当前最短路径和前驱顶点
            // 即,当已经"顶点k的最短路径"之后,更新"未获取最短路径的顶点的最短路径和前驱顶点"。
            for(int j=0;j<costs.length;j++){
                int temp = (costs[k][j] == Max) ? Max : (min+costs[k][j]);
                if(flag[j] == false && temp<distance[j]){
                    distance[j] = temp;
                    route[j] = k;
                }
            }
        }
    }
    /**
     *
     * @return 最短距离
     */
    public int[] getDistance() {
        return distance;
    }
    /**
     *
     * @return 顶点start"到"顶点i"的最短路径所经历的全部顶点中,位于"顶点i"之前的那个顶点。
     */
    public int[] getRoute() {
        return route;
    }
}
### Dijkstra算法Java实现 Dijkstra算法用于计算加权连通图中从一个给定起点到其他所有节点的最短路径。此算法采用贪心策略逐步构建最短路径树。 下面是一个完整的Dijkstra算法Java实现: ```java import java.util.Arrays; public class DijkstraAlgorithm { private static final int NO_PARENT = -1; public void calculateShortestPath(int[][] adjacencyMatrix, int startVertex) { int nVertices = adjacencyMatrix[0].length; int[] shortestDistances = new int[nVertices]; boolean[] added = new boolean[nVertices]; Arrays.fill(shortestDistances, Integer.MAX_VALUE); Arrays.fill(added, false); shortestDistances[startVertex] = 0; int lastAddedVertex = startVertex; for (int i = 1; i < nVertices; ++i) { int nearestVertex = -1; int shortestDistance = Integer.MAX_VALUE; for (int vertexIndex = 0; vertexIndex < nVertices; ++vertexIndex) { if (!added[vertexIndex] && shortestDistances[lastAddedVertex] + adjacencyMatrix[lastAddedVertex][vertexIndex] < shortestDistance) { nearestVertex = vertexIndex; shortestDistance = shortestDistances[lastAddedVertex] + adjacencyMatrix[lastAddedVertex][vertexIndex]; } } shortestDistances[nearestVertex] = shortestDistance; added[nearestVertex] = true; lastAddedVertex = nearestVertex; } printSolution(startVertex, shortestDistances); } private void printSolution(int startVertex, int[] distances) { System.out.println("Source Vertex: " + startVertex); for (int vertexIndex = 0; vertexIndex < distances.length; ++vertexIndex) { System.out.println(String.format("Destination Vertex : %d Distance from Source: %d", vertexIndex, distances[vertexIndex])); } } } ``` 上述代码定义了一个`calculateShortestPath`方法来执行Dijkstra算法,接收邻接矩阵表示法下的图以及起始顶点作为参数[^2]。该函数初始化距离数组将初始结点的距离设为零;接着迭代更新最近未访问过的邻居直到遍历完所有的顶点打印最终的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值