热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

floyd算法代码_FloydWarshall算法及其伪代码

floyd算法代码弗洛伊德沃霍尔(FloydWarshall)TheFloydWarshallalgorithm,itisthealgorithminwhichthereisthe

floyd算法代码

弗洛伊德·沃霍尔 (Floyd Warshall)

The Floyd Warshall algorithm, itis the algorithm in which there is the use of different characterization of structure for a shortest path that we used in the matrix multiplication which is based on all pair algorithms. The algorithm considers the intermediate vertices of the shortest path, where an intermediate vertex of a simple path p = is any vertex of p other than v1 or VL, that is, a vertex in the set {v2, v3,..., vl-1}.

Floyd Warshall算法是一种算法,其中我们在基于所有对算法的矩阵乘法中使用了最短路径的不同结构表征。 该算法考虑最短路径的中间顶点,其中简单路径p = 的中间顶点是除v1或VL以外的p的任何顶点,即设置{v2,v3,...,vl-1} 。

The Floyd Warshall algorithm is based on the following observation. Suppose the vertices of the G be V = {1, 2,..., n}, and assume a subset {1, 2, ..., k} of the vertices for some k. For any pair of vertices i, j £ v consider that all paths from i to j which have their intermediate vertices all drawn from {1, 2,..., k}, and now let p be the minimum weight path from among them. The relationship between path p and shortest paths from i to j with all intermediate vertices in a set {1, 2,..., k-1} is exploited by the Floyd Warshall algorithm.

Floyd Warshall算法基于以下观察。 假设G的顶点是V = {1,2,...,N},并且假设顶点对某个k的子集{1,2,...,K}。 对于任何一对顶点i , j£v都认为从i到j的所有路径的中间顶点都取自{1,2,...,k} ,现在令p是其中的最小权重路径。 Floyd Warshall算法利用路径p和从i到j的最短路径之间的所有中间顶点在集合{1,2,...,k-1}中的关系。

Let the weight of a shortest path from vertex i to the vertex j with all intermediate vertices in the set {1, 2, ..., k} be d (ij) ^ (k), when k = 0, a path from vertex i to vertex j with no intermediate vertex numbered higher than 0 has no intermediate vertices at all. It has at most one edge, hence (ij) ^ (0) = w (ij), A recursive definition is given by

设从顶点i到顶点j且集合{1,2,...,k}中所有中间顶点的最短路径的权重为d(ij)^(k) ,当k = 0时 ,从从顶点i到顶点j ,没有中间顶点的编号大于0,根本没有中间顶点。 它最多具有一个边,因此(ij)^(0)= w(ij) ,递归定义为

