Im almost done with a homework assignment that multiplies polynomials and has to have its like terms simplified and in order from highest degree to lowest. The 2 statements are also already sorted. My program works perfectly, but it takes too long to get the result (like 2 minutes on my machine), and the web site i use to submit it says time limit exceeded. For the actual multiplication (not shown here), it takes very little time but the combining of like terms takes a while. It takes in 1 linked list that has 2 statements combined, i.e:
2 2 2 1 1 1 //means 2x^2 + 2x + x
*
3 2 5 1 1 0 //means 3x^2 + 5x + 1
and i turn it into 2 2 2 1 1 1 3 2 5 1 1 0 for processing.
Anyone know how i could speed this up a bit? Thanks.
public MyLinkedList add(MyLinkedList combinedList ) {
MyLinkedList tempCombinedList = new MyLinkedList();
MyLinkedList resultList = new MyLinkedList();
//check highest power now that its sorted.
tempCombinedList=null;
tempCombinedList = new MyLinkedList();
int highestPower=0;
//we need to find highest power
for(int l=2;l<=combinedList.size();l=l+2) {
if((Integer)combinedList.get(l)>highestPower) {
highestPower=(Integer)combinedList.get(l);
System.out.println("highest power is "+highestPower);
}
}
int tempAddition=0;
while(highestPower!=-1) {
for(int z=2;z<=combinedList.size();z=z+2) {
if((Integer)combinedList.get(z)==highestPower) {
tempAddition=tempAddition+(Integer)combinedList.get(z-1);
}
}
if((tempAddition!=0)) { //we arent allowed to have a 0 coefficient in there....
resultList.add(tempAddition);
resultList.add(highestPower);
}
else if(((tempAddition==0)&&(highestPower==0))) { //unless the exponent is 0 too
resultList.add(tempAddition);
resultList.add(highestPower);
}
tempAddition=0; //clear the variable for the next roud
highestPower--; //go down in power and check again.
}
return resultList;
}
Your code look like you are using a list with alternating factor and exponent. This is likely not the reason for your performance problem, but makes the code harder to read – additionally to your casting.
Use a class like
and then you can have a
List<Monomial>instead of your list of integers. Firstly, this makes your code more readable, and secondly, you can now (after the multiplication) simply sort your list, so you later don’t have to go through the whole list again and again.Then, as you are always using the
.get(i)access to your list, don’t use a linked list, use an ArrayList (or a similar structure). (For a linked list, you for each access have to iterate through the list to get the element you want, for a array-list not.) Alternatively, use an Iterator (or the enhanced for loop) instead of index-access.Actually, if you sort (and simplify) the factors before multiplying, you can multiply them already in the right sequence so you don’t really have to simplify afterwards. As an example, it is
(You get the scheme: in each line the factors from the first polynomial are sorted downwards, the ones from the second polynomial upwards).
(And you can leave away the 0 terms already at the beginning, if you want).