Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Big-O notation
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 51 - 75 of 90 - Expand all  -  Translate all to Translated (View all originals) < Older  Newer >
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Duncan Booth  
View profile  
 More options Apr 16 2003, 9:39 am
Newsgroups: comp.lang.python
From: Duncan Booth <dun...@NOSPAMrcp.co.uk>
Date: Wed, 16 Apr 2003 13:39:41 +0000 (UTC)
Local: Wed, Apr 16 2003 9:39 am
Subject: Re: Big-O notation
Roy Smith <r...@panix.com> wrote in news:roy-
92A049.08504216042...@reader1.panix.com:

>  Imagine you've got a program which runs in O(N^2)
> time.  If you could find a way to reduce it to O(N), for an input set of
> just 10 items, you would have speeded it up by a factor of 10!  Compared
> to upgrading your Python interpreter, you did 50-100 times better tuning
> the algorithm.  Now try to consider the implications of going from
> O(N^2) to O(N) with 25,000 input items!

I believe you are making some unreasonable assumptions here. Remember that
if I have an algorithm that is O(N^2) that is really just a shorthand for
saying that it will have a running time a*N^2 + b*N * c where a, b, and c
are constants but for sufficiently large N only the N^2 term matters.

Now imagine that you have a program that runs in O(N^2) time and you have
10 items. If a is 1 and b is 1000, the N^2 term doesn't start to dominate
until you have at least 1000 items. If these are times in milliseconds,
then your 10 item program would take ~10 seconds to run, but improving the
algorithm from O(N^2) to O(N) (with the same value for b) would have no
noticeable effect.

Yes, if you have 25,000 items then for my chosen values of a and b the O(N)
algorithm is probably going to result in a significant speedup (unless it
increases b by a factor of more than 26), but you can't just simplify this
to say that O(N) is going to be better in all cases.

If you try to apply this to the real world you may find there are all
manner of compromises to be made. The most obvious one is in sorting where
a real implementation of an O(n log n) sort will adapt to using an O(n^2)
sort on subsets of the data because it turns out that the dumb sorting
techniques are faster until n gets quite large.

--
Duncan Booth                                             dun...@rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ken R.  
View profile  
 More options Apr 16 2003, 9:58 am
Newsgroups: comp.lang.python
From: kril...@hotmail.com (Ken R.)
Date: 16 Apr 2003 06:58:34 -0700
Local: Wed, Apr 16 2003 9:58 am
Subject: Re: Big-O notation
Peter van Kampen <n...@datatailors.xs4all.nl> wrote in message <news:slrnb9q90a.mhg.news@datatailors.com>...
> In article <mailman.1050457621.2105.python-l...@python.org>, Mike C.
> Fletcher wrote:
> > Andrew Koenig wrote:

> > Okay, so now we have "The Practical Coder's Guide to big-O Notation" :)
> > .  We rock ;) ,
> > Mike

> You do, you know...c.l.p really does.

> I think I almost 'get it'. Except who or what decides what Big-O a
> certain algorithm has. Is that by 'hunch' or by running benchmarks or
> are there still other ways to recognize 'Big-O's (except for the
> formal computations that I understand are not very common)?

It's been nearly forever since my compsci algorithms class so be
gentle if I'm glaringly wrong :). Big O notation is a way to convey
the magnitude of work that it takes for an algorithm to do it's job
(ignoring any constant time). So, for a sufficiently large N, the work
required for an algorithm is something like N or NlogN etc.. No one
decides what order an algorithm is, it is an inherant property of the
algorithm.

If you execute quicksort on a wide variety of sample sets of different
sizes then measure and plot the processor time vs sample size, it will
approximate the graph of NlogN. Therefore the algorithm has order
NLogN. The same will apply to bubble sort (order N^2), etc.

I haven't seen the referenced Big-O posting but there is a decent
explanation of  Big-O and sorting algorithms here
http://www.autoobjects.com/Home/Teaching/CmpE_126/CmpE_126_Lectures/S...

Ken


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Python and Schools" by Andres Rosado
Andres Rosado  
View profile  
 More options Apr 16 2003, 10:05 am
