def prepareforATC(self, graph, Q, Wq): flag = defaultdict(int) color = 1 for v in graph.keys(): if not flag[v]: GraphFunc.dfsWithLable(v, graph, flag, color) color += 1 countforQlabel = { color: sum(flag[q] == color for q in Q) for color in range(color) } qlabellist = sorted(countforQlabel, key=lambda x: -countforQlabel[x]) countforGlabel = { color: sum(flag[i] == color for i in graph) for color in range(color) } maxcount = 1 for color in qlabellist: if countforQlabel[color] != countforQlabel[qlabellist[0]]: break maxcount = max(maxcount, countforGlabel[color]) maxcolor = -1 for color in qlabellist: if countforQlabel[color] != countforQlabel[qlabellist[0]]: break if maxcount == countforGlabel[color]: maxcolor = color break ans = [v for v in Q if flag[v] == maxcolor] return ans
def MaintainKDTruss(self, k, d, graph, Q, Wq): Gt = copy.deepcopy(graph) while GraphFunc.connected(Gt, Q): TE, supE = ATindex.StructuralTrussness(Gt) distG = GraphFunc.getDistG(Gt, Q) edgestodelete = {} for v in distG: if distG[v] > d: for u in Gt[v][0]: edgestodelete[self.cedge(u, v)] = 1 for e in supE: if supE[e] < k - 2: edgestodelete[e] = 1 if len(edgestodelete) == 0: return Gt Gt = GraphFunc.deleteEdges(Gt, edgestodelete) if len(Gt) == 0: break return Gt
def AttributedTrussness(graph,Wq,attrTE,attrSupE): attrTE = {} attrSupE = {} if '' not in attrTE.keys() or '' not in attrSupE.keys(): attrTE[''],attrSupE[''] = StructuralTrussness(graph) for w in Wq: if w not in attrTE.keys() or w not in attrSupE.keys(): flag = [v for v in graph.keys() if w in graph[v][1]] subgraph = GraphFunc.buildNewGraph(graph, flag) attrTE[w], attrSupE[w] = StructuralTrussness(subgraph) return attrTE, attrSupE
def computeGainFunc(k, graph, Wq): gain = {} fHWq = attributeScore(graph, Wq) for v in graph.keys(): flagv = {} for u in graph[v][0]: if len(graph[u][0]) == k - 1: flagv[u] = 0 else: flagv[u] = 1 flagv[v] = 0 flagset = [] for u in graph.keys(): if flagv.get(u) is None: flagset.append(u) elif flagv[u] == 1: flagset.append(u) newG = GraphFunc.buildNewGraph(graph, flagset) if len(newG) == 0: gain[v] = INF else: gain[v] = fHWq - attributeScore( GraphFunc.buildNewGraph(graph, flagset), Wq) return gain
def ExtendtoGt(self, steinerT, origraph, Wq, TE): que = queue.Queue() flag = defaultdict(int) newG = {} for v in steinerT.keys(): que.put(v) flag[v] = 1 newG[v] = [ copy.deepcopy(steinerT[v]), copy.deepcopy(origraph[v][1]) ] Wqset = set(Wq) while not que.empty(): u = que.get() for v in origraph[u][0]: if len(newG.keys()) >= self.szlimits: return newG else: # AttributeScore条件约束 newgwithu = GraphFunc.addNodewithG(newG, origraph, v) # a1 = AttributeScoreFunc.attributeScore(newgwithu, Wqset) # a2 = AttributeScoreFunc.attributeScore(newG, Wqset) boolflag = AttributeScoreFunc.attributeScore(newgwithu, Wqset) >= AttributeScoreFunc. \ attributeScore(newG, Wqset) if not boolflag: boolflag = ( AttributeScoreFunc.thetaFuncforWqSet( newG, Wqset & set(origraph[v][1])) >= AttributeScoreFunc.attributeScore(newG, Wqset) / 2 * len(newG)) if not boolflag: boolflag = (AttributeScoreFunc.attributeScore( newgwithu, Wqset | set(origraph[v][1])) >= AttributeScoreFunc.attributeScore( newgwithu, Wqset)) if not boolflag: continue ''' if AttributeScoreFunc.thetaFuncforG(newG, list(Wq_extend & set(oriorigraph[v][1]))) < AttributeScoreFunc.\ attributeScore(newG, Wq_extend) / (2 * len(newG)): continue ''' if newG.get(v) is None: newG[v] = [[], copy.deepcopy(origraph[v][1])] if newG[v][0].count(u) == 0: newG[v][0].append(u) newG[u][0].append(v) if flag[v] == 0: #Wq_extend = Wq_extend & set(graph[v][1]) flag[v] = 1 que.put(v) return newG
def BULK(self, graph, Q, Wq, k, d): l = 0 Gl1 = copy.deepcopy(graph) distG = GraphFunc.getDistG(graph, Q) d = max(max(distG.values()), d) S = [v for v in Gl1.keys() if distG[v] <= d] Gl2 = GraphFunc.buildNewGraph(Gl1, S) ktemp = self.INF TE, supE = ATindex.StructuralTrussness(Gl2) for q in Q: ktmp = max(TE[self.cedge(q, v)] for v in Gl2[q][0]) k = min(ktemp, ktmp) k = min(ktemp, k) while True: if k == 2: break if GraphFunc.connected(self.MaintainKDTruss(k, d, Gl2, Q, Wq), Q): break k -= 1 Gl = self.MaintainKDTruss(k, d, Gl2, Q, Wq) maxfunc, ansg = -self.INF, None while GraphFunc.connected(Gl, Q): attriscore = AttributeScoreFunc.attributeScore(Gl, Wq) if maxfunc < attriscore: maxfunc, ansg = attriscore, copy.deepcopy(Gl) gain = AttributeScoreFunc.computeGainFunc(k, Gl, Wq) mingain = self.INF for v in gain: if gain[v] < mingain and not v in Q: mingain = gain[v] if mingain == self.INF: break S = [v for v in gain.keys() if gain[v] == mingain and not v in Q] if len(S) == 0: break S = random.sample( S, max(1, int(len(S) * self.erlta / (self.erlta + 1)))) edgesToDelete = GraphFunc.getNeighborEdges(Gl, S) Gl = GraphFunc.deleteEdges(Gl, edgesToDelete) Gl = self.MaintainKDTruss(k, d, Gl, Q, Wq) return list(ansg.keys())