搜索
首页Javajava教程寻找最短路径

两个顶点之间的最短路径是总权重最小的路径。
给定一个边上具有非负权重的图,荷兰计算机科学家 Edsger Dijkstra 发现了一种著名的算法,用于查找两个顶点之间的最短路径。为了找到从顶点 s 到顶点 v 的最短路径,Dijkstra 算法 找到从 s 到所有顶点的最短路径。因此迪杰斯特拉算法被称为单源最短路径算法。该算法使用 cost[v] 来存储从顶点 v 到源顶点 s最短路径 的成本。 成本[s]0。最初将无穷大分配给所有其他顶点的 cost[v]。该算法重复地在 V – T 中寻找 cost[u] 最小的顶点 u 并将 u 移动到 T .

该算法在下面的代码中描述。

Input: a graph G = (V, E) with non-negative weights
Output: a shortest path tree with the source vertex s as the root
 1 ShortestPathTree getShortestPath(s) {
 2 Let T be a set that contains the vertices whose
 3 paths to s are known; Initially T is empty;
 4 Set cost[s] = 0; and cost[v] = infinity for all other vertices in V;
 5
 6 while (size of T  cost[u] + w(u, v)) {
11 cost[v] = cost[u] + w(u, v); parent[v] = u;
12 }
13 }
14 }

这个算法与 Prim 寻找最小生成树的算法非常相似。两种算法都将顶点分为两个集合:TV - T。在 Prim 算法的情况下,集合 T 包含已添加到树中的顶点。在 Dijkstra 的情况下,集合 T 包含已找到到源的最短路径的顶点。两种算法都会重复从 V – T 中查找顶点并将其添加到 T。在 Prim 算法的情况下,顶点与集合中边缘权重最小的某个顶点相邻。在 Dijkstra 算法中,该顶点与集合中某个顶点相邻,且对源的总成本最小。

算法首先将 cost[s] 设置为 0(第 4 行),将所有其他顶点的 cost[v] 设置为无穷大。然后,它不断地将一个顶点(比如 u)从 V – T 添加到 T 中,且 cost[u] 最小(第 7 行– 8)、如下图a.将 u 添加到 T 后,算法会更新每个 vcost[v]parent[v] > 不在 T 中,如果 (u, v) 位于 Tcost[v] > cost[u] + w(u, v)(第 10-11 行)。

Finding Shortest Paths

让我们使用下图a中的图来说明Dijkstra算法。假设源顶点是

1。因此,cost[1] = 0 并且所有其他顶点的成本最初为 ∞,如下图 b 所示。我们使用 parent[i] 来表示路径中 i 的父级。为了方便起见,将源节点的父节点设置为 -1.

Finding Shortest Paths

初始设置

T为空。该算法选择成本最小的顶点。在本例中,顶点为 1。该算法将1添加到T,如下图a所示。然后,它调整与 1 相邻的每个顶点的成本。现在更新了顶点 2、0、6、3 及其父节点的成本,如下图 b.

Finding Shortest Paths

顶点

2063 与源顶点相邻,并且顶点 2V-T 中成本最小的一个,因此将 2 添加到 T,如下图所示,并更新顶点的成本和父级V-T 并与 2 相邻。 cost[0] 现已更新为 6,其父级设置为 2。从12的箭头线表示将2添加到后,12的父级T.

Finding Shortest Paths

现在 T 包含 {1, 2}。顶点 0V-T 中成本最小的顶点,因此将 0 添加到 T,如下图所示,并更新V-T 中且与 0 相邻的顶点的成本和父级(如果适用)。 cost[5] 现已更新为 10,其父项设置为 0cost[6] 现已更新为 8 及其父级设置为 0.

Finding Shortest Paths

现在 T 包含 {1, 2, 0}。顶点 6V-T 中成本最小的顶点,因此将 6 添加到 T,如下图所示,并更新V-T 中且与 6 相邻的顶点的成本和父级(如果适用)。

Finding Shortest Paths

现在 T 包含 {1, 2, 0, 6}。顶点 35V-T 中成本最小的一个。您可以将 35 添加到 T 中。让我们将 3 添加到 T,如下图所示,并更新 V-T 中且与 3 相邻的顶点的成本和父级如果适用。 cost[4] 现已更新为 18,其父级设置为 3

Finding Shortest Paths

现在 T 包含 {1, 2, 0, 6, 3}。顶点 5V-T 中成本最小的顶点,因此将 5 添加到 T,如下图所示,并更新V-T 中且与 5 相邻的顶点的成本和父级(如果适用)。 cost[4] 现已更新为 10,其父级设置为 5

Finding Shortest Paths

现在 T 包含 {1, 2, 0, 6, 35}。顶点 4V-T 中成本最小的顶点,因此将 4 添加到 T,如下图所示。

Finding Shortest Paths

如您所见,该算法本质上是从源顶点找到所有最短路径,从而生成以源顶点为根的树。我们将此树称为单源全最短路径树(或简称为最短路径树)。要对该树建模,请定义一个名为 ShortestPathTree 的类,该类扩展 Tree 类,如下图所示。 ShortestPathTree 被定义为 WeightedGraph.java 中第 200-224 行 WeightedGraph 中的内部类。