Newsgroups: comp.lang.python
From: Andres Rosado <aros...@softhome.net>
Date: Wed, 16 Apr 2003 09:22:25 -0400
Local: Wed, Apr 16 2003 9:22 am
Subject: Re: Python and Schools
On 05:06 AM 4/11/2003 -0400, python-list-requ...@python.org feed this fish
to the sharks:

>- if you learn programming with Python and later have to use Java or
>Visual Basic, it will be the most frustrating experience for the young
>fellows.

Pardon my ignorance, but how could it be a frustating experience?

--
Andres Rosado
Email: andr...@despammed.com
ICQ: 66750646
Homepage: http://andres980.tripod.com/
Get my PGP key at http://andres980.tripod.com/pgp-key.asc

"Cat's tough. He went out fighting."
         -- Depth Charge, on Cheetor's apparent demise,
            "Feral Scream" Part 1  


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steve Holden  
View profile  
 More options Apr 16 2003, 10:22 am
Newsgroups: comp.lang.python
From: "Steve Holden" <shol...@holdenweb.com>
Date: Wed, 16 Apr 2003 14:22:02 GMT
Local: Wed, Apr 16 2003 10:22 am
Subject: Re: Python and Schools
"Andres Rosado" <aros...@softhome.net> wrote in message

news:mailman.1050499405.10336.python-list@python.org...

> On 05:06 AM 4/11/2003 -0400, python-list-requ...@python.org feed this fish
> to the sharks:
> >- if you learn programming with Python and later have to use Java or
> >Visual Basic, it will be the most frustrating experience for the young
> >fellows.

> Pardon my ignorance, but how could it be a frustating experience?

The frustration will arise from the non-availability of the various simple
features that Python provides to make programming easier.

For a real example, consider the contortions I have to go through when
writing VBScript ASP code for a client's project if I want a list (array) of
strings. Where in Python I can write:

    sarr = ['the', 'quick', 'brown', 'fox', 'jumps' 'over', 'the', 'lazy',
'dog]

in VBScript I must resort to one of [nontested code follows]:

    dim sarr(9)
    sarr(1) = 'the'
    sarr(2) = 'quick'
        ...
    sarr(8) = 'dog'

or possibly

    sarr = split("the quick brown fox jumps over the lazy dog")

Now imagine the further complexity when each element of sarr needs to have
structure itself, remembering that VBScript has no data structure constructs
except the array (well, I understand there's now a third-party module that
lets you create collections or some similar object, but there's still no way
to create an object with attributes).

Hope this makes it clearer.

regards
--
Steve Holden                                  http://www.holdenweb.com/
How lucky am I?      http://www.google.com/search?q=Steve+Holden&btnI=1
Python Web Programming                 http://pydish.holdenweb.com/pwp/


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Big-O notation" by Peter van Kampen
Peter van Kampen  
View profile  
 More options Apr 16 2003, 10:43 am
Newsgroups: comp.lang.python
From: Peter van Kampen <n...@datatailors.xs4all.nl>
Date: Wed, 16 Apr 2003 16:40:42 +0200
Local: Wed, Apr 16 2003 10:40 am
Subject: Re: Big-O notation

In article <roy-92A049.08504216042...@reader1.panix.com>, Roy Smith wrote:

[snip excellent stuff]

Well, I'm amazed at, and grateful for, all the time and effort you
guys put in. I also received an excellent reply by email. So a big
thank you to all.

PterK

P.S. I agree that this would be a valuable resource on a webpage as
Andrew suggested. It get's pointed out quite often that concatenating
strings in loop is an expensive operation in python and I have always
tried to keep that in mind but it is much nicer if you understand why...

--
Peter van Kampen
pterk -- at -- datatailors.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Python and Schools" by Andrew Bennetts
Andrew Bennetts  
View profile  
 More options Apr 16 2003, 11:05 am
Newsgroups: comp.lang.python
From: Andrew Bennetts <andrew-pythonl...@puzzling.org>
Date: Thu, 17 Apr 2003 00:33:46 +1000
Local: Wed, Apr 16 2003 10:33 am
Subject: Re: Python and Schools
On Wed, Apr 16, 2003 at 02:22:02PM +0000, Steve Holden wrote:

[...]

Or, if my memory isn't failing me:

    sarr = Array("the", "quick", ... "dog")

-Andrew.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Big-O notation" by Courageous
Courageous  
View profile  
 More options Apr 16 2003, 11:32 am
Newsgroups: comp.lang.python
From: Courageous <jkra...@san.rr.com>
Date: Wed, 16 Apr 2003 15:31:16 GMT
Local: Wed, Apr 16 2003 11:31 am
Subject: Re: Big-O notation

>I think I almost 'get it'. Except who or what decides what Big-O a
>certain algorithm has.

It's mathematics. There's neither opinion nor "benchmarks".

C//


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Courageous  
View profile  
 More options Apr 16 2003, 11:36 am
Newsgroups: comp.lang.python
From: Courageous <jkra...@san.rr.com>
Date: Wed, 16 Apr 2003 15:35:17 GMT
Local: Wed, Apr 16 2003 11:35 am
Subject: Re: Big-O notation

>I believe you are making some unreasonable assumptions here. Remember that
>if I have an algorithm that is O(N^2) that is really just a shorthand for
>saying that it will have a running time a*N^2 + b*N * c where a, b, and c
>are constants but for sufficiently large N only the N^2 term matters.
>Now imagine that you have a program that runs in O(N^2) time and you have
>10 items. If a is 1 and b is 1000, ...

Ah, the practical optimizer. We encounter so few of these in this day
and age. :-)

