搜索
首页后端开发C++使用弗洛伊德-沃沙尔算法找到任意两个节点之间的最短路径

使用弗洛伊德-沃沙尔算法找到任意两个节点之间的最短路径

Sep 20, 2023 pm 02:21 PM
节点最短路径弗洛伊德-沃沙尔算法

C++有一个宏,它被定义为一段代码或期望的值,并且每当用户需要时,它将被重复使用。弗洛伊德-沃尔夏尔算法是在给定的加权图中找到所有顶点对之间最短路径的过程。该算法遵循动态规划的方法来找到最小权重图。

让我们通过图表来理解弗洛伊德-沃尔夏尔算法的含义 -

使用弗洛伊德-沃沙尔算法找到任意两个节点之间的最短路径

以顶点1为源,顶点4为目的地,求它们之间的最短路径。

我们已经看到有两条路径可以连接到目标顶点4。

  • 1 -> 4 – 边的权重为5

  • 1 -> 8 -> 3 -> 4 – 边权重(1+2+1)为4。

在给定的图 I 中,我们看到两个顶点之间连接的最小边。所以这里顶点 8 和顶点 3 连接顶点 1 到顶点 4 的路径以及边是4。另一方面,顶点1到顶点4之间有一条有向边,边的权重为5

现在我们比较两个权重,即45。因此,这里 4 是根据 Floyd Warshall 算法计算的图的最小权重。

在本文中,我们将使用 Floyd Warshall 算法求解任意两个给定节点之间的最短路径。

语法

以下语法用于程序中 -

data_type[][] array_name;

参数

data_type[][] - 用户提到的数据类型。第一个数组代表行,第二个数组代表列。所以,这代表了二维数组。

array_name - 为数组指定的名称。

算法

  • 我们将使用头文件 'iostream' 启动程序,并将宏位置定义为 'INF'(无穷大),因为稍后它将满足二维数组矩阵和 if-else 语句。

  • 接下来,我们创建名为 'printPath' 的全局函数定义,并接受三个参数,即 'parent[]'、'i''j' 验证路径是否存在。

  • 然后我们开始主函数,并将值‘4’存储到变量‘V’中,该变量表示顶点的数量。其次,将值以邻接矩阵的形式存储到变量‘dist[V][V]’中。正如我们所知,dist[i][j]表示2D矩阵,它定义了从‘i’‘j’的边的权重,通过存储邻接矩阵。

  • 接下来,我们正在为变量‘parent’初始化2D数组,并且大小为[V][V]

  • 在此变量中,我们用来显示每对顶点 'i''j' w.r.t 'parent[i][j]'.

  • 通过使用两个嵌套的for循环,我们将找到最短路径。第一个for循环迭代矩阵中的行。然后,通过第二个for循环迭代矩阵中的列,然后我们将使用if-else语句检查以下条件 -

    • 如果 (dist[i][j] != INF && i != j) { parent[i][j] = i;}

      的中文翻译为:parent[i][j] = i;}

      在if语句中,我们使用‘and’ (&&)运算符来表示两个条件,如果这两个条件都满足,那么‘i’将被存储到‘parent[i][j]’中,从而验证路径存在。

    • 其他{ parent[i][j] = -1;}

      的中文翻译为:parent[i][j] = -1;}

      在 else 语句中,我们将“-1”初始化为“parent[i][j]”,以验证该路径不存在。

  • 现在我们从三个嵌套的 for 循环开始,并在 0 到 V-1 的范围内应用 k 个顶点,在这个范围的帮助下,我们的最短距离和路径将被更新。在第三个嵌套循环中,我们使用以下条件,例如

