j.gohel
December 8th, 2007, 01:20 AM
I have just started learning Algorithms.
And i have got stucked at on point.
Actually in the book which i am referring there is an exercise of
multiplying 2 matrices i.e one matrix is (m by n) length and another matrix is (n by p) length
Algorithm :
for(variable i := 1 to m) {
for(variable j := 1 to p) {
for(variable k := 1 to n) {
... here the resultant matrix is built
}
}
}
Now the first question was to find out the time complexity which is as follows:
Starting from top,
the first outermost loop will execute m times,
the second outermost loop will execute m*p times, and the innermost loop will execute m*p*n times.
The second question is : Under what conditions is it profitable to interchange the two outermost loops?
I am stuck at this question.
As far as execution time is considered, interchanging the loops won't make any difference, as per me.
So if i am correct then is there anything else which can make the algorithm efficient by interchanging the two outermost loops ?
Hope i will get my problem's solution.
Thanks in advance.
And i have got stucked at on point.
Actually in the book which i am referring there is an exercise of
multiplying 2 matrices i.e one matrix is (m by n) length and another matrix is (n by p) length
Algorithm :
for(variable i := 1 to m) {
for(variable j := 1 to p) {
for(variable k := 1 to n) {
... here the resultant matrix is built
}
}
}
Now the first question was to find out the time complexity which is as follows:
Starting from top,
the first outermost loop will execute m times,
the second outermost loop will execute m*p times, and the innermost loop will execute m*p*n times.
The second question is : Under what conditions is it profitable to interchange the two outermost loops?
I am stuck at this question.
As far as execution time is considered, interchanging the loops won't make any difference, as per me.
So if i am correct then is there anything else which can make the algorithm efficient by interchanging the two outermost loops ?
Hope i will get my problem's solution.
Thanks in advance.