C//


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Big-O notation (was: Re: Python and Schools)" by Christopher A. Craig
Christopher A. Craig  
View profile  
 More options Apr 16 2003, 12:05 pm
Newsgroups: comp.lang.python
From: list-pyt...@ccraig.org (Christopher A. Craig)
Date: 16 Apr 2003 11:14:23 -0400
Local: Wed, Apr 16 2003 11:14 am
Subject: Re: Big-O notation (was: Re: Python and Schools)
Erik Max Francis <m...@alcyone.com> writes:

> It also means that one doesn't bother distinction between logarithms
> base 10, e, 2, etc.; O(ln n) ~ O(lg n) ~ O(lb n).  Similarly, O(2^n) ~
> O(10^n) ~ O(e^n) too.

The second part of this isn't true.  If a process f(n) has a running
time T(N) for in input of size N, said process is O(N) if there exist
constants "k" and "j" such that O(N)*k <= T(N) for all N>=j

While there does exist a constant (log(A)/log(B)) for which
log_A(N)*k <= log_B(N) for all sufficiently large N, there is
no constant for which (A**N)*k <= B**N for all sufficiently large N
when B>A.

--
Christopher A. Craig <list-pyt...@ccraig.org>
"Never let schooling get in the way of your education"  Mark Twain


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Python and Schools" by Steve Holden
Steve Holden  
View profile  
 More options Apr 16 2003, 3:13 pm
Newsgroups: comp.lang.python
From: "Steve Holden" <shol...@holdenweb.com>
Date: Wed, 16 Apr 2003 19:13:30 GMT
Local: Wed, Apr 16 2003 3:13 pm
Subject: Re: Python and Schools
"Andrew Bennetts" <andrew-pythonl...@puzzling.org> wrote ...

Indeed, though that's new to me (never used VB, just the scripting version).
Now let's create a dictionary-of-objects equivalent using the
DictionaryOfObjects() function ;-)

regards
--
Steve Holden                                  http://www.holdenweb.com/
How lucky am I?      http://www.google.com/search?q=Steve+Holden&btnI=1
Python Web Programming                 http://pydish.holdenweb.com/pwp/


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Big-O notation" by A. Lloyd Flanagan
A. Lloyd Flanagan  
View profile  
 More options Apr 16 2003, 3:58 pm
Newsgroups: comp.lang.python
From: alloydflana...@attbi.com (A. Lloyd Flanagan)
Date: 16 Apr 2003 12:58:30 -0700
Local: Wed, Apr 16 2003 3:58 pm
Subject: Re: Big-O notation
Peter van Kampen <n...@datatailors.xs4all.nl> wrote in message <news:slrnb9q90a.mhg.news@datatailors.com>...

