What does O(log n) mean exactly?

后端 未结 30 2402
执念已碎
执念已碎 2020-11-22 01:19

I am learning about Big O Notation running times and amortized times. I understand the notion of O(n) linear time, meaning that the size of the input affects the g

相关标签:
30条回答
  • 2020-11-22 02:02

    Simply put: At each step of your algorithm you can cut the work in half. (Asymptotically equivalent to third, fourth, ...)

    0 讨论(0)
  • 2020-11-22 02:03

    Overview

    Others have given good diagram examples, such as the tree diagrams. I did not see any simple code examples. So in addition to my explanation, I'll provide some algorithms with simple print statements to illustrate the complexity of different algorithm categories.

    First, you'll want to have a general idea of Logarithm, which you can get from https://en.wikipedia.org/wiki/Logarithm . Natural science use e and the natural log. Engineering disciples will use log_10 (log base 10) and computer scientists will use log_2 (log base 2) a lot, since computers are binary based. Sometimes you'll see abbreviations of natural log as ln(), engineers normally leave the _10 off and just use log() and log_2 is abbreviated as lg(). All of the types of logarithms grow in a similar fashion, that is why they share the same category of log(n).

    When you look at the code examples below, I recommend looking at O(1), then O(n), then O(n^2). After you are good with those, then look at the others. I've included clean examples as well as variations to demonstrate how subtle changes can still result in the same categorization.

    You can think of O(1), O(n), O(logn), etc as classes or categories of growth. Some categories will take more time to do than others. These categories help give us a way of ordering the algorithm performance. Some grown faster as the input n grows. The following table demonstrates said growth numerically. In the table below think of log(n) as the ceiling of log_2.

    Simple Code Examples Of Various Big O Categories:

    O(1) - Constant Time Examples:

    • Algorithm 1:

    Algorithm 1 prints hello once and it doesn't depend on n, so it will always run in constant time, so it is O(1).

    print "hello";
    
    • Algorithm 2:

    Algorithm 2 prints hello 3 times, however it does not depend on an input size. Even as n grows, this algorithm will always only print hello 3 times. That being said 3, is a constant, so this algorithm is also O(1).

    print "hello";
    print "hello";
    print "hello";
    

    O(log(n)) - Logarithmic Examples:

    • Algorithm 3 - This acts like "log_2"

    Algorithm 3 demonstrates an algorithm that runs in log_2(n). Notice the post operation of the for loop multiples the current value of i by 2, so i goes from 1 to 2 to 4 to 8 to 16 to 32 ...

    for(int i = 1; i <= n; i = i * 2)
      print "hello";
    
    • Algorithm 4 - This acts like "log_3"

    Algorithm 4 demonstrates log_3. Notice i goes from 1 to 3 to 9 to 27...

    for(int i = 1; i <= n; i = i * 3)
      print "hello";
    
    • Algorithm 5 - This acts like "log_1.02"

    Algorithm 5 is important, as it helps show that as long as the number is greater than 1 and the result is repeatedly multiplied against itself, that you are looking at a logarithmic algorithm.

    for(double i = 1; i < n; i = i * 1.02)
      print "hello";
    

    O(n) - Linear Time Examples:

    • Algorithm 6

    This algorithm is simple, which prints hello n times.

    for(int i = 0; i < n; i++)
      print "hello";
    
    • Algorithm 7

    This algorithm shows a variation, where it will print hello n/2 times. n/2 = 1/2 * n. We ignore the 1/2 constant and see that this algorithm is O(n).

    for(int i = 0; i < n; i = i + 2)
      print "hello";
    

    O(n*log(n)) - nlog(n) Examples:

    • Algorithm 8

    Think of this as a combination of O(log(n)) and O(n). The nesting of the for loops help us obtain the O(n*log(n))

    for(int i = 0; i < n; i++)
      for(int j = 1; j < n; j = j * 2)
        print "hello";
    
    • Algorithm 9

    Algorithm 9 is like algorithm 8, but each of the loops has allowed variations, which still result in the final result being O(n*log(n))

    for(int i = 0; i < n; i = i + 2)
      for(int j = 1; j < n; j = j * 3)
        print "hello";
    

    O(n^2) - n squared Examples:

    • Algorithm 10

    O(n^2) is obtained easily by nesting standard for loops.

    for(int i = 0; i < n; i++)
      for(int j = 0; j < n; j++)
        print "hello";
    
    • Algorithm 11

    Like algorithm 10, but with some variations.

    for(int i = 0; i < n; i++)
      for(int j = 0; j < n; j = j + 2)
        print "hello";
    

    O(n^3) - n cubed Examples:

    • Algorithm 12

    This is like algorithm 10, but with 3 loops instead of 2.

    for(int i = 0; i < n; i++)
      for(int j = 0; j < n; j++)
        for(int k = 0; k < n; k++)
          print "hello";
    
    • Algorithm 13

    Like algorithm 12, but with some variations that still yield O(n^3).

    for(int i = 0; i < n; i++)
      for(int j = 0; j < n + 5; j = j + 2)
        for(int k = 0; k < n; k = k + 3)
          print "hello";
    

    Summary

    The above give several straight forward examples, and variations to help demonstrate what subtle changes can be introduced that really don't change the analysis. Hopefully it gives you enough insight.

    0 讨论(0)
  • 2020-11-22 02:04

    The logarithm

    Ok let's try and fully understand what a logarithm actually is.

    Imagine we have a rope and we have tied it to a horse. If the rope is directly tied to the horse, the force the horse would need to pull away (say, from a man) is directly 1.

    Now imagine the rope is looped round a pole. The horse to get away will now have to pull many times harder. The amount of times will depend on the roughness of the rope and the size of the pole, but let's assume it will multiply one's strength by 10 (when the rope makes a complete turn).

    Now if the rope is looped once, the horse will need to pull 10 times harder. If the human decides to make it really difficult for the horse, he may loop the rope again round a pole, increasing it's strength by an additional 10 times. A third loop will again increase the strength by a further 10 times.

    We can see that for each loop, the value increases by 10. The number of turns required to get any number is called the logarithm of the number i.e. we need 3 posts to multiple your strength by 1000 times, 6 posts to multiply your strength by 1,000,000.

    3 is the logarithm of 1,000, and 6 is the logarithm of 1,000,000 (base 10).

    So what does O(log n) actually mean?

    In our example above, our 'growth rate' is O(log n). For every additional loop, the force our rope can handle is 10 times more:

    Turns | Max Force
      0   |   1
      1   |   10
      2   |   100
      3   |   1000
      4   |   10000
      n   |   10^n
    

    Now the example above did use base 10, but fortunately the base of the log is insignificant when we talk about big o notation.

    Now let's imagine you are trying to guess a number between 1-100.

    Your Friend: Guess my number between 1-100! 
    Your Guess: 50
    Your Friend: Lower!
    Your Guess: 25
    Your Friend: Lower!
    Your Guess: 13
    Your Friend: Higher!
    Your Guess: 19
    Your Friend: Higher!
    Your Friend: 22
    Your Guess: Lower!
    Your Guess: 20
    Your Friend: Higher!
    Your Guess: 21
    Your Friend: YOU GOT IT!  
    

    Now it took you 7 guesses to get this right. But what is the relationship here? What is the most amount of items that you can guess from each additional guess?

    Guesses | Items
      1     |   2
      2     |   4
      3     |   8
      4     |   16
      5     |   32
      6     |   64
      7     |   128
      10    |   1024
    

    Using the graph, we can see that if we use a binary search to guess a number between 1-100 it will take us at most 7 attempts. If we had 128 numbers, we could also guess the number in 7 attemps but 129 numbers will takes us at most 8 attempts (in relations to logarithms, here we would need 7 guesses for a 128 value range, 10 guesses for a 1024 value range. 7 is the logarithm of 128, 10 is the logarithm of 1024 (base 2)).

    Notice that I have bolded 'at most'. Big-O notation always refers to the worse case. If you're lucky, you could guess the number in one attempt and so the best case is O(1), but that's another story.

    We can see that for every guess our data set is shrinking. A good rule of thumb to identify if an algorithm has a logarithmtic time is to see if the data set shrinks by a certain order after each iteration

    What about O(n log n)?

    You will eventually come across a linearithmic time O(n log(n)) algorithm. The rule of thumb above applies again, but this time the logarithmic function has to run n times e.g. reducing the size of a list n times, which occurs in algorithms like a mergesort.

    You can easily identify if the algorithmic time is n log n. Look for an outer loop which iterates through a list (O(n)). Then look to see if there is an inner loop. If the inner loop is cutting/reducing the data set on each iteration, that loop is (O(log n)), and so the overall algorithm is = O(n log n).

    Disclaimer: The rope-logarithm example was grabbed from the excellent Mathematician's Delight book by W.Sawyer.

    0 讨论(0)
  • 2020-11-22 02:04

    Divide and conquer algorithms usually have a logn component to the running time. This comes from the repeated halving of the input.

    In the case of binary search, every iteration you throw away half of the input. It should be noted that in Big-O notation, log is log base 2.

    Edit: As noted, the log base doesn't matter, but when deriving the Big-O performance of an algorithm, the log factor will come from halving, hence why I think of it as base 2.

    0 讨论(0)
  • 2020-11-22 02:04

    Algorithms in the Divide and Conquer paradigm are of complexity O(logn). One example here, calculate your own power function,

    int power(int x, unsigned int y)
    {
        int temp;
        if( y == 0)
            return 1;
        temp = power(x, y/2);
        if (y%2 == 0)
            return temp*temp;
        else
            return x*temp*temp;
    }
    

    from http://www.geeksforgeeks.org/write-a-c-program-to-calculate-powxn/

    0 讨论(0)
  • 2020-11-22 02:05

    I cannot understand how to identify a function with a log time.

    The most common attributes of logarithmic running-time function are that:

    • the choice of the next element on which to perform some action is one of several possibilities, and
    • only one will need to be chosen.

    or

    • the elements on which the action is performed are digits of n

    This is why, for example, looking up people in a phone book is O(log n). You don't need to check every person in the phone book to find the right one; instead, you can simply divide-and-conquer by looking based on where their name is alphabetically, and in every section you only need to explore a subset of each section before you eventually find someone's phone number.

    Of course, a bigger phone book will still take you a longer time, but it won't grow as quickly as the proportional increase in the additional size.


    We can expand the phone book example to compare other kinds of operations and their running time. We will assume our phone book has businesses (the "Yellow Pages") which have unique names and people (the "White Pages") which may not have unique names. A phone number is assigned to at most one person or business. We will also assume that it takes constant time to flip to a specific page.

    Here are the running times of some operations we might perform on the phone book, from fastest to slowest:

    • O(1) (in the worst case): Given the page that a business's name is on and the business name, find the phone number.

    • O(1) (in the average case): Given the page that a person's name is on and their name, find the phone number.

    • O(log n): Given a person's name, find the phone number by picking a random point about halfway through the part of the book you haven't searched yet, then checking to see whether the person's name is at that point. Then repeat the process about halfway through the part of the book where the person's name lies. (This is a binary search for a person's name.)

    • O(n): Find all people whose phone numbers contain the digit "5".

    • O(n): Given a phone number, find the person or business with that number.

    • O(n log n): There was a mix-up at the printer's office, and our phone book had all its pages inserted in a random order. Fix the ordering so that it's correct by looking at the first name on each page and then putting that page in the appropriate spot in a new, empty phone book.

    For the below examples, we're now at the printer's office. Phone books are waiting to be mailed to each resident or business, and there's a sticker on each phone book identifying where it should be mailed to. Every person or business gets one phone book.

    • O(n log n): We want to personalize the phone book, so we're going to find each person or business's name in their designated copy, then circle their name in the book and write a short thank-you note for their patronage.

    • O(n2): A mistake occurred at the office, and every entry in each of the phone books has an extra "0" at the end of the phone number. Take some white-out and remove each zero.

    • O(n · n!): We're ready to load the phonebooks onto the shipping dock. Unfortunately, the robot that was supposed to load the books has gone haywire: it's putting the books onto the truck in a random order! Even worse, it loads all the books onto the truck, then checks to see if they're in the right order, and if not, it unloads them and starts over. (This is the dreaded bogo sort.)

    • O(nn): You fix the robot so that it's loading things correctly. The next day, one of your co-workers plays a prank on you and wires the loading dock robot to the automated printing systems. Every time the robot goes to load an original book, the factory printer makes a duplicate run of all the phonebooks! Fortunately, the robot's bug-detection systems are sophisticated enough that the robot doesn't try printing even more copies when it encounters a duplicate book for loading, but it still has to load every original and duplicate book that's been printed.

    0 讨论(0)
提交回复
热议问题