• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Help me review for exam

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone explain to me the difference between:

For each of the following loops with a method call, determine the overall complexity. As above, assume that method f takes constant time, and that method g takes time linear in the value of its parameter.

1. for (j = 0; j < N; j++) f(j);

2. for (j = 0; j < N; j++) g(j);

3. for (j = 0; j < N; j++) g(k);

I understand why number 1 is O(n), but I don't get it why 2 and 3 are O(n^2). Can you explain it to me in a non-mathematical way. Thanks
 
Saloon Keeper
Posts: 12147
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tonks,

In the second case, you perform g(0), g(1), g(2), ..., g(N). If g performs in linear time, then the loop will take 0 + 1 + 2 + ... + N to complete, or (N^2)/2. The division by 2 has no bearing on the complexity. The complexity class only considers the degree of the polynomial, in this case N^2.

The third case has a wrong answer. The answer is O(N*k), or O(N) if k remains constant. Since k is a constant, the method call will always perform in constant time.
 
Tonks Gabriel
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stephen,

I thought Linear time is O(n)?


Also, how would you determine the big o notation of the following given:

Example:

8n^3+5000n^2, 5. (m + k)(10kn3 + 50kn), or 2^n+1
 
Stephan van Hulst
Saloon Keeper
Posts: 12147
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, linear time is O(n). Why do you ask?

As for your examples, the only thing that matters is the degree (the highest power) of the polynomial.

For your three examples, those would be O(n^3), O(mk(n^3) + (k^2)(n^3)) and O(2^n). The second one can be simplified to O(n^3) if m increases less fast that cubic time, and k increases less fast than power 1.5 time.
 
Stephan van Hulst
Saloon Keeper
Posts: 12147
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that your third example is in exponential time, not polynomial time.
 
It wasn't my idea to go to some crazy nightclub in the middle of nowhere. I just wanted to stay home and cuddle with this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic