For a class I had to write a program that does stuff with tables. I did the hard part, but now I need to write an algorithm complexity analysis for it. I understand BASICALLY what this is, and I can apply it to simple things like a loop, but the prof wants an analysis for the ENTIRE code. How do I do this?
First:
// CARTESIAN PRODUCT
// headers first
for (int i = 0; i < relation1[0].length; i++) {
cartProd[0][i] = relation1[0][i];
}
for (int i = 0; i < relation2[0].length; i++) {
cartProd[0][(i + relation1[0].length)] = relation2[0][i];
}
// Merge rows, but skip the first row of everything (headers)
for (int z = 1; z < relation1.length; z++) {
for (int i = 1; i < relation2.length; i++) {
for (int j = 0; j < relation2[0].length + relation1[0].length; j++) {
int cartRow = (i + ((relation2.length - 1) * (z - 1)));
if (j < relation1[0].length) {
cartProd[cartRow][j] = relation1[z][j];
} else {
cartProd[cartRow][j] = relation2[i][j - relation1[0].length];
}
}
}
}
System.out.println("\nCartesian Product: \n"
+ Arrays.deepToString(cartProd).replaceAll("],", "]," + System.getProperty("line.separator")));
Second:
for (int i = 1, colCount = 0; i < relation1.length; i++) {
for (int j = 0; j < relation1[0].length; j++) {
if (relation1[0][j].equals(commonColumnName)) {
commonWord = relation1[i][j];
for (int x = 0; x < relation1[0].length; x++) {
if (CurrentColumnValid(relation1[0][x], header_natural)) {
naturalJoinString += ((relation1[i][x]) + " ");
if(i==1) {
myNaturalHeaders[colCount]=relation1[0][x]; colCount++;
}
}
}
// Right
for (int a = 0; a < relation2.length; a++) {
for (int b = 0; b < relation2[0].length; b++) {
if (relation2[0][b].equals(commonColumnName) && relation2[a][b].equals(commonWord)) {
for (int c = 0; c < relation2[0].length; c++) {
if (CurrentColumnValid(relation2[0][c], header_natural)
&& (!relation2[a][c].equals(commonWord))) {
naturalJoinString += ((relation2[a][c]) + " ");
if(i==1) {
myNaturalHeaders[colCount]=relation2[0][c]; colCount++;
}
}
}
}
}
}
}
}
}
The problem is, I am pretty sure that I made this as complicated as possible. As far as I can tell, the way I wrote the program it is bulletproof testwise, so I want to keep this. BUT, I have no clue how to write a complexity analysis for an entire program, let alone four for loops within one another :/