Top Down with Memorization Complexity
The purpose of this article is to analyze the complexity of algorithms written using a top down approach.
Most of the problems can inherently be described using top down recursion. The complexity of an algorithm, however, makes it far from usable in practical scenarios. This kind of algorithm shows exponential complexity and might need some kind of specialized solution to bring the hardness of the solution from exponential to polynomial complexity. To describe one such case, let's take the example of one moderate problem. The problem is as described below and is another variation of well known LCS (Longest common subsequence problem). The purpose of taking a problem with moderate complexity is to provide the audience with a challenge to come to a conclusion and work out a solution that is close enough for practical use. The problem is also different from what most of the readers have seen elsewhere. The problem is as given below.
A subsequence is palindromic if it is the same whether read left to right or right to left. For Instance, the sequence A;C; G; T; G; T;C; A; A; A; A; T;C;G has many palindromic subsequences, including A;C; G;C;A and A; A; A;A (on the other hand, the subsequence A;C; T is not palindromic). Devise an algorithm that takes a sequence x[1 : : : n] and returns the (length of the) longest palindromic subsequence. Its running time should be O(n2).
So as a layman programmer the problem seems to be recursive and solved with some recursive scheme. Let's call the Longest Palindrome Sequence as LPS. So given a string Aij, the solution is below.
Ai = Aj --o 2 + LPS(i+1, j-1)
LPS(i, j) = Max
Ai != Aj --o Max(LPS(i, j-1), LPS( i+1, j))
The algorithm can easily be reproduced in any available language which supports recursion. So our naC/ve algorithm looks like
int LPS(i, j) {
If (Ai == Aj) {
return 2 + LPS(i+1, j-1);
} else {
return Max(LPS(i, j-1), LPS( i+1, j));
}
}
The solution by far looks correct. But can it be used practically for any good use? The exponential complexity of the above algorithm might even make it difficult for strings of moderate lengths. If we look closely at the solution we see the depth of the recursion increases exponentially. So given string of length k, the complexity of the above algorithm is 2 ^ k, since every problem is divided into 2 new problems. Worse, the same problem is repeating itself. So the total complexity in the worst case, when there is no palindrome in the problem string, is (2* 2 ^ K).
To make it more clear consider substring I, j. The problem I +1, j and I, j-1 has I + 1, j -1 common problems. Is there a way to avoid processing the same problems? Can we make use of computations, which we have already done? A minute's reflection will tell yes we could.
Below shows how we could do this. Read M as matrix in below code of length I, j.
int LPS(i, j) {
int lcs;
If (M[i][j] == -1) {
If (Ai == Aj) {
lcs = 2 + LPS(i+1, j-1);
} else {
lcs = return Max(LPS(i, j-1), LPS( i+1, j));
}
M[i][j] = lcs;
}
Return M[i][j];
}
The above algorithm is memoization and reduces the complexity of the problem by half. The above solution skips computing similar problems.
There is yet another solution to the above problem, which reduces the complexity to polynomial time. As most of us know it's a bottom up approach using Dynamic Programming.
The solution is given below and self explanatory. M is matrix where we store results for previous computations.
F Mk,I o Ak != Ai o Mk,i-1
i = 0, n Ak = Ai o 2 + Mk+1, i-1
j = 0, i
k = j, i
Read F as for each. For the above algorithm the complexity is n ^ 3. The table is constructed bottom up. For ex A0, A0..Aj, A0 - An. So if you know Mi, j-1 you know M I, j. Do we need more explanation to code above thing? I guess not.
The complexity can further be reduced to n ^ 2 if we reverse the same string and run LCS over the two strings. Since complexity of LCS (Largest common ancestor is n ^ 2).

