You need to implement a breadth-first search or a depth-first search. Start at a
, and stop when you reach d
, or when you exhaust all options.
In your case, the depth-first search is somewhat easier to implement, because "plain" C lacks built-in dynamic queues needed for the breadth-first search.
If you do not care about the efficiency and you do not mind updating the matrix, implement the Floyd-Warshall algorithm: it is formulated specifically for adjacency matrices, and takes only five lines to implement:
for (int k = 0 ; k != N ; k++)
for (int i = 0 ; i != N ; i++)
for (int j = 0 ; j != N ; j++)
if (matrix[i][k] && matrix[k][j])
matrix[i][j] = 1;
After running this algorithm, the resultant matrix
contains the transitive closure of the original one.