Floyd Warshall算法,分别用C语言,C++,java,python编写出来
Floyd-Warshall算法,中文称弗洛伊德算法或佛洛伊德算法,是解决任意两点间的最短路径的一种算法,可以正确处理有向图或负权(但不可存在负权回路)的最短路径问题,同时也被用于计算有向图的传递闭包。
Floyd Warshall算法,分别用C语言,C++,java,python编写出来
下面是Floyd-Warshall算法的不同语言实现示例:
1. C语言:
#include <stdio.h>
#define INF 99999
#define V 4
void printSolution(int dist[][V]);
void floydWarshall(int graph[][V]) {
int dist[V][V], i, j, k;
for (i = 0; i < V; i++) {
for (j = 0; j < V; j++) {
dist[i][j] = graph[i][j];
}
}
for (k = 0; k < V; k++) {
for (i = 0; i < V; i++) {
for (j = 0; j < V; j++) {
if (dist[i][k] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
printSolution(dist);
}
void printSolution(int dist[][V]) {
printf(“The following matrix shows the shortest distances between every pair of vertices:\n”);
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (dist[i][j] == INF)
printf(“%7s”, “INF”);
else
printf(“%7d”, dist[i][j]);
}
printf(“\n”);
}
}
//以下为隐藏代码
2. C++:
#include <iostream>
#define INF 99999
#define V 4
void printSolution(int dist[][V]);
void floydWarshall(int graph[][V]) {
int dist[V][V], i, j, k;
for (i = 0; i < V; i++) {
for (j = 0; j < V; j++) {
dist[i][j] = graph[i][j];
}
}
for (k = 0; k < V; k++) {
for (i = 0; i < V; i++) {
for (j = 0; j < V; j++) {
if (dist[i][k] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
printSolution(dist);
}
void printSolution(int dist[][V]) {
std::cout << “The following matrix shows the shortest distances between every pair of vertices:\n”;
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (dist[i][j] == INF)
std::cout << “INF\t”;
else
std::cout << dist[i][j] << “\t”;
}
std::cout << std::endl;
}
}
3. Java:
public class FloydWarshall {
static final int INF = 99999;
static final int V = 4;
void printSolution(int dist[][]) {
System.out.println(“The following matrix shows the shortest distances between every pair of vertices:”);
for (int i = 0; i < V; ++i) {
for (int j = 0; j < V; ++j) {
if (dist[i][j] == INF)
System.out.print(“INF\t”);
else
System.out.print(dist[i][j] + “\t”);
}
System.out.println();
}
}
void floydWarshall(int graph[][]) {
int dist[][] = new int[V][V];
int i, j, k;
for (i = 0; i < V; ++i)
for (j = 0; j < V; ++j)
dist[i][j] = graph[i][j];
for (k = 0; k < V; ++k) {
for (i = 0; i < V; ++i) {
for (j = 0; j < V; ++j) {
if (dist[i][k] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
printSolution(dist);
4. Python:
INF = 99999
V = 4
def printSolution(dist):
print(“The following matrix shows the shortest distances between every pair of vertices:”)
for i in range(V):
for j in range(V):
if dist[i][j] == INF:
print(“INF”, end=”\t”)
else:
print(dist[i][j], end=”\t”)
print()
def floydWarshall(graph):
dist = graph.copy()
for k in range(V):
for i in range(V):
for j in range(V):
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
printSolution(dist)
这些示例展示了使用不同编程语言实现Floyd-Warshall算法的方式。请根据您选择的编程语言,使用相应的示例来实现Floyd-Warshall算法。
扫码关注微信公众号,免费查看完整算法内容。
1.本站资源都是白菜价出售,同样的东西,我们不卖几百,也不卖几十,甚至才卖几块钱,一个永久会员能下载全站100%源码了,所以单独购买也好,会员也好均不提供相关技术服务。
2.如果源码下载地址失效请联系站长QQ进行补发。
3.本站所有资源仅用于学习及研究使用,请必须在24小时内删除所下载资源,切勿用于商业用途,否则由此引发的法律纠纷及连带责任本站和发布者概不承担。资源除标明原创外均来自网络整理,版权归原作者或本站特约原创作者所有,如侵犯到您权益请联系本站删除!
4.本站站内提供的所有可下载资源(软件等等)本站保证未做任何负面改动(不包含修复bug和完善功能等正面优化或二次开发);但本网站不能保证资源的准确性、安全性和完整性,由于源码具有复制性,一经售出,概不退换。用户下载后自行斟酌,我们以交流学习为目的,并不是所有的源码都100%无错或无bug;同时本站用户必须明白,【188资源网】对提供下载的软件等不拥有任何权利(本站原创和特约原创作者除外),其版权归该资源的合法拥有者所有。
5.请您认真阅读上述内容,购买即以为着您同意上述内容。
188资源网 » Floyd Warshall算法,分别用C语言,C++,java,python编写出来