- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我已经为一个 100 x 100 的邻接矩阵编写了代码,它表示以下有向图:
我正在尝试使用 Floyd-Warshall 算法为图中所有蓝色节点对找到最短路径。你如何只找到所选节点的所有对最短路径?这是我到目前为止编写的代码:
public class AdjacencyMatrix
{
public static final int NUM_NODES = 100;
public static final int INF = Integer.MAX_VALUE;
public static boolean even(int num)
{
return num%2==0;
}
public static boolean odd(int num)
{
return num%2==1;
}
public static void initialize(int [][] adjMat, int N)
{
for(int i = 0; i < N; i++)
for(int j = 0; j <N; j++)
adjMat[i][j]=INF;
for(int x = 0; x<N; x++)
{
int row = x/10;
int column = x%10;
if (even(row)) {
if (column!=9)
adjMat[x][x+1]=1;
}
if (odd(row)) {
if (column!=0)
adjMat[x][x-1]=1;
}
if (even(column)){
if (row!=9)
adjMat[x][x+10]=1;
}
if (odd(column)) {
if (row!=0)
adjMat[x][x-10]=1;
}
}
}
public void floydWarshall(int[][] adjMat, int N)
{
adjMat = new int[N][N];
initialize(adjMat, NUM_NODES);
for(int k = 0; k < N; ++k) {
for(int i = 0; i < N; ++i) {
for(int j = 0; j < N; ++j) {
adjMat[i][j] = Math.min(adjMat[i][j], adjMat[i][k] + adjMat[k][j]);
}
}
}
}
public static void main(String[] args)
{
int adjMat[][] = new int[NUM_NODES][NUM_NODES];
initialize(adjMat, NUM_NODES);
int A,B,C,D,E,F,G,H,I,W;
A = 20;
B = 18;
C = 47;
D = 44;
E = 53;
F = 67;
G = 95;
H = 93;
I = 88;
W = 66;
System.out.println(adjMat[A][B]);
System.out.println();
}
}
最佳答案
首先,不要在floydWarshall()中给adjMat参数赋新值,因为退出后该值不会被保存。
下一步是检查 adjMat[i][k] 和 adjMat[k][j] 是否与 INF 相等,如果是则继续循环:
for(int k = 0; k < N; ++k) {
for(int i = 0; i < N; ++i) {
for(int j = 0; j < N; ++j) {
if (adjMat[i][k] != INF && adjMat[k][j] != INF) {
adjMat[i][j] = Math.min(adjMat[i][j], adjMat[i][k] + adjMat[k][j]);
}
关于java - Floyd Warshall算法实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42614847/
我的输入矩阵如下: 0 1 1 0 0 0 1 0 1 0 0 1 0 0 0 0 我的 Warshall 算法代码如下: int V = A.length; for(int k = 0;
我想在这个图问题中重建从源到目标顶点的路径。 如何存储路径,以及在找到从 s 到 d 的最小成本后如何检索它? 请帮我找到一个简单的答案? 例如在这一点上, adjmat[i][j] = Math.m
是否可以使用邻接表对 Floyd Warshall 进行编码?我必须处理文本文件中的一百万个顶点,因此邻接矩阵不是解决方案。任何实现已经可用?请帮忙。 最佳答案 您不能将 Floyd Warshall
我已经在 MySQL 存储过程中实现了 Warshall 的算法。不幸的是,该过程需要很长时间才能完成。我是编写存储过程的初学者,您知道我可以做些什么来让它更快吗? 简要说明:我正在尝试计算邻接表的传
我实现了 Floyd-Warshall 算法。根据他们的矩阵,我可以得到正确的结果,关于两个地方之间的最短路径和他们的距离。我的问题是如何打印从 i 到 j 的最短距离。我做了一些研究,发现了一个类似
这可能是一个糟糕的问题,因为我的代表很低,但我已经研究了几个小时的其他解决方案,我的代码似乎与我遇到的工作解决方案几乎相同。请不要忽略基于低代表的问题。 输出矩阵 d[][] 包含给定顶点对之间最短路
Mr. Rowan plans to make a walking tour of Paris. However, since he is a little lazy, he wants to tak
假设我有 9 个顶点。所以我有 9x9 解决方案矩阵和 matrix[6,0] = infinity, matrix[6,9]=1, matrix[9,0]=1 现在算法的工作原理如下: for k
This维基百科页面解释了 Floyd Warshall 算法,用于查找图中节点之间的最短路径。维基百科页面使用图像左侧的图表 作为起始图(在 k = 0 时的第一次迭代之前),然后显示剩余的迭代(k
我知道当图中有负权重环时,没有找到最小距离的方法,也就没有最小距离的意义了。我的问题是,如果我们向 Floyd Warshall 算法提供具有负权重循环的图,会发生什么情况?它会在 O(n3) 内无限
有人可以告诉我这个过程在 for 迭代中的时间复杂度吗?这段代码是FloydWarshall算法的“重构路径”部分。prev[n][n]是最短路径中源节点和目的节点之间的节点矩阵。printAllSP
问题陈述:https://www.hackerrank.com/challenges/floyd-city-of-blinding-lights 代码: import scala.io.StdIn._
我正在为一项作业实现 Floyd-Warshall 算法,但输出矩阵不正确。我已经在网上与其他人仔细检查了我的算法,它看起来和其他人一样。我只是错过了什么吗?感谢您的帮助。 我的输入文件是: 4 0
我想使用 Floyd-warshall 算法找到带权无向图任意两个顶点之间的最大距离。为此,我做了一些改动: 我添加负权重而不是正权重。 然后我找出最短路径。 但它没有给我正确的输出。谁能指出我犯的错
由于 Floyd-Warshall 算法是动态的,这意味着它必须始终提供最佳解决方案,对吗?因此,让我感到困惑的是,在算法的每个部分中,这些最佳解决方案的性质是什么——特别是,我试图理解以下三个问题:
我试图解决 INOI 2014 paper 中的第二个问题IE。 FREETICKET 并使用 Floyd-Warshall 算法计算答案。我的代码似乎在最后的子任务中失败,并且似乎为几个测试用例提供
我正在尝试实现 Warshall 算法来查找邻接矩阵的传递闭包。这就是我的功能: public static int[][] warshall(int A[][]){ int R[][] =
我像那里一样实现算法 http://en.algoritmy.net/article/45708/Floyd-Warshall-algorithm . void Graph::floydWarsha
我正在尝试在 python 3 中实现 Warshall 算法,以创建一个矩阵,每个点之间的距离最短。 这应该是一个简单的实现,我制作了一个矩阵并用每个点之间的距离填充它。 但是,我得到了错误的结果,
我正在编写一个程序,该程序使用 Warshall 算法来查找表示关系的矩阵的传递闭包。这是伪代码中算法的链接:http://people.cs.pitt.edu/~adamlee/courses/cs
我是一名优秀的程序员,十分优秀!