gpt4 book ai didi

java - 邻接矩阵图实现

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:57:53 31 4
gpt4 key购买 nike

我正在尝试使用邻接矩阵实现以下图表: graph

正在编写的程序将找到从每家商店到所有其他商店的最短距离。这是正在使用的代码:

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)
{
adjMat = new int[N][N];
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)
{
int[] array = {A,B,C,D,E,F,G,H,I,W};
adjMat = floydWarshall(adjMat, N);




return adjMat;
}

public static void printGrid(int[][] adjMat)
{
for (int i=0; i<NUM_NODES; ++i)
{
for (int j=0; j<NUM_NODES; ++j)
{
if (adjMat[i][j]==INF)
System.out.printf("%5s", "INF");
else
System.out.printf("%5d",adjMat[i][j]);
}
System.out.println();
}
}

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();
}
}

如何将 100 x 100 数组压缩为 10 x 10,其中仅包含图中突出显示节点的所有对最短路径?

最佳答案

我已经修改了您的 Floyd-Warshall 实现,以便为邻接矩阵的对角线元素正确初始化 adjMat,它的值应该为 0。我还更改了 floydWarshall 方法不重新分配 adjMat,它已经在 main 方法中分配。我还在您的 arrayCondenser 方法中删除了对 floydWarshall 的重复调用。我还修改了 arrayCondenser 方法来计算压缩数组,并添加了一个 printCondensedGrid 方法来显示压缩数组:

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) {
for (int i = 0; i < adjMat.length; i++)
for (int j = 0; j < adjMat.length; j++) {
if (i == j) {
adjMat[i][j] = 0;
} else {
adjMat[i][j] = INF;
}
}

for (int x = 0; x < adjMat.length; 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 void floydWarshall(int[][] adjMat) {
// commented this out because you are also allocating
// adjMat in the main method.
//adjMat = new int[adjMat.length][adjMat.length];
initialize(adjMat);

for (int k = 0; k < adjMat.length; ++k) {
for (int i = 0; i < adjMat.length; ++i) {
for (int j = 0; j < adjMat.length; ++j) {
adjMat[i][j] = Math.min(adjMat[i][j], adjMat[i][k] + adjMat[k][j]);
}
}
}

//return adjMat;
}

public static int[][] arrayCondenser(int[][] adjMat, int [] array) {
int[][] condensedArray = new int[array.length][array.length];
//adjMat = floydWarshall(adjMat, N);

for (int storeFrom = 0; storeFrom < array.length; storeFrom++) {
for (int storeTo = 0; storeTo < array.length; storeTo++) {
condensedArray[storeFrom][storeTo] = adjMat[array[storeFrom]][array[storeTo]];
}
}

return condensedArray;
}

public static void printGrid(int[][] adjMat) {
System.out.println("Adjacency Matrix: ");
System.out.printf("%5s", " ");
for (int i = 0; i < adjMat.length; i++) {
System.out.printf("%5d", i);
}
System.out.println();
System.out.printf("%4s+", " ");
for (int i = 0; i < adjMat.length; i++) {
System.out.printf("%5s", "===");
}
System.out.println();
for (int i = 0; i < adjMat.length; ++i) {
System.out.printf("%4d|", i);

for (int j = 0; j < adjMat[i].length; ++j) {
if (adjMat[i][j] == INF)
System.out.printf("%5s", "INF");
else
System.out.printf("%5d", adjMat[i][j]);
}
System.out.println();
}
}
public static void printCondensedGrid(int[][] adjMat, int stores[]) {
System.out.println("Condensed grid: ");
System.out.printf("%5s", " ");
for (int i = 0; i < stores.length; i++) {
System.out.printf("%5d", stores[i]);
}
System.out.println();
System.out.printf("%4s+", " ");
for (int i = 0; i < adjMat.length; i++) {
System.out.printf("%5s", "===");
}
System.out.println();
for (int i = 0; i < adjMat.length; ++i) {
System.out.printf("%4d|", stores[i]);

for (int j = 0; j < adjMat[i].length; ++j) {
if (adjMat[i][j] == INF)
System.out.printf("%5s", "INF");
else
System.out.printf("%5d", adjMat[i][j]);
}
System.out.println();
}
}

public static void main(String[] args) {

int adjMat[][] = new int[NUM_NODES][NUM_NODES];
int[] stores = { A, B, C, D, E, F, G, H, I, W };

floydWarshall(adjMat);

printGrid(adjMat);
int condensedArray[][] = arrayCondenser(adjMat, stores);
printCondensedGrid(condensedArray, stores);

System.out.println();
}
}

关于java - 邻接矩阵图实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42589662/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com