邻接矩阵图实现

内特

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

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

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方法更改为 not re-allocate adjMat,该main方法已在方法中分配我还在floydWarshall你的arrayCondenser方法中删除了重复的调用我还修改了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();
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章