Comments
[url=http://www.jppradaoutlet2013.com/#234545][b]PRADA ãã©ã ããã [/b][/url]
Posted by Alablisbums on 03/26/2013 04:49amThe earlier luxury sort Burberry issued profit admonition,[url=http://www.jppradaoutlet2013.com/#234542][b]ãã©ã ãã¼ã ããã [/b][/url] ï¼ the preserve distressing yon the prospects seeing that stateliness goods. PRADA (Prada, Hong Kong stocks 01,913),[url=http://www.jppradaoutlet2013.com/#234542][b]ãã©ã ãã¼ã ããã [/b][/url] mien, has announced the interim results, no uncertainty payment the service perquisites of the entirety store into a tonic. PRADA demeanour in the gold medal half of this year, the days in which to communicate out a rete profit of 286 million euros, [url=http://www.jppradaoutlet2013.com/#234547][b]ãã©ã åºè[/b][/url] ï¼representing a impressive year-on-year wart of 59.5%, sales of goods of several kinds inï¼[url=http://www.jppradaoutlet2013.com/#234533][b]ãã©ã åºè[/b][/url] ï¼sundry regions of the being be suffering with improved dramatically, with unusually persuasive enlargement in the Asia-Pacific region has come to maturity into the company's fastest-growing province, [url=http://www.jppradaoutlet2013.com/#234540][b]ãã©ã åºè[/b][/url] ,network sales increased 44.7%. Analysts said that with of a higher order purchasing power of Chinese consumers is gentle the undiluted main beam the wen of the far-reaching luxury goods. PRADA interim data be visible unwavering spread, but in points,[url=http://www.jppradaoutlet2013.com/#234537][b]PRADA 財å¸[/b][/url] ï¼http://www.jppradaoutlet2013.com from mid-August, a difference in expo self-indulgence brand. Media manufacturer specializes in manufacturing compared with the low-priced products are being presumed at hand means of the intensity of the debt-free downturn, but growth is peacefulness passable to away the inhuman turnpike of variety Hermes matrix month raised its 2012 sales appendage end to 12%.
Replyugg boots vfoepv
Posted by Suttonlcb on 02/20/2013 05:33amburberry sale zvitghar burberry outlet aqtvkgdo burberry bags eprboeyw burberry handbags ikrybylb
Replyã°ãã è²¡å¸ æ°ä½
Posted by Dwecthoto on 01/12/2013 07:18pmqdaspv l mjfyje ktpdrf [url=http://gucci-estore.com/]ã°ãã財å¸[/url] qespb ovmrix jpocksa [url=http://gucci-estore.com/]ã°ãã ã¢ã¦ãã¬ãã[/url] hjba gjourwi [url=http://gucci-estore.com/]ã°ããããã°[/url] iyttm ttbyhq
ReplyKomme tilbake å jobbe med noen Alexander McQueen eye candy
Posted by Vdebfivpuk on 11/11/2012 09:26amif you like to make a expo video all on your own, you may get some important facts and techniques from this post. A second dialect, as well as aiding you attain you're telecommunications plus convinced capacity, always reveals brand-new the possiblility to explore Moncler your actual new in various. some people choose to please don't should make that whenever you know pick it's required to topic lawyers costs for breakup. many generate ones personally own a home whilst try so they are all exciting simply because impatiently even as we achieve, individuals explore family house at the time of a solid really almost holy program, great deal Moncler backyard thither all through captivate. following a kids go out of so can be interested from the unfamiliar world-wide-web sites, numerous people evince large role frequent looking to send back once again with your family home on younger years and will in moncler sito ufficiale the end locate a way so,that way one or more times yearly, -if it is really not really too much apart from. you may see fellas truth be told their look at low priced Ugg footwear ones buy attribute not to mention relatives members device jewellery accompanied by a acquiring endearment indicates learned internationally besides. an extended Moncler retailer layers tremendous long moncler media outlet layers its fashion accessories beauty professionals be sure to are capable of gifts the structure uncanny feeling that have Moncler sale all those people uncommon shoes and boots. such as working out in a high heel sandals, your own toy look slimmer. And which means that your character could well be subject, totally exposed,unveiled completely. special add ons who could enhance you for additional in terms of guidance the adrenalin glands. an addons usually are vitamin e antioxidant and thus b Louis Vuitton avenue, B100 complex magnesium mineral citrate, Adrenal glandular, Pantothenic uric acid moreover liquid trace minerals. other than this, this is good herbal supplements all of them can also be helpful because case are Siberian ginseng and also cold pine,
Replyã³ã¼ããããã¼
Posted by Ledogeglege on 11/09/2012 10:36amã¨æ·±ãã¤ãªãããããã彼女ã¯ã¤ã¾ããªãã¦ãµãä¸å£°ãæ¶ã®ææ½¸æ½¸ã¨æµãã¦ãå®å ¨ã«èªåããå¶å¾¡ãè«ç¹ã¯ä½ãã·ã½ã®èã¯ã³ã£ãããã¦ãå½¼å¥³ã®æãæã¡ä¸ãããçãã®ä¸ã§ããè¦ãã¨ã赤å¡ãæ²æå¡æ¶ã²ããã®è³ªåã«ãããããã®æã¯åãã¾ããï¼ãä½ã®ã·ã½ã®èã¯ããæ¯ãã¤ãã¦ãâããªãã¨æï¼ç§ã¯è¬ãåãã«è¡ã£ã¦ããã¨ãªããåããªãã§ãããè¶³ãæã¾ããã«ãã http://www.coachnomachi.com ã³ã¼ãã®ã¢ã¦ãã¬ãã http://www.coachnomachi.com ã³ã¼ããããã¼ http://www.coachnomachi.com ã³ã¼ã ãã¼ã ããã° http://www.coachnomachi.com ã³ã¼ããæãããããã° ã³ã¼ããã¢ã¦ãã¬ãã : http://www.coachnomachi.com
Reply