I want to calculate the distance between two consecutive trips of a sequence

The expression that I know for that is to compute a matrix between all types of the intervals and given this matrix M[type1,type2] , I can compute the interval between any interval i of type ti and its successor through M[ ti, typeOfNext(i) ]. This works well with optional intervals too

Now, I'm wondering how we can achieve some kind of sparsity in the matrix M. Indeed, i may have a few successors only, so could we take advantage of this in order to compute only a sparse matrix instead of pre-compute the matrix of all types. Since I use primary keys of the intervals as types, this leads indeed to a huge matrix if I want to store all these infos .

In a nutshell, does M absolutely need to be dense, storing all the relations between types whereas the actual possible relations are much less ?

thanks

David