4.3 最短路径问题:Dijkstra、Floyd

4.3 最短路径问题:Dijkstra、Floyd

在图论中,最短路径问题是一种经典问题,它的目标是在给定的图中找到两个顶点之间的最短路径。在这一节,我们将会介绍两种最常用的解决最短路径问题的算法:Dijkstra算法和Floyd算法。

Dijkstra算法

Dijkstra算法是由荷兰计算机科学家艾兹格·迪科斯彻提出的一种用于在图中找到单源最短路径的算法,尤其是在权重图中。这个算法使用了广度优先搜索的思想,并且采用了优先队列的数据结构来迅速找到下一个最短的路径。

import java.util.*;

class Edge {
    int to, weight;

    public Edge(int to, int weight) {
        this.to = to;
        this.weight = weight;
    }
}

public class Dijkstra {
    public void shortestPath(List<Edge>[] graph, int start) {
        int n = graph.length;
        PriorityQueue<Edge> pq = new PriorityQueue<>(Comparator.comparingInt(e -> e.weight));
        int[] dist = new int[n];
        Arrays.fill(dist, Integer.MAX_VALUE);
        dist[start] = 0;
        pq.add(new Edge(start, 0));

        while (!pq.isEmpty()) {
            Edge edge = pq.poll();
            int v = edge.to;
            if (dist[v] < edge.weight) continue;
            for (Edge e : graph[v]) {
                int to = e.to;
                int weight = e.weight;
                if (dist[v] + weight < dist[to]) {
                    dist[to] = dist[v] + weight;
                    pq.add(new Edge(to, dist[to]));
                }
            }
        }

        for (int i = 0; i < n; ++i) {
            System.out.printf("Shortest distance from %d to %d: %d\n", start, i, dist[i]);
        }
    }
}

Floyd算法

Floyd算法(Floyd-Warshall算法)是一种解决任意两点之间的最短路径的问题的算法。与Dijkstra算法不同,Floyd算法能够处理有向图和负权重的边,但是不能处理存在负权重环路的图。

public class Floyd {
    public void shortestPath(int[][] graph) {
        int n = graph.length;
        int[][] dist = new int[n][n];
        for (int i = 0; i < n; i++) {
            System.arraycopy(graph[i], 0, dist[i], 0, n);
        }

        for (int k = 0; k < n; k++) {
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    if (dist[i][k] + dist[k][j] < dist[i][j]) {
                        dist[i][j] = dist[i][k] + dist[k][j];
                    }
                }
            }
        }

        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                System.out.printf("Shortest distance from %d to %d: %d\n", i, j, dist[i][j]);
            }
        }
    }
}

在最短路径问题中,Dijkstra算法和Floyd算法各有优势,选择使用哪一种算法主要取决于问题的具体情况。在下一节中,我们将介绍更复杂的图论问题以及如何解决这些问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AmosCloud2013

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值