Finding Shortest Paths

getShortestPath(int sourceVertex) 方法在 WeightedGraph.java 的第 156-197 行中实现。该方法将所有其他顶点的 cost[sourceVertex] 设置为 0 (第 162 行),并将 cost[v] 设置为无穷大(第 159-161 行)。 sourceVertex 的父级设置为 -1(第 166 行)。 T 是一个列表,存储添加到最短路径树中的顶点(第 169 行)。我们使用 T 的列表而不是集合来记录添加到 T 的顶点的顺序。

最初,T 是空的。要展开 T,该方法执行以下操作:

  1. Find the vertex u with the smallest cost[u] (lines 175–181) and add it into T (line 183).
  2. After adding u in T, update cost[v] and parent[v] for each v adjacent to u in V-T if cost[v] > cost[u] + w(u, v) (lines 186–192).

Once all vertices from s are added to T, an instance of ShortestPathTree is created (line 196).

The ShortestPathTree class extends the Tree class (line 200). To create an instance of ShortestPathTree, pass sourceVertex, parent, T, and cost (lines 204–205). sourceVertex becomes the root in the tree. The data fields root, parent, and searchOrder are defined in the Tree class, which is an inner class defined in AbstractGraph.

Note that testing whether a vertex i is in T by invoking T.contains(i) takes O(n) time, since T is a list. Therefore, the overall time complexity for this implemention is O(n^3).

Dijkstra’s algorithm is a combination of a greedy algorithm and dynamic programming. It is a greedy algorithm in the sense that it always adds a new vertex that has the shortest distance to the source. It stores the shortest distance of each known vertex to the source and uses it later to avoid redundant computing, so Dijkstra’s algorithm also uses dynamic programming.

The code below gives a test program that displays the shortest paths from Chicago to all other cities in Figure below and the shortest paths from vertex 3 to all vertices for the graph in Figure below a, respectively.

Finding Shortest Paths

Finding Shortest Paths

package demo;

public class TestShortestPath {

    public static void main(String[] args) {
String[] vertices = {"Seattle", "San Francisco", "Los Angeles", "Denver", "Kansas City", "Chicago", "Boston", "New York", "Atlanta", "Miami", "Dallas", "Houston"};

        int[][] edges = {
                {0, 1, 807}, {0, 3, 1331}, {0, 5, 2097},
                {1, 0, 807}, {1, 2, 381}, {1, 3, 1267},
                {2, 1, 381}, {2, 3, 1015}, {2, 4, 1663}, {2, 10, 1435},
                {3, 0, 1331}, {3, 1, 1267}, {3, 2, 1015}, {3, 4, 599}, {3, 5, 1003},
                {4, 2, 1663}, {4, 3, 599}, {4, 5, 533}, {4, 7, 1260}, {4, 8, 864}, {4, 10, 496},
                {5, 0, 2097}, {5, 3, 1003}, {5, 4, 533}, {5, 6, 983}, {5, 7, 787},
                {6, 5, 983}, {6, 7, 214},
                {7, 4, 1260}, {7, 5, 787}, {7, 6, 214}, {7, 8, 888},
                {8, 4, 864}, {8, 7, 888}, {8, 9, 661}, {8, 10, 781}, {8, 11, 810},
                {9, 8, 661}, {9, 11, 1187},
                {10, 2, 1435}, {10, 4, 496}, {10, 8, 781}, {10, 11, 239},
                {11, 8, 810}, {11, 9, 1187}, {11, 10, 239}
        };

        WeightedGraph<string> graph1 = new WeightedGraph(vertices, edges);
        WeightedGraph<string>.ShortestPathTree tree1 = graph1.getShortestPath(graph1.getIndex("Chicago"));
        tree1.printAllPaths();

        // Display shortest paths from Houston to Chicago       
        System.out.println("Shortest path from Houston to Chicago: ");
        java.util.List<string> path = tree1.getPath(graph1.getIndex("Houston"));
        for(String s: path) {
            System.out.print(s + " ");
        }

        edges = new int[][]{
            {0, 1, 2}, {0, 3, 8},
            {1, 0, 2}, {1, 2, 7}, {1, 3, 3},
            {2, 1, 7}, {2, 3, 4}, {2, 4, 5},
            {3, 0, 8}, {3, 1, 3}, {3, 2, 4}, {3, 4, 6},
            {4, 2, 5}, {4, 3, 6}
        };

        WeightedGraph<integer> graph2 = new WeightedGraph(edges, 5);
        WeightedGraph<integer>.ShortestPathTree tree2 = graph2.getShortestPath(3);
        System.out.println("\n");
        tree2.printAllPaths();
    }

}

</integer></integer></string></string></string>

