Guest User

Untitled

a guest
Jun 28th, 2016
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. 143269498 function calls in 185.193 seconds
  2.  
  3. Ordered by: standard name
  4.  
  5. ncalls tottime percall cumtime percall filename:lineno(function)
  6. 1 47.656 47.656 183.583 183.583 ex2_big.py:18(cluster_alg)
  7. 1 1.090 1.090 185.193 185.193 ex2_big.py:2(<module>)
  8. 192670 0.165 0.000 0.165 0.000 ex2_big.py:39(union)
  9. 11247652 13.388 0.000 13.388 0.000 ex2_big.py:49(find)
  10. 110327340 120.790 0.000 122.365 0.000 ex2_big.py:5(all_string_with_diff)
  11. 14511524 1.175 0.000 1.175 0.000 ex2_big.py:6(<genexpr>)
  12. 5000000 0.355 0.000 0.355 0.000 ex2_big.py:61(<genexpr>)
  13. 596364 0.044 0.000 0.044 0.000 {len}
  14. 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
  15. 2 0.010 0.005 0.010 0.005 {method 'keys' of 'dict' objects}
  16. 200001 0.144 0.000 0.144 0.000 {method 'split' of 'str' objects}
  17. 200001 0.020 0.000 0.020 0.000 {method 'strip' of 'str' objects}
  18. 1 0.000 0.000 0.000 0.000 {open}
  19. 993940 0.355 0.000 0.355 0.000 {range}
  20.  
  21. def all_string_with_diff(tup,k):
  22. perms = [(j for j in range(i-1,len(tup)-k+i))for i in range(1,k+1)]
  23. for x in product(*perms):
  24. l = list(tup)
  25. for y in x:
  26. if l[y] =='1':
  27. l[y] = '0'
  28. else:
  29. l[y] = '1'
  30. yield tuple(l)
  31.  
  32. import sys
  33. from itertools import product
  34.  
  35. def all_string_with_diff(tup,k):
  36. li = list
  37. tu = tuple
  38. perms = [(j for j in range(i-1,len(tup)-k+i))for i in range(1,k+1)]
  39. for x in product(*perms):
  40. l = li(tup)
  41. for y in x:
  42. if l[y] =='1':
  43. l[y] = '0'
  44. else:
  45. l[y] = '1'
  46. yield tu(l)
  47.  
  48.  
  49.  
  50. def cluster_alg(nodes,n):
  51. d = 1
  52. i = 0
  53. while d<3:
  54. for n1 in nodes.keys():
  55. i = i+1
  56. if i%1000 ==0:
  57. print(d,i)
  58. for n2 in all_string_with_diff(n1,d):
  59. if n2 in nodes:
  60. r1 = find(nodes,n1)
  61. r2 = find(nodes,n2)
  62. if r1 != r2:
  63. union(nodes, r1, r2)
  64. n = n-1
  65. d = d+1
  66. return n
  67.  
  68.  
  69.  
  70.  
  71. def union(clusters, r1, r2):
  72. r1 = clusters[r1]
  73. r2 = clusters[r2]
  74. if r1[1]>= r2[1]:
  75. r2[0] = r1[0]
  76. r1[1] = r1[1] + r2[1]
  77. else:
  78. r1[0] = r2[0]
  79. r2[1] = r2[1] + r1[1]
  80.  
  81. def find(clusters, u):
  82. while clusters[u][0]!= u:
  83. u = clusters[u][0]
  84. return u
  85.  
  86. nodes = {}
  87. n = 0
  88. for index, line in enumerate(open(sys.argv[1], 'r')):
  89. if index==0:
  90. node_data = line.strip().split(" ")
  91. n = int(node_data[0])
  92. else:
  93. arr = tuple(i for i in line.strip().split(" "))
  94. if arr in nodes:
  95. n = n-1
  96. else:
  97. nodes[arr] = [arr,1]
  98.  
  99.  
  100. print(cluster_alg(nodes,n))
Add Comment
Please, Sign In to add comment