> In article <mailman.1050457621.2105.python-l...@python.org>, Mike C.
> Fletcher wrote:
> I think I almost 'get it'. Except who or what decides what Big-O a
> certain algorithm has. Is that by 'hunch' or by running benchmarks or
> are there still other ways to recognize 'Big-O's (except for the
> formal computations that I understand are not very common)?

Technically the only way to determine the O() for a particular
algorithm is to do a complete mathematical analysis.  One of the other
posts has an excellent simple example.  You can do comparative times
for particular data sets, and fit a curve to the times, but that
doesn't guarantee that the algorithm will behave the same way for some
other set of data sets.

This mathematical analysis of an algorithm's behavior (and best-case,
average, and worst-case performance) can be trivial.  For example, x +
1 is O(1).  Or it can be so complex that proving the exact behavior is
a Ph.D. thesis, or even an unsolved problem.

> But these are not really 'algorithms'. Most algorithms are a lot more
> complex even when reduced to it's essentials. I understand why
> quicksort is better than bubblesort but I don't see an obvious way to
> label quicksort O(??).

Everything's an algorithm :).  Quicksort analysis isn't terribly
complicated as these things go.  There's got to be an analysis up on
the web somewhere.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bjorn Pettersen  
View profile  
 More options Apr 16 2003, 4:05 pm
Newsgroups: comp.lang.python
From: "Bjorn Pettersen" <BPetter...@NAREX.com>
Date: Wed, 16 Apr 2003 13:37:55 -0600
Local: Wed, Apr 16 2003 3:37 pm
Subject: RE: Big-O notation

> From: Ken R. [mailto:kril...@hotmail.com]

[...]
> It's been nearly forever since my compsci algorithms class so be
> gentle if I'm glaringly wrong :).

ditto.

[...]

> No one decides what order an algorithm is, it is an inherant property
> of the algorithm.

... which implies that it must have been _proven_ (it's a theoretical
system :-)

> If you execute quicksort on a wide variety of sample sets of different
> sizes then measure and plot the processor time vs sample size, it will
> approximate the graph of NlogN. Therefore the algorithm has order
> NLogN. The same will apply to bubble sort (order N^2), etc.

[...]