d (ij) ^ (k) ={ w (ij) if k = 0
{Min (d (ij) ^ (k-1), d (ik) ^ (k-1) + d (kj) ^ (k-1) if k >= 1

Floyd Warshall算法 (Floyd Warshall Algorithm)

1. n = rows [W]
2. D ^ (0) = W
3. For k = 1 to n
4. Do for I = 1 to n
5. Do for j = 1 to n
6. d (ij) ^ (k) = min (d(ij) ^ (k-1), d (ik) ^ (k-1) + d(kj) ^ (k-1))
7. return D ^ (n)

The running time of the Floyd Warshall algorithm is determined by the triply nested for loop of line 3 to 6. Each execution of the line 6 takes O (1) time. Thus the algorithm runs in O (n ^ 3) time.

Floyd Warshall算法的运行时间由第3行到第6行的循环的三重嵌套确定。第6行的每次执行花费O(1)时间。 因此,该算法运行时间为O(n ^ 3)。

In the Floyd Warshall algorithm, there are many ways for the constructing the shortest paths. One way is to compute the matrix D of the shortest path weights and then construct the predecessor matrix π from the matrix D. This method can be implemented to run in O (n ^ 3) time.

Floyd Warshall算法中 ,有许多方法可以构造最短路径。 一种方法是计算最短路径权重的矩阵D,然后从矩阵D构造先前的矩阵π。可以将该方法实现为在O(n ^ 3)时间内运行。

Example:

例:

floyd warshall

The sequence of matrices D ^ (k) and π ^ k (k computed by the Floyd Warshall algorithm) for given graph is computed as follows:

矩阵d ^(k)和π^ K(K 内由Floyd-Warshall算法计算的)对于给定的图形的序列被计算为如下:

floyd warshall 1

References:

参考文献:

  • Introduction to Algorithms, 3rd Edition

    算法简介,第3版

  • CHAPTER 26: ALL-PAIRS SHORTEST PATHS

    第26章:全对最短路径

  • All-Pairs Shortest Paths

    全对最短路径

翻译自: https://www.includehelp.com/algorithms/floyd-warshall-algorithm-with-its-pseudo-code.aspx

floyd算法代码



推荐阅读
  • 本文讨论了clone的fork与pthread_create创建线程的不同之处。进程是一个指令执行流及其执行环境,其执行环境是一个系统资源的集合。在调用系统调用fork创建一个进程时,子进程只是完全复制父进程的资源,这样得到的子进程独立于父进程,具有良好的并发性。但是二者之间的通讯需要通过专门的通讯机制,另外通过fork创建子进程系统开销很大。因此,在某些情况下,使用clone或pthread_create创建线程可能更加高效。 ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • This article discusses the efficiency of using char str[] and char *str and whether there is any reason to prefer one over the other. It explains the difference between the two and provides an example to illustrate their usage. ... [详细]
  • SpringMVC接收请求参数的方式总结
    本文总结了在SpringMVC开发中处理控制器参数的各种方式,包括处理使用@RequestParam注解的参数、MultipartFile类型参数和Simple类型参数的RequestParamMethodArgumentResolver,处理@RequestBody注解的参数的RequestResponseBodyMethodProcessor,以及PathVariableMapMethodArgumentResol等子类。 ... [详细]
  • oracle主键和唯一索引,Oracle 主键、唯一键与唯一索引的区别
    如果我们让主键约束或者唯一键约束失效,Oracle自动创建的唯一索引是否会受到影响?SQLdroptabletestpurge;Tabledroppe ... [详细]
  • 这篇文章主要讲解了“怎么用Python写一个电信客户流失预测模型”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入, ... [详细]
  • 电话号码的字母组合解题思路和代码示例
    本文介绍了力扣题目《电话号码的字母组合》的解题思路和代码示例。通过使用哈希表和递归求解的方法,可以将给定的电话号码转换为对应的字母组合。详细的解题思路和代码示例可以帮助读者更好地理解和实现该题目。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • 本文讨论了一个数列求和问题,该数列按照一定规律生成。通过观察数列的规律,我们可以得出求解该问题的算法。具体算法为计算前n项i*f[i]的和,其中f[i]表示数列中有i个数字。根据参考的思路,我们可以将算法的时间复杂度控制在O(n),即计算到5e5即可满足1e9的要求。 ... [详细]
  • 李逍遥寻找仙药的迷阵之旅
    本文讲述了少年李逍遥为了救治婶婶的病情,前往仙灵岛寻找仙药的故事。他需要穿越一个由M×N个方格组成的迷阵,有些方格内有怪物,有些方格是安全的。李逍遥需要避开有怪物的方格,并经过最少的方格,找到仙药。在寻找的过程中,他还会遇到神秘人物。本文提供了一个迷阵样例及李逍遥找到仙药的路线。 ... [详细]
  • 本文介绍了在wepy中运用小顺序页面受权的计划,包含了用户点击作废后的从新受权计划。 ... [详细]
  • 本文讨论了编写可保护的代码的重要性,包括提高代码的可读性、可调试性和直观性。同时介绍了优化代码的方法,如代码格式化、解释函数和提炼函数等。还提到了一些常见的坏代码味道,如不规范的命名、重复代码、过长的函数和参数列表等。最后,介绍了如何处理数据泥团和进行函数重构,以提高代码质量和可维护性。 ... [详细]
author-avatar
多米音乐_35782132
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有