Longest Increasing Subsequence has been a classic Dynamic Programming problem. O(N^2) has been around for a while but more interesting is the following O(n log n) solution.
Problem Input : Sequence of integers (or any comparable items)
Output : Longest increasing subsequence of the sequence, which may not be contiguous.
For example : We have sequence : 1,8,2,7,3,6,4,5 which has the longest increasing subsequence : 1,2,3,4,5.
Note that the numbers are non-contiguous.
[Read More]
To find the longest substring with unique characters in a given string
#include <stdio.h> #include <string.h> typedef struct { const char \*start; size\_t len; }Substring; Substring longestSubstring(const char \*s){ Substring ret = {s, 1}; Substring cur = {s, 1}; size\_t i, len = strlen(s); const char \*p = NULL; for(i = 1; i < len; ++i){ p = memchr(cur.start, s, cur.len); if(p){ if(cur.len > ret.len) ret = cur; cur.len -= (p - cur.start) + 1; cur.start = p + 1; } cur.len++; } if(cur.
[Read More]
Find longest palindrome in a string.
The Algorithm that you mentioned to take the LCS o…
Anonymous - Jun 6, 2014
The Algorithm that you mentioned to take the LCS of S and S’ where S’ is the reverse of S is a flawed Algorithm, Please check this post
http://leetcode.com/2011/11/longest-palindromic-substring-part-i.html
Find longest palindrome in a string.
I cannot think of any that has a complexity less than O(n*n).
one of the ways would be :
1. Take the string (s1) and its direct reversal (s2)
2. Slide s2 over s1 per character and count the number of similar characters starting from s2′s last character
Hence, first:
———————— ABCDAMNMADGHJ
JHGDAMNMADCBA Count =1
Then,
————————ABCDAMNMADGHJ
–JHGDAMNMADCBA Count=0
.
.
.
————————ABCDAMNMADGHJ
————————JHGDAMNMADCBA Count=7
.
.
.
————————ABCDAMNMADGHJ Count= 0 We can exit at this point
[Read More]
Longest common substring revisited
For calculating longest substring we can use following algorithms:
1. Dynamic Algorithm
2. Hirschberg’s algorithm
Reducing the space for LCS lengths
When you’ve run out of main memory, any estimate of runtime based on big-O analysis becomes useless. The system either crashes or thrashes, paging in virtual memory. By contrast, if we can reduce the memory required by a program, the time analysis should still hold — we never have to page in more time.
Once you’ve watched the dynamic programming solution a few times, you’ll realise that the LCS lengths (the numbers in the grid) are computed row by row, with each row only depending on the row above.
[Read More]
Hirschberg’s linear space algorithm in C++
It should work on any container type which supports forward and reverse iteration. It’s similar to the Python implementation, but note:
rather than copy slices of the original sequences, it passes around iterators and reverse iterators into these sequences rather than recursively accumulating sums of sub-LCS results, it ticks off items in a members array, xs_in_lcs /\* C++ implementation of "A linear space algorithm for computing maximal common subsequences" D.
[Read More]
Hirschberg's algorithm
Hirschberg’s algorithm, named after its inventor, Dan Hirschberg, is a dynamic programming algorithm that finds the least cost sequence alignment between two strings, where cost is measured as Levenshtein edit distance, defined to be the sum of the costs of insertions, replacements, deletions, and null actions needed to change one string to the other. Hirschberg’s algorithm is simply described as a divide and conquer version of the Needleman-Wunsch algorithm.
The Needleman-Wunsch algorithm finds an optimal alignment in O(nm) time, using O(nm) space.
[Read More]
Dynamic Programming Algorithm (DPA) for Edit-Distance
The words `computer’ and `commuter’ are very similar, and a change of just one letter, p->m will change the first word into the second. The word `sport’ can be changed into `sort’ by the deletion of the `p’, or equivalently, `sort’ can be changed into `sport’ by the insertion of `p’.
The edit distance of two strings, s1 and s2, is defined as the minimum number of point mutations required to change s1 into s2, where a point mutation is one of:
[Read More]
Dynamic Programming Practise Problem Set 3
Calculate M = M1 × M2 × M3 × M4, where the dimensions of the matrices are M1: 10,20 M2: 20,50 M3: 50,1 M4: 1,100
a) Calculating M = M1 × ( M2 × ( M3 × M4 ) )
b) Calculating M = ( M1 × ( M2 × M3 ) ) × M4
2 Calculate the length of longest sub string in Hello and Aloha
3. Calculate longest sub sequence in houseboat and computer.
[Read More]