简体   繁体   English

带有 if 语句的嵌套循环的时间复杂度

[英]Time complexity of nested loops with if statement

I am taking a class on advanced C++ but I am getting stumped trying to find the Big-Θ of nested for-loops that are locked behind if-statements.我在高级 C++ 上使用 class 但我很难找到锁定在 if 语句后面的嵌套 for 循环的 Big-Θ。 Unfortunately my professor (for some strange reason) just expects us to know this from a pre-req class (even though I took it at a different college with different course content) and will not take the time to teach me.不幸的是,我的教授(出于某种奇怪的原因)只是希望我们从 pre-req class 中知道这一点(即使我在不同的大学学习了不同的课程内容)并且不会花时间教我。

I don't want anyone to solve my homework for me and I genuinely want to learn the concepts, so I created my own unique function down below.我不希望任何人为我解决我的作业,我真的很想学习这些概念,所以我在下面创建了自己独特的 function。 I just cannot wrap my head around whether this type of function is Θ(n) or Θ(n^2) given that the second loop only runs a few times.鉴于第二个循环只运行了几次,我只是无法理解这种类型的 function 是 Θ(n) 还是 Θ(n^2)。 Any general explanation or maybe pointers in the right direction of how I can figure these types of problems out would be greatly appreciated:)任何一般性的解释或关于我如何解决这些类型的问题的正确方向的指示将不胜感激:)

Note: Assume the variable n is a positive integer of any size.注意:假设变量 n 是任意大小的正 integer。

int count = 20;
for (int i = 0; i < n; i ++) {
    if (i == count) {
        for (int j = 0; j < count; j++) {
            // Some O(1) code
            // Maybe a print to console or something
        }
        count *= 2;
    }
}

The first question you need to ask is, what are you counting?你需要问的第一个问题是,你在数什么? Usually you can find some decent thing to count, instead of just "instructions", to get a good guess.通常你可以找到一些体面的东西来计算,而不仅仅是“说明”,以获得一个好的猜测。 Once you have your big-O value, you can double check the value is right.一旦你有了你的大 O 值,你可以仔细检查这个值是否正确。

int count = 20;
for (int i = 0; i < n; i ++) {

Ok, this obviously runs O(n) times.好的,这显然会运行 O(n) 次。

    if (i == count) {

On a first pass, this is entered O(n/20) times.在第一次通过时,输入 O(n/20) 次。 This may change.这可能会改变。

        for (int j = 0; j < count; j++) {

This runs O(count) times.这运行 O(count) 次。

            // Some O(1) code
            // Maybe a print to console or something
        }

        count *= 2;

now this gets interesting.现在这变得有趣了。 Each time count doubles.每次计数加倍。

So the first inner loop runs after 20 outer loops, and then does 20 O(1) work.所以第一个内部循环在 20 个外部循环之后运行,然后执行 20 个 O(1) 工作。

Count doubles.计数双打。 The second inner loop runs after 40 outer loops, and does 40 O(1) work.第二个内循环在 40 个外循环之后运行,并且完成了 40 O(1) 的工作。

Count doubles.计数双打。 The third inner loop runs after 80 outer loops, and does 80 O(1) work.第三个内循环在 80 个外循环之后运行,并且完成了 80 O(1) 的工作。

Count doubles.计数双打。 The forth inner loop runs after 160 outer loops, and does 160 O(1) work in the inner loop.第四个内循环在 160 个外循环之后运行,在内循环中执行 160 O(1) 次。

Now you have to turn this into math.现在你必须把它变成数学。 You could guess, then check.你可以猜测,然后检查。 But suppose you can't guess?但假设你猜不到?

Well, graph it!嗯,画图吧!

 n   |    inner steps
------+----------------
  20  |    20
  40  |    20+40 = 60
  80  |    20+40+80 = 140
 160  |    20+40+80+160 = 300
 320  |    20+40+80+160+320 = 620

toss that on a graphing program.把它扔到一个绘图程序上。 What does the graph look like ?图表是什么样的? If it curves, taking the logarithm might give you slope from which you can find the exponential of a polynomial.如果它弯曲,取对数可能会给你一个斜率,你可以从中找到多项式的指数。

Remember to use a scatter plot.请记住使用散点图 plot。 You don't care what happens at 60, 100, or 240 all that much.你不在乎在 60、100 或 240 时会发生什么。 You care about the peaks, which happen at 20 and every doubling.你关心峰值,它发生在 20 和每翻一番。 A scatter plot drops your graphed points way down.散点图 plot 将您的图形点向下放置。

Also count the outer loop steps and the comparisons with count to ensure they aren't huge.还要计算外部循环步骤和与 count 的比较,以确保它们不是很大。

Once you have a hypothesis, work out how to prove your answer right.一旦你有了一个假设,就想办法证明你的答案是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM