Design an efficient algorithm for finding the longest exact repeat within a text.
Solution:
For finding the longest exact repeat within a text we can follow:
We can create the suffix tree using suffix array and LCP array that being said you can achieve the same thing using these(Suffix and LCP) array as if you create suffix tree directly. Thus longest exact repeated problem for a string S of length n can be solved in O(n) time using both the suffix array A and the LCP array L. Will construct suffix array and LCP array.
Time to build Suffix Array is O(n) using DC3 Algorithm.
Time to build LCP array is O(n) using linear scan of Suffix
Array.
Time to find the Longest Repeated Sub-String O(n) linear scan of
LCP array followed by returning sub-string from original string ,
its also O(n) in worst case.
Running code in java as follows:
import java.util.Arrays; public class LRS { // return the longest common prefix of s and t public static String lcp(String s, String t) { int n = Math.min(s.length(), t.length()); for (int i = 0; i < n; i++) { if (s.charAt(i) != t.charAt(i)) return s.substring(0, i); } return s.substring(0, n); } // return the longest repeated string in s public static String lrs(String s) { // form the N suffixes int n = s.length(); String[] suffixes = new String[n]; for (int i = 0; i < n; i++) { suffixes[i] = s.substring(i, n); } // sort them Arrays.sort(suffixes); // find longest repeated substring by comparing adjacent sorted suffixes String lrs = ""; for (int i = 0; i < n-1; i++) { String x = lcp(suffixes[i], suffixes[i+1]); if (x.length() > lrs.length()) lrs = x; } return lrs; } // read in text, replacing all consecutive whitespace with a single space // then compute longest repeated substring public static void main(String[] args) { String s = StdIn.readAll(); s = s.replaceAll("\\s+", " "); StdOut.println("'" + lrs(s) + "'"); } }
Please give thumbsup, if you like it. Thanks.
Design an efficient algorithm for finding the longest exact repeat within a text.
Design an efficient algorithm to find the longest path in a directed acyclic graph. (Must handle general real-valued weights on the edges, including negative values.) Use C, Java, or Python.
JAVA: (29.1) The text introduced Prim’s algorithm for finding a minimum spanning tree. Kruskal’s algorithm is another well-known algorithm for finding a minimum spanning tree. The algorithm repeatedly finds a minimum- weight edge and adds it to the tree if it does not cause a cycle. The process ends when all vertices are in the tree. Design and implement an algorithm for finding an MST using Kruskal’s algorithm.
(Q4 - 30 pts: 15, 15) a) Give an O (n) time algorithm for finding the longest (simple) path in a tree on n vertices. Prove the correctness of your algorithm. Give a polynomial time algorithm for finding the longest (simple) path in a graph whose blocks have size bounded by a constant. Prove the correctness of your algorithm. b)
Design an algorithm ( code C ) to calculate the frequency of each of the vowels (lowercase and uppercase) in a text. The program will ask the user for a text, which will be entered by keyboard and will save, in a table (in%), the frequency of each of the vowels within the text, taking into account all the characters of the text. The text will end with the character '' # ''. Includes the emty sequence.
Write an algorithm and design a flowchart for finding X^y ?. For example: 2^3=2*2*2=8
Describe a strategy and design an algorithm for finding optimal solutions to such search problems. You may use any example based on search algorithms we have studied in the class, or any combination or modified version of any heuristic based search algorithm. Please include a description of what your search space might look like, what the states might be, and what may be heuristics that could be selected. Search and Heuristics
2. (40 pts) Let A, B, and C be three strings each n characters long. We want to compute the longest subsequence that is common to all three strings. (a) Let us first consider the following greedy algorithm for this problem. Find the longest common subsequence between any pair of strings, namely, LCS(A, B) LCS(B, C), LCS(A, C). Then, find the longest common subsequence between this LCS and the 3rd string. That is, supposing that the longest common pair wise...
2. (40 pts) Let A, B, and C be three strings each n characters long. We want to compute the longest subsequence that is common to all three strings. (a) Let us first consider the following greedy algorithm for this problem. Find the longest common subsequence between any pair of strings, namely, LCS(A, B). LCS(B,C), LCS(A, C). Then, find the longest common subsequence between this LCS and the 3rd string. That is, supposing that the longest common pair wise subsequence...
Provide a most efficient divide-and-conquer algorithm for determining the smallest and second smallest values in a given unordered set of numbers. Provide a recurrence equation expressing the time complexity of the algorithm, and derive its exact solution in the number of comparisons. For simplicity, you may assume the size of the problem to be an exact power of a the number 2
Exercise 3: Searching Applications Design and implement an algorithm that determines whether or not a given array of integers, list1, is completely contained within another given array of integers, list2. Consider two different scenarios: (1) Both arrays are unsorted and (2) both arrays are sorted and write functions unsortedSearch and sortedSearch for (1) and (2), respectively. Your algorithm for (2) is expected to be more efficient than the one for (1). Please Answer in c++ and show the algorithm working