I'm reading and implementing this tutorial, the author explains everything pretty clearly, the only thing I'm missing is how he decides which trail to use (pg. 12). I understand that one should prefer trails with the least amount of active S-Box and maximize the bias of the trail (in fact, finding the optimal trail seems to be the most important step when trying to apply linear cryptanalysis to a symmetric encryption algorithm).
It seems to me like some sort of dynamic programming problem, but I wonder if there is a general algorithm to solve the problem of finding the optimal trail (or at least, a series of candidate trails).