Skip to content

Commit 8a951a7

Browse files
committed
Added LCP Array
1 parent 7f5ccd9 commit 8a951a7

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.jwetherell.algorithms.data_structures;
2+
3+
import java.util.ArrayList;
4+
5+
/**
6+
* In computer science, the longest common prefix array (LCP array) is an auxiliary
7+
* data structure to the suffix array. It stores the lengths of the longest common
8+
* prefixes (LCPs) between all pairs of consecutive suffixes in a sorted suffix array.
9+
*
10+
* https://en.wikipedia.org/wiki/LCP_array
11+
*
12+
* @author Jakub Szarawarski <kubaszarawarski@gmail.com>
13+
*/
14+
public class LCPArray {
15+
private static final char DEFAULT_END_SEQ_CHAR = '$';
16+
private char END_SEQ_CHAR;
17+
18+
private SuffixArray suffixArrayBuilder;
19+
20+
private ArrayList<Integer> LCP;
21+
22+
public LCPArray(CharSequence sequence){
23+
this(sequence, DEFAULT_END_SEQ_CHAR);
24+
}
25+
26+
public LCPArray(CharSequence sequence, char endChar) {
27+
END_SEQ_CHAR = endChar;
28+
suffixArrayBuilder = new SuffixArray(sequence, endChar);
29+
}
30+
31+
public ArrayList<Integer> getLCPArray() {
32+
if(LCP == null){
33+
LCPAlgorithm();
34+
}
35+
return LCP;
36+
}
37+
38+
private void LCPAlgorithm() {
39+
ArrayList<Integer> LCPR = getLCPR();
40+
getLCPfromLCPR(LCPR);
41+
}
42+
43+
private ArrayList<Integer> getLCPR() {
44+
ArrayList<Integer> KMRArray = suffixArrayBuilder.getKMRarray();
45+
ArrayList<Integer> suffixArray = suffixArrayBuilder.getSuffixArray();
46+
String string = suffixArrayBuilder.getString();
47+
int length = KMRArray.size();
48+
49+
ArrayList<Integer> LCPR = new ArrayList<Integer>(); // helper array, LCP[i] = LCPR[suffixArray[i]]
50+
51+
int startingValue = 0;
52+
53+
for(int i=0; i<length; i++){
54+
if(KMRArray.get(i).equals(0)){
55+
LCPR.add(0);
56+
startingValue = 0;
57+
}
58+
else{
59+
int LCPRValue = startingValue;
60+
int predecessor = suffixArray.get(KMRArray.get(i)-1);
61+
62+
while(string.charAt(i+LCPRValue) == string.charAt(predecessor+LCPRValue))
63+
LCPRValue++;
64+
65+
LCPR.add(LCPRValue);
66+
startingValue = LCPRValue-1 > 0 ? LCPRValue-1 : 0;
67+
}
68+
}
69+
70+
return LCPR;
71+
}
72+
73+
private void getLCPfromLCPR(ArrayList<Integer> LCPR) {
74+
ArrayList<Integer> suffixArray = suffixArrayBuilder.getSuffixArray();
75+
int length = suffixArray.size();
76+
77+
LCP = new ArrayList<Integer>();
78+
LCP.add(null); //no value for LCP[0]
79+
80+
for(int i=1; i<length; i++){
81+
LCP.add(LCPR.get(suffixArray.get(i)));
82+
}
83+
}
84+
}

src/com/jwetherell/algorithms/data_structures/SuffixArray.java

+4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public ArrayList<Integer> getKMRarray() {
4848
return KMRarray;
4949
}
5050

51+
public String getString(){
52+
return string;
53+
}
54+
5155
/**
5256
* Creates suffix array using KMR algorithm with O(n log^2 n) complexity.
5357
*

0 commit comments

Comments
 (0)