if (dist[i][j] < dist[i][k] + dist[k][j]){
   dist[i][j] = dist[i][k] + dist[k][j]; 
   parent[i][j] = parent[k][j];	
}

    这里 K 正在更新二维数组矩阵中的行和列的部分,这验证了新更新的最短路径和距离。

  • 接下来,我们通过给定以下条件,打印出所有顶点对的最短距离和路径

    • 通过使用两个嵌套的 for 循环,我们打印最短距离和路径。

    • 通过在第二个for循环下使用if语句,我们将检查dist[i][j]是否等于无穷大。如果发现它是无穷大,则打印“INF”,否则打印最短路径。

  • 最后,我们调用名为 'printPath()' 的函数,通过传递三个参数(parent[i]、i、和j。

示例

在这个程序中,我们将使用Floyd Warshall算法计算任意两个节点之间的最短路径。

#include <iostream> 
using namespace std; 
#define INF 1000000000 // Infinity

void printPath(int parent[], int i, int j) {
   if (i == j) 
      cout << i << " "; 
   else if (parent[j] == -1) 
     cout << "No path exists"; 
   else
   { 
      printPath(parent, i, parent[j]); 
      cout << j << " "; 
   }
} 

int main() 
{
   int V = 4; 
   // V represent number of vertices
   int dist[V][V] = {{0, 2, INF, 4}, 
      {6, 0, 5, 3}, 
      {INF, 10, 0, 1}, 
      {7, 9, 8, 0}}; 
   // Represent the graph using adjacency matrix

   // Apply the Floyd Warshall algorithm to find the shortest paths
   int parent[V][V];
   for (int i = 0; i < V; i++) 
   { 
      for (int j = 0; j < V; j++) 
      {
         if (dist[i][j] != INF && i != j)
         parent[i][j] = i;
         else
         parent[i][j] = -1;
      }
   }
   // update the path and distance using the k vertex range from 0 to V-1.
   for (int k = 0; k < V; k++) 
   { 
      for (int i = 0; i < V; i++) 
      { 
         for (int j = 0; j < V; j++) 
         { 
            if (dist[i][j] > dist[i][k] + dist[k][j]) 
            {
               dist[i][j] = dist[i][k] + dist[k][j];
               parent[i][j] = parent[k][j];	
            }
         }
      }
   }

   // Print shortest distances and paths between all pairs of vertices
   for (int i = 0; i < V; i++) 
   { 
      for (int j = 0; j < V; j++) 
      { 
         cout << "The Shortest distance between " << i << " and " << j << " is ";
         if (dist[i][j] == INF) 
            cout << "INF "; 
         else
            cout << dist[i][j] << " ";

         cout << "and the shortest path is:- ";
         printPath(parent[i], i, j);
         cout << endl;
      } 
   } 

   return 0; 
}

输出

The Shortest distance between 0 and 0 is 0 and the shortest path is:- 0 
The Shortest distance between 0 and 1 is 2 and the shortest path is:- 0 1 
The Shortest distance between 0 and 2 is 7 and the shortest path is:- 0 1 2 
The Shortest distance between 0 and 3 is 4 and the shortest path is:- 0 3 
The Shortest distance between 1 and 0 is 6 and the shortest path is:- 1 0 
The Shortest distance between 1 and 1 is 0 and the shortest path is:- 1 
The Shortest distance between 1 and 2 is 5 and the shortest path is:- 1 2 
The Shortest distance between 1 and 3 is 3 and the shortest path is:- 1 3 
The Shortest distance between 2 and 0 is 8 and the shortest path is:- 2 3 0 
The Shortest distance between 2 and 1 is 10 and the shortest path is:- 2 1 
The Shortest distance between 2 and 2 is 0 and the shortest path is:- 2 
The Shortest distance between 2 and 3 is 1 and the shortest path is:- 2 3 
The Shortest distance between 3 and 0 is 7 and the shortest path is:- 3 0 
The Shortest distance between 3 and 1 is 9 and the shortest path is:- 3 1 
The Shortest distance between 3 and 2 is 8 and the shortest path is:- 3 2 
The Shortest distance between 3 and 3 is 0 and the shortest path is:- 3 

结论

我们学习了使用Floyd Warshall算法找到任意两个节点之间的最短路径的概念。我们使用邻接矩阵作为程序的输入,通过它我们找到了最短路径和距离。此外,为了更新路径和距离,我们使用了k个顶点来更新行和列。在我们的日常生活中,我们在谷歌地图上搜索最短路线或路径,从一个起点到目的地。

以上是使用弗洛伊德-沃沙尔算法找到任意两个节点之间的最短路径的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:tutorialspoint。如有侵权,请联系admin@php.cn删除
C:死亡还是简单地发展?C:死亡还是简单地发展?Apr 24, 2025 am 12:13 AM

1)c relevantduetoItsAverity and效率和效果临界。2)theLanguageIsconTinuellyUped,withc 20introducingFeaturesFeaturesLikeTuresLikeSlikeModeLeslikeMeSandIntIneStoImproutiMimproutimprouteverusabilityandperformance.3)

