The principle of Floyd's algorithm is an algorithm that uses the idea of dynamic programming to find the shortest path between multiple source points in a given weighted graph. It is similar to the Dijkstra algorithm. It is also an algorithm that uses the idea of dynamic programming to find the shortest path between multiple source points in a given weighted graph. Algorithm for finding the shortest path in a weighted graph.

Floyd algorithm Also known as the interpolation method, it is a method that uses the idea of dynamic programming to find multiple sources in a given weighted graph The algorithm for the shortest path between points is similar to Dijkstra's algorithm. The algorithm is named after Robert Floyd, one of the founders, winner of the 1978 Turing Award, and professor of computer science at Stanford University
In computer science, the Floyd-Warshall algorithm is a Algorithm for finding shortest paths in weighted graphs with positive or negative edge weights (but no negative periods). A single execution of the algorithm will find the length (weighted) of the shortest path between all pairs of vertices. Although it does not return details of the path itself, the path can be reconstructed with simple modifications to the algorithm. Versions of this algorithm can also be used to find the transitive closure of a relation R, or (related to the Schulze voting system) the widest path between all pairs of vertices in a weighted graph.
The Floyd-Warshall algorithm is an example of dynamic programming and was published in its currently accepted form by Robert Floyd in 1962. However, it is essentially the same algorithm for finding transitive closures of graphs published previously by Bernard Roy in 1959 and Stephen Warshall in 1962, and is closely related to Kleene's algorithm in 1956) for converting deterministic finite automata Convert to regular expression. The modern formulation of the algorithm as three nested for loops was first described by Peter Ingerman in 1962.
This algorithm is also called Floyd algorithm, Roy-Warshall algorithm, Roy-Floyd algorithm or WFI algorithm.
Core Idea Edit
1. Path Matrix
Find every two of a graph’s weight matrix Shortest path matrix between points.
Starting from the weighted adjacency matrix A=[a(i,j)] n×n of the graph, recursively perform n updates, that is, from the matrix D(0)=A, according to a formula, construct The matrix D(1) is obtained; and the same formula is used to construct D(2) from D(1);...; finally, the matrix D(n) is constructed from D(n-1) using the same formula. The elements in row i and column j of matrix D(n) are the shortest path length from vertex i to vertex j. D(n) is called the distance matrix of the graph. At the same time, a successor node matrix path can also be introduced to record the distance between two points. the shortest path.
Use relaxation technology (relaxation operation) to relax all other points between i and j. So the time complexity is O(n^3);
2. State transition equation
The state transition equation is as follows: map[i,j]:=min {map[i,k] map[k,j],map[i,j]};
map[i,j] represents the shortest distance from i to j, and K is the exhaustive list of i,j Breakpoint, the initial value of map[n,n] should be 0, or follow the meaning of the question.
Of course, if this road is not accessible, special processing must be performed, for example, there is no map[i,k] road.
Algorithm process editor
1, start from any unilateral path. The distance between all two points is the weight of the edge. If there is no edge connecting the two points, the weight is infinite.
2, for each pair of vertices u and v, see if there is a vertex w such that the path from u to w to v is shorter than the known path. If so update it.
Represent the graph as an adjacency matrix G. If there is a path from Vi to Vj, then G[i][j]=d, d represents the length of the path; otherwise G[i][j ] = infinity. Define a matrix D to record the information of the inserted point. D[i][j] represents the point that needs to be passed from Vi to Vj. Initialize D[i][j]=j. Insert each vertex into the graph and compare the distance after the insertion with the original distance, G[i][j] = min( G[i][j], G[i][k] G[k][j] ), if the value of G[i][j] becomes smaller, then D[i][j]=k. G contains the information of the shortest road between two points, while D contains the information of the shortest path.
For example, we want to find the path from V5 to V1. According to D, if D(5,1)=3, it means that the path from V5 to V1 passes through V3 is {V5,V3,V1}. If D(5,3)=3, it means that V5 and V3 are directly connected. If D (3,1)=1, indicating that V3 and V1 are directly connected.
Time complexity and space complexity editing
Time complexity: O(n^3);
Space complexity:O(n ^2)
Advantages and Disadvantages Analysis and Edit
Floyd algorithm is suitable for APSP (All Pairs Shortest Paths, multi-source shortest path). It is a dynamic programming algorithm, dense The graph has the best effect, and the edge weights can be positive or negative. This algorithm is simple and effective. Due to the compact triple loop structure, for dense graphs, the efficiency is higher than executing |V| times of Dijkstra's algorithm and higher than executing |V| times of SPFA algorithm.
Advantages: Easy to understand, can calculate the shortest distance between any two nodes, and the code is simple to write.
Disadvantages: The time complexity is relatively high and it is not suitable for calculating large amounts of data.
The above is the detailed content of What is the principle of floyd algorithm?. For more information, please follow other related articles on the PHP Chinese website!
What is the best free remote access softwareJul 23, 2025 am 01:39 AMDepending on the needs, AnyDesk is suitable for simple remote control, ChromeRemoteDesktop is suitable for cross-platform use, and MicrosoftRemoteDesktop is suitable for connection between Windows devices. AnyDesk has intuitive operation, supports multi-platform, and has convenient transmission functions, but it requires payment for commercial purposes; ChromeRemoteDesktop relies on a browser, does not require installation, is highly secure, but has weak performance; MicrosoftRemoteDesktop is integrated into Windows, is easy to set up, safe and reliable, but has limited cross-platform support; when choosing, you should pay attention to issues such as authorization restrictions, security measures, network speed and advertising interference.
Why can't I hear anyone in my voice chat applicationJul 23, 2025 am 01:34 AMIf you can't hear anyone in a voice chat app, it's usually caused by audio settings, hardware issues, or application-specific errors. First check the audio output settings of the device to make sure that the correct output device is not muted and the correct output device is selected; second check the internal settings of the application to make sure that it is not muted and the speaker is selected correctly; then test the hardware device, such as changing the headset or updating the driver; finally close the background application that may conflict or restart the device to resolve the problem. Follow these steps to troubleshoot most problems can be easily solved.
How to fix 'The requested operation was unsuccessful'Jul 23, 2025 am 01:32 AMWhen encountering the "Therequestedoperation wasunsuccessful" error, it is first clear that the system, program or device operation fails due to insufficient permissions, service failure, driver problems or cache conflicts. 1. Check and install system updates, restart related services such as Bluetooth or printing services; 2. Run programs as administrators or adjust PowerShell execution policies; 3. Check hardware connections, update or roll back drivers; 4. Clean temporary files, reset application data, or clear browser cache. This error can usually be solved by checking the basic problems one by one according to the operating scenario.
How to run a program as an administratorJul 23, 2025 am 01:25 AMIn Windows systems, there are four ways to run programs as administrators. 1. Right-click the program and select "Run as administrator", which is suitable for temporary escalation of authority; 2. Check "Run this program as administrator" in the compatibility settings of program properties to achieve default escalation of authority; 3. Use command line tools such as CMD or PowerShell to enter the runas command, or create a shortcut to enable administrator privileges; 4. Notes include ensuring that there is an administrator account, reasonably setting the UAC level, and troubleshooting the possible reasons for the failure of operation. These methods have applicable scenarios, but all need to pay attention to system security and permission restrictions.
How to fix 'Printer driver is unavailable'Jul 23, 2025 am 01:24 AMWhen encountering the "Printerdriversunavailable" problem, it is usually not a printer hardware failure, but a driver or system setup problem. It can be solved through the following steps: 1. Confirm that the printer model is compatible with the operating system, check the official website support list, and replace the general driver or upgrade the firmware if necessary; 2. Reinstall or update the driver, restart the computer automatically after uninstalling through the Device Manager, or manually download and install from the official website; 3. Check and start the Windows PrintSpooler service, set to automatically start and restart the service; 4. Use the system's own printer troubleshooting tool to automatically detect and fix the problem. Check it in order, and most cases can be solved.
How to fix 'Ethernet doesn't have a valid IP configuration'Jul 23, 2025 am 01:23 AMRelease the IP address: Enter "ipconfig/release" in the command prompt and press Enter. This step will remove the IP address currently occupied by the computer. 2. Renew the IP address: Then enter "ipconfig/renew" and press Enter, the system will try to re-get the new IP address from the router. Through these two steps, network connection failures caused by IP allocation problems can be fixed.
How to fix 'A component of the operating system has expired'Jul 23, 2025 am 01:22 AMWhen the prompt "Acomponent of the operating system has expired" appears, first confirm whether the system is genuine; if it is an unofficial or beta system, you need to upgrade to the official version, clean up the residual components and avoid using the unofficial system again. Specific steps: 1. Check the activation status, if it is not activated, enter the genuine key; 2. Download the Microsoft official ISO production boot disk, back up the data and perform custom installation; 3. Run the sfc/scannow and DISM commands to repair the system files, and use tools to clean up the residual services if necessary; 4. It is recommended to download the original system from the official website or purchase equipment with pre-installed genuine system, avoid using the Ghost system or permanent activation tool to ensure safety and stability
How to fix 'This installation package could not be opened'Jul 23, 2025 am 01:12 AMThe installer error "This installation package cannot be opened" is usually caused by installation files or system settings problems. You can try the following methods to solve it: 1. Check whether the installation files are corrupt, re-download and verify file integrity; 2. Run the installer as an administrator; 3. Enable the Windows Installer service and run the system file inspector if necessary; 4. Temporarily disable the antivirus software to prevent interference; 5. Adjust the compatibility settings to adapt to the older version of Windows. In most cases, you can quickly fix the problem by starting with simple steps.

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools







