Elliptic geometry: Difference between revisions

From formulasearchengine
Jump to navigation Jump to search
en>Rgdboer
 
en>Quondum
Comparison with Euclidean geometry: 90-90-90 → 90°–90°–90°, as small as desired → arbitrarily small
Line 1: Line 1:
The initially website I would like to share with we is www.startyourdiet.com. You have to register for this website nevertheless a basic membership is completely free. This website has many tools to aid you lose the pounds. There are tools for daily fat monitoring, a tool to program weight reduction goals, an automated goal planning tool, a diet profile, an advanced meal planner and tracker, a daily food diary, plus a weight-tracking chart. There is additionally a calculator for you to figure your body mass index (BMI). www.startyourdiet.com equally has an online community for help as you lose fat. You additionally get your own blog.<br><br>Weight loss is a major issue for almost all of the individuals considering it causes wellness dangers. So if you join CAL system, a issue can be solved conveniently plus securely. During the course of the system, you can have limitless access to the doctor and health staff. We can see weight loss clinics where the doctor could guide we at each step accurately. You can reach know that the rate of weight reduction change in a body in an ongoing basis. Your weekly follow-up to the clinic usually give we accurate information regarding a progress. The doctor will guide we right exercises or in the event you want to have rapid fat reduction, then the doctor might guide you so by providing you with certain drugs.<br><br>This beneficial service was invented by a Belgian polymath called, Adolphe Quetelet back inside the 1800s. It has first been called the Quetelet index before being renamed because the BMI. This is extremely helpful inside keeping track of how much weight you may be losing plus how much fat you require gain to be fit. Combine this with a diet appropriate for a eating habits and cravings plus exercise and sooner or later you will own a magnificently fit and healthy body, with a BMI range of ideal wellness.<br><br>Here is a [http://safedietplansforwomen.com/bmi-calculator bmi calculator] from the CDC (Centers for Disease Control and Prevention). Below is a 2 part video that follows 6 persons with diabetes which goes to Dr. Cousen's center in Arizona for 30 days. The main change the folks produced was to consume a raw plant-based diet.<br><br>Then, moderately active women are a little different! Woman at 119lbs should have 1,200 calories; 120 to 134 should have 1,400 calories; 135 to 144lbs 1,600 calories; and ladies that are 145lbs and over could make certain they get 1,800 calories a day.<br><br>This signifies that the question of the ideal fat range for athletic ladies inside their 40's is a tough nut to crack. Your body is the body, regardless what age you're and regardless what amount of exercise you do. Ladies, in the event you want to have the body of a 21-year-old, you are able to do it.<br><br>There are three main places in California where there are California Medical Weight Management system. San Ramon, Watsonville plus Santa Clara are 3 leading places. Get an appointment fixed with all the doctor from any of the clinic plus fill the free consultation shape online. Losing fat is fun and convenient for certain from here. It is not surprisingly the last time to lose fat should you follow it correctly. Moreover, it delivers countless more benefits which we can get once you join it. You will truly feel a lifetime changed. Hurry up and join the fat reduction program now.
{{Distinguish|longest common substring problem}}
The '''longest common subsequence''' ('''LCS''') '''problem'''  is to find the longest [[subsequence]] common to all sequences in a set of sequences (often just two). (Note that a subsequence is different from a [[substring]], for the terms of the former need not be consecutive terms of the original sequence.) It is a classic [[computer science]] problem, the basis of [[file comparison]] programs such as [[diff]], and has applications in [[bioinformatics]].
<!-- todo: add definition and example -->
 
== Complexity ==
For the general case of an arbitrary number of input sequences, the problem is [[NP-hard]].<ref>{{cite journal| author = David Maier| title = The Complexity of Some Problems on Subsequences and Supersequences| journal = J. ACM| volume = 25| year = 1978| pages = 322&ndash;336| doi = 10.1145/322063.322075| publisher = ACM Press| | issue = 2}}</ref> When the number of sequences is constant, the problem is solvable in polynomial time by [[dynamic programming]] (see ''Solution'' below). Assume you have <math>N</math> sequences of lengths <math>n_1, ..., n_N</math>. A naive search would test each of the <math>2^{n_1}</math> subsequences of the first sequence to determine whether they are also subsequences of the remaining sequences; each subsequence may be tested in time linear in the lengths of the remaining sequences, so the time for this algorithm would be
:<math>O\left( 2^{n_1} \sum_{i>1} n_i\right).</math>
 
For the case of two sequences of ''n'' and ''m'' elements, the running time of the dynamic programming approach is [[Big O notation|O]](''n'' × ''m''). For an arbitrary number of input sequences, the dynamic programming approach gives a solution in
 
:<math>O\left(N \prod_{i=1}^{N} n_i\right).</math>
 
There exist methods with lower complexity,<ref name="BHR00">
{{cite journal | author = L. Bergroth and H. Hakonen and T. Raita | title = A Survey of Longest Common Subsequence Algorithms | journal = SPIRE | volume = 00 | year = 2000 | isbn = 0-7695-0746-8 | pages = 39&ndash;48 | doi = 10.1109/SPIRE.2000.878178 | publisher = IEEE Computer Society}}</ref>
which often depend on the length of the LCS, the size of the alphabet, or both.
 
Notice that the LCS is not necessarily unique; for example the LCS of "ABC" and "ACB" is both "AB" and "AC". Indeed the LCS problem is often defined to be finding ''all'' common subsequences of a maximum length. This problem inherently has higher complexity, as the number of such subsequences is exponential in the worst case,<ref name="">{{cite arxiv | author = Ronald I. Greenberg | title = Bounds on the Number of Longest Common Subsequences  | date = 2003-08-06  | eprint = cs.DM/0301030}}</ref> even for only two input strings.
 
== Solution for two sequences ==
The LCS problem has an [[optimal substructure]]:  the problem can be broken down into smaller, simple "subproblems", which can be broken down into yet simpler subproblems, and so on, until, finally, the solution becomes trivial. The LCS problem also has [[overlapping subproblems]]:  the solution to a higher subproblem depends on the solutions to several of the lower subproblems. Problems with these two properties—optimal substructure and overlapping subproblems—can be approached by a problem-solving technique called [[dynamic programming]], in which the solution is built up starting with the simplest subproblems. The procedure requires [[memoization]]—saving the solutions to one level of subproblem in a table (analogous to writing them to a ''memo'', hence the name) so that the solutions are available to the next level of subproblems.
This method is illustrated here.
 
=== Prefixes ===
The subproblems become simpler as the sequences become shorter.  Shorter sequences are conveniently described using the term ''prefix''.  A prefix of a sequence is the sequence with the end cut off.  Let ''S'' be the sequence (AGCA).  Then, the sequence (AG) is one of the prefixes of ''S''.  Prefixes are denoted with the name of the sequence, followed by a subscript to indicate how many characters the prefix contains.<ref>{{cite book
| last = Xia | first = Xuhua
| title = Bioinformatics and the Cell:  Modern Computational Approaches in Genomics, Proteomics and Transcriptomics
| year = 2007
| publisher = Springer
| location = New York
| page = 24
| isbn = 0-387-71336-0
}}</ref>  The prefix (AG) is denoted ''S''<sub>2</sub>, since it contains the first 2 elements of ''S''.  The possible prefixes of ''S'' are
:''S''<sub>1</sub> = (A)
:''S''<sub>2</sub> = (AG)
:''S''<sub>3</sub> = (AGC)
:''S''<sub>4</sub> = (AGCA).
 
The solution to the LCS problem for two arbitrary sequences, ''X'' and ''Y'', amounts to construct some function, ''LCS''(''X'', ''Y''), that gives the longest subsequences common to ''X'' and ''Y''.  That function relies on the following two properties.
 
=== First property ===
Suppose that two sequences both end in the same element.  To find their LCS, shorten each sequence by removing the last element, find the LCS of the shortened sequences, and to that LCS append the removed element.
:For example, here are two sequences having the same last element:  (BANANA) and (ATANA).
:Remove the same last element. Repeat the procedure until you find no common last element. The removed sequence will be (ANA).
:The sequences now under consideration:  (BAN) and (AT)
:The LCS of these last two sequences is, by inspection, (A).
:Append the removed element, (ANA), giving (AANA), which, by inspection, is the LCS of the original sequences.
 
In terms of prefixes,
: if ''x<sub>n</sub>''=''y<sub>m</sub>'', ''LCS''(''X<sub>n</sub>'', ''Y<sub>m</sub>'') = (''LCS''( ''X<sub>n''-1</sub>, ''Y<sub>m''-1</sub>), ''x<sub>n</sub>'')
where the comma indicates that the following element, ''x<sub>n</sub>'', is appended to the sequence.  Note that the LCS for ''X<sub>n</sub>'' and ''Y<sub>m</sub>'' involves determining the LCS of the shorter sequences, ''X<sub>n''-1</sub> and ''Y<sub>m''-1</sub>.
 
=== Second property ===
Suppose that the two sequences X and Y do not end in the same symbol.
Then the LCS of X and Y is the longer of the two sequences LCS(X<sub>n</sub>,Y<sub>m-1</sub>) and LCS(X<sub>n-1</sub>,Y<sub>m</sub>).
 
To understand this property, consider the two following sequences :
 
sequence X: ABCDEFG (n elements)<br />
sequence Y: BCDGK (m elements)
 
The LCS of these two sequences either ends with a G (the last element of sequence X) or does not.
 
'''Case 1: the LCS ends with a G'''<br />
Then it cannot end with a K. Thus it does not hurt to remove the K from sequence Y: if K were in the LCS, it would be its last character; as a consequence K is not in the LCS. We can then write: LCS(X<sub>n</sub>,Y<sub>m</sub>) = LCS(X<sub>n</sub>, Y<sub>m-1</sub>).
 
'''Case 2: the LCS does not end with a G'''<br />
Then it does not hurt to remove the G from the sequence X (for the same reason as above). And then we can write: LCS(X<sub>n</sub>,Y<sub>m</sub>) = LCS(X<sub>n-1</sub>, Y<sub>m</sub>).
 
In any case, the LCS we are looking for is one of LCS(X<sub>n</sub>, Y<sub>m-1</sub>) or LCS(X<sub>n-1</sub>, Y<sub>m</sub>). Those two last LCS are both common subsequences to X and Y. LCS(X,Y) is the longest. Thus its value is the longest sequence of LCS(X<sub>n</sub>, Y<sub>m-1</sub>) and LCS(X<sub>n-1</sub>, Y<sub>m</sub>).
 
=== ''LCS'' function defined ===
Let two sequences be defined as follows:  ''X'' = (''x''<sub>1</sub>, ''x''<sub>2</sub>...''x''<sub>m</sub>) and ''Y'' = (''y''<sub>1</sub>, ''y''<sub>2</sub>...''y''<sub>n</sub>).  The prefixes of ''X'' are ''X''<sub>1, 2,...m</sub>; the prefixes of ''Y'' are ''Y''<sub>1, 2,...n</sub>.  Let ''LCS''(''X''<sub>''i''</sub>, ''Y''<sub>''j''</sub>) represent the set of longest common subsequence of prefixes ''X<sub>i</sub>'' and ''Y<sub>j</sub>''.  This set of sequences is given by the following.
 
:<math>
LCS\left(X_{i},Y_{j}\right) =
\begin{cases}
  \empty
& \mbox{ if }\ i = 0 \mbox{ or }  j = 0 \\
  \textrm{  } LCS\left(X_{i-1},Y_{j-1}\right) +  1
& \mbox{ if } x_i = y_j \\
  \mbox{longest}\left(LCS\left(X_{i},Y_{j-1}\right),LCS\left(X_{i-1},Y_{j}\right)\right)
& \mbox{ if } x_i \ne y_j \\
\end{cases}
</math>
 
To find the longest subsequences common to ''X<sub>i</sub>'' and ''Y<sub>j</sub>'', compare the elements ''x<sub>i</sub>'' and ''y<sub>j</sub>''.  If they are equal, then the sequence ''LCS''(''X''<sub>''i''-1</sub>, ''Y''<sub>''j''-1</sub>) is extended by that element, ''x<sub>i</sub>''.  If they are not equal, then the longer of the two sequences, ''LCS''(''X''<sub>''i''</sub>, ''Y''<sub>''j''-1</sub>), and ''LCS''(''X''<sub>''i''-1</sub>, ''Y''<sub>''j''</sub>), is retained.  (If they are both the same length, but not identical, then both are retained.)  Notice that the subscripts are reduced by 1 in these formulas.  That can result in a subscript of 0.  Since the sequence elements are defined to start at 1, it was necessary to add the requirement that the LCS is empty when a subscript is zero.
 
=== Worked example ===
The longest subsequence common to ''C'' = (AGCAT), and ''R'' = (GAC) will be found.  Because the ''LCS'' function uses a "zeroth" element, it is convenient to define zero prefixes that are empty for these sequences:  ''C''<sub>0</sub> =  Ø; and ''R''<sub>0</sub> =  Ø.  All the prefixes are placed in a table with ''C'' in the first row (making it a <u>c</u>olumn header) and ''R'' in the first column (making it a <u>r</u>ow header).
 
{| class="wikitable" style="text-align:center"
|+ LCS Strings
|-
!  || 0 || A || G || C || A || T
|-
! 0
| Ø || Ø || Ø || Ø || Ø || Ø
|-
! G
| Ø
|
|
|
|
|
|-
! A
| Ø
|
|
|
|
|
|-
! C
| Ø
|
|
|
|
|
|-
|}
 
This table is used to store the LCS sequence for each step of the calculation.  The second column and second row have been filled in with Ø, because when an empty sequence is compared with a non-empty sequence, the longest common subsequence is always an empty sequence.
 
''LCS''(''R''<sub>1</sub>, ''C''<sub>1</sub>) is determined by comparing the first elements in each sequence.  G and A are not the same, so this LCS gets (using the "second property") the longest of the two sequences, ''LCS''(''R''<sub>1</sub>, ''C''<sub>0</sub>) and  ''LCS''(''R''<sub>0</sub>, ''C''<sub>1</sub>).  According to the table, both of these are empty, so ''LCS''(''R''<sub>1</sub>, ''C''<sub>1</sub>) is also empty, as shown in the table below.  The arrows indicate that the sequence comes from both the cell above, ''LCS''(''R''<sub>0</sub>, ''C''<sub>1</sub>) and the cell on the left, ''LCS''(''R''<sub>1</sub>, ''C''<sub>0</sub>).
 
''LCS''(''R''<sub>1</sub>, ''C''<sub>2</sub>) is determined by comparing G and G.  They match, so G is appended to the upper left sequence, ''LCS''(''R''<sub>0</sub>, ''C''<sub>1</sub>), which is (Ø), giving (ØG), which is (G).
 
For ''LCS''(''R''<sub>1</sub>, ''C''<sub>3</sub>), G and C do not match.  The sequence above is empty; the one to the left contains one element, G.  Selecting the longest of these, ''LCS''(''R''<sub>1</sub>, ''C''<sub>3</sub>) is (G).  The arrow points to the left, since that is the longest of the two sequences.
 
''LCS''(''R''<sub>1</sub>, ''C''<sub>4</sub>), likewise, is (G).
 
''LCS''(''R''<sub>1</sub>, ''C''<sub>5</sub>), likewise, is (G).
 
{| class="wikitable" style="text-align:center"
|+ "G" Row Completed
|-
!  || Ø || A || G || C || A || T
|-
! Ø
| Ø || Ø || Ø || Ø || Ø || Ø
|-
! G
| Ø
| <math>\overset{\ \ \uparrow}{\leftarrow}</math>Ø
| <math>\overset{\nwarrow}{\ }</math>(G)
| <math>\overset{\ }{\leftarrow}</math>(G)
| <math>\overset{\ }{\leftarrow}</math>(G)
| <math>\overset{\ }{\leftarrow}</math>(G)
|-
! A
| Ø
|
|
|
|
|
|-
! C
| Ø
|
|
|
|
|
|-
|}
 
For ''LCS''(''R''<sub>2</sub>, ''C''<sub>1</sub>), A is compared with A.  The two elements match, so A is appended to Ø, giving (A).
 
For ''LCS''(''R''<sub>2</sub>, ''C''<sub>2</sub>), A and G do not match, so the longest of ''LCS''(''R''<sub>1</sub>, ''C''<sub>2</sub>), which is (G), and ''LCS''(''R''<sub>2</sub>, ''C''<sub>1</sub>), which is (A), is used.  In this case, they each contain one element, so this LCS is given two subsequences:  (A) and (G).
 
For ''LCS''(''R''<sub>2</sub>, ''C''<sub>3</sub>), A does not match C.  ''LCS''(''R''<sub>2</sub>, ''C''<sub>2</sub>) contains sequences (A) and (G); LCS(''R''<sub>1</sub>, ''C''<sub>3</sub>) is (G), which is already contained in ''LCS''(''R''<sub>2</sub>, ''C''<sub>2</sub>).  The result is that ''LCS''(''R''<sub>2</sub>, ''C''<sub>3</sub>) also contains the two subsequences, (A) and (G).
 
For ''LCS''(''R''<sub>2</sub>, ''C''<sub>4</sub>), A matches A, which is appended to the upper left cell, giving (GA).
 
For ''LCS''(''R''<sub>2</sub>, ''C''<sub>5</sub>), A does not match T.  Comparing the two sequences, (GA) and (G), the longest is (GA), so ''LCS''(''R''<sub>2</sub>, ''C''<sub>5</sub>) is (GA).
 
{| class="wikitable" style="text-align:center"
|+ "G" & "A" Rows Completed
|-
!  || Ø || A || G || C || A || T
|-
! Ø
| Ø || Ø || Ø || Ø || Ø || Ø
|-
! G
| Ø
| <math>\overset{\ \ \uparrow}{\leftarrow}</math>Ø
| <math>\overset{\nwarrow}{\ }</math>(G)
| <math>\overset{\ }{\leftarrow}</math>(G)
| <math>\overset{\ }{\leftarrow}</math>(G)
| <math>\overset{\ }{\leftarrow}</math>(G)
|-
! A
| Ø
| <math>\overset{\nwarrow}{\ }</math>(A)
| <math>\overset{\ \ \uparrow}{\leftarrow}</math>(A) & (G)
| <math>\overset{\ \ \uparrow}{\leftarrow}</math>(A) & (G)
| <math>\overset{\nwarrow}{\ }</math>(GA)
| <math>\overset{\ }{\leftarrow}</math>(GA)
|-
! C
| Ø
|
|
|
|
|
|-
|}
 
For ''LCS''(''R''<sub>3</sub>, ''C''<sub>1</sub>), C and A do not match, so ''LCS''(''R''<sub>3</sub>, ''C''<sub>1</sub>) gets the longest of the two sequences, (A).
 
For ''LCS''(''R''<sub>3</sub>, ''C''<sub>2</sub>), C and G do not match.  Both ''LCS''(''R''<sub>3</sub>, ''C''<sub>1</sub>) and ''LCS''(''R''<sub>2</sub>, ''C''<sub>2</sub>) have one element.  The result is that ''LCS''(''R''<sub>3</sub>, ''C''<sub>2</sub>) contains the two subsequences, (A) and (G).
 
For ''LCS''(''R''<sub>3</sub>, ''C''<sub>3</sub>), C and C match, so C is appended to ''LCS''(''R''<sub>2</sub>, ''C''<sub>2</sub>), which contains the two subsequences, (A) and (G), giving (AC) and (GC).
 
For ''LCS''(''R''<sub>3</sub>, ''C''<sub>4</sub>), C and A do not match.  Combining ''LCS''(''R''<sub>3</sub>, ''C''<sub>3</sub>), which contains (AC) and (GC), and ''LCS''(''R''<sub>2</sub>, ''C''<sub>4</sub>), which contains (GA), gives a total of three sequences:  (AC), (GC), and (GA).
 
Finally, for ''LCS''(''R''<sub>3</sub>, ''C''<sub>5</sub>), C and T do not match.  The result is that ''LCS''(''R''<sub>3</sub>, ''C''<sub>5</sub>) also contains the three sequences, (AC), (GC), and (GA).
 
{| class="wikitable" style="text-align:center"
|+ Completed LCS Table
|-
!  || Ø || A || G || C || A || T
|-
! Ø
| Ø || Ø || Ø || Ø || Ø || Ø
|-
! G
| Ø
| <math>\overset{\ \ \uparrow}{\leftarrow}</math>Ø
| <math>\overset{\nwarrow}{\ }</math>(G)
| <math>\overset{\ }{\leftarrow}</math>(G)
| <math>\overset{\ }{\leftarrow}</math>(G)
| <math>\overset{\ }{\leftarrow}</math>(G)
|-
! A
| Ø
| <math>\overset{\nwarrow}{\ }</math>(A)
| <math>\overset{\ \ \uparrow}{\leftarrow}</math>(A) & (G)
| <math>\overset{\ \ \uparrow}{\leftarrow}</math>(A) & (G)
| <math>\overset{\nwarrow}{\ }</math>(GA)
| <math>\overset{\ }{\leftarrow}</math>(GA)
|-
! C
| Ø
| <math>\overset{\ \uparrow}{\ }</math>(A)
| <math>\overset{\ \ \uparrow}{\leftarrow}</math>(A) & (G)
| <math>\overset{\nwarrow}{\ }</math>(AC) & (GC)
| <math>\overset{\ \ \uparrow}{\leftarrow}</math>(AC) & (GC) & (GA)
| <math>\overset{\ \ \uparrow}{\leftarrow}</math>(AC) & (GC) & (GA)
|-
|}
 
The final result is that the last cell contains all the longest subsequences common to (AGCAT) and (GAC); these are (AC), (GC), and (GA).  The table also shows the longest common subsequences for every possible pair of prefixes.  For example, for (AGC) and (GA), the longest common subsequence are (A) and (G).
 
=== Traceback approach ===
Calculating the LCS of a row of the LCS table requires only the solutions to the current row and the previous row.  Still, for long sequences, these sequences can get numerous and long, requiring a lot of storage space.  Storage space can be saved by saving not the actual subsequences, but the length of the subsequence and the direction of the arrows, as in the table below.
 
{| class="wikitable" style="text-align:center"
|+ Storing length, rather than sequences
|-
!  || Ø || A || G || C || A || T
|-
! Ø
| 0 || 0 || 0 || 0 || 0 || 0
|-
! G
| 0
| <math>\overset{\ \ \uparrow}{\leftarrow}</math>0
| <math>\overset{\nwarrow}{\ }</math>1
| <math>\overset{\ }{\leftarrow}</math>1
| <math>\overset{\ }{\leftarrow}</math>1
| <math>\overset{\ }{\leftarrow}</math>1
|-
! A
| 0
| <math>\overset{\nwarrow}{\ }</math>1
| <math>\overset{\ \ \uparrow}{\leftarrow}</math>1
| <math>\overset{\ \ \uparrow}{\leftarrow}</math>1
| <math>\overset{\nwarrow}{\ }</math>2
| <math>\overset{\ }{\leftarrow}</math>2
|-
! C
| 0
| <math>\overset{\ \uparrow}{\ }</math>1
| <math>\overset{\ \ \uparrow}{\leftarrow}</math>1
| <math>\overset{\nwarrow}{\ }</math>2
| <math>\overset{\ \ \uparrow}{\leftarrow}</math>2
| <math>\overset{\ \ \uparrow}{\leftarrow}</math>2
|-
|}
 
The actual subsequences are deduced in a "traceback" procedure that follows the arrows backwards, starting from the last cell in the table.  When the length decreases, the sequences must have had a common element.  Several paths are possible when two arrows are shown in a cell.  Below is the table for such an analysis, with numbers colored in cells where the length is about to decrease.  The bold numbers trace out the sequence, (GA).<ref>{{cite book | author = [[Thomas H. Cormen]], [[Charles E. Leiserson]], [[Ronald L. Rivest]] and [[Clifford Stein]] | title = [[Introduction to Algorithms]] | publisher = MIT Press and McGraw-Hill | year = 2001 | isbn = 0-262-53196-8 | edition = 2nd | chapter = 15.4 | pages = 350–355 }}</ref>
 
{| class="wikitable" style="text-align:center"
|+ Traceback example
|-
!  || Ø || A || G || C || A || T
|-
! Ø
| 0 || style="background:silver" | '''0''' || 0 || 0 || 0 || 0
|-
! G
| style="background:silver" | 0
| <math>\overset{\ \ \uparrow}{\leftarrow}</math>0
| style="background:silver;color:#FF6600" | <math>\overset{\nwarrow}{\ }</math>'''1'''
| style="background:silver" | <math>\overset{\ }{\leftarrow}</math>'''1'''
| <math>\overset{\ }{\leftarrow}</math>1
| <math>\overset{\ }{\leftarrow}</math>1
|-
! A
| 0
| style="background:silver;color:#FF6600" | <math>\overset{\nwarrow}{\ }</math>1
| style="background:silver" | <math>\overset{\ \ \uparrow}{\leftarrow}</math>1
| <math>\overset{\ \ \uparrow}{\leftarrow}</math>1
| style="background:silver;color:#FF6600" | <math>\overset{\nwarrow}{\ }</math>'''2'''
| style="background:silver" | <math>\overset{\ }{\leftarrow}</math>'''2'''
|-
! C
| 0
| <math>\overset{\ \uparrow}{\ }</math>1
| <math>\overset{\ \ \uparrow}{\leftarrow}</math>1
| style="background:silver;color:#FF6600" | <math>\overset{\nwarrow}{\ }</math>2
| style="background:silver" | <math>\overset{\ \ \uparrow}{\leftarrow}</math>2
| style="background:silver" | <math>\overset{\ \ \uparrow}{\leftarrow}</math>'''2'''
|-
|}
 
== Relation to other problems ==
For two strings <math>X_{1 \dots m}</math> and <math>Y_{1 \dots n}</math>, the length of the [[shortest common supersequence problem|shortest common supersequence]] is related to the length of the LCS by<ref name="BHR00" />
 
:<math>\left|SCS(X,Y)\right| = n + m - \left|LCS(X,Y)\right|.</math>
 
The [[Levenshtein distance|edit distance]] when only insertion and deletion is allowed (no substitution), or when the cost of the substitution is the double of the cost of an insertion or  deletion, is:
 
:<math>d'(X,Y) = n + m - 2 \cdot \left|LCS(X,Y)\right|.</math>
 
== Code for the dynamic programming solution ==
{{unreferenced section|date=March 2013}}
=== Computing the length of the LCS ===
The function below takes as input sequences <code>X[1..m]</code> and <code>Y[1..n]</code> computes the LCS between <code>X[1..i]</code> and <code>Y[1..j]</code> for all <code>1 ≤ i ≤ m</code> and <code>1 ≤ j ≤ n</code>, and stores it in <code>C[i,j]</code>. <code>C[m,n]</code> will contain the length of the LCS of <code>X</code> and <code>Y</code>.
 
'''function''' LCSLength(X[1..m], Y[1..n])
    C = array(0..m, 0..n)
    '''for''' i := 0..m
        C[i,0] = 0
    '''for''' j := 0..n
        C[0,j] = 0
    '''for''' i := 1..m
        '''for''' j := 1..n
            '''if''' X[i] = Y[j]
                C[i,j] := C[i-1,j-1] + 1
            '''else'''
                C[i,j] := max(C[i,j-1], C[i-1,j])
    '''return''' C[m,n]
 
Alternatively, [[memoization]] could be used.
 
=== Reading out an LCS ===
The following function [[backtracking|backtracks]] the choices taken when computing the <code>C</code> table. If the last characters in the prefixes are equal, they must be in an LCS. If not, check what gave the largest LCS of keeping <math>x_i</math> and <math>y_j</math>, and make the same choice. Just choose one if they were equally long. Call the function with <code>i=m</code> and <code>j=n</code>.
 
'''function''' backtrack(C[0..m,0..n], X[1..m], Y[1..n], i, j)
    '''if''' i = 0 '''or''' j = 0
        '''return''' ""
    '''else if ''' X[i] = Y[j]
        '''return''' backtrack(C, X, Y, i-1, j-1) + X[i]
    '''else'''
        '''if''' C[i,j-1] > C[i-1,j]
            '''return''' backtrack(C, X, Y, i, j-1)
        '''else'''
            '''return''' backtrack(C, X, Y, i-1, j)
 
=== Reading out all LCSs ===
If choosing <math>x_i</math> and <math>y_j</math> would give an equally long result, read out both resulting subsequences. This is returned as a set by this function. Notice that this function is not polynomial, as it might branch in almost every step if the strings are similar.
 
'''function''' backtrackAll(C[0..m,0..n], X[1..m], Y[1..n], i, j)
    '''if''' i = 0 '''or''' j = 0
        '''return''' {""}
    '''else if''' X[i] = Y[j]
        '''return''' {Z + X[i] '''for all''' Z '''in''' backtrackAll(C, X, Y, i-1, j-1)}
    '''else'''
        R := {}
        '''if''' C[i,j-1] ≥ C[i-1,j]
            R := backtrackAll(C, X, Y, i, j-1)
        '''if''' C[i-1,j] ≥ C[i,j-1]
            R := R ∪ backtrackAll(C, X, Y, i-1, j)
        '''return''' R
 
=== Print the diff ===
This function will backtrack through the C matrix, and print the [[diff]] between the two sequences. Notice that you will get a different answer if you exchange <code>≥</code> and <code>&lt;</code>, with <code>&gt;</code> and <code>≤</code> below.
 
'''function''' printDiff(C[0..m,0..n], X[1..m], Y[1..n], i, j)
    '''if''' i > 0 '''and''' j > 0 '''and''' X[i] = Y[j]
        printDiff(C, X, Y, i-1, j-1)
        print "  " + X[i]
    '''else if''' j > 0 '''and''' (i = 0 '''or''' C[i,j-1] ≥ C[i-1,j])
        printDiff(C, X, Y, i, j-1)
        print "+ " + Y[j]
    '''else if''' i > 0 '''and''' (j = 0 '''or''' C[i,j-1] < C[i-1,j])
        printDiff(C, X, Y, i-1, j)
        print "- " + X[i]
    '''else'''
        print ""
 
=== Example ===
Let <math>X</math> be “<code>XMJYAUZ</code>” and <math>Y</math> be “<code>MZJAWXU</code>”. The longest common subsequence between <math>X</math> and <math>Y</math> is “<code>MJAU</code>”. The table <code>C</code> shown below, which is generated by the function <code>LCSLength</code>, shows the lengths of the longest common subsequences between prefixes of <math>X</math> and <math>Y</math>. The <math>i</math>th row and <math>j</math>th column shows the length of the LCS between <math>X_{1..i}</math> and <math>Y_{1..j}</math>.
 
{| class="wikitable" style="text-align: center;"
|-
! colspan="2" rowspan="2" |
! 0 !! 1 !! 2 !! 3 !! 4 !! 5 !! 6 !! 7
|-
! Ø !! M !! Z !! J !! A !! W !! X !! U
|-
! 0 !! Ø
| <u>0</u> || 0 || 0 || 0 || 0 || 0 || 0 || 0
|-
! 1 !! X
| <u>0</u> || 0 || 0 || 0 || 0 || 0 || 1 || 1
|-
! 2 !! M
| 0 || <u>1</u> || <u>1</u> || 1 || 1 || 1 || 1 || 1
|-
! 3 !! J
| 0 || 1 || 1 || <u>2</u> || 2 || 2 || 2 || 2
|-
! 4 !! Y
| 0 || 1 || 1 || <u>2</u> || 2 || 2 || 2 || 2
|-
! 5 !! A
| 0 || 1 || 1 || 2 || <u>3</u> || <u>3</u> || <u>3</u> || 3
|-
! 6 !! U
| 0 || 1 || 1 || 2 || 3 || 3 || 3 || <u>4</u>
|-
! 7 !! Z
| 0 || 1 || 2 || 2 || 3 || 3 || 3 || <u>4</u>
|}
 
The underlined numbers show the path the function <code>backtrack</code> would follow from the bottom right to the top left corner, when reading out an LCS. If the current symbols in <math>X</math> and <math>Y</math> are equal, they are part of the LCS, and we go both up and left. If not, we go up or left, depending on which cell has a higher number. This corresponds to either taking the LCS between <math>X_{1..i-1}</math> and <math>Y_{1..j}</math>, or <math>X_{1..i}</math> and <math>Y_{1..j-1}</math>.
 
== Code optimization ==
Several optimizations can be made to the algorithm above to speed it up for real-world cases.
 
=== Reduce the problem set ===
The C matrix in the naive algorithm [[quadratic growth|grows quadratically]] with the lengths of the sequences.  For two 100-item sequences, a 10,000-item matrix would be needed, and 10,000 comparisons would need to be done.  In most real-world cases, especially source code diffs and patches, the beginnings and ends of files rarely change, and almost certainly not both at the same time.  If only a few items have changed in the middle of the sequence, the beginning and end can be eliminated.  This reduces not only the memory requirements for the matrix, but also the number of comparisons that must be done.
 
'''function''' LCS(X[1..m], Y[1..n])
    start := 1
    m_end := m
    n_end := n
    ''trim off the matching items at the beginning''
    '''while''' start ≤ m_end '''and''' start ≤ n_end '''and''' X[start] = Y[start]
        start := start + 1
    ''trim off the matching items at the end''
    '''while''' start ≤ m_end '''and''' start ≤ n_end '''and''' X[m_end] = Y[n_end]
        m_end := m_end - 1
        n_end := n_end - 1
    C = array(start-1..m_end, start-1..n_end)
    ''only loop over the items that have changed''
    '''for''' i := start..m_end
        '''for''' j := start..n_end
            ''the algorithm continues as before ...''
 
In the best case scenario, a sequence with no changes, this optimization would completely eliminate the need for the C matrix.  In the worst case scenario, a change to the very first and last items in the sequence, only two additional comparisons are performed.
 
=== Reduce the comparison time ===
Most of the time taken by the naive algorithm is spent performing comparisons between items in the sequences.  For textual sequences such as source code, you want to view lines as the sequence elements instead of single characters.  This can mean comparisons of relatively long strings for each step in the algorithm.  Two optimizations can be made that can help to reduce the time these comparisons consume.
 
=== Reduce strings to hashes ===
A [[hash function]] or [[checksum]] can be used to reduce the size of the strings in the sequences.  That is, for source code where the average line is 60 or more characters long, the hash or checksum for that line might be only 8 to 40 characters long.  Additionally, the randomized nature of hashes and checksums would guarantee that comparisons would short-circuit faster, as lines of source code will rarely be changed at the beginning.
 
There are three primary drawbacks to this optimization.  First, an amount of time needs to be spent beforehand to precompute the hashes for the two sequences.  Second, additional memory needs to be allocated for the new hashed sequences.  However, in comparison to the naive algorithm used here, both of these drawbacks are relatively minimal.
 
The third drawback is that of [[hash collision|collisions]].  Since the checksum or hash is not guaranteed to be unique, there is a small chance that two different items could be reduced to the same hash.  This is unlikely in source code, but it is possible.  A cryptographic hash would therefore be far better suited for this optimization, as its entropy is going to be significantly greater than that of a simple checksum.  However, the benefits may not be worth the setup and computational requirements of a cryptographic hash for small sequence lengths.
 
=== Reduce the required space ===
If only the length of the LCS is required, the matrix can be reduced to a <math>2\times \min(n,m)</math> matrix with ease, or to a <math>\min(m,n)+1</math> vector (smarter) as the dynamic programming approach only needs the current and previous columns of the matrix. [[Hirschberg's algorithm]] allows the construction of the optimal sequence itself in the same quadratic time and linear space bounds.<ref>{{cite journal|authorlink = Dan Hirschberg|author=Hirschberg, D. S.|title=A linear space algorithm for computing maximal common subsequences|journal=Communications of the ACM|volume=18|issue=6|year=1975|pages=341–343|doi=10.1145/360825.360861}}</ref>
 
== See also ==
* [[Longest increasing subsequence]]
* [[Longest alternating subsequence]]
 
== References ==
{{reflist}}
<!-- Dead note "GJ78": {{cite book|author = [[Michael R. Garey]] and [[David S. Johnson]] | year = 1979 | title = Computers and Intractability: A Guide to the Theory of NP-Completeness| publisher = W.H. Freeman | isbn = 0-7167-1045-5| pages = 228}} A421: SR10. -->
 
== External links ==
{{Wikibooks|Algorithm implementation|Strings/Longest common subsequence|Longest common subsequence}}
* [http://nist.gov/dads/HTML/longestCommonSubsequence.html Dictionary of Algorithms and Data Structures: longest common subsequence]
* [http://rosettacode.org/wiki/Longest_common_subsequence A collection of implementations of the longest common subsequence in many programming languages]
 
<!-- case of fixed number of input strings -->
<!-- case of arbitrary number of input strings -->
 
{{DEFAULTSORT:Longest Common Subsequence Problem}}
[[Category:Problems on strings]]
[[Category:Combinatorics]]
[[Category:Dynamic programming]]
[[Category:Polynomial-time problems]]
[[Category:NP-complete problems]]

Revision as of 00:40, 4 January 2014

Template:Distinguish The longest common subsequence (LCS) problem is to find the longest subsequence common to all sequences in a set of sequences (often just two). (Note that a subsequence is different from a substring, for the terms of the former need not be consecutive terms of the original sequence.) It is a classic computer science problem, the basis of file comparison programs such as diff, and has applications in bioinformatics.

Complexity

For the general case of an arbitrary number of input sequences, the problem is NP-hard.[1] When the number of sequences is constant, the problem is solvable in polynomial time by dynamic programming (see Solution below). Assume you have N sequences of lengths n1,...,nN. A naive search would test each of the 2n1 subsequences of the first sequence to determine whether they are also subsequences of the remaining sequences; each subsequence may be tested in time linear in the lengths of the remaining sequences, so the time for this algorithm would be

O(2n1i>1ni).

For the case of two sequences of n and m elements, the running time of the dynamic programming approach is O(n × m). For an arbitrary number of input sequences, the dynamic programming approach gives a solution in

O(Ni=1Nni).

There exist methods with lower complexity,[2] which often depend on the length of the LCS, the size of the alphabet, or both.

Notice that the LCS is not necessarily unique; for example the LCS of "ABC" and "ACB" is both "AB" and "AC". Indeed the LCS problem is often defined to be finding all common subsequences of a maximum length. This problem inherently has higher complexity, as the number of such subsequences is exponential in the worst case,[3] even for only two input strings.

Solution for two sequences

The LCS problem has an optimal substructure: the problem can be broken down into smaller, simple "subproblems", which can be broken down into yet simpler subproblems, and so on, until, finally, the solution becomes trivial. The LCS problem also has overlapping subproblems: the solution to a higher subproblem depends on the solutions to several of the lower subproblems. Problems with these two properties—optimal substructure and overlapping subproblems—can be approached by a problem-solving technique called dynamic programming, in which the solution is built up starting with the simplest subproblems. The procedure requires memoization—saving the solutions to one level of subproblem in a table (analogous to writing them to a memo, hence the name) so that the solutions are available to the next level of subproblems. This method is illustrated here.

Prefixes

The subproblems become simpler as the sequences become shorter. Shorter sequences are conveniently described using the term prefix. A prefix of a sequence is the sequence with the end cut off. Let S be the sequence (AGCA). Then, the sequence (AG) is one of the prefixes of S. Prefixes are denoted with the name of the sequence, followed by a subscript to indicate how many characters the prefix contains.[4] The prefix (AG) is denoted S2, since it contains the first 2 elements of S. The possible prefixes of S are

S1 = (A)
S2 = (AG)
S3 = (AGC)
S4 = (AGCA).

The solution to the LCS problem for two arbitrary sequences, X and Y, amounts to construct some function, LCS(X, Y), that gives the longest subsequences common to X and Y. That function relies on the following two properties.

First property

Suppose that two sequences both end in the same element. To find their LCS, shorten each sequence by removing the last element, find the LCS of the shortened sequences, and to that LCS append the removed element.

For example, here are two sequences having the same last element: (BANANA) and (ATANA).
Remove the same last element. Repeat the procedure until you find no common last element. The removed sequence will be (ANA).
The sequences now under consideration: (BAN) and (AT)
The LCS of these last two sequences is, by inspection, (A).
Append the removed element, (ANA), giving (AANA), which, by inspection, is the LCS of the original sequences.

In terms of prefixes,

if xn=ym, LCS(Xn, Ym) = (LCS( Xn-1, Ym-1), xn)

where the comma indicates that the following element, xn, is appended to the sequence. Note that the LCS for Xn and Ym involves determining the LCS of the shorter sequences, Xn-1 and Ym-1.

Second property

Suppose that the two sequences X and Y do not end in the same symbol. Then the LCS of X and Y is the longer of the two sequences LCS(Xn,Ym-1) and LCS(Xn-1,Ym).

To understand this property, consider the two following sequences :

sequence X: ABCDEFG (n elements)
sequence Y: BCDGK (m elements)

The LCS of these two sequences either ends with a G (the last element of sequence X) or does not.

Case 1: the LCS ends with a G
Then it cannot end with a K. Thus it does not hurt to remove the K from sequence Y: if K were in the LCS, it would be its last character; as a consequence K is not in the LCS. We can then write: LCS(Xn,Ym) = LCS(Xn, Ym-1).

Case 2: the LCS does not end with a G
Then it does not hurt to remove the G from the sequence X (for the same reason as above). And then we can write: LCS(Xn,Ym) = LCS(Xn-1, Ym).

In any case, the LCS we are looking for is one of LCS(Xn, Ym-1) or LCS(Xn-1, Ym). Those two last LCS are both common subsequences to X and Y. LCS(X,Y) is the longest. Thus its value is the longest sequence of LCS(Xn, Ym-1) and LCS(Xn-1, Ym).

LCS function defined

Let two sequences be defined as follows: X = (x1, x2...xm) and Y = (y1, y2...yn). The prefixes of X are X1, 2,...m; the prefixes of Y are Y1, 2,...n. Let LCS(Xi, Yj) represent the set of longest common subsequence of prefixes Xi and Yj. This set of sequences is given by the following.

LCS(Xi,Yj)={ if i=0 or j=0LCS(Xi1,Yj1)+1 if xi=yjlongest(LCS(Xi,Yj1),LCS(Xi1,Yj)) if xiyj

To find the longest subsequences common to Xi and Yj, compare the elements xi and yj. If they are equal, then the sequence LCS(Xi-1, Yj-1) is extended by that element, xi. If they are not equal, then the longer of the two sequences, LCS(Xi, Yj-1), and LCS(Xi-1, Yj), is retained. (If they are both the same length, but not identical, then both are retained.) Notice that the subscripts are reduced by 1 in these formulas. That can result in a subscript of 0. Since the sequence elements are defined to start at 1, it was necessary to add the requirement that the LCS is empty when a subscript is zero.

Worked example

The longest subsequence common to C = (AGCAT), and R = (GAC) will be found. Because the LCS function uses a "zeroth" element, it is convenient to define zero prefixes that are empty for these sequences: C0 = Ø; and R0 = Ø. All the prefixes are placed in a table with C in the first row (making it a column header) and R in the first column (making it a row header).

LCS Strings
0 A G C A T
0 Ø Ø Ø Ø Ø Ø
G Ø
A Ø
C Ø

This table is used to store the LCS sequence for each step of the calculation. The second column and second row have been filled in with Ø, because when an empty sequence is compared with a non-empty sequence, the longest common subsequence is always an empty sequence.

LCS(R1, C1) is determined by comparing the first elements in each sequence. G and A are not the same, so this LCS gets (using the "second property") the longest of the two sequences, LCS(R1, C0) and LCS(R0, C1). According to the table, both of these are empty, so LCS(R1, C1) is also empty, as shown in the table below. The arrows indicate that the sequence comes from both the cell above, LCS(R0, C1) and the cell on the left, LCS(R1, C0).

LCS(R1, C2) is determined by comparing G and G. They match, so G is appended to the upper left sequence, LCS(R0, C1), which is (Ø), giving (ØG), which is (G).

For LCS(R1, C3), G and C do not match. The sequence above is empty; the one to the left contains one element, G. Selecting the longest of these, LCS(R1, C3) is (G). The arrow points to the left, since that is the longest of the two sequences.

LCS(R1, C4), likewise, is (G).

LCS(R1, C5), likewise, is (G).

"G" Row Completed
Ø A G C A T
Ø Ø Ø Ø Ø Ø Ø
G Ø Ø (G) (G) (G) (G)
A Ø
C Ø

For LCS(R2, C1), A is compared with A. The two elements match, so A is appended to Ø, giving (A).

For LCS(R2, C2), A and G do not match, so the longest of LCS(R1, C2), which is (G), and LCS(R2, C1), which is (A), is used. In this case, they each contain one element, so this LCS is given two subsequences: (A) and (G).

For LCS(R2, C3), A does not match C. LCS(R2, C2) contains sequences (A) and (G); LCS(R1, C3) is (G), which is already contained in LCS(R2, C2). The result is that LCS(R2, C3) also contains the two subsequences, (A) and (G).

For LCS(R2, C4), A matches A, which is appended to the upper left cell, giving (GA).

For LCS(R2, C5), A does not match T. Comparing the two sequences, (GA) and (G), the longest is (GA), so LCS(R2, C5) is (GA).

"G" & "A" Rows Completed
Ø A G C A T
Ø Ø Ø Ø Ø Ø Ø
G Ø Ø (G) (G) (G) (G)
A Ø (A) (A) & (G) (A) & (G) (GA) (GA)
C Ø

For LCS(R3, C1), C and A do not match, so LCS(R3, C1) gets the longest of the two sequences, (A).

For LCS(R3, C2), C and G do not match. Both LCS(R3, C1) and LCS(R2, C2) have one element. The result is that LCS(R3, C2) contains the two subsequences, (A) and (G).

For LCS(R3, C3), C and C match, so C is appended to LCS(R2, C2), which contains the two subsequences, (A) and (G), giving (AC) and (GC).

For LCS(R3, C4), C and A do not match. Combining LCS(R3, C3), which contains (AC) and (GC), and LCS(R2, C4), which contains (GA), gives a total of three sequences: (AC), (GC), and (GA).

Finally, for LCS(R3, C5), C and T do not match. The result is that LCS(R3, C5) also contains the three sequences, (AC), (GC), and (GA).

Completed LCS Table
Ø A G C A T
Ø Ø Ø Ø Ø Ø Ø
G Ø Ø (G) (G) (G) (G)
A Ø (A) (A) & (G) (A) & (G) (GA) (GA)
C Ø (A) (A) & (G) (AC) & (GC) (AC) & (GC) & (GA) (AC) & (GC) & (GA)

The final result is that the last cell contains all the longest subsequences common to (AGCAT) and (GAC); these are (AC), (GC), and (GA). The table also shows the longest common subsequences for every possible pair of prefixes. For example, for (AGC) and (GA), the longest common subsequence are (A) and (G).

Traceback approach

Calculating the LCS of a row of the LCS table requires only the solutions to the current row and the previous row. Still, for long sequences, these sequences can get numerous and long, requiring a lot of storage space. Storage space can be saved by saving not the actual subsequences, but the length of the subsequence and the direction of the arrows, as in the table below.

Storing length, rather than sequences
Ø A G C A T
Ø 0 0 0 0 0 0
G 0 0 1 1 1 1
A 0 1 1 1 2 2
C 0 1 1 2 2 2

The actual subsequences are deduced in a "traceback" procedure that follows the arrows backwards, starting from the last cell in the table. When the length decreases, the sequences must have had a common element. Several paths are possible when two arrows are shown in a cell. Below is the table for such an analysis, with numbers colored in cells where the length is about to decrease. The bold numbers trace out the sequence, (GA).[5]

Traceback example
Ø A G C A T
Ø 0 0 0 0 0 0
G 0 0 1 1 1 1
A 0 1 1 1 2 2
C 0 1 1 2 2 2

Relation to other problems

For two strings X1m and Y1n, the length of the shortest common supersequence is related to the length of the LCS by[2]

|SCS(X,Y)|=n+m|LCS(X,Y)|.

The edit distance when only insertion and deletion is allowed (no substitution), or when the cost of the substitution is the double of the cost of an insertion or deletion, is:

d(X,Y)=n+m2|LCS(X,Y)|.

Code for the dynamic programming solution

Before you choose any particular company it is vital to understand in full how the different plans can vary. There is no other better method than to create a message board so that people can relax and "chill" on your website and check out your articles more. You should read the HostGator review, even before registering with a web hosting company. but Hostgator in addition considers the surroundings. You can even use a Hostgator reseller coupon for unlimited web hosting at HostGator! Most of individuals by no means go for yearly subscription and choose month to month subscription. Several users commented that this was the deciding factor in picking HostGator but in any case there is a 45 day Money Back Guarantee and there is no contract so you can cancel at any time. GatorBill is able to send you an email notice about the new invoice. In certain cases a dedicated server can offer less overhead and a bigger revenue in investments. With the plan come a Free Billing Executive, Free sellers account and Free Hosting Templates.



This is one of the only things that require you to spend a little money to make money. Just go make an account, get a paypal account, and start selling. To go one step beyond just affiliating products and services is to create your own and sell it through your blog. Not great if you really enjoy trying out all the themes. Talking in real time having a real person causes it to be personal helping me personally to sort out how to proceed. The first step I took was search for a discount code, as I did with HostGator. Using a HostGator coupon is a beneficial method to get started. As long as the necessities are able to preserve the horizontal functionality of your site, you would pretty much be fine.

Computing the length of the LCS

The function below takes as input sequences X[1..m] and Y[1..n] computes the LCS between X[1..i] and Y[1..j] for all 1 ≤ i ≤ m and 1 ≤ j ≤ n, and stores it in C[i,j]. C[m,n] will contain the length of the LCS of X and Y.

function LCSLength(X[1..m], Y[1..n])
    C = array(0..m, 0..n)
    for i := 0..m
       C[i,0] = 0
    for j := 0..n
       C[0,j] = 0
    for i := 1..m
        for j := 1..n
            if X[i] = Y[j]
                C[i,j] := C[i-1,j-1] + 1
            else
                C[i,j] := max(C[i,j-1], C[i-1,j])
    return C[m,n]

Alternatively, memoization could be used.

Reading out an LCS

The following function backtracks the choices taken when computing the C table. If the last characters in the prefixes are equal, they must be in an LCS. If not, check what gave the largest LCS of keeping xi and yj, and make the same choice. Just choose one if they were equally long. Call the function with i=m and j=n.

function backtrack(C[0..m,0..n], X[1..m], Y[1..n], i, j)
    if i = 0 or j = 0
        return ""
    else if  X[i] = Y[j]
        return backtrack(C, X, Y, i-1, j-1) + X[i]
    else
        if C[i,j-1] > C[i-1,j]
            return backtrack(C, X, Y, i, j-1)
        else
            return backtrack(C, X, Y, i-1, j)

Reading out all LCSs

If choosing xi and yj would give an equally long result, read out both resulting subsequences. This is returned as a set by this function. Notice that this function is not polynomial, as it might branch in almost every step if the strings are similar.

function backtrackAll(C[0..m,0..n], X[1..m], Y[1..n], i, j)
    if i = 0 or j = 0
        return {""}
    else if X[i] = Y[j]
        return {Z + X[i] for all Z in backtrackAll(C, X, Y, i-1, j-1)}
    else
        R := {}
        if C[i,j-1] ≥ C[i-1,j]
            R := backtrackAll(C, X, Y, i, j-1)
        if C[i-1,j] ≥ C[i,j-1]
            R := R ∪ backtrackAll(C, X, Y, i-1, j)
        return R

Print the diff

This function will backtrack through the C matrix, and print the diff between the two sequences. Notice that you will get a different answer if you exchange and <, with > and below.

function printDiff(C[0..m,0..n], X[1..m], Y[1..n], i, j)
    if i > 0 and j > 0 and X[i] = Y[j]
        printDiff(C, X, Y, i-1, j-1)
        print "  " + X[i]
    else if j > 0 and (i = 0 or C[i,j-1] ≥ C[i-1,j])
        printDiff(C, X, Y, i, j-1)
        print "+ " + Y[j]
    else if i > 0 and (j = 0 or C[i,j-1] < C[i-1,j])
        printDiff(C, X, Y, i-1, j)
        print "- " + X[i]
    else
        print ""

Example

Let X be “XMJYAUZ” and Y be “MZJAWXU”. The longest common subsequence between X and Y is “MJAU”. The table C shown below, which is generated by the function LCSLength, shows the lengths of the longest common subsequences between prefixes of X and Y. The ith row and jth column shows the length of the LCS between X1..i and Y1..j.

0 1 2 3 4 5 6 7
Ø M Z J A W X U
0 Ø 0 0 0 0 0 0 0 0
1 X 0 0 0 0 0 0 1 1
2 M 0 1 1 1 1 1 1 1
3 J 0 1 1 2 2 2 2 2
4 Y 0 1 1 2 2 2 2 2
5 A 0 1 1 2 3 3 3 3
6 U 0 1 1 2 3 3 3 4
7 Z 0 1 2 2 3 3 3 4

The underlined numbers show the path the function backtrack would follow from the bottom right to the top left corner, when reading out an LCS. If the current symbols in X and Y are equal, they are part of the LCS, and we go both up and left. If not, we go up or left, depending on which cell has a higher number. This corresponds to either taking the LCS between X1..i1 and Y1..j, or X1..i and Y1..j1.

Code optimization

Several optimizations can be made to the algorithm above to speed it up for real-world cases.

Reduce the problem set

The C matrix in the naive algorithm grows quadratically with the lengths of the sequences. For two 100-item sequences, a 10,000-item matrix would be needed, and 10,000 comparisons would need to be done. In most real-world cases, especially source code diffs and patches, the beginnings and ends of files rarely change, and almost certainly not both at the same time. If only a few items have changed in the middle of the sequence, the beginning and end can be eliminated. This reduces not only the memory requirements for the matrix, but also the number of comparisons that must be done.

function LCS(X[1..m], Y[1..n])
    start := 1
    m_end := m
    n_end := n
    trim off the matching items at the beginning
    while start ≤ m_end and start ≤ n_end and X[start] = Y[start]
        start := start + 1
    trim off the matching items at the end
    while start ≤ m_end and start ≤ n_end and X[m_end] = Y[n_end]
        m_end := m_end - 1
        n_end := n_end - 1
    C = array(start-1..m_end, start-1..n_end)
    only loop over the items that have changed
    for i := start..m_end
        for j := start..n_end
            the algorithm continues as before ...

In the best case scenario, a sequence with no changes, this optimization would completely eliminate the need for the C matrix. In the worst case scenario, a change to the very first and last items in the sequence, only two additional comparisons are performed.

Reduce the comparison time

Most of the time taken by the naive algorithm is spent performing comparisons between items in the sequences. For textual sequences such as source code, you want to view lines as the sequence elements instead of single characters. This can mean comparisons of relatively long strings for each step in the algorithm. Two optimizations can be made that can help to reduce the time these comparisons consume.

Reduce strings to hashes

A hash function or checksum can be used to reduce the size of the strings in the sequences. That is, for source code where the average line is 60 or more characters long, the hash or checksum for that line might be only 8 to 40 characters long. Additionally, the randomized nature of hashes and checksums would guarantee that comparisons would short-circuit faster, as lines of source code will rarely be changed at the beginning.

There are three primary drawbacks to this optimization. First, an amount of time needs to be spent beforehand to precompute the hashes for the two sequences. Second, additional memory needs to be allocated for the new hashed sequences. However, in comparison to the naive algorithm used here, both of these drawbacks are relatively minimal.

The third drawback is that of collisions. Since the checksum or hash is not guaranteed to be unique, there is a small chance that two different items could be reduced to the same hash. This is unlikely in source code, but it is possible. A cryptographic hash would therefore be far better suited for this optimization, as its entropy is going to be significantly greater than that of a simple checksum. However, the benefits may not be worth the setup and computational requirements of a cryptographic hash for small sequence lengths.

Reduce the required space

If only the length of the LCS is required, the matrix can be reduced to a 2×min(n,m) matrix with ease, or to a min(m,n)+1 vector (smarter) as the dynamic programming approach only needs the current and previous columns of the matrix. Hirschberg's algorithm allows the construction of the optimal sequence itself in the same quadratic time and linear space bounds.[6]

See also

References

43 year old Petroleum Engineer Harry from Deep River, usually spends time with hobbies and interests like renting movies, property developers in singapore new condominium and vehicle racing. Constantly enjoys going to destinations like Camino Real de Tierra Adentro.

External links

DTZ's auction group in Singapore auctions all types of residential, workplace and retail properties, retailers, homes, accommodations, boarding houses, industrial buildings and development websites. Auctions are at the moment held as soon as a month.

Whitehaven @ Pasir Panjang – A boutique improvement nicely nestled peacefully in serene Pasir Panjang personal estate presenting a hundred and twenty rare freehold private apartments tastefully designed by the famend Ong & Ong Architect. Only a short drive away from Science Park and NUS Campus, Jade Residences, a recent Freehold condominium which offers high quality lifestyle with wonderful facilities and conveniences proper at its door steps. Its fashionable linear architectural fashion promotes peace and tranquility living nestled within the D19 personal housing enclave. Rising workplace sector leads real estate market efficiency, while prime retail and enterprise park segments moderate and residential sector continues in decline International Market Perspectives - 1st Quarter 2014

There are a lot of websites out there stating to be one of the best seek for propertycondominiumhouse, and likewise some ways to discover a low cost propertycondominiumhouse. Owning a propertycondominiumhouse in Singapore is the dream of virtually all individuals in Singapore, It is likely one of the large choice we make in a lifetime. Even if you happen to're new to Property listing singapore funding, we are right here that will help you in making the best resolution to purchase a propertycondominiumhouse at the least expensive value.

Jun 18 ROCHESTER in MIXED USE IMPROVEMENT $1338000 / 1br - 861ft² - (THE ROCHESTER CLOSE TO NORTH BUONA VISTA RD) pic real property - by broker Jun 18 MIXED USE IMPROVEMENT @ ROCHESTER @ ROCHESTER PK $1880000 / 1br - 1281ft² - (ROCHESTER CLOSE TO NORTH BUONA VISTA) pic real estate - by broker Tue 17 Jun Jun 17 Sunny Artwork Deco Gem Near Seashore-Super Deal!!! $103600 / 2br - 980ft² - (Ventnor) pic actual estate - by owner Jun 17 Freehold semi-d for rent (Jalan Rebana ) $7000000 / 5909ft² - (Jalan Rebana ) actual property - by dealer Jun sixteen Ascent @ 456 in D12 (456 Balestier Highway,Singapore) pic real property - by proprietor Jun 16 RETAIL SHOP AT SIM LIM SQUARE FOR SALE, IT MALL, ROCHOR, BUGIS MRT $2000000 / 506ft² - (ROCHOR, BUGIS MRT) pic real estate - by dealer HDB Scheme Any DBSS BTO

In case you are eligible to purchase landed houses (open solely to Singapore residents) it is without doubt one of the best property investment choices. Landed housing varieties solely a small fraction of available residential property in Singapore, due to shortage of land right here. In the long term it should hold its worth and appreciate as the supply is small. In truth, landed housing costs have risen the most, having doubled within the last eight years or so. However he got here back the following day with two suitcases full of money. Typically we've got to clarify to such folks that there are rules and paperwork in Singapore and you can't just buy a home like that,' she said. For conveyancing matters there shall be a recommendedLondon Regulation agency familiar with Singapore London propertyinvestors to symbolize you

Sales transaction volumes have been expected to hit four,000 units for 2012, close to the mixed EC gross sales volume in 2010 and 2011, in accordance with Savills Singapore. Nevertheless the last quarter was weak. In Q4 2012, sales transactions were 22.8% down q-q to 7,931 units, in line with the URA. The quarterly sales discount was felt throughout the board. When the sale just starts, I am not in a hurry to buy. It's completely different from a private sale open for privileged clients for one day solely. Orchard / Holland (D09-10) House For Sale The Tembusu is a singular large freehold land outdoors the central area. Designed by multiple award-profitable architects Arc Studio Architecture + Urbanism, the event is targeted for launch in mid 2013. Post your Property Condos Close to MRT

  1. One of the biggest reasons investing in a Singapore new launch is an effective things is as a result of it is doable to be lent massive quantities of money at very low interest rates that you should utilize to purchase it. Then, if property values continue to go up, then you'll get a really high return on funding (ROI). Simply make sure you purchase one of the higher properties, reminiscent of the ones at Fernvale the Riverbank or any Singapore landed property Get Earnings by means of Renting

    In its statement, the singapore property listing - website link, government claimed that the majority citizens buying their first residence won't be hurt by the new measures. Some concessions can even be prolonged to chose teams of consumers, similar to married couples with a minimum of one Singaporean partner who are purchasing their second property so long as they intend to promote their first residential property. Lower the LTV limit on housing loans granted by monetary establishments regulated by MAS from 70% to 60% for property purchasers who are individuals with a number of outstanding housing loans on the time of the brand new housing purchase. Singapore Property Measures - 30 August 2010 The most popular seek for the number of bedrooms in Singapore is 4, followed by 2 and three. Lush Acres EC @ Sengkang

    Discover out more about real estate funding in the area, together with info on international funding incentives and property possession. Many Singaporeans have been investing in property across the causeway in recent years, attracted by comparatively low prices. However, those who need to exit their investments quickly are likely to face significant challenges when trying to sell their property – and could finally be stuck with a property they can't sell. Career improvement programmes, in-house valuation, auctions and administrative help, venture advertising and marketing, skilled talks and traisning are continuously planned for the sales associates to help them obtain better outcomes for his or her shoppers while at Knight Frank Singapore. No change Present Rules

    Extending the tax exemption would help. The exemption, which may be as a lot as $2 million per family, covers individuals who negotiate a principal reduction on their existing mortgage, sell their house short (i.e., for lower than the excellent loans), or take part in a foreclosure course of. An extension of theexemption would seem like a common-sense means to assist stabilize the housing market, but the political turmoil around the fiscal-cliff negotiations means widespread sense could not win out. Home Minority Chief Nancy Pelosi (D-Calif.) believes that the mortgage relief provision will be on the table during the grand-cut price talks, in response to communications director Nadeam Elshami. Buying or promoting of blue mild bulbs is unlawful.

    A vendor's stamp duty has been launched on industrial property for the primary time, at rates ranging from 5 per cent to 15 per cent. The Authorities might be trying to reassure the market that they aren't in opposition to foreigners and PRs investing in Singapore's property market. They imposed these measures because of extenuating components available in the market." The sale of new dual-key EC models will even be restricted to multi-generational households only. The models have two separate entrances, permitting grandparents, for example, to dwell separately. The vendor's stamp obligation takes effect right this moment and applies to industrial property and plots which might be offered inside three years of the date of buy. JLL named Best Performing Property Brand for second year running

    The data offered is for normal info purposes only and isn't supposed to be personalised investment or monetary advice. Motley Fool Singapore contributor Stanley Lim would not personal shares in any corporations talked about. Singapore private home costs increased by 1.eight% within the fourth quarter of 2012, up from 0.6% within the earlier quarter. Resale prices of government-built HDB residences which are usually bought by Singaporeans, elevated by 2.5%, quarter on quarter, the quickest acquire in five quarters. And industrial property, prices are actually double the levels of three years ago. No withholding tax in the event you sell your property. All your local information regarding vital HDB policies, condominium launches, land growth, commercial property and more

    There are various methods to go about discovering the precise property. Some local newspapers (together with the Straits Instances ) have categorised property sections and many local property brokers have websites. Now there are some specifics to consider when buying a 'new launch' rental. Intended use of the unit Every sale begins with 10 p.c low cost for finish of season sale; changes to 20 % discount storewide; follows by additional reduction of fiftyand ends with last discount of 70 % or extra. Typically there is even a warehouse sale or transferring out sale with huge mark-down of costs for stock clearance. Deborah Regulation from Expat Realtor shares her property market update, plus prime rental residences and houses at the moment available to lease Esparina EC @ Sengkang
  2. 2.0 2.1 One of the biggest reasons investing in a Singapore new launch is an effective things is as a result of it is doable to be lent massive quantities of money at very low interest rates that you should utilize to purchase it. Then, if property values continue to go up, then you'll get a really high return on funding (ROI). Simply make sure you purchase one of the higher properties, reminiscent of the ones at Fernvale the Riverbank or any Singapore landed property Get Earnings by means of Renting

    In its statement, the singapore property listing - website link, government claimed that the majority citizens buying their first residence won't be hurt by the new measures. Some concessions can even be prolonged to chose teams of consumers, similar to married couples with a minimum of one Singaporean partner who are purchasing their second property so long as they intend to promote their first residential property. Lower the LTV limit on housing loans granted by monetary establishments regulated by MAS from 70% to 60% for property purchasers who are individuals with a number of outstanding housing loans on the time of the brand new housing purchase. Singapore Property Measures - 30 August 2010 The most popular seek for the number of bedrooms in Singapore is 4, followed by 2 and three. Lush Acres EC @ Sengkang

    Discover out more about real estate funding in the area, together with info on international funding incentives and property possession. Many Singaporeans have been investing in property across the causeway in recent years, attracted by comparatively low prices. However, those who need to exit their investments quickly are likely to face significant challenges when trying to sell their property – and could finally be stuck with a property they can't sell. Career improvement programmes, in-house valuation, auctions and administrative help, venture advertising and marketing, skilled talks and traisning are continuously planned for the sales associates to help them obtain better outcomes for his or her shoppers while at Knight Frank Singapore. No change Present Rules

    Extending the tax exemption would help. The exemption, which may be as a lot as $2 million per family, covers individuals who negotiate a principal reduction on their existing mortgage, sell their house short (i.e., for lower than the excellent loans), or take part in a foreclosure course of. An extension of theexemption would seem like a common-sense means to assist stabilize the housing market, but the political turmoil around the fiscal-cliff negotiations means widespread sense could not win out. Home Minority Chief Nancy Pelosi (D-Calif.) believes that the mortgage relief provision will be on the table during the grand-cut price talks, in response to communications director Nadeam Elshami. Buying or promoting of blue mild bulbs is unlawful.

    A vendor's stamp duty has been launched on industrial property for the primary time, at rates ranging from 5 per cent to 15 per cent. The Authorities might be trying to reassure the market that they aren't in opposition to foreigners and PRs investing in Singapore's property market. They imposed these measures because of extenuating components available in the market." The sale of new dual-key EC models will even be restricted to multi-generational households only. The models have two separate entrances, permitting grandparents, for example, to dwell separately. The vendor's stamp obligation takes effect right this moment and applies to industrial property and plots which might be offered inside three years of the date of buy. JLL named Best Performing Property Brand for second year running

    The data offered is for normal info purposes only and isn't supposed to be personalised investment or monetary advice. Motley Fool Singapore contributor Stanley Lim would not personal shares in any corporations talked about. Singapore private home costs increased by 1.eight% within the fourth quarter of 2012, up from 0.6% within the earlier quarter. Resale prices of government-built HDB residences which are usually bought by Singaporeans, elevated by 2.5%, quarter on quarter, the quickest acquire in five quarters. And industrial property, prices are actually double the levels of three years ago. No withholding tax in the event you sell your property. All your local information regarding vital HDB policies, condominium launches, land growth, commercial property and more

    There are various methods to go about discovering the precise property. Some local newspapers (together with the Straits Instances ) have categorised property sections and many local property brokers have websites. Now there are some specifics to consider when buying a 'new launch' rental. Intended use of the unit Every sale begins with 10 p.c low cost for finish of season sale; changes to 20 % discount storewide; follows by additional reduction of fiftyand ends with last discount of 70 % or extra. Typically there is even a warehouse sale or transferring out sale with huge mark-down of costs for stock clearance. Deborah Regulation from Expat Realtor shares her property market update, plus prime rental residences and houses at the moment available to lease Esparina EC @ Sengkang
  3. Template:Cite arxiv
  4. 20 year-old Real Estate Agent Rusty from Saint-Paul, has hobbies and interests which includes monopoly, property developers in singapore and poker. Will soon undertake a contiki trip that may include going to the Lower Valley of the Omo.

    My blog: http://www.primaboinca.com/view_profile.php?userid=5889534
  5. 20 year-old Real Estate Agent Rusty from Saint-Paul, has hobbies and interests which includes monopoly, property developers in singapore and poker. Will soon undertake a contiki trip that may include going to the Lower Valley of the Omo.

    My blog: http://www.primaboinca.com/view_profile.php?userid=5889534
  6. One of the biggest reasons investing in a Singapore new launch is an effective things is as a result of it is doable to be lent massive quantities of money at very low interest rates that you should utilize to purchase it. Then, if property values continue to go up, then you'll get a really high return on funding (ROI). Simply make sure you purchase one of the higher properties, reminiscent of the ones at Fernvale the Riverbank or any Singapore landed property Get Earnings by means of Renting

    In its statement, the singapore property listing - website link, government claimed that the majority citizens buying their first residence won't be hurt by the new measures. Some concessions can even be prolonged to chose teams of consumers, similar to married couples with a minimum of one Singaporean partner who are purchasing their second property so long as they intend to promote their first residential property. Lower the LTV limit on housing loans granted by monetary establishments regulated by MAS from 70% to 60% for property purchasers who are individuals with a number of outstanding housing loans on the time of the brand new housing purchase. Singapore Property Measures - 30 August 2010 The most popular seek for the number of bedrooms in Singapore is 4, followed by 2 and three. Lush Acres EC @ Sengkang

    Discover out more about real estate funding in the area, together with info on international funding incentives and property possession. Many Singaporeans have been investing in property across the causeway in recent years, attracted by comparatively low prices. However, those who need to exit their investments quickly are likely to face significant challenges when trying to sell their property – and could finally be stuck with a property they can't sell. Career improvement programmes, in-house valuation, auctions and administrative help, venture advertising and marketing, skilled talks and traisning are continuously planned for the sales associates to help them obtain better outcomes for his or her shoppers while at Knight Frank Singapore. No change Present Rules

    Extending the tax exemption would help. The exemption, which may be as a lot as $2 million per family, covers individuals who negotiate a principal reduction on their existing mortgage, sell their house short (i.e., for lower than the excellent loans), or take part in a foreclosure course of. An extension of theexemption would seem like a common-sense means to assist stabilize the housing market, but the political turmoil around the fiscal-cliff negotiations means widespread sense could not win out. Home Minority Chief Nancy Pelosi (D-Calif.) believes that the mortgage relief provision will be on the table during the grand-cut price talks, in response to communications director Nadeam Elshami. Buying or promoting of blue mild bulbs is unlawful.

    A vendor's stamp duty has been launched on industrial property for the primary time, at rates ranging from 5 per cent to 15 per cent. The Authorities might be trying to reassure the market that they aren't in opposition to foreigners and PRs investing in Singapore's property market. They imposed these measures because of extenuating components available in the market." The sale of new dual-key EC models will even be restricted to multi-generational households only. The models have two separate entrances, permitting grandparents, for example, to dwell separately. The vendor's stamp obligation takes effect right this moment and applies to industrial property and plots which might be offered inside three years of the date of buy. JLL named Best Performing Property Brand for second year running

    The data offered is for normal info purposes only and isn't supposed to be personalised investment or monetary advice. Motley Fool Singapore contributor Stanley Lim would not personal shares in any corporations talked about. Singapore private home costs increased by 1.eight% within the fourth quarter of 2012, up from 0.6% within the earlier quarter. Resale prices of government-built HDB residences which are usually bought by Singaporeans, elevated by 2.5%, quarter on quarter, the quickest acquire in five quarters. And industrial property, prices are actually double the levels of three years ago. No withholding tax in the event you sell your property. All your local information regarding vital HDB policies, condominium launches, land growth, commercial property and more

    There are various methods to go about discovering the precise property. Some local newspapers (together with the Straits Instances ) have categorised property sections and many local property brokers have websites. Now there are some specifics to consider when buying a 'new launch' rental. Intended use of the unit Every sale begins with 10 p.c low cost for finish of season sale; changes to 20 % discount storewide; follows by additional reduction of fiftyand ends with last discount of 70 % or extra. Typically there is even a warehouse sale or transferring out sale with huge mark-down of costs for stock clearance. Deborah Regulation from Expat Realtor shares her property market update, plus prime rental residences and houses at the moment available to lease Esparina EC @ Sengkang