What is the Big O analysis of this algorithm?(Stackoverflow)
2015-03-20 16:17
501 查看
Question:
I'm working on a data structures course and I'm not sure how to proceed w/ this Big O analysis:
My initial idea is that this is O(n^3) after reduction, because the innermost loop will only run when
no remainder and the multiplication rule is inapplicable. Is my reasoning correct here?
Answer:
Let's ignore the outer loop for a second here, and let's analyze it in terms of
The mid loop runs
and is invoking the inner loop whenever
this means that the inner loop summation of running time is:
The last equality comes from sum of arithmetic progression.
The above is in
repeat this to the outer loop which runs from
you will get running time of
since you actually have:
The last equation comes from sum of cubes
And the above is in
which is your complexity.
I'm working on a data structures course and I'm not sure how to proceed w/ this Big O analysis:
sum = 0; for(i = 1; i < n; i++) for(j = 1; j < i*i; j++) if(j % i == 0) for(k = 0; k < j; k++) sum++;
My initial idea is that this is O(n^3) after reduction, because the innermost loop will only run when
j/
ihas
no remainder and the multiplication rule is inapplicable. Is my reasoning correct here?
Answer:
Let's ignore the outer loop for a second here, and let's analyze it in terms of
i.
The mid loop runs
i^2times,
and is invoking the inner loop whenever
j%i == 0, that means you run it on
i, 2i, 3i, ...,i^2, and at each time you run until the relevant
j,
this means that the inner loop summation of running time is:
i + 2i + 3i + ... + (i-1)*i = i(1 + 2 + ... + i-1) = i* [i*(i-1)/2]
The last equality comes from sum of arithmetic progression.
The above is in
O(i^3).
repeat this to the outer loop which runs from
1to
nand
you will get running time of
O(n^4),
since you actually have:
C*1^3 + C*2^3 + ... + C*(n-1)^3 = C*(1^3 + 2^3 + ... + (n-1)^3) = = C/4 * (n^4 - 2n^3 + n^2)
The last equation comes from sum of cubes
And the above is in
O(n^4),
which is your complexity.
相关文章推荐
- android.util.AndroidRuntimeException Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? com.uethinking.microvideo.manag
- What is the maximum amount of memory any single process on Windows can address? Is this different than the maximum virtual memor
- What Is the Purpose of This Book?
- 软件随想录(local.joelonsoftware.com/wiki)-2001年05月05日 这个国家的狗做什么工作? - What Is the Work of Dogs in this Cou
- What is the name of the “-->” operator?(Stackoverflow)
- What is the name of the “-->” operator?(Stackoverflow)
- What is the content of J2EE EAR, WAR and JAR files?
- Managed, Unmanaged, Native: What Kind of Code Is This?
- WPF Compilation Error: Unknown Error. This implementation is not part of the Windows Platform FIPS validated cryptographic algor
- The query processor ran out of internal resources and could not produce a query plan. This is a rare event and only expected for
- Version 1.3.1_01 of the JVM is not suitable for this product.Version:1.4.1 or greater is required。
- What is the easiest way of getting OpenLayers to configure itself for printing?
- What is the difference of x=x+3 and x+=3?
- This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms.
- Sharepoint Error: "This server is not the indexer of this search application"
- [转]Managed, Unmanaged, Native: What Kind of Code Is This?
- Problem 16 - What is the sum of the digits of the number 2^1000?
- What is the maximum length of a URL?
- Eclipse错误:Version 1.3.1_01 of the JVM is not suitable for this product
- [转]Managed, Unmanaged, Native: What Kind of Code Is This?