A commonly held belief, but as I said, it has to be proven, not
measured. Importantly, the statement entirely depends on the innocent
phrase "wide variety of sample [data]" (which would be valid if you were
trying to approximate the statistical mean of the algorithm). When
measuring, most people (should be?) are interested in _expected_
performance however, which means you need a "representable sample" (of
the domain), but even when well defined and acknowledged
programmers/scientists/... have historically proven to be highly
subjective when determining 'representable' <wink>. [Hint: you're
assuming you're only sorting random data].

-- bjorn


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
sik0fewl  
View profile  
 More options Apr 16 2003, 4:59 pm
Newsgroups: comp.lang.python
From: sik0fewl <xxdigitalhel...@hotmail.com>
Date: Wed, 16 Apr 2003 14:56:36 -0600
Local: Wed, Apr 16 2003 4:56 pm
Subject: Re: Big-O notation
Peter van Kampen <n...@datatailors.xs4all.nl> wrote in message
<news:slrnb9q90a.mhg.news@datatailors.com>...

>I think I almost 'get it'. Except who or what decides what Big-O a
>certain algorithm has. Is that by 'hunch' or by running benchmarks or
>are there still other ways to recognize 'Big-O's (except for the
>formal computations that I understand are not very common)?

I'll try to give a simple explanation. Big-O is the upper bound of the time
it will take to complete the function.

There's usually a variable factor in determining how long an operation will
take.

Inserting into a hash table:
O(1) - constant time. This means that the amount of time it takes to insert
into a hash table is not dependent on the size of the table. This is because
the position to insert the item is calculated (the hash) and then inserted.
This, of course, changes when there starts being collisions.

Inserting into the first free spot of an array:
O(n) - linear time. n is the size of the array. This means when you cycle
through the array you have to go through n items (at the most).

And it carries on. I forget exactly how to calculate them, but you can
understand the simple ones.

Here's some really simple code to show different Big-O's

n = 24

# O(1)
# constant time
print n

# O(n)
# linear time
for x in range(0,n):
     print x

# O(n^2)
# quadratic time
for x in range(0,n):
     for y in range(0,n):
         print x, y

# O(x^n)
# exponential time
for y in range(0, x**n):
     print y

Keep in mind that Big-O is the *upper bound*. These all illustrate the
maximum time. Any real code is most likely going to reach it's "destination"
before it hits the end (though not always).

--
Hope this didn't make you more confused than ever,
Ryan


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bjorn Pettersen  
View profile  
 More options Apr 16 2003, 6:05 pm
Newsgroups: comp.lang.python
From: "Bjorn Pettersen" <BPetter...@NAREX.com>
Date: Wed, 16 Apr 2003 15:35:53 -0600
Local: Wed, Apr 16 2003 5:35 pm
Subject: RE: Big-O notation
> From: sik0fewl [mailto:xxdigitalhel...@hotmail.com]

> Peter van Kampen <n...@datatailors.xs4all.nl> wrote in message
> <news:slrnb9q90a.mhg.news@datatailors.com>...
> >I think I almost 'get it'. Except who or what decides what Big-O a
> >certain algorithm has. Is that by 'hunch' or by running benchmarks or
> >are there still other ways to recognize 'Big-O's (except for the
> >formal computations that I understand are not very common)?

> I'll try to give a simple explanation. Big-O is the upper
> bound of the time it will take to complete the function.

[...]

google(algorithm complexity) == lecture notes
(http://www.geog.buffalo.edu/classes/geo554/week4.pdf)

[also "time complexity", "space complexity"].

 O() ::= The big-O notation gives an indication of the growth
         rate of a function. It identifies the dominant term
         of the function.

And, for the practically minded...

Rules of Thumb
- A loop that processes each data item in turn is O(n).
- A loop within a loop
    Do n
      Do m
  Is O(m*n)
- If each data set is halved each time through a loop
  then there will be O(log2n) iterations. (log2n is
  the number of times you repeatedly half n to reach 1).
- An algorithm that processes every subset of n data
  items has complexity O(2**n).
- An algorithm that processes every permutation of n
  data items has complexity O(n!).
- If the number of operations is the same whatever
  the number of data items then the complexity function
  does not depend on n. We say the operation has complexity
  O(1).

-- bjorn


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven Taschuk  
View profile  
 More options Apr 16 2003, 6:05 pm
Newsgroups: comp.lang.python
From: Steven Taschuk <stasc...@telusplanet.net>
Date: Wed, 16 Apr 2003 15:54:38 -0600
Local: Wed, Apr 16 2003 5:54 pm
Subject: Re: Big-O notation
Quoth A. Lloyd Flanagan:
  [...]

> This mathematical analysis of an algorithm's behavior (and best-case,
> average, and worst-case performance) can be trivial.  For example, x +
> 1 is O(1).  [...]

I would have thought it's O(log x), due to the possibility of
carries.

--
Steven Taschuk                                        stasc...@telusplanet.net
"Study this book; read a word then ponder on it.  If you interpret the meaning
 loosely you will mistake the Way."         -- Musashi, _A Book of Five Rings_


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven Taschuk  
View profile  
 More options Apr 16 2003, 6:05 pm
Newsgroups: comp.lang.python
From: Steven Taschuk <stasc...@telusplanet.net>
Date: Wed, 16 Apr 2003 15:53:16 -0600
Local: Wed, Apr 16 2003 5:53 pm
Subject: Re: Big-O notation
Quoth Duncan Booth:
  [...]

> I believe you are making some unreasonable assumptions here. Remember that
> if I have an algorithm that is O(N^2) that is really just a shorthand for
> saying that it will have a running time a*N^2 + b*N * c where a, b, and c
> are constants but for sufficiently large N only the N^2 term matters.

Not to dispute your general point, but a nit: O(n^2) doesn't imply
a polynomial expansion such as you have above; the actual runtime
could be, for example, 3n^2 + log n + 18/n.  That is, the
additional terms might be anything which (asymptotically) grows
slower than n^2.

--
Steven Taschuk              Aral: "Confusion to the enemy, boy."
stasc...@telusplanet.net    Mark: "Turn-about is fair play, sir."
                             -- _Mirror Dance_, Lois McMaster Bujold


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Python and Schools" by Steven Taschuk
Steven Taschuk  
View profile  
 More options Apr 16 2003, 6:05 pm
Newsgroups: comp.lang.python
From: Steven Taschuk <stasc...@telusplanet.net>
Date: Wed, 16 Apr 2003 15:54:21 -0600
Local: Wed, Apr 16 2003 5:54 pm
Subject: Re: Python and Schools
Quoth Greg Brondo:
  [...]

> I understand why the code is bad (copying the string each time in
> memory as it grows).  However, I've never understood the notation's
> O(N) and O(N2)?

Others have given good explanations of the usual use of big-O
notation, namely, to describe roughly how the running time of an
algorithm relates to the size of its input.  Imho this is actually
a convenient but slightly sloppy way of speaking; strictly
speaking one counts operations performed.  Roy Smith's excellent
post, for example, focusses on memory copy operations,
essentially; that's a good choice for that problem, while counting
string concatenations would not be, if we're interested in
predicting runtime.

And you can use big-O notation for counting things other than
operations.  For example, it is correct usage to describe a
trade-off as being between, say, an algorithm which runs in O(n)
time but needs O(n^2) memory to do it, and an alternative which
runs in O(n^2) time but only needs O(n) memory.

--
Steven Taschuk                  stasc...@telusplanet.net
"Telekinesis would be worth patenting."  -- James Gleick


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Big-O notation" by Andrew Dalke
Andrew Dalke  
View profile  
 More options Apr 16 2003, 6:06 pm
Newsgroups: comp.lang.python
From: "Andrew Dalke" <ada...@mindspring.com>
Date: Wed, 16 Apr 2003 16:10:16 -0600
Local: Wed, Apr 16 2003 6:10 pm
Subject: Re: Big-O notation
A. Lloyd Flanagan

> This mathematical analysis of an algorithm's behavior (and best-case,
> average, and worst-case performance) can be trivial.  For example, x +
> 1 is O(1).

