简单Dijkstra算法


Dijkstra算法是单源最短路径算法,它通过贪心法求得某一点在相邻区域的最优解,所以它不能处理存在负边的图。Dijkstra算法会遍历很大范围的节点,从而得出短路径的最优解。


算法思想

设G = ( V, E )是简单图(不含有自环) ,V是图中的顶点集合,E是边集合。V集合中每个顶点带权(从源点到该点的路径总长),未明确权或未设置权的顶点放在集合U,已设置权且不再改变权的顶点放在集合S。当顶点从U移动到S的过程中,顶点权值小于所有相邻节点的权值。当终点的权确定后,即找到从源点到终点的最短路径。


算法过程

1、从起点开始,访问所有与起点邻接且未确定长度的点
2、设置邻接点最小路径长度,此时起点路径长度已确定
3、从所有未确定长度的点中找出路径长度最小的点,设置改点为起点
4、重复1过程,直至找到终点或点集合为空


邻接矩阵

现有无向图如下,求从A到E的最短路径

这里写图片描述

求得相邻矩阵matrix如下

01234567
ABCDEFGH
0A053
1B5022
2C30436
3D24025
4E2205
5F304
6G550
7H640


完整代码

设置权的结构,包含路径节点和路径长度

import java.util.*;

public class Main {

    static class NodeWeight implements Comparable<NodeWeight> {
        Integer node;
        Integer length;
        List<Integer> pathList = new LinkedList<>();

        @Override
        public int compareTo(NodeWeight node) {
            return this.length.compareTo(node.length);
        }
    }

    public static void main(String[] args) {
        Integer[][] matrix = new Integer[8][8];
        matrix[1][0] = 5;
        matrix[2][0] = 3;
        matrix[3][1] = 2;
        matrix[3][2] = 4;
        matrix[4][1] = 2;
        matrix[4][3] = 2;
        matrix[5][2] = 3;
        matrix[6][3] = 5;
        matrix[6][4] = 5;
        matrix[7][2] = 6;
        matrix[7][5] = 4;
        for (Integer i = 0; i < matrix.length; i++) {
            for (Integer j = 0; j < matrix[i].length; j++) {
                if (matrix[i][j] != null) {
                    matrix[j][i] = matrix[i][j];
                }
                if (i.equals(j)) {
                    matrix[i][j] = 0;
                }
            }
        }
        print(sortDijkstra(0, 0, matrix));
    }

    private static void addWeight(NodeWeight nodeWeight, List<NodeWeight> nodeWeightList) {
        ListIterator<NodeWeight> listIterator = nodeWeightList.listIterator();
        while (true) {
            //根据权重为节点排序
            //根据前后节点的长度选择合适的位置插入该节点
            Integer pre = Integer.MIN_VALUE;
            if (listIterator.hasPrevious()) {
                pre = listIterator.previous().length;
                listIterator.next();
            }
            Integer next = listIterator.hasNext() ? listIterator.next().length : Integer.MAX_VALUE;
            if (nodeWeight.length >= pre && nodeWeight.length < next) {
                listIterator.add(nodeWeight);
                break;
            }
        }
    }

    private static NodeWeight sortDijkstra(Integer startNode, Integer endNode, Integer[][] matrix) {
        //权重列表(有序),权重集合,移除节点集合
        List<NodeWeight> nodeWeightList = new LinkedList<>();
        NodeWeight[] nodeWeights = new NodeWeight[matrix.length];
        Boolean[] excludes = new Boolean[matrix.length];
        //初始化起始节点
        nodeWeights[startNode] = new NodeWeight();
        nodeWeights[startNode].node = startNode;
        nodeWeights[startNode].length = 0;
        nodeWeights[startNode].pathList = new ArrayList<>();
        nodeWeights[startNode].pathList.add(startNode);
        //设置当前节点,设置当前节点路径上的最小路径目标节点
        Integer currentNode = startNode;
        while (true) {
            for (Integer targetNode = 0; targetNode < matrix[currentNode].length; targetNode++) {
                if (!targetNode.equals(currentNode) && matrix[currentNode][targetNode] != null && excludes[targetNode] == null) {
                    //如果目的节点不等于当前节点,目的节点不为空,目的节点未被移除
                    Integer targetNodePathLength = nodeWeights[currentNode].length + matrix[currentNode][targetNode];
                    if (nodeWeights[targetNode] == null) {
                        //目的节点路径为空
                        nodeWeights[targetNode] = new NodeWeight();
                        nodeWeights[targetNode].node = targetNode;
                        nodeWeights[targetNode].length = targetNodePathLength;
                        nodeWeights[targetNode].pathList = new ArrayList<>(nodeWeights[currentNode].pathList);
                        nodeWeights[targetNode].pathList.add(targetNode);
                        addWeight(nodeWeights[targetNode], nodeWeightList);
                    } else if (nodeWeights[targetNode].length > targetNodePathLength) {
                        nodeWeights[targetNode].length = targetNodePathLength;
                        nodeWeights[targetNode].pathList = new ArrayList<>(nodeWeights[currentNode].pathList);
                        nodeWeights[targetNode].pathList.add(targetNode);
                    }
                }
            }
            //如果集合为空或者终点已经确定权值,则路径查找结束
            if (nodeWeightList.size() > 0 && !currentNode.equals(endNode)) {
                excludes[currentNode] = false;
                currentNode = nodeWeightList.get(0).node;
                nodeWeightList.remove(0);
            } else {
                return nodeWeights[endNode];
            }
        }
    }

    private static void print(NodeWeight nodeWeight) {
        Character[] charNode = new Character[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};
        System.out.print(nodeWeight.length + "\t" + charNode[nodeWeight.node] + "\t");
        for (Integer i : nodeWeight.pathList) {
            System.out.print(charNode[i] + " - ");
        }
        System.out.println();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值