def generalGreedy(G, k, p): ''' Finds initial seed set S using general greedy heuristic Input: G -- networkx Graph object k -- number of initial nodes needed p -- propagation probability Output: S -- initial set of k nodes to propagate ''' import time start = time.time() R = 1 # number of times to run Random Cascade S = [] # set of selected nodes # add node to S if achieves maximum propagation for current chosen + this node for i in range(k): s = PQ() # priority queue for v in G.nodes(): if v not in S: s.add_task(v, 0) # initialize spread value for j in range(R): # run R times Random Cascade [priority, count, task] = s.entry_finder[v] s.add_task(v, priority - runICmodel_n(G, S + [v], p)[0] / R) # add normalized spread value task, priority = s.pop_item() S.append(task) # print(i, k, time.time() - start) return S
def degreeDiscountIC(G, k, Ep=0): ''' Finds initial set of nodes to propagate in Independent Cascade model (with priority queue) Input: G -- networkx graph object k -- number of nodes needed p -- propagation probability Output: S -- chosen k nodes ''' S = [] dd = PQ() # degree discount t = dict() # number of adjacent vertices that are in S d = dict() # degree of each vertex # initialize degree discount for u in G.nodes(): d[u] = sum([G[u][v]['weight'] for v in G[u]]) # each edge adds degree 1 # d[u] = len(G[u]) # each neighbor adds degree 1 dd.add_task(u, -d[u]) # add degree of each node t[u] = 0 # add vertices to S greedily for i in range(k): u, priority = dd.pop_item() # extract node with maximal degree discount S.append(u) for v in G[u]: if v not in S: t[v] += G[u][v]['weight'] # increase number of selected neighbors # print(t[v]) p = Ep[u][v]['weight'] priority = d[v] - 2*t[v] - (d[v] - t[v])*t[v]*p # discount of degree dd.add_task(v, -priority) return S
def degreeDiscountIAC3(G, k, Ep): ''' Finds initial set of nodes to propagate in Independent Cascade model (with priority queue) Input: G -- networkx graph object k -- number of nodes needed Ep -- propagation probability graph Output: S -- chosen k nodes ''' S = [] dd = PQ() # degree discount t = dict() # number of adjacent vertices that are in S d = dict() # degree of each vertex # initialize degree discount for u in G.nodes(): d[u] = sum([G[u][v]['weight'] * Ep[u][v]['weight'] for v in G[u]]) # each edge adds degree 1 # d[u] = len(G[u]) # each neighbor adds degree 1 dd.add_task(u, -d[u]) # add degree of each node t[u] = 0 # add vertices to S greedily for i in range(k): u, priority = dd.pop_item( ) # extract node with maximal degree discount S.append(u) for v in G[u]: if v not in S: multi = 1 add = 0 for n in G.predecessors(v): if n in S: multi *= (1 - Ep[n][v]) for n in G[v]: if n in S: add -= Ep[v][n] else: add += Ep[v][n] add += 1 priority = add * multi dd.add_task(v, -priority) return S
def degreeDiscountStar(G,k,Ep): S = [] scores = PQ() d = dict() t = dict() for u in G: d[u] = sum([G[u][v]['weight']*Ep[u][v]['weight'] for v in G[u]]) t[u] = 0 score = -((1-Ep[u][v]['weight'])**t[u])*(1+(d[u]-t[u])*Ep[u][v]['weight']) scores.add_task(u, score) for iteration in range(k): u, priority = scores.pop_item() print(iteration, -priority) S.append(u) for v in G[u]: if v not in S: t[v] += G[u][v]['weight'] score = -((1-p)**t[u])*(1+(d[u]-t[u])*Ep[u][v]['weight']) scores.add_task(v, score) return S
def degreeDiscountIAC3(G, k, Ep): # config中默认配置这个为oracle ''' Finds initial set of nodes to propagate in Independent Cascade model (with priority queue) Input: G -- networkx graph object k -- number of nodes needed Ep -- propagation probability graph, 传播概率图 Output: S -- chosen k nodes ''' S = [] dd = PQ() # degree discount t = dict() # number of adjacent vertices that are in S d = dict() # degree of each vertex # initialize degree discount for u in G.nodes(): d[u] = sum([G[u][v]['weight']*Ep[u][v]['weight'] for v in G[u]]) + 1 # each edge adds degree 1 # d[u] = len(G[u]) # each neighbor adds degree 1 dd.add_task(u, -d[u]) # add degree of each node. degree最大,则-d[u]最小,则排在heap的前面(默认小根堆min-heap) t[u] = 0 # add vertices to S greedily for i in range(k): u, priority = dd.pop_item() # extract node with maximal degree discount S.append(u) for v in G[u]: if v not in S: multi = 1 add = 0 for n in G.predecessors(v): # 前缀结点(有边指向v的结点) if n in S: # 这些结点在S中,但是并没有激活v,的概率 multi *= (1 - Ep[n][v]['weight']) for n in G[v]: if n in S: continue else: add += Ep[v][n]['weight'] add += 1 priority = add * multi dd.add_task(v, -priority) return S
def Greedy(G, k, p): """ Input: G -- networkx Graph object k -- number of initial nodes needed p -- propagation probability Output: S -- initial set of k nodes to propagate """ R = 1 # number of times to run Random Cascade S = [] # set of selected nodes # add node to S if achieves maximum propagation for current chosen + this node for i in range(k): s = PQ() # priority queue for v in G.nodes(): if v not in S: s.add_task(v, 0) # initialize spread value for j in range(R): # run R times Random Cascade [priority, count, task] = s.entry_finder[v] s.add_task(v, priority - runIC(G, S + [v], p)[0] / R) # add normalized spread value task, priority = s.pop_item() S.append(task) return S