Skip to content Skip to sidebar Skip to footer

Reducing The Complexity/computation Time For A Basic Graph Formula

I tried to use the basic formula (got it from another SO question), for calculating the max number of different edge sets, for n vertices: 2**(n*(n-1)/2) but it's good only for sm

Solution 1:

Here's an easy way to speed this up quite considerably: 2 ** x is always equal to 1 << x, so long as x is a non-negative integer; but the latter is hundreds of times faster, because it's just shifting bits rather than doing arithmetic.

>>> def slow(n):
...     return 2 ** (n * (n-1) // 2)
... 
>>> def fast(n):
...     return 1 << (n * (n-1) // 2)
... 
>>> from timeit import timeit
>>> timeit(lambda: slow(1000), number=1000)
1.5656050549987413
>>> timeit(lambda: fast(1000), number=1000)
0.005352460000722203

Post a Comment for "Reducing The Complexity/computation Time For A Basic Graph Formula"