def gc_vertices(self): ''' get all the vertices in the giant component ''' idx_gc = 0 num_gc = 0 for i in range(1, self.__n + 1): root = self.find(i) if self.__rank[root] > num_gc: idx_gc = root num_gc = self.__rank[root] from glb import log, logging log = logging.getLogger(__name__) log.info('%d, giant_component/total: %d/%d.' % (self.__num_components, num_gc, self.__n)) set_vtx = set() for i in range(1, self.__n + 1): if self.find(i) == idx_gc: set_vtx.add(i) return set_vtx
def gen_gc(ipt, opt): idx_orign = reindex(ipt, 1) uf = UnionFind(idx_orign.num_indices()) lines = open(ipt, 'r').readlines() lines = map(lambda x: x.split(), lines) lines = filter(lambda x: len(x) >= 2, lines) for line in lines: a = idx_orign.get_idx_by_key(int(line[0])) b = idx_orign.get_idx_by_key(int(line[1])) if a != -1 and b != -1: uf.union(a, b) set_gc = uf.gc_vertices() writer = open(opt, 'w') for line in lines: a = idx_orign.get_idx_by_key(int(line[0])) b = idx_orign.get_idx_by_key(int(line[1])) if a in set_gc and b in set_gc: write = line writer.write(' '.join(map(lambda x: str(x), write)) + '\n') from glb import log, logging log = logging.getLogger(__name__) log.info('giant component has been written to file.')
#! /usr/bin/env python # -*- coding: utf-8 -*- import os, copy as cp, scipy.io as sio, numpy as np from graph import Graph from glb import basepath, logging import giantcomponent as gc, smooth as sm, core, coreExt as ce log = logging.getLogger(__name__) def run(lmd, which, times): num_iter = 1 iptdir = os.path.join(basepath, 'data/bench/' + which) optdir = os.path.join(basepath, bd + '/' + which) if not os.path.isdir(optdir): os.makedirs(optdir) list_nmi = [] list_err = [] first = True list_errmat_expected = [] list_errmat_actual = [] for i in range(1, 11): fmt = '%s.t%02d' % (which, i) log.info(fmt + '...') ipt_edges = os.path.join(iptdir, fmt + '.edges') path_gc = os.path.join(optdir, fmt + '.gc') ipt_comm1 = os.path.join(iptdir, fmt + '.comm1') path_hubs = os.path.join(optdir, fmt + '.hubs.csv') #gc.gen_gc(ipt_edges, path_gc) g = Graph() #sm.create_graph(path_gc, g) sm.create_graph(ipt_edges, g)