ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Alignment.h File Reference

The alignments of two strings \( P \) and \( T \) of exercises 13.7 to 13.9 of the book: spaces are inserted in \( P \) or \( T \), never one in front of the other, and each column scores \( +1 \) if the characters are equal, \( -1 \) if they differ and \( -2 \) if one is a space. More...

#include <algorithm>
#include "ADS/Contract.h"
#include <cstddef>
#include <string>
#include <utility>
#include <vector>
#include "ADS/string/impl/Alignment.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

Enumerations

enum class  ADS::Alignment { ADS::Alignment::GLOBAL , ADS::Alignment::SEMIGLOBAL , ADS::Alignment::LOCAL }
 the kind of alignment More...

Functions

template<class S>
int ADS::alignmentScore (const S &P, const S &T, Alignment kind)
 the best score of an alignment of \( P \) and \( T \)
template<class S>
std::pair< S, S > ADS::globalAlignment (const S &P, const S &T, typename S::value_type gap=' ')
 a best global alignment, the two strings with the spaces as gap

Detailed description

The alignments of two strings \( P \) and \( T \) of exercises 13.7 to 13.9 of the book: spaces are inserted in \( P \) or \( T \), never one in front of the other, and each column scores \( +1 \) if the characters are equal, \( -1 \) if they differ and \( -2 \) if one is a space.

The best score \( \mathit{DP}[ i ][ j ] \) of the prefixes \( P( i ) \) and \( T( j ) \) is

\[ \max\{ \mathit{DP}[ i ][ j - 1 ] - 2 , \mathit{DP}[ i - 1 ][ j ] - 2 , \mathit{DP}[ i - 1 ][ j - 1 ] + \delta_{ij} \} \]

with \( \delta_{ij} = \pm 1 \), \( \Theta( m n ) \) time:

  • global, \( \mathit{DP}[ i ][ 0 ] = -2 i \) and \( \mathit{DP}[ 0 ][ j ] = -2 j \), the score in \( \mathit{DP}[ m ][ n ] \);
  • semiglobal, the spaces before the start and after the end of either string free: the first row and column are 0, the score the largest of the last row and column;
  • local, the best alignment of two substrings: 0 is a fourth term of the maximum, and the score the largest entry.

The contracts [see Contract.h]: the two strings of a global alignment have the same length, at least that of the longer string. The operations are defined in Alignment.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, exercises 13.7-13.9, https://algoritmicamente.it/materiali/libro/capitoli/cap13.pdf
S. B. Needleman, C. D. Wunsch, A general method applicable to the search for similarities in the amino acid sequence of two proteins, Journal of Molecular Biology 48(3), 443-453, 1970, doi:10.1016/0022-2836(70)90057-4
T. F. Smith, M. S. Waterman, Identification of common molecular subsequences, Journal of Molecular Biology 147(1), 195-197, 1981, doi:10.1016/0022-2836(81)90087-5
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa