Advertisement
Guest User

comparing subclique functions

a guest
Nov 25th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | None | 0 0
  1. import timeit
  2.  
  3. setup = """
  4. from itertools import combinations
  5. import networkx as nx
  6. from networkx.generators import fast_gnp_random_graph
  7.  
  8. G = fast_gnp_random_graph(200, 0.75, seed=1234)
  9.  
  10. def is_subclique(G, nodelist):
  11.    for (u,v) in combinations(nodelist,2):  #check each possible pair
  12.        if not G.has_edge(u,v):
  13.            return False #if any edge is missing we're done
  14.    return True  #if we get to here, then every edge was there.  It's True.
  15.  
  16. def is_complete(G):
  17.    n = G.order()
  18.    return n*(n-1)/2 == G.size()
  19. """
  20.  
  21.  
  22. print(
  23.     "original function took: ",
  24.     min(timeit.Timer("is_subclique(G, [1,2,3,4,5])", setup=setup).repeat(5,10))
  25.     )
  26.  
  27. print(
  28.     "new function took: ",
  29.     min(timeit.Timer("is_complete(G.subgraph([1,2,3,4,5]))", setup=setup).repeat(5,10))
  30.     )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement