


Find the shortest path between any two nodes using the Floyd-Warshal algorithm
C There is a macro, which is defined as a piece of code or an expected value, and it will be reused whenever the user needs it. The Floyd-Walshall algorithm is the process of finding the shortest path between all pairs of vertices in a given weighted graph. The algorithm follows a dynamic programming approach to find the minimum weight graph.
Let us understand the meaning of Freud-Walschal algorithm through diagrams -

Taking vertex 1 as the source and vertex 4 as the destination, find the shortest path between them.
We have seen that there are two paths that can be connected to target vertex 4.
1 -> 4 – The edge has a weight of 5
1 -> 8 -> 3 -> 4 – The edge weight (1 2 1) is 4.
In the given graph I, we see the smallest edge connecting two vertices. So here the path between vertex 8 and vertex 3 connecting vertex 1 to vertex 4 and the edge is 4. On the other hand, there is a directed edge between vertex 1 and vertex 4, and the edge weight is 5.
Now we compare two weights, namely 4 and 5. Therefore, here 4 is the minimum weight of the graph calculated according to the Floyd Warshall algorithm.
In this article, we will use Floyd Warshall algorithm to find the shortest path between any two given nodes.
grammar
The following syntax is used in the program -
data_type[][] array_name;
parameter
data_type[][] - The data type mentioned by the user. The first array represents rows and the second array represents columns. So, this represents a two-dimensional array.
array_name - The name given to the array.
algorithm
We will start the program with the header file 'iostream' and define the macro position as 'INF' (infinity) because later it will satisfy the two-dimensional Array matrices and if-else statements.
Next, we create a global function definition named 'printPath' and accepts three parameters, namely 'parent[]', 'i' and 'j' Verify that the path exists.
Then we start the main function and store the value ‘4’ into the variable ‘V’, which represents the number of vertices. Second, store the value in the form of an adjacency matrix into the variable 'dist[V][V]'. As we know, dist[i][j] represents a 2D matrix, which defines the weight of the edge from 'i' to 'j', By storing the adjacency matrix.
Next, we are initializing the 2D array for the variable 'parent', and the size is [V][V].
By using two nested for loops, we will find the shortest path. The first for loop iterates over the rows in the matrix. Then, iterate over the columns in the matrix through the second for loop and then we will check the following condition using if-else statement -
If (dist[i][j] != INF && i != j) { The Chinese translation of parent[i][j] = i;}
is: parent[i][j] = i;}In the if statement, we use the 'and' (&&) operator to express two conditions. If both conditions are met, then 'i' will be stored in 'parent[i ][j]', thereby verifying that the path exists.
other{ The Chinese translation of parent[i][j] = -1;}
is: parent[i][j] = -1;}In the else statement, we initialize "-1" to "parent[i][j]" to verify that the path does not exist.
Now we start with three nested for loops and apply k vertices in the range 0 to V-1, with the help of this range, our shortest distance and the path will be updated. In the third nested loop we use the following condition like
In this variable we use to display each pair of vertices 'i' and 'j' w.r.t 'parent[i][j]'< /b> .
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]; }
Next, we print out the shortest distance and path of all vertex pairs by giving the following conditions
By using two nested for loops, we print the shortest distance and path.
By using the if statement under the second for loop, we will check if dist[i][j] is equal to infinity. If it is found to be infinity, print "INF", otherwise print the shortest path.
Finally, we call the function named 'printPath()' by passing three parameters (parent[i], i, and j.
Here K is updating the row and column parts of the two-dimensional array matrix, which verifies the newly updated shortest path and distance.
Example
In this program, we will use Floyd Warshall algorithm to calculate the shortest path between any two nodes.
#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个顶点来更新行和列。在我们的日常生活中,我们在谷歌地图上搜索最短路线或路径,从一个起点到目的地。
The above is the detailed content of Find the shortest path between any two nodes using the Floyd-Warshal algorithm. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In trees, the term "sum of the shortest paths of all pairs of nodes" refers to the calculation of the sum of the individual shortest paths of all pairs of nodes. An effective method is to use the dual DFS (depth first search) algorithm. The distance between the selected node and every other node is determined during the first DFS pass. The tree is traversed again during the second DFS pass, considering each node as a potential LCA (lowest common ancestor) and calculating the sum of distances between pairs of descendant nodes of the selected LCA. Using this method you can calculate the sum of the shortest paths for all pairs of nodes in the tree and ensure an ideal solution Methods used Dual DFS (Depth First Search) method Dynamic programming method Dual DFS (Depth First Search) method for the tree All pairs of shortest paths

When doing computer programming, sometimes it is necessary to find the minimum weight of a subtree originating from a specific node, provided that the subtree cannot contain nodes that are more than D units away from the specified node. This problem arises in various fields and applications, including graph theory, tree-based algorithms, and network optimization. A subtree is a subset of a larger tree structure, with the specified node serving as the root node of the subtree. A subtree contains all descendants of the root node and their connecting edges. A node's weight refers to a specific value assigned to that node, which can represent its importance, significance, or other relevant metrics. In this problem, the goal is to find the minimum weight among all nodes in a subtree while limiting the subtree to nodes that are at most D units away from the root node. In the following article, we will delve into the complexity of mining minimum weights from subtrees

How to implement the node copy and cut functions of mind maps through Vue and jsmind? Mind map is a common thinking tool that can help us organize our thoughts and sort out our thinking logic. The node copy and cut functions are commonly used operations in mind maps, which allow us to reuse existing nodes more conveniently and improve the efficiency of thinking organization. In this article, we will use the two tools Vue and jsmind to implement the node copy and cut functions of the mind map. First, we need to install Vue and jsmind and create

Overview of how to use Java to implement the shortest path algorithm: The shortest path algorithm is an important application in graph theory and is widely used in network routing, map navigation and other fields. In this article, we will learn how to implement the shortest path algorithm using Java and provide concrete code examples. Algorithm idea: There are many ways to implement the shortest path algorithm, among which the two most famous algorithms are Dijkstra algorithm and A* algorithm. Here we will focus on the implementation of Dijkstra's algorithm. The basis of Dijkstra's algorithm

The methods for deleting nodes in js are: 1. The removeChild() method is used to remove the specified child node from the parent node. It requires two parameters. The first parameter is the child node to be deleted, and the second parameter is the parent node. Node; 2. The parentNode.removeChild() method can be called directly through the parent node to delete the child node; 3. The remove() method can directly delete the node without specifying the parent node; 4. The innerHTML attribute is used to delete the node. content.

C++ has a macro, which is defined as a piece of code or an expected value, and it will be reused whenever the user needs it. The Floyd-Walshall algorithm is the process of finding the shortest path between all pairs of vertices in a given weighted graph. The algorithm follows a dynamic programming approach to find the minimum weight graph. Let us understand the meaning of Floyd-Walshall algorithm through a diagram - take vertex 1 as the source and vertex 4 as the destination and find the shortest path between them. We have seen that there are two paths that can be connected to the target vertex 4. 1->4 – the edge has a weight of 51->8->3->4 – the edge weight (1+2+1) is 4. In the given graph I, we see the smallest edge connecting two vertices. So here the vertex

This article mainly introduces how to create, delete, append and replace element nodes in js. I hope it will be helpful to friends in need!

To check if a given path between two centers of a graph conforms to the shortest path, this can be calculated by comparing the entire edge weight along the given path to the shortest distance between combinations of the same centers using a reliable shortest path method, such as Dijkstra's calculation or Floyd−Warshall calculation. If all edge weights on a given path match the most limited deletion, then it represents the simplest path. Also: If the overall edge weight is more prominent than the shortest distance, it indicates that there is a short distance between the two centers in the graph. Methods Used Dijkstra's Algorithm Floyd−Warshall Algorithm with Edge Reversal Cost Greedy Algorithm Dijkstra's calculation may be a popular graph traversal calculation
