You are on page 1of 3

Max Common Sequence

public class Greedy {


// beg ‫מחילים מאיבר מספר‬arr ‫ בתוך מערך‬elem ‫חיפוש איבר‬
// ‫ במידה והאיבר לא נמצא במערך‬,‫הפונקציה מחזירה מיקום האיבר‬
// -1 ‫הפונקציה מחזירה‬
public static int find(int beg, char arr[], char elem){
int index = -1;
for (int i=beg; i<arr.length; i++){
if (arr[i]==elem){
index = i;
break;
}
}
return index;
}
//‫אלגוריתם חמדני לחיפוש מחרוזת משותפת‬
public static char [] greedy(char[] X, char [] Y){
char temp[] = new char[X.length];
int ind = 0, count = 0, index = 0, beg = 0;
while (ind < X.length){
index = find(beg, Y, X[ind]);
if (index != -1){
temp[count++] = X[ind];
beg = index+1;
}
ind = ind + 1;
}
// resise
if (count > 0){
char res[] = new char[count];
for (int i=0; i<count; i++)
res[i] = temp[i];
return res;
}
return null;
}
‫ חישוב בשיטת מטריצה את אורכה של המחרוזת המשותפת הארוכה ביותר‬//
public static int[][] maxSeqLength(char[] X, char [] Y){
int row = X.length+1, col = Y.length+1;
int temp[][] = new int[row][col];
int i=0, j=0, count=0;
// first column
for (i=0; i<row; i++) temp[i][0]=0;
// first row
for (j=0; j<col; j++) temp[0][j]=0;
// Matrix Interior
for (i=1; i<row; i++) {
for (j=1; j<col; j++) {
if (X[i-1]==Y[j-1]){
temp[i][j] = temp[i-1][j-1] + 1;
}
else{
temp[i][j] = Math.max(temp[i-1][j], temp[i][j-1]);
}
}
}
return temp;
}
1
‫הפונקציה מקבלת שתי מחרוזת ומטריצה המכילה את אורכה של המחרוזת‬
) maxSeqLength)( ‫המשותפת הארוכה ביותר (תוצאה של פונקצית‬

// max common sequence


public static char[] maxSequence(int mat[][],char[] X, char [] Y){
int row = mat.length;
int col = mat[0].length;
int seqLength = mat[row-1][col-1];
char result[] = new char[seqLength];
int i=row-1, j=col-1, count=seqLength-1;
while (i>0 && j>0){
if (X[i-1]==Y[j-1]){
result[count--]=X[i-1];
i = i - 1;
j = j - 1;
}
else if (mat[i][j]==mat[i][j-1]){
j = j - 1;
}
else{
i = i - 1;
}
}
return result;
}
//‫בדיקת פונקציה שמחשבת את המחרוזת המשותפת‬
//‫הארוכה ביותר של שתי מחרוזות נתונות‬
public static void testMaxSeq(){
//char X[]={'b','d','c','a','b','a'};
//char Y[]={'a','b','c','b','d','a','b'};
//char X[]={'a','a','a','a','c','c'};
//char Y[]={'a','a','a','c','b','a','c'};
//char X[]={'c','a','a','d','a','d'};// != greedy
//char Y[]={'a','a','a','a','c','d','b'}; //!= greedy
int numOfLetters=4, sizeX = 10, sizeY = 10;
char X[] = MyLibrary.randomCharArray(sizeX,numOfLetters);
MyLibrary.printCharArray(X);
char Y[]=MyLibrary.randomCharArray(sizeY,numOfLetters);
MyLibrary.printCharArray(Y);
// precious algorithm
int mat[][] = maxSeqLength(X, Y);
System.out.println(mat[mat.length-1][mat[0].length-1]);
MyLibrary.printCharArray(maxSequence(mat, X, Y));
}
public static void main(String[] args) {
//testGreedy();
testMaxSeq();
}
}

2
//‫בדיקה של אלגוריתם חמדני מול אלגוריתם שנותן את אורכה‬
// ‫של המחרוזת המשותפת הארוכה ביותר‬

public static void testGreedy(){


//char X[]={'b','d','c','a','b','a'};
//char Y[]={'a','b','c','b','d','a','b'};
//char X[]={'a','a','a','a','c','c'};
//char Y[]={'a','a','a','c','b','a','c'};
//char X[]={'c','a','a','d','a','d'};// != greedy
//char Y[]={'a','a','a','a','c','d','b'}; //!= greedy
int numOfLetters=4, sizeX = 20, sizeY = 20;
int countEquals=0, total = 10000;
for (int i=0; i<total; i++){
char X[] = MyLibrary.randomCharArray(sizeX,numOfLetters);
//MyLibrary.printCharArray(X);
char Y[]=MyLibrary.randomCharArray(sizeY,numOfLetters);
//MyLibrary.printCharArray(Y);
// greedy algorithm
char resGreedy[] = greedy(X, Y);
//MyLibrary.printCharArray(resGreedy);
// precious algorithm
int mat[][] = maxSeqLength(X, Y);
if (resGreedy!=null && mat[mat.length-1][mat[0].length-
1]==resGreedy.length) countEquals++;
}
double percent = (100.0*(double)countEquals)/total;
System.out.println("total "+total+", countEquals "+countEquals+"
,percent "+percent+"%");

OUTPUT

////////////////////// numOfLetters=4, sizeX = 6, sizeY = 7;


// total 1000000, countEquals 655331 ,percent 0.655331
// total 1000000, countEquals 655679 ,percent 0.655679
// total 1000000, countEquals 655850 ,percent 0.65585
// total 1000000, countEquals 655442 ,percent 0.655442

////////////////////// numOfLetters=4, sizeX = 10, sizeY = 10;


// total 100000, countEquals 30068 ,percent 0.30068
// total 100000, countEquals 29941 ,percent 0.29941

//////////////////////numOfLetters=4, sizeX = 20, sizeY = 20;


// total 10000, countEquals 220 ,percent 0.022
// total 10000, countEquals 187 ,percent 0.0187
// total 10000, countEquals 203 ,percent 0.0203
// total 10000, countEquals 213 ,percent 0.0213

You might also like