public boolean[][] computeTransitiveClosure(AdjacencyMatrixGraph g)
    {
	int n = g.getCardV();

	// Array t is triply indexed.  The first index is the
	// iteration number, and the last two indices are the vertex
	// indices i and j.  Think of t as indexed by t[k][i][j].

	// Warning: Since i and j index vertices, they run from 0 to
	// n-1.  Since k indexes an iteration, it runs from 0 to n.
	// But when we use k to index a vertex, we need to subtract 1.
	boolean t[][][] = new boolean[n+1][n][n];

	// Start by computing t[0].
	for (int i = 0; i < n; i++)
	    for (int j = 0; j < n; j++)
		t[0][i][j] = (i == j) || g.edgeExists(i, j);

	// Run the main loop.
	for (int k = 1; k <= n; k++) {
	    for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
		    t[k][i][j] = t[k-1][i][j] ||
			(t[k-1][i][k-1] && t[k-1][k-1][j]);
	}

	return t[n];
    }