python warshall algorithm stackoverflow 1

python warshall algorithm stackoverflow

def warshall(a):
    assert (len(row) == len(a) for row in a)
    n = len(a)
    for k in range(n):
        for i in range(n):
            for j in range(n):
                a[i][j] = a[i][j] or (a[i][k] and a[k][j])
    return a

Here is what the above code is Doing:
1. It’s checking that the matrix is square.
2. It’s setting n to the length of the matrix.
3. It’s iterating over the rows of the matrix.
4. It’s iterating over the columns of the matrix.
5. It’s setting the value of the current cell to the logical OR of the current cell and the logical AND of the cell above it and the cell to the left of it.

Similar Posts