Or not so trivial.  If x can be an arbitrary size integer, represented
in memory as a chunk of bits, the value 'n' for the number of bits,
and a sign flag, then addition could be O(n) == O(log(x)).  Eg,
suppose    x =  1111111111111111111111111111111111111111
add 1 to get = 10000000000000000000000000000000000000000
so 49 bits were affected.

But for fixed size integers, the above is true.

                    Andrew
                    da...@dalkescientific.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Erik Max Francis  
View profile  
 More options Apr 16 2003, 6:43 pm
Newsgroups: comp.lang.python
From: Erik Max Francis <m...@alcyone.com>
Date: Wed, 16 Apr 2003 15:43:13 -0700
Local: Wed, Apr 16 2003 6:43 pm
Subject: Re: Big-O notation

Peter van Kampen wrote:
> I think I almost 'get it'. Except who or what decides what Big-O a
> certain algorithm has. Is that by 'hunch' or by running benchmarks or
> are there still other ways to recognize 'Big-O's (except for the
> formal computations that I understand are not very common)?

It's just mathematics.  You look at the behavior of the algorithm or
data structure as the number of elements gets very large (in order to
drown out fixed-cost or second-order effects).

> for example

> bigO_N(i):
>     for n in xrange(i):
>         print n

> Would be O(N) right?

That's right.  It iterates over each element, and that takes O(n).

> bigO_N2(i,j):
>     for n in xrange(i):
>         for o in xrange(j):
>             print i,j

> This would be O(N**2)? Actually O(i*j) I suppose but if i==j N**2
> applies.

That's right also.  You're looking at the case where the number of
elements being processed -- even if there are multiple different sets --
grows large.  In this case you're iterating over two lists, but for each
list you're iterating over the other, so as i, j -> oo the behavior is
O(n^2).

> But these are not really 'algorithms'. Most algorithms are a lot more
> complex even when reduced to it's essentials. I understand why

quicksort is better than bubblesort but I don't see an obvious way to
label quicksort O(??).

But there is always some set of behaviors that are core to the
algorithm, and that's what you look at as the number of elements goes to
infinity.  Bubble sort is O(n^2), but quicksort is O(n ln n).

One also often speaks of worst case and average cases (this ties into
what people mean by "amortized O(f(n))" behavior).  For instance, take
finding an element in a sorted binary tree.  The average case is O(ln
n), but the worst case (if the tree is maximally uneven) is O(n).

Adding an element to the end of a dynamically resizing vector is average
case O(1), but worst case O(n), but since this worst case happens on
average n times, we call it "amortized O(1)" behavior.

The big-O notation is one of those things that's really highly
mathematical (particularly if you want to talk about things rigorously),
but it's also something that's vitally important for even novice
programmers to understand very well when they're using algorithms to
solve complex problems that need to be scalable.

--
 Erik Max Francis / m...@alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ The only source of knowledge is experience.
\__/ Albert Einstein
    Bosskey.net / http://www.bosskey.net/
 A personal guide to online multiplayer first person shooters.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Big-O notation (was: Re: Python and Schools)" by Erik Max Francis
Erik Max Francis  
View profile  
 More options Apr 16 2003, 6:53 pm
Newsgroups: comp.lang.python
From: Erik Max Francis <m...@alcyone.com>
Date: Wed, 16 Apr 2003 15:53:09 -0700
Local: Wed, Apr 16 2003 6:53 pm
Subject: Re: Big-O notation (was: Re: Python and Schools)

"Christopher A. Craig" wrote:
> Erik Max Francis <m...@alcyone.com> writes:

> > Similarly, O(2^n) ~ O(10^n) ~ O(e^n) too.

> The second part of this isn't true.  If a process f(n) has a running
> time T(N) for in input of size N, said process is O(N) if there exist
> constants "k" and "j" such that O(N)*k <= T(N) for all N>=j

> While there does exist a constant (log(A)/log(B)) for which
> log_A(N)*k <= log_B(N) for all sufficiently large N, there is
> no constant for which (A**N)*k <= B**N for all sufficiently large N
> when B>A.

[scribbles on paper]

Right you are, thanks for that important correction.  The ratio of two
logarithmic functions with different bases is a constant, but the ratio
of two exponential functions with different bases is a different
exponential function, e^(k n), where k is the difference in the natural
logarithms of the two original bases.  So indeed, O(a^n) !~ O(b^n) for a
!= b.

Good catch!

--
 Erik Max Francis / m...@alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ The only source of knowledge is experience.
\__/ Albert Einstein
    Bosskey.net / http://www.bosskey.net/
 A personal guide to online multiplayer first person shooters.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Python and Schools" by Tim Ottinger
Tim Ottinger  
View profile  
 More options Apr 16 2003, 10:16 pm
Newsgroups: comp.lang.python
From: Tim Ottinger <TOtti...@indy.rr.com>
Date: Thu, 17 Apr 2003 02:24:22 GMT
Local: Wed, Apr 16 2003 10:24 pm
Subject: Re: Python and Schools

Donnal Walter wrote:
> I am a physican first and programmer second. I've never taken a course
> in computer science, and I do not know the meaning of O(N). OTOH, just
> from following comp.lang.python I think I have a pretty good idea why

>     s += str(i)

> would be a bad idea. Nevertheless, I certainly would not be opposed to
> deepening my understanding. (PythonIAN is great, BTW.)

I came in late. If we're talking about schools, I'm not sure whether
we're talking about colleges and adult ed, or high school, or elementary
school. Are we talking about teaching people who know nothing about
programming?

If a 3rd grader wrote the s += str(i) or s = s+str(i) I would be pretty
impressed. I don't expect them to understand O() notation. I wouldn't
be to thrilled to see it in professional code. I don't expect that it
would make it through the PEPs. But it would be expressing the basic
idea, and then later it could be explained why it is so awfully slow
on the big loops.

Maybe not too bad for 5th or 6th grade, since they're far from writing
any professional code. You tolerate more with rank beginners, and you
correct it in time.

tim


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "get pid from popen3 or get redirection in execl" by Yu Wang
Yu Wang  
View profile  
 More options Apr 17 2003, 3:05 am
Newsgroups: comp.lang.python
From: Yu Wang <Yu.W...@synopsys.com>
Date: Thu, 17 Apr 2003 14:07:36 +0800
Local: Thurs, Apr 17 2003 2:07 am
Subject: get pid from popen3 or get redirection in execl
Hi,all
   Does "execl", or whatever this family, support redirection of output?
   Say, I have a script: ( on my Linux7.2,C shell environment,with
python2.2.1)

command="somecommand"
arg="-i somefile > output file"
execl(command,arg)

what's wrong with this?

And,
    how could I get child pid from "popen3(somecommand)".

Thanks to you.
Regards.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Python and Schools" by Ian Bicking
Ian Bicking  
View profile  
 More options Apr 17 2003, 3:05 am
Newsgroups: comp.lang.python
From: Ian Bicking <i...@colorstudy.com>
Date: 17 Apr 2003 01:39:05 -0500
Local: Thurs, Apr 17 2003 2:39 am
Subject: Re: Python and Schools

On Wed, 2003-04-16 at 21:24, Tim Ottinger wrote:
> Donnal Walter wrote:
> > I am a physican first and programmer second. I've never taken a course
> > in computer science, and I do not know the meaning of O(N). OTOH, just
> > from following comp.lang.python I think I have a pretty good idea why

> >     s += str(i)

> > would be a bad idea. Nevertheless, I certainly would not be opposed to
> > deepening my understanding. (PythonIAN is great, BTW.)

> I came in late. If we're talking about schools, I'm not sure whether
> we're talking about colleges and adult ed, or high school, or elementary
> school. Are we talking about teaching people who know nothing about
> programming?

It's also a question of what you're trying to teach.  Something that has
always impressed me with Logo is that it's more of a teaching
philosophy, and a language goes with it.  The intention behind Logo
isn't to teach programming... rather, programming is a medium in which
to teach all sorts of other great stuff.

I personally believe that programming is *the* way pre-algebra and
algebra should be taught.  Logo shows it works very well for geometry as
well.  But you really have to get rid of the idea that you're teaching
programming -- it does the children a disservice anyway, because
programming is a rather niche skill in the larger picture.  When you're
teaching *with* programming, big-O notation and many other programming
notions are just a distraction.

> Maybe not too bad for 5th or 6th grade, since they're far from writing
> any professional code. You tolerate more with rank beginners, and you
> correct it in time.

I'd go further -- if you're not trying to teach them to become
programmers it's not a matter of tolerating, it's completely okay to be
inefficient.  It'd be like criticizing a student's handwriting on the
caption they made for a picture in art class, or criticizing the
aesthetic of a graph made for algebra.

  Ian


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Erik Max Francis  
View profile  
 More options Apr 17 2003, 3:13 am
Newsgroups: comp.lang.python
From: Erik Max Francis <m...@alcyone.com>
Date: Thu, 17 Apr 2003 00:13:09 -0700
Local: Thurs, Apr 17 2003 3:13 am
Subject: Re: Python and Schools

Ian Bicking wrote:
> It's also a question of what you're trying to teach.  Something that
> has
> always impressed me with Logo is that it's more of a teaching
> philosophy, and a language goes with it.  The intention behind Logo
> isn't to teach programming... rather, programming is a medium in which
> to teach all sorts of other great stuff.

And, I might point out, Logo really gets a bad rap by the general
populace as a "toy" language that just involves little turtles running
around like silly things.  In actuality, Logo was originally designed
for abstract manipulation of symbols (_logo_ means "word" in Greek); the
turtle graphics stuff came later.  Logo is in fact a fun little cousin
of the Lisp family.

--
 Erik Max Francis / m...@alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ Can I walk with you / 'Till the day that my heart stops beating
\__/ India Arie
    Polly Wanna Cracka? / http://www.pollywannacracka.com/
 The Internet resource for interracial relationships.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "get pid from popen3 or get redirection in execl" by Yu Wang
Yu Wang  
View profile  
 More options Apr 17 2003, 5:05 am
Newsgroups: comp.lang.python
From: Yu Wang <Yu.W...@synopsys.com>
Date: Thu, 17 Apr 2003 16:04:51 +0800
Local: Thurs, Apr 17 2003 4:04 am
Subject: Re: get pid from popen3 or get redirection in execl
Sorry for this stupid question. I've found at least two ways to resolve it.
use sys.stderr, and dup2() function both work for it.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Messages 51 - 75 of 90 < Older  Newer >
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google