简体   繁体   中英

How to automate a loop function to work (x) times/make it work recursively

I want to create a distance-matrix from an adjacency-matrix (ie. input an adjacency matrix from a function and it works out how many vertices between each vertex and outputs it in a matrix) Example below.

https://imgur.com/a/0k65tkN

I went about the problem using for-loops. The program works in producing a correct matrix, however, it only does so for up to a distance of 3. My for-loops follow a pattern. How can I go about replicating this process for as many times as I want without copying it out 1000 times?

The basic premise is: if [i][j]=1 and [j][k]=1 then [i][k]=2

Is there a better way of doing this?

static void distanceMatrix(int distance, int result[][], int size) {
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                if (adjMatrix[i][j] == 1 && (result[i][k] > 1 || result[i][k] == 0) && distance >= 1 && i != k) {
                    result[i][j] = 1;
                    for (int k = 0; k < size; k++) {
                        if ((adjMatrix[j][k] == 1) && (result[i][k] > 2 || result[i][k] == 0) && distance >= 2
                                && i != k) {
                            result[i][k] = 2;
                            for (int l = 0; l < size; l++) {
                                if ((adjMatrix[k][l] == 1) && (result[i][l] > 3 || result[i][l] == 0) && distance >= 3
                                        && i != l) {
                                    result[i][l] = 3;
                                }
                            }
                        }
                    }
                }
            }
        }
    }

For reference, the parameter inputs are as below:

distance: the maximum distance that should be calculated (ie. if input is 2, then only distances of 0,1,2 are calculated)

result[][]: the empty matrix for the distance matrix to be put into

size: the number of total vertices (matrix will be size x size)

You can basically put all your code which repeats into a recursive method. It is important this method has the necessary parameters, to keep track of the depth, as well as values that are set outside of the repeating part of your code (for example i ).

static void recursiveFunction(int distance, int matrix[][], int size, int row, int prevRow, int depth) {
    for (int i = 0; i < size; i++) {
        if ((adjMatrix[prevRow][i] == 1) && (matrix[row][i] > depth || matrix[row][i] == 0)
                && row != i) {
            matrix[row][i] = depth;
            if (depth < distance) {
                recursiveFunction(distance, matrix, size , row, i, depth +1);
            }
        }
    }
}

static void distanceMatrix(int distance, int result[][], int size) {
    for (int i = 0; i < size; i++) {
        recursiveFunction(distance, result, size, i, i, 1);
    }
}

Please excuse the uncreative names for the function and the parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM