Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Algorithms
  4. Big Oh Computation

Big Oh Computation

Scheduled Pinned Locked Moved Algorithms
question
20 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Ian Uy

    Good Day, Given a pseudo code below: main() { for(int i=0;i<n;i++)> { x[i]=func1(func2(x[i])); } } func1(x[]) { for(int i=0;i<length(x);i++)> //Do something here } func2(x[]) { for(int i=0;i<length(x);i++)> //Do something here } How can I compute for the Asymptotic notation? I know that the loop in main() is O(n) and both func1 and func2 are O(n). But How can I combine the 3? Can I even combine the 3 to get O(N^3)? or it will be just O(3N)? Thanks! :)

    It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

    J Offline
    J Offline
    Jason Lepack LeppyR64
    wrote on last edited by
    #2

    What would be your analysis of this?

    main()
    {
    for(int i=0;i<n;i++)>
    {
    for(int j=0;j<n;j++)>
    {
    for(int k=0;k<n;k++)>
    {
    // do the something from func2
    }
    // do the something from func 1
    }
    }
    }

    Edit: Ah, better.

    I 1 Reply Last reply
    0
    • J Jason Lepack LeppyR64

      What would be your analysis of this?

      main()
      {
      for(int i=0;i<n;i++)>
      {
      for(int j=0;j<n;j++)>
      {
      for(int k=0;k<n;k++)>
      {
      // do the something from func2
      }
      // do the something from func 1
      }
      }
      }

      Edit: Ah, better.

      I Offline
      I Offline
      Ian Uy
      wrote on last edited by
      #3

      O(n). Right? :^)

      It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

      J 1 Reply Last reply
      0
      • I Ian Uy

        O(n). Right? :^)

        It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

        J Offline
        J Offline
        Jason Lepack LeppyR64
        wrote on last edited by
        #4

        How many times do we count to n? Hint: it's not 3 ;)

        main()
        {
        int count = 0;
        for(int i=0;i<n;i++)>
        {
        for(int j=0;j<n;j++)>
        {
        for(int k=0;k<n;k++)>
        {
        count++;
        }
        }
        }
        }

        If this were a complexity of O(n) then count would be 3n. However it's not, what's the value of count at the end? I'm not downing you, I'm just trying to help you come to the conclusion yourself.

        I 1 Reply Last reply
        0
        • J Jason Lepack LeppyR64

          How many times do we count to n? Hint: it's not 3 ;)

          main()
          {
          int count = 0;
          for(int i=0;i<n;i++)>
          {
          for(int j=0;j<n;j++)>
          {
          for(int k=0;k<n;k++)>
          {
          count++;
          }
          }
          }
          }

          If this were a complexity of O(n) then count would be 3n. However it's not, what's the value of count at the end? I'm not downing you, I'm just trying to help you come to the conclusion yourself.

          I Offline
          I Offline
          Ian Uy
          wrote on last edited by
          #5

          hmm. So its O(N^3). Can you please give a code example that will yield 3N? So I can properly differentiate the two.

          It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

          J 1 Reply Last reply
          0
          • I Ian Uy

            hmm. So its O(N^3). Can you please give a code example that will yield 3N? So I can properly differentiate the two.

            It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

            J Offline
            J Offline
            Jason Lepack LeppyR64
            wrote on last edited by
            #6

            main()
            {
            int count = 0;
            for(int i=0;i<n;i++)>
            {
            count++;
            }

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

            }

            I 1 Reply Last reply
            0
            • J Jason Lepack LeppyR64

              main()
              {
              int count = 0;
              for(int i=0;i<n;i++)>
              {
              count++;
              }

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

              }

              I Offline
              I Offline
              Ian Uy
              wrote on last edited by
              #7

              Ah I see. Thank you!

              It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

              J 1 Reply Last reply
              0
              • I Ian Uy

                Ah I see. Thank you!

                It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

                J Offline
                J Offline
                Jason Lepack LeppyR64
                wrote on last edited by
                #8

                So as you can see now. When you nest loops you multiply the complexity. When you have loops side by side, you add.

                I S 2 Replies Last reply
                0
                • J Jason Lepack LeppyR64

                  So as you can see now. When you nest loops you multiply the complexity. When you have loops side by side, you add.

                  I Offline
                  I Offline
                  Ian Uy
                  wrote on last edited by
                  #9

                  When you say we add, that means N+N+N or simply 3N. Correct me if I'm wrong but there is no such thing as O(3N) but only O(N)?

                  It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

                  J 1 Reply Last reply
                  0
                  • I Ian Uy

                    When you say we add, that means N+N+N or simply 3N. Correct me if I'm wrong but there is no such thing as O(3N) but only O(N)?

                    It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

                    J Offline
                    J Offline
                    Jason Lepack LeppyR64
                    wrote on last edited by
                    #10

                    That's exactly correct, on both counts.

                    1 Reply Last reply
                    0
                    • J Jason Lepack LeppyR64

                      So as you can see now. When you nest loops you multiply the complexity. When you have loops side by side, you add.

                      S Offline
                      S Offline
                      supercat9
                      wrote on last edited by
                      #11

                      leppyr64 wrote:

                      When you nest loops you multiply the complexity.

                      If the number of iterations on an inner loop is proportional to the size of the problem, and will be the same for all iterations of the outer loop, then the complexity of the outer loop is a factor of N greater than that of the inner loop. The same will be true if the number of iterations for the inner loop will be uniformly distributed over a range, the top of which is proportional to the size of the problem. There will be occasions, however, where nested loops aren't nearly as bad as they appear because, while the worst-case time of the inner loops is proportional to the size of the problem, they will almost always execute much faster. In Shell Sort, for example, there are three nested loops. In the last iteration of the outer loop, the middle loop will execute N-1 times and the inner loop may (worst-case) execute O(N) times, but the worst-case time is far less than O(N^2).

                      I 1 Reply Last reply
                      0
                      • S supercat9

                        leppyr64 wrote:

                        When you nest loops you multiply the complexity.

                        If the number of iterations on an inner loop is proportional to the size of the problem, and will be the same for all iterations of the outer loop, then the complexity of the outer loop is a factor of N greater than that of the inner loop. The same will be true if the number of iterations for the inner loop will be uniformly distributed over a range, the top of which is proportional to the size of the problem. There will be occasions, however, where nested loops aren't nearly as bad as they appear because, while the worst-case time of the inner loops is proportional to the size of the problem, they will almost always execute much faster. In Shell Sort, for example, there are three nested loops. In the last iteration of the outer loop, the middle loop will execute N-1 times and the inner loop may (worst-case) execute O(N) times, but the worst-case time is far less than O(N^2).

                        I Offline
                        I Offline
                        Ian Uy
                        wrote on last edited by
                        #12

                        Hmmm, how about this nested loop?

                        main()
                        {
                        List<int[]> n = new List<int[]>();

                        for(int i=0;i<n.count;i++)>
                        {
                        	n\[i\]=new int\[100\];
                        	for(int j=0;j<n\[i\].length;j++)>
                        	{
                        		for(int k=0;k<n\[i\].length;k++)>
                        		{
                        			
                        		}
                        	}
                        }
                        

                        }

                        Will this still be O(N^3)? The two inner loops is not proportional to the size of the problem.

                        It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

                        M 1 Reply Last reply
                        0
                        • I Ian Uy

                          Hmmm, how about this nested loop?

                          main()
                          {
                          List<int[]> n = new List<int[]>();

                          for(int i=0;i<n.count;i++)>
                          {
                          	n\[i\]=new int\[100\];
                          	for(int j=0;j<n\[i\].length;j++)>
                          	{
                          		for(int k=0;k<n\[i\].length;k++)>
                          		{
                          			
                          		}
                          	}
                          }
                          

                          }

                          Will this still be O(N^3)? The two inner loops is not proportional to the size of the problem.

                          It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

                          M Offline
                          M Offline
                          Mark Churchill
                          wrote on last edited by
                          #13

                          This is when you can start talking about O(N*M^2), where M is the "usual" size of an array stuffed in the list. In reality you'd probably have some idea about how many elements are put in the arrays. Say you were storing four integers in each array, and had hundreds of these arrays in the list, you would probably refer to it as O(N), as the M is fairly insignificant. If the arrays contained n.Length items, then it would be ^3... Big-O notation is a very rough analysis, you are generally interested in the highest power. Depends on data too, some sort algorithms are worst case O(N^2), but the likely case is much gentler.

                          Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
                          Alpha release: Entanglar: Transparant multiplayer framework for .Net games.

                          I 1 Reply Last reply
                          0
                          • M Mark Churchill

                            This is when you can start talking about O(N*M^2), where M is the "usual" size of an array stuffed in the list. In reality you'd probably have some idea about how many elements are put in the arrays. Say you were storing four integers in each array, and had hundreds of these arrays in the list, you would probably refer to it as O(N), as the M is fairly insignificant. If the arrays contained n.Length items, then it would be ^3... Big-O notation is a very rough analysis, you are generally interested in the highest power. Depends on data too, some sort algorithms are worst case O(N^2), but the likely case is much gentler.

                            Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
                            Alpha release: Entanglar: Transparant multiplayer framework for .Net games.

                            I Offline
                            I Offline
                            Ian Uy
                            wrote on last edited by
                            #14

                            I see. So in the example above, if N>100, M (Integer in each array) does not really matter? But if N < M ,then it matters. So I can say that the best case is just O(N) and the worst is O(N^3)?

                            It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

                            1 Reply Last reply
                            0
                            • I Ian Uy

                              Good Day, Given a pseudo code below: main() { for(int i=0;i<n;i++)> { x[i]=func1(func2(x[i])); } } func1(x[]) { for(int i=0;i<length(x);i++)> //Do something here } func2(x[]) { for(int i=0;i<length(x);i++)> //Do something here } How can I compute for the Asymptotic notation? I know that the loop in main() is O(n) and both func1 and func2 are O(n). But How can I combine the 3? Can I even combine the 3 to get O(N^3)? or it will be just O(3N)? Thanks! :)

                              It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

                              I Offline
                              I Offline
                              Ian Uy
                              wrote on last edited by
                              #15

                              Hey Guys, don't want to start another thread so here it goes...

                              for(int i=0;i<n;i++)>
                              int x=1+1; //Constant Operation

                              is this O(N) or O(1)? I'm just doing constant operation inside a loop.

                              It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

                              M M 2 Replies Last reply
                              0
                              • I Ian Uy

                                Hey Guys, don't want to start another thread so here it goes...

                                for(int i=0;i<n;i++)>
                                int x=1+1; //Constant Operation

                                is this O(N) or O(1)? I'm just doing constant operation inside a loop.

                                It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

                                M Offline
                                M Offline
                                Member 4194593
                                wrote on last edited by
                                #16

                                It is O(1). It has nothing to do with what is inside the loop (as long as it is not another loop dependent upon "n"), just how many times you execute the loop. In your stated case, it is linear, for each increase of n you only execute the loop one more time.

                                1 Reply Last reply
                                0
                                • I Ian Uy

                                  Hey Guys, don't want to start another thread so here it goes...

                                  for(int i=0;i<n;i++)>
                                  int x=1+1; //Constant Operation

                                  is this O(N) or O(1)? I'm just doing constant operation inside a loop.

                                  It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

                                  M Offline
                                  M Offline
                                  Mark Churchill
                                  wrote on last edited by
                                  #17

                                  You are doing the constant operation N times...

                                  Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
                                  Alpha release: Entanglar: Transparant multiplayer framework for .Net games.

                                  I 1 Reply Last reply
                                  0
                                  • M Mark Churchill

                                    You are doing the constant operation N times...

                                    Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
                                    Alpha release: Entanglar: Transparant multiplayer framework for .Net games.

                                    I Offline
                                    I Offline
                                    Ian Uy
                                    wrote on last edited by
                                    #18

                                    Yeah. But it doesn't matter right? Its still O(n). It doesn't matter if the operations inside the loop is a expensive or inexpensive operation?

                                    It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

                                    M 1 Reply Last reply
                                    0
                                    • I Ian Uy

                                      Yeah. But it doesn't matter right? Its still O(n). It doesn't matter if the operations inside the loop is a expensive or inexpensive operation?

                                      It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

                                      M Offline
                                      M Offline
                                      Mark Churchill
                                      wrote on last edited by
                                      #19

                                      Yep, it doesnt matter if the execution time is trivial. The main thing is that if you double N then you double the execution time. N*10 -> 10*t, etc. In that exact case the compiler would probably optimise it to x = 2 ;)

                                      Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
                                      Alpha release: Entanglar: Transparant multiplayer framework for .Net games.

                                      1 Reply Last reply
                                      0
                                      • I Ian Uy

                                        Good Day, Given a pseudo code below: main() { for(int i=0;i<n;i++)> { x[i]=func1(func2(x[i])); } } func1(x[]) { for(int i=0;i<length(x);i++)> //Do something here } func2(x[]) { for(int i=0;i<length(x);i++)> //Do something here } How can I compute for the Asymptotic notation? I know that the loop in main() is O(n) and both func1 and func2 are O(n). But How can I combine the 3? Can I even combine the 3 to get O(N^3)? or it will be just O(3N)? Thanks! :)

                                        It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

                                        S Offline
                                        S Offline
                                        sableng
                                        wrote on last edited by
                                        #20

                                        i think it's just O(n). see http://pages.cs.wisc.edu/~vernon/cs367/notes/3.COMPLEXITY.html[^]

                                        1 Reply Last reply
                                        0
                                        Reply
                                        • Reply as topic
                                        Log in to reply
                                        • Oldest to Newest
                                        • Newest to Oldest
                                        • Most Votes


                                        • Login

                                        • Don't have an account? Register

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • World
                                        • Users
                                        • Groups