All shortest paths from Chicago are:
A path from Chicago to Seattle: Chicago Seattle (cost: 2097.0)
A path from Chicago to San Francisco:
Chicago Denver San Francisco (cost: 2270.0)
A path from Chicago to Los Angeles:
Chicago Denver Los Angeles (cost: 2018.0)
A path from Chicago to Denver: Chicago Denver (cost: 1003.0)
A path from Chicago to Kansas City: Chicago Kansas City (cost: 533.0)
A path from Chicago to Chicago: Chicago (cost: 0.0)
A path from Chicago to Boston: Chicago Boston (cost: 983.0)
A path from Chicago to New York: Chicago New York (cost: 787.0)
A path from Chicago to Atlanta:
Chicago Kansas City Atlanta (cost: 1397.0)
A path from Chicago to Miami:
Chicago Kansas City Atlanta Miami (cost: 2058.0)
A path from Chicago to Dallas: Chicago Kansas City Dallas (cost: 1029.0)
A path from Chicago to Houston:
Chicago Kansas City Dallas Houston (cost: 1268.0)
Shortest path from Houston to Chicago:
Houston Dallas Kansas City Chicago

All shortest paths from 3 are:
A path from 3 to 0: 3 1 0 (cost: 5.0)
A path from 3 to 1: 3 1 (cost: 3.0)
A path from 3 to 2: 3 2 (cost: 4.0)
A path from 3 to 3: 3 (cost: 0.0)
A path from 3 to 4: 3 4 (cost: 6.0)

The program creates a weighted graph for Figure above in line 27. It then invokes the getShortestPath(graph1.getIndex("Chicago")) method to return a Path object that contains all shortest paths from Chicago. Invoking printAllPaths() on the ShortestPathTree object displays all the paths (line 30).

The graphical illustration of all shortest paths from Chicago is shown in Figure below. The shortest paths from Chicago to the cities are found in this order: Kansas City, New York, Boston, Denver, Dallas, Houston, Atlanta, Los Angeles, Miami, Seattle, and San Francisco.

以上是寻找最短路径的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
如何使用咖啡因或Guava Cache等库在Java应用程序中实现多层缓存?如何使用咖啡因或Guava Cache等库在Java应用程序中实现多层缓存?Mar 17, 2025 pm 05:44 PM

本文讨论了使用咖啡因和Guava缓存在Java中实施多层缓存以提高应用程序性能。它涵盖设置,集成和绩效优势,以及配置和驱逐政策管理最佳PRA

Java的类负载机制如何起作用,包括不同的类载荷及其委托模型?Java的类负载机制如何起作用,包括不同的类载荷及其委托模型?Mar 17, 2025 pm 05:35 PM

Java的类上载涉及使用带有引导,扩展程序和应用程序类负载器的分层系统加载,链接和初始化类。父代授权模型确保首先加载核心类别,从而影响自定义类LOA

如何在Java中实施功能编程技术?如何在Java中实施功能编程技术?Mar 11, 2025 pm 05:51 PM

本文使用lambda表达式,流API,方法参考和可选探索将功能编程集成到Java中。 它突出显示了通过简洁性和不变性改善代码可读性和可维护性等好处

如何将JPA(Java持久性API)用于具有高级功能(例如缓存和懒惰加载)的对象相关映射?如何将JPA(Java持久性API)用于具有高级功能(例如缓存和懒惰加载)的对象相关映射?Mar 17, 2025 pm 05:43 PM

本文讨论了使用JPA进行对象相关映射,并具有高级功能,例如缓存和懒惰加载。它涵盖了设置,实体映射和优化性能的最佳实践,同时突出潜在的陷阱。[159个字符]

如何将Maven或Gradle用于高级Java项目管理,构建自动化和依赖性解决方案?如何将Maven或Gradle用于高级Java项目管理,构建自动化和依赖性解决方案?Mar 17, 2025 pm 05:46 PM

本文讨论了使用Maven和Gradle进行Java项目管理,构建自动化和依赖性解决方案,以比较其方法和优化策略。

如何将Java的Nio(新输入/输出)API用于非阻滞I/O?如何将Java的Nio(新输入/输出)API用于非阻滞I/O?Mar 11, 2025 pm 05:51 PM

本文使用选择器和频道使用单个线程有效地处理多个连接的Java的NIO API,用于非阻滞I/O。 它详细介绍了过程,好处(可伸缩性,性能)和潜在的陷阱(复杂性,

如何使用适当的版本控制和依赖项管理创建和使用自定义Java库(JAR文件)?如何使用适当的版本控制和依赖项管理创建和使用自定义Java库(JAR文件)?Mar 17, 2025 pm 05:45 PM

本文使用Maven和Gradle之类的工具讨论了具有适当的版本控制和依赖关系管理的自定义Java库(JAR文件)的创建和使用。

如何使用Java的插座API进行网络通信?如何使用Java的插座API进行网络通信?Mar 11, 2025 pm 05:53 PM

本文详细介绍了用于网络通信的Java的套接字API,涵盖了客户服务器设置,数据处理和关键考虑因素,例如资源管理,错误处理和安全性。 它还探索了性能优化技术,我

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境