私は、隣接行列を使用して次のグラフを実装しようとしています: 隣接行列、グラフの実装
書き込まれているプログラムは、他のすべてのストアにすべての店舗からの最短距離を見つけるでしょう。私は、グラフ内の強調表示されたノードのすべての対の最短パスを含む10×10までの100×100アレイを凝縮するにはどうすればよい
public class AdjacencyMatrix
{
public static final int NUM_NODES = 100;
public static final int INF = 99999;
public static final int A = 20;
public static final int B = 18;
public static final int C = 47;
public static final int D = 44;
public static final int E = 53;
public static final int F = 67;
public static final int G = 95;
public static final int H = 93;
public static final int I = 88;
public static final int W = 66;
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 static int[][] floydWarshall(int[][] adjMat, int N)
{
\t adjMat = new int[N][N];
\t initialize(adjMat, N);
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]);
}
}
}
return adjMat;
}
public static int[][] arrayCondenser(int[][] adjMat, int N)
{
\t int[] array = {A,B,C,D,E,F,G,H,I,W};
\t adjMat = floydWarshall(adjMat, N);
\t
\t
\t
\t
\t return adjMat;
}
public static void printGrid(int[][] adjMat)
{
\t \t for (int i=0; i<NUM_NODES; ++i)
\t \t {
\t \t for (int j=0; j<NUM_NODES; ++j)
\t \t {
\t \t if (adjMat[i][j]==INF)
\t \t System.out.printf("%5s", "INF");
\t \t else
\t \t System.out.printf("%5d",adjMat[i][j]);
\t \t }
\t \t System.out.println();
\t \t }
}
public static void main(String[] args)
{
int adjMat[][] = new int[NUM_NODES][NUM_NODES];
adjMat = floydWarshall(adjMat, NUM_NODES);
printGrid(adjMat);
int A,B,C,D,E,F,G,H,I,W;
System.out.println();
}
}
:これは、使用されるコードであります?
私は、フォーム 'adjMatのあなたのループ内の任意のステートメントが表示されない[x]の[ x-1] = 1 'となり、奇数行の画像にはエッジが存在する。 –
奇数行の奇数列の場合は、 'adjMat [x] [x-10] = 1'という形式の文が必要ですが、偶数行の奇数列の場合にのみ発生します。 –
また、ディメンションには100要素(索引付けされた0〜99)が必要ですが、99しか持たないのです。 –