Java 实现 Dijsktra 算法

4 篇文章 0 订阅
1 篇文章 0 订阅

看看书中的描述:
这里写图片描述
这里写图片描述
这里写图片描述

书中是用C++实现的,C++比较难,懂个思路就行,这里用java实现:

这里写图片描述

package graph;

public class Dijkstra {

    private static final int MAXSIZE = 1000;
    private static final int INF = 1200;
    private static final int N = 6;

    public static void main(String[] args) {
        System.out.println("------------Dijsktra------------");
        int[][] data = {
                {MAXSIZE, 12, MAXSIZE, 12, 5, MAXSIZE},
                {MAXSIZE, MAXSIZE, 7, MAXSIZE, 4, MAXSIZE},
                {MAXSIZE, MAXSIZE, MAXSIZE,  MAXSIZE, MAXSIZE, 1},
                {MAXSIZE, MAXSIZE, MAXSIZE, MAXSIZE, MAXSIZE, 7},
                {MAXSIZE, MAXSIZE, 14, 9, MAXSIZE, 32},
                {MAXSIZE, MAXSIZE, MAXSIZE, MAXSIZE, MAXSIZE, MAXSIZE},
        };
        int v = 1;
        System.out.println("打印有向图的邻接矩阵:");
        for(int i = 0; i < N; i++) {
            for(int j = 0; j < N; j++) {
                System.out.print("  " + data[i][j]);
            }
            System.out.println();
        }
        System.out.println("打印原点1到其它各点的最短路径及其长度:");
        toDijsktra(data, v);
    }

    private static void toDijsktra(int[][] data, int v) {
        //dist[]顶点到顶点的最短距离
        int[] dist = new int[N];
        //path[]存放上一个顶点
        int[] path = new int[N];
        //s[]存放已经加入到最短路径的顶点
        int[] s = new int[N];
        int f = v - 1;
        //初始化第一个顶点
        for(int i = 0; i < N; i++) {
            dist[i] = data[f][i];
            if(dist[i] != MAXSIZE) {
                path[i] = v;
            }
            else {
                path[i] = 0;
            }
        }
        for(int i = 0; i < N; i++) {
            s[i] = 0;
        }
        //将第一个顶点加入最短路径集合
        s[f] = 1;
        //第一个顶点到第一个顶点的距离为0
        dist[f] = 0;
        int k = 0;
        for(int i = 0; i < N - 1; i++) {
            int min = INF;
            //该for循环找出最短的边
            for(int j = 0; j < N; j++) {
                if((s[j] == 0) && (dist[j] < min)) {
                    min = dist[j];
                    //最短边的顶点赋给K
                    k = j;
                }
            }
            //将K顶点加入最短路径集合
            s[k] = 1;
            //更新d[]的值,保证d[j]里面的值是有第一个顶点到k顶点的最短距离
            for(int j = 0; j < N; j++) {
                if((s[j] == 0) && (dist[j] > (dist[k] + data[k][j]))) {
                    dist[j] = dist[k] + data[k][j];
                    path[j] = k + 1;
                }
            }
        }
        for(int i = 0; i < N; i++) {
            System.out.print(v + "到"  + (i+1) + "的最短距离是");
            System.out.print(dist[i]);
            System.out.println();
            int pre = path[i];
            System.out.print("路径:" + (i+1));
            while(pre != 0) {
                System.out.print("<------" + pre);
                pre = path[pre-1];
            }
            System.out.println();
            System.out.println("-----------------------");
        }
    }
}

运行结果:

这里写图片描述

以下是用 Matlab 实现 Dijkstra 算法的示例代码: ```matlab function [dist, path] = dijkstra(adj, src, dest) % DIJKSTRA Find the shortest path from a source node to a destination node % in a graph represented by an adjacency matrix using Dijkstra's algorithm. % adj: adjacency matrix of the graph % src: source node index % dest: destination node index % dist: shortest distance from the source node to each node % path: shortest path from the source node to the destination node % % Example: % adj = [0 3 9 0 0; % 3 0 1 7 0; % 9 1 0 5 8; % 0 7 5 0 4; % 0 0 8 4 0]; % [dist, path] = dijkstra(adj, 1, 5); n = size(adj, 1); % number of nodes dist = inf(1, n); % initialize distances to infinity prev = zeros(1, n); % initialize previous node indices to zero visited = false(1, n); % initialize visited flags to false dist(src) = 0; % distance from source to itself is zero for i = 1:n % find the node with the smallest tentative distance [min_dist, u] = min(dist(~visited)); if isempty(u) break; % all remaining nodes are unreachable end % mark the selected node as visited visited(u) = true; % update the distances of its neighbors for v = 1:n if adj(u, v) ~= 0 && ~visited(v) alt = dist(u) + adj(u, v); if alt < dist(v) dist(v) = alt; prev(v) = u; end end end % check if we have reached the destination node if u == dest break; end end % construct the shortest path from the source to the destination path = []; u = dest; while prev(u) ~= 0 path = [u path]; u = prev(u); end if u == src path = [u path]; end end ``` 该函数的输入参数是邻接矩阵 `adj`,源节点索引 `src` 和目标节点索引 `dest`,输出参数是最短距离数组 `dist` 和最短路径数组 `path`。该函数使用了 Dijkstra 算法来计算最短路径,其中包括了对节点距离的初始化、标记已访问的节点、更新邻居节点的距离等过程。最后,根据存储的前驱节点信息,构造从源节点到目标节点的最短路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值