C在现代世界中:应用和行业C在现代世界中:应用和行业Apr 23, 2025 am 12:10 AM

C 在现代世界中的应用广泛且重要。1)在游戏开发中,C 因其高性能和多态性被广泛使用,如UnrealEngine和Unity。2)在金融交易系统中,C 的低延迟和高吞吐量使其成为首选,适用于高频交易和实时数据分析。

C XML库:比较和对比选项C XML库:比较和对比选项Apr 22, 2025 am 12:05 AM

C 中有四种常用的XML库:TinyXML-2、PugiXML、Xerces-C 和RapidXML。1.TinyXML-2适合资源有限的环境,轻量但功能有限。2.PugiXML快速且支持XPath查询,适用于复杂XML结构。3.Xerces-C 功能强大,支持DOM和SAX解析,适用于复杂处理。4.RapidXML专注于性能,解析速度极快,但不支持XPath查询。

C和XML:探索关系和支持C和XML:探索关系和支持Apr 21, 2025 am 12:02 AM

C 通过第三方库(如TinyXML、Pugixml、Xerces-C )与XML交互。1)使用库解析XML文件,将其转换为C 可处理的数据结构。2)生成XML时,将C 数据结构转换为XML格式。3)在实际应用中,XML常用于配置文件和数据交换,提升开发效率。

C#vs. C:了解关键差异和相似之处C#vs. C:了解关键差异和相似之处Apr 20, 2025 am 12:03 AM

C#和C 的主要区别在于语法、性能和应用场景。1)C#语法更简洁,支持垃圾回收,适用于.NET框架开发。2)C 性能更高,需手动管理内存,常用于系统编程和游戏开发。

C#与C:历史,进化和未来前景C#与C:历史,进化和未来前景Apr 19, 2025 am 12:07 AM

C#和C 的历史与演变各有特色,未来前景也不同。1.C 由BjarneStroustrup在1983年发明,旨在将面向对象编程引入C语言,其演变历程包括多次标准化,如C 11引入auto关键字和lambda表达式,C 20引入概念和协程,未来将专注于性能和系统级编程。2.C#由微软在2000年发布,结合C 和Java的优点,其演变注重简洁性和生产力,如C#2.0引入泛型,C#5.0引入异步编程,未来将专注于开发者的生产力和云计算。

C#vs. C:学习曲线和开发人员的经验C#vs. C:学习曲线和开发人员的经验Apr 18, 2025 am 12:13 AM

C#和C 的学习曲线和开发者体验有显着差异。 1)C#的学习曲线较平缓,适合快速开发和企业级应用。 2)C 的学习曲线较陡峭,适用于高性能和低级控制的场景。

C#vs. C:面向对象的编程和功能C#vs. C:面向对象的编程和功能Apr 17, 2025 am 12:02 AM

C#和C 在面向对象编程(OOP)中的实现方式和特性上有显着差异。 1)C#的类定义和语法更为简洁,支持如LINQ等高级特性。 2)C 提供更细粒度的控制,适用于系统编程和高性能需求。两者各有优势,选择应基于具体应用场景。

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脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

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