Advertisement
Guest User

Untitled

a guest
Nov 24th, 2015
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.97 KB | None | 0 0
  1.     def _breake_lcc(self, G, remove_ratio):
  2.         '''
  3.        G:graph
  4.        remove_ratio = ratio for removing high degree nodes
  5.        '''
  6.         #lcc of complete graph
  7.         l = gt.label_largest_component(G)
  8.         lcc_graph = gt.GraphView(G,vfilt=l)
  9.         lcc_org_size = float(lcc_graph.num_vertices())
  10.         lcc_size = [lcc_org_size/lcc_org_size]
  11.         #print 'lcc_size_zero: ', lcc_graph.num_vertices()
  12.         #extract the vertices inside lcc
  13.         node_ind = np.where(l.a==1)[0]
  14.         #compute the degree of all nodes
  15.         degree = lcc_graph.degree_property_map('total')
  16.         #extract the degreee of nodes in the lcc
  17.         lcc_vertex_degree =  degree.a[node_ind]
  18.         #sort the nodes based on degree
  19.         sorted_idx = np.argsort(lcc_vertex_degree)[::-1]
  20.         sorted_vertex = set(node_ind[sorted_idx])
  21.         #number of nodes
  22.         n = len(node_ind)
  23.         #number of top x% degree nodes to be removed -ratio to size
  24.         remove_size = int((n*remove_ratio)/100.)
  25.         print 'remove size',remove_size
  26.         step = 1
  27.         #extract top x% vertices to remove
  28.         nodes_to_be_removed = list(sorted_vertex)[:remove_size]
  29.         #removed the above vertices from the list of valid vertices
  30.         valid_vertices = list(sorted_vertex - set(nodes_to_be_removed))
  31.         #continue until there is no other node to remove
  32.         while len(valid_vertices)>0:
  33.             print step * remove_ratio
  34. #            print len(valid_vertices)
  35.             #filter out those nodes from lcc.
  36.             #THIS IS AUTOMATICALLY UPDATE LCC_GRAPH
  37.             l.a[np.array(nodes_to_be_removed)] = False
  38.             #I REALIZED GRAPHVIEW DOES NOT TAKE CARE OF ISOLATED NODE AUTOMATICALLY
  39.             #SO I CHECK FOR THEM MANUALLY
  40.             ind_remove = [int(i) for i in lcc_graph.vertices() if i.out_degree()==0]
  41.             if len(ind_remove)>0:
  42.                 print len(ind_remove)
  43.                 mask = lcc_graph.new_vertex_property("bool")
  44.                 #mask.a[::] = True
  45.                 mask.a = l.a.copy()
  46.                 #l.a[np.array(ind_remove)] = False
  47.                 mask.a[np.array(ind_remove)] = False
  48.                 #mask.a[nodes_to_be_removed] = False
  49.                 #print np.all(mask.a==l.a)
  50.                 lcc_graph = gt.GraphView(lcc_graph, vfilt=mask)
  51.                 #update the valid nodes
  52.                 valid_vertices = list(set(valid_vertices) - set(ind_remove))
  53.  
  54.             print len(valid_vertices), len(sorted_vertex),lcc_graph.num_vertices()
  55.             lcc_temp_size = float(lcc_graph.num_vertices())
  56.             #print 'valid', len(valid_vertices),lcc_temp_size
  57.             nodes_to_be_removed = valid_vertices[:int(remove_size)]
  58.            
  59.             print lcc_temp_size,lcc_org_size
  60.             rel_size = lcc_temp_size/lcc_org_size
  61.             lcc_size.append(rel_size)
  62.             print 'lcc size', rel_size
  63.             print '------------'
  64.             step+=1
  65.         return lcc_size
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement