Last active
April 1, 2017 09:54
-
-
Save ResearcherOne/0cacee29dc15835ee6e81cc695c4b003 to your computer and use it in GitHub Desktop.
LongestIncreasingSubsequenceDetector v1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LongestIncreasingSubsequenceDetector { | |
private int[] extractedLongestIncreasingSubsequence; | |
private int extractedLongestIncreasingSubsequenceLength; | |
private int extractedLongestIncreasingSubsequenceEndingIndex; | |
private int extractedLongestIncreasingSubsequenceStartingIndex; | |
private int[] optResults; | |
private int[] initialNumberSequence; | |
LongestIncreasingSubsequenceDetector() { | |
} | |
public int[] getLongestIncreasingSubsequence() { | |
return extractedLongestIncreasingSubsequence; | |
} | |
public int getLongestIncreasingSubsequenceLength() { | |
return extractedLongestIncreasingSubsequenceLength; | |
} | |
public int getLongestIncreasingSubsequenceEndingIndex() { | |
return extractedLongestIncreasingSubsequenceEndingIndex; | |
} | |
public int getLongestIncreasingSubsequenceStartingIndex() { | |
return extractedLongestIncreasingSubsequenceStartingIndex; | |
} | |
public void extractLongestIncreasingSubsequenceFeatures(int[] numberSequence) { //Return biggest OPT | |
initialNumberSequence = numberSequence; | |
int numberSequenceLength = numberSequence.length; | |
optResults = new int[numberSequenceLength]; //any opt can be at min. 1, initial value of 0 refers to uninitialized cell | |
/* | |
optResults[0] = 1; //initial case | |
for(int i=startingIndex+1; i<numberSequenceLength; i++) { | |
int sequenceIndex = i - startingIndex; | |
//optResults[sequenceIndex] = | |
} | |
*/ | |
} | |
public int longestIncreasingSubsequenceLengthThatStartsFromIndex(int startingIndex) { //Return biggest OPT | |
return OPT(startingIndex, initialNumberSequence); | |
} | |
private int OPT(int j, int[] numberSequence) { //longest increasing sub sequence that exactly starts from j | |
if(j == numberSequence.length-1) { //Special case: Last number's length at the sequence, is one. We already know it. | |
if(optResults[j]==0) { | |
System.out.println("OPT("+j+"): MISS"); | |
optResults[j] = 1; | |
} else { | |
System.out.println("OPT("+j+"): HIT"); | |
} | |
return optResults[j]; | |
} else { | |
boolean isNextNumberSmaller = (numberSequence[j+1] <= numberSequence[j]); | |
if(isNextNumberSmaller) { //Then, this is subsequence with length 1 | |
if(optResults[j]==0) { | |
System.out.println("OPT("+j+"): MISS"); | |
optResults[j] = 1; | |
} else { | |
System.out.println("OPT("+j+"): HIT"); | |
} | |
return optResults[j]; | |
} else { | |
if(optResults[j+1]==0) { | |
System.out.println("OPT("+j+"): MISS"); | |
optResults[j+1] = OPT(j+1, numberSequence); | |
} else { | |
System.out.println("OPT("+j+"): HIT"); | |
} | |
return 1+optResults[j+1]; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
public class Main { | |
public static void main(String[] args) { | |
int[] numberSequence = new int[]{1,2,3,1,2,3}; | |
LongestIncreasingSubsequenceDetector LISD = new LongestIncreasingSubsequenceDetector(); | |
LISD.extractLongestIncreasingSubsequenceFeatures(numberSequence); | |
System.out.println("Input number sequence: "+Arrays.toString(numberSequence)); | |
for(int i=0; i<numberSequence.length;i++) { | |
int result = LISD.longestIncreasingSubsequenceLengthThatStartsFromIndex(i); | |
System.out.println("Longest subseq length that starts at "+i+": "+result); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment