2

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 :/

Cœur
  • 37,241
  • 25
  • 195
  • 267
Thomas
  • 193
  • 1
  • 6
  • 14
  • Can you fix the code? You have extra brackets lingering on the second part or your formatting is off. What are first and second; two separate files? Have you tried to come up with anything with the basic knowledge you have? I can't hurt to try and fail. – ChiefTwoPencils Sep 17 '16 at 04:18
  • I just grabbed the two main 'algorithms' from the code. For some reason the website didn't like the way my code was formatted as a whole. The code works fine, I just need to know how to calculate complexity of these algorithms. – Thomas Sep 17 '16 at 04:46

0 Answers0