Advertisement
Guest User

Untitled

a guest
Apr 25th, 2015
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. import collections
  2. import sys
  3.  
  4. # setup the graph
  5. G = {
  6. 1:set([ 2, 3, 5, 6,]),
  7. 2:set([ 1, 4,]),
  8. 3:set([ 1, 6, 7,]),
  9. 4:set([ 2, 5, 7, 8,]),
  10. 5:set([ 1, 4, 6, 8, 9, 10,]),
  11. 6:set([ 1, 3, 5, 7,]),
  12. 7:set([ 3, 4, 6, 9,]),
  13. 8:set([ 4, 5, 9,]),
  14. 9:set([ 5, 7, 8, 20,]),
  15. 10:set([ 5, 11, 12, 14, 15,]),
  16. 11:set([ 10, 12, 13, 14,]),
  17. 12:set([ 10, 11, 13, 14, 15,]),
  18. 13:set([ 11, 12, 15,]),
  19. 14:set([ 10, 11, 12, 25,]),
  20. 15:set([ 10, 12, 13,]),
  21. 16:set([ 17, 19, 20, 21, 22,]),
  22. 17:set([ 16, 18, 19, 20,]),
  23. 18:set([ 17, 20, 21, 22,]),
  24. 19:set([ 16, 17,]),
  25. 20:set([ 9, 16, 17, 18,]),
  26. 21:set([ 16, 18,]),
  27. 22:set([ 16, 18, 23,]),
  28. 23:set([ 22, 24, 25, 26, 27,]),
  29. 24:set([ 23, 25, 26, 27,]),
  30. 25:set([ 14, 23, 24, 26, 27,]),
  31. 26:set([ 23, 24, 25,]),
  32. 27:set([ 23, 24, 25,]),
  33. }
  34. Gvol = 102
  35.  
  36. # G is graph as dictionary-of-sets
  37. alpha=0.99
  38. tol=0.01
  39. seed=[1]
  40.  
  41. x = {} # Store x, r as dictionaries
  42. r = {} # initialize residual
  43. Q = collections.deque() # initialize queue
  44. for s in seed:
  45. r[s] = 1/len(seed)
  46. Q.append(s)
  47. while len(Q) > 0:
  48. v = Q.popleft() # v has r[v] > tol*deg(v)
  49. if v not in x: x[v] = 0.
  50. x[v] += (1-alpha)*r[v]
  51. mass = alpha*r[v]/(2*len(G[v]))
  52. for u in G[v]: # for neighbors of u
  53. assert u is not v, "contact dgleich@purdue.edu for self-links"
  54. if u not in r: r[u] = 0.
  55. if r[u] < len(G[u])*tol and \
  56. r[u] + mass >= len(G[u])*tol:
  57. Q.append(u) # add u to queue if large
  58. r[u] = r[u] + mass
  59. r[v] = mass*len(G[v])
  60. if r[v] >= len(G[v])*tol: Q.append(v)
  61. print str(x)
  62.  
  63.  
  64. # Find cluster, first normalize by degree
  65. for v in x: x[v] = x[v]/len(G[v])
  66. # now sort x's keys by value, decreasing
  67. sv = sorted(x.iteritems(), key=lambda x: x[1], reverse=True)
  68. S = set()
  69. volS = 0.
  70. cutS = 0.
  71. bestcond = 1.
  72. bestset = sv[0]
  73. for p in sv:
  74. s = p[0] # get the vertex
  75. volS += len(G[s]) # add degree to volume
  76. for v in G[s]:
  77. if v in S:
  78. cutS -= 1
  79. else:
  80. cutS += 1
  81. print "v: %4i cut: %4f vol: %4f"%(s, cutS,volS)
  82. S.add(s)
  83. if cutS/min(volS,Gvol-volS) < bestcond:
  84. bestcond = cutS/min(volS,Gvol-volS)
  85. bestset = set(S) # make a copy
  86. print "Best set conductance: %f"%(bestcond)
  87. print " set = ", str(bestset)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement