Reducing the complexity/computation time for a basic graph formula [closed]

2024/7/5 12:16:14

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 small range of numbers - then it gets too complex.

Is there a way to improve this formula/reduce the complexity?

Answer

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
https://en.xdnf.cn/q/119933.html

Related Q&A

Find All Possible Fixed Size String Python

Problem: I want to generate all possible combination from 36 characters that consist of alphabet and numbers in a fixed length string. Assume that the term "fixed length" is the upper bound f…

What is the concept of namespace when importing a function from another module?

main.py:from module1 import some_function x=10 some_function()module1.py:def some_function():print str(x)When I execute the main.py, it gives an error in the moduel1.py indicating that x is not availab…

How to pass a literal value to a kedro node? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 4 years ago.This po…

How to Loop a List and Extract required data (Beautiful Soup)

I need help in looping a list and extracting the src links. This is my list and the code: getimages = getDetails.find_all(img) #deleting the first image in the list getimages[0].decompose() print(getim…

square root without pre-defined function in python

How can one find the square root of a number without using any pre-defined functions in python?I need the main logic of how a square root of a program works. In general math we will do it using HCF bu…

How do I sort a text file by three columns with a specific order to those columns in Python?

How do I sort a text file by three columns with a specific order to those columns in Python?My text_file is in the following format with whitespaces between columns:Team_Name Team_Mascot Team_Color Te…

regular expression to search only one-digit number

Im trying to find sentences having only one digit number along with.sentence="Im 30 years old." print(re.match("[0-9]", sentence)then it returns<re.Match object; span=(0, 1), mat…

Automate adding new column and field names to all csv files in directories [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 3…

Connect the python app to a database using centos 7

I am new to all this I have apython app already helo.mysql.py and need to Connect the python app to a database. I am using centos 7 and have it installed on a ec2 instance if anyone can help please he…

How do I restart my program in Python? (see code)

if option == C:radius = float(raw_input("Enter Radius: ")) area = pi * radius**2print "Working..."sleep(1)print ("Area: %.2f. \n%s" % (area, hint))elif option == T:base = …