We study the problem of similarity detection by sequence alignment with gaps, using a recently established theoretical framework based on the morphology of alignment paths. Alignments of sequences without mutual correlations are found to have scale-invariant statistics. This is the basis for a scaling theory of alignments of correlated sequences. Using a simple Markov model of evolution, we generate sequences with well-de ned mutual correlations and quantify the delity of an alignment in an unambiguous way. The scaling theory predicts the dependence of the delity on the alignment parameters and on the statistical evolution parameters characterizing the sequence correlations. Speci c criteria for the optimal choice of alignment parameters emerge from this theory. The results are veri ed by extensive numerical simulations. Key words: sequence comparison, alignment algorithm, homology; evolution model, longest common subsequence.