狄克斯特拉算法的Java实现

package cn.like.study.algorithm;

import java.util.*;

/**
 * @program: MyCode
 * @description: 狄克斯特拉算法
 * 算法步骤:1.判断是否有未处理的节点
 * 2.若有,获得其中离起点最近的节点
 * 3.遍历该节点所有邻居并更行其开销
 * 4.如果有邻居的开销被更新,同时更新父节点
 * 5.该节点标记为已处理
 * 6.重复第1步
 * @author: like
 * @create: 2018-04-22 10:49
 **/
public class Dijkstra {
    //设置没有已知到达路径的标记
    private static int NOWAY_SIGN = Integer.MAX_VALUE;
    private static final String START = "start";
    private static final String END = "end";

    public void getMinStep(String start, String end, Map<String, Map<String, Integer>> graph) {
        //各节点的最少花费
        Map<String, Integer> costs = graph.get(start);
        //各节点最少花费时的父节点
        Map<String, String> parents = new HashMap<String, String>();
        //已处理的节点
        HashSet<String> processed = new HashSet<String>();
        //在未处理的节点中找到开销最小的节点
        String node = findLowestCostNode(costs, processed);
        while (node != null && graph.get(node) != null) {
            int cost = costs.get(node);
            //遍历当前节点的所有邻居
            Iterator iterator = graph.get(node).entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, Integer> entry = (Map.Entry) iterator.next();
                //通过node节点到该节点的最小消耗
                int newCost = cost + entry.getValue();
                //更新从start到该节点的最小消耗
                if (!costs.containsKey(entry.getKey()) || costs.get(entry.getKey()) > newCost) {
                    costs.put(entry.getKey(), newCost);
                    parents.put(entry.getKey(), node);
                }
            }
            //该节点加入已处理
            processed.add(node);
            //找出当前最小消耗的节点
            node = findLowestCostNode(costs, processed);
        }
        printPath(parents, costs.get(END));
    }

    public void initParents(String start, Map<String, Integer> startGraph, Map<String, String> parents) {
        Iterator iterator = startGraph.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Integer> entry = (Map.Entry) iterator.next();
            parents.put(entry.getKey(), start);
        }
    }

    /**
     * 找出未处理节点中消耗最小的节点
     *
     * @param costs
     * @param processed
     * @return
     */
    public String findLowestCostNode(Map<String, Integer> costs, HashSet<String> processed) {
        int lowestCost = NOWAY_SIGN;
        String lowestCostNode = null;
        Iterator iterator = costs.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Integer> entry = (Map.Entry) iterator.next();
            if (!processed.contains(entry.getKey()) && entry.getValue() < lowestCost) {
                lowestCost = entry.getValue();
                lowestCostNode = entry.getKey();
            }
        }
        return lowestCostNode;
    }

    public void printPath(Map<String, String> parents, int cost) {
        Stack<String> stack = new Stack<String>();
        String parent = parents.get(END);
        while (parent != null) {
            if (START.equalsIgnoreCase(parent)) {
                stack.push(START);
                break;
            }
            stack.push(parent);
            parent = parents.get(parent);
        }
        StringBuffer path = new StringBuffer();
        while (!stack.empty()) {
            String node = stack.pop();
            if (path.length() != 0) {
                path.append("->");
            }
            path.append(node);
        }
        System.out.println("最优路线:" + START + "->" + path.toString() + "->" + END);
        System.out.println("其开销为:" + cost);
    }

    public static void main(String[] args) {
        Map<String, Map<String, Integer>> graph = new HashMap<String, Map<String, Integer>>();
        Map<String, Integer> start = new HashMap<String, Integer>();
        start.put("A", 5);
        start.put("B", 2);
        graph.put(START, start);
        Map<String, Integer> a = new HashMap<String, Integer>();
        a.put("C", 4);
        a.put("D", 2);
        graph.put("A", a);
        Map<String, Integer> b = new HashMap<String, Integer>();
        b.put("A", 8);
        b.put("D", 7);
        graph.put("B", b);
        Map<String, Integer> c = new HashMap<String, Integer>();
        c.put("D", 6);
        c.put(END, 3);
        graph.put("C", c);
        Map<String, Integer> d = new HashMap<String, Integer>();
        d.put(END, 1);
        graph.put("D", d);
        Dijkstra dijkstra = new Dijkstra();
        dijkstra.getMinStep(START, END, graph);
    }
}

测试示例:

输出结果:

最优路线:start->A->D->end
其开销为:8

转载于:https://my.oschina.net/u/3821897/blog/1799213

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
狄克斯特拉算法(Dijkstra's algorithm)是一种用于解决单源最短路径问题的经典算法。它可以找到从一个顶点到其他所有顶点的最短路径。 以下是狄克斯特拉算法Java实现: ```java import java.util.*; public class DijkstraAlgorithm { private static final int INF = Integer.MAX_VALUE; public static void dijkstra(int[][] graph, int start) { int n = graph.length; int[] dist = new int[n]; boolean[] visited = new boolean[n]; Arrays.fill(dist, INF); dist[start] = 0; for (int i = 0; i < n - 1; i++) { int minDist = INF; int minIndex = -1; for (int j = 0; j < n; j++) { if (!visited[j] && dist[j] < minDist) { minDist = dist[j]; minIndex = j; } } visited[minIndex] = true; for (int j = 0; j < n; j++) { if (!visited[j] && graph[minIndex][j] != 0 && dist[minIndex] != INF && dist[minIndex] + graph[minIndex][j] < dist[j]) { dist[j] = dist[minIndex] + graph[minIndex][j]; } } } // 打印最短路径 System.out.println("顶点\t最短距离"); for (int i = 0; i < n; i++) { System.out.println(i + "\t" + dist[i]); } } public static void main(String[] args) { int[][] graph = { {0, 4, 0, 0, 0, 0, 0, 8, 0}, {4, 0, 8, 0, 0, 0, 0, 11, 0}, {0, 8, 0, 7, 0, 4, 0, 0, 2}, {0, 0, 7, 0, 9, 14, 0, 0, 0}, {0, 0, 0, 9, 0, 10, 0, 0, 0}, {0, 0, 4, 14, 10, 0, 2, 0, 0}, {0, 0, 0, 0, 0, 2, 0, 1, 6}, {8, 11, 0, 0, 0, 0, 1, 0, 7}, {0, 0, 2, 0, 0, 0, 6, 7, 0} }; dijkstra(graph, 0); } } ``` 这段代码实现狄克斯特拉算法,通过传入一个邻接矩阵表示的图和起始顶点,计算出从起始顶点到其他所有顶点的最短路径,并打印出最短距离。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值