def generateSeedExpectedInfDictUsingDAG2(self, k_prod, s_set, mioa_dict): node_rank_dict = {i: 0.0 for i in self.seed_cost_dict[0]} for s_node in s_set[k_prod]: node_rank_dict[s_node] = 1.0 for i in mioa_dict[s_node]: if mioa_dict[s_node][i] > node_rank_dict[i]: node_rank_dict[i] = mioa_dict[s_node][i] s_total_set = set(s for k in range(self.num_product) for s in s_set[k]) seed_exp_inf_dict = {} for s_node in s_set[k_prod]: mioa_set = set() s_node_dict = {s_node: (1.0, s_node)} source_heap = [] for i in self.graph_dict[s_node]: s_node_dict[i] = (self.graph_dict[s_node][i], s_node) heap.heappush_max(source_heap, (self.graph_dict[s_node][i], i)) # -- it will not find a better path than the existing MIP -- # -- because if this path exists, it should be pop earlier from the heap. -- while source_heap: (i_prob, i_node) = heap.heappop_max(source_heap) i_prev = s_node_dict[i_node][1] if i_node in s_total_set: continue if node_rank_dict[i_prev] <= node_rank_dict[i_node]: continue mioa_set.add(i_node) if i_node in seed_exp_inf_dict: seed_exp_inf_dict[i_node].append(i_prob) else: seed_exp_inf_dict[i_node] = [i_prob] if i_node in self.graph_dict: for ii_node in self.graph_dict[i_node]: # -- not yet find MIP from source_node to ii_node -- if ii_node not in mioa_set: ii_prob = round( i_prob * self.graph_dict[i_node][ii_node], 4) if ii_prob >= self.prob_threshold: # -- if ii_node is in heap -- if ii_node in s_node_dict: ii_prob_d = s_node_dict[ii_node][0] if ii_prob > ii_prob_d: s_node_dict[ii_node] = (ii_prob, i_node) source_heap.remove( (ii_prob_d, ii_node)) source_heap.append((ii_prob, ii_node)) heap.heapify_max(source_heap) # -- if ii_node is not in heap -- else: s_node_dict[ii_node] = (ii_prob, i_node) heap.heappush_max(source_heap, (ii_prob, ii_node)) return seed_exp_inf_dict
def generateMIA(self, s_set): ### mioa_dict[i_node_u][i_node_v] = (prob, path): MIP from i_node_u to i_node_v ### miia_dict[i_node_v][i_node_u] = (prob, path): MIP from i_node_u to i_node_v mioa_dict, miia_dict = {}, {} sub_graph = copy.deepcopy(self.graph_dict) s_total_set = set(s for k in range(self.num_product) for s in s_set[k]) for s_node in s_total_set: induceGraph(sub_graph, s_node) for source_node in sub_graph: ### source_dict[i_node] = (prob, in-neighbor) mioa_dict[source_node] = {} source_dict = {source_node: (1.0, source_node)} source_heap = [] for i in sub_graph[source_node]: source_dict[i] = (sub_graph[source_node][i], source_node) heap.heappush_max(source_heap, (sub_graph[source_node][i], i)) # -- it will not find a better path than the existing MIP -- # -- because if this path exists, it should be pop earlier from the heap. -- while source_heap: (i_prob, i_node) = heap.heappop_max(source_heap) i_prev = source_dict[i_node][1] # -- find MIP from source_node to i_node -- i_path = [i_node, i_prev] while i_prev != source_dict[i_prev][1]: i_prev = source_dict[i_prev][1] i_path.append(i_prev) i_path.reverse() mioa_dict[source_node][i_node] = (i_prob, i_path) if i_node not in miia_dict: miia_dict[i_node] = {source_node: (i_prob, i_path)} else: miia_dict[i_node][source_node] = (i_prob, i_path) if i_node in sub_graph: for ii_node in sub_graph[i_node]: # -- not yet find MIP from source_node to ii_node -- if ii_node not in mioa_dict[source_node]: ii_prob = round(i_prob * sub_graph[i_node][ii_node], 4) if ii_prob >= self.prob_threshold: # -- if ii_node is in heap -- if ii_node in source_dict: ii_prob_d = source_dict[ii_node][0] if ii_prob > ii_prob_d: source_dict[ii_node] = (ii_prob, i_node) source_heap.remove((ii_prob_d, ii_node)) source_heap.append((ii_prob, ii_node)) heap.heapify_max(source_heap) # -- if ii_node is not in heap -- else: source_dict[ii_node] = (ii_prob, i_node) heap.heappush_max(source_heap, (ii_prob, ii_node)) return mioa_dict, miia_dict
def generateMIOA(self): mioa_dict = {} for source_node in self.graph_dict: ### source_dict[i_node] = (prob, in-neighbor) mioa_dict[source_node] = {} source_dict = { i: (self.graph_dict[source_node][i], source_node) for i in self.graph_dict[source_node] } source_dict[source_node] = (1.0, source_node) source_heap = [(self.graph_dict[source_node][i], i) for i in self.graph_dict[source_node]] heap.heapify_max(source_heap) # -- it will not find a better path than the existing MIP -- # -- because if this path exists, it should be pop earlier from the heap. -- while source_heap: (i_prob, i_node) = heap.heappop_max(source_heap) i_prev = source_dict[i_node][1] # -- find MIP from source_node to i_node -- i_path = [i_node, i_prev] while i_prev != source_node: i_prev = source_dict[i_prev][1] i_path.append(i_prev) i_path.pop() i_path.reverse() mioa_dict[source_node][i_node] = (i_prob, i_path) if i_node in self.graph_dict: for ii_node in self.graph_dict[i_node]: # -- not yet find MIP from source_node to ii_node -- if ii_node not in mioa_dict[source_node]: ii_prob = round( i_prob * self.graph_dict[i_node][ii_node], 4) if ii_prob >= self.prob_threshold: # -- if ii_node is in heap -- if ii_node in source_dict: ii_prob_d = source_dict[ii_node][0] if ii_prob > ii_prob_d: source_dict[ii_node] = (ii_prob, i_node) source_heap.remove( (ii_prob_d, ii_node)) source_heap.append((ii_prob, ii_node)) heap.heapify_max(source_heap) # -- if ii_node is not in heap -- else: source_dict[ii_node] = (ii_prob, i_node) heap.heappush_max(source_heap, (ii_prob, ii_node)) return mioa_dict
def generateSeedDAGDict(self, dag_dict, s_set_k): sdag_dict = {} s_set = set(s for s in s_set_k if s in dag_dict) for s_node in s_set: sdag_dict[s_node] = {} source_dict = { i: (dag_dict[s_node][i], s_node) for i in dag_dict[s_node] } source_dict[s_node] = (1.0, s_node) source_heap = [(dag_dict[s_node][i], i) for i in dag_dict[s_node]] heap.heapify_max(source_heap) # -- it will not find a better path than the existing MIP -- # -- because if this path exists, it should be pop earlier from the heap. -- while source_heap: (i_prob, i_node) = heap.heappop_max(source_heap) i_prev = source_dict[i_node][1] # -- find MIP from source_node to i_node -- i_path = [i_node, i_prev] while i_prev != s_node: i_prev = source_dict[i_prev][1] i_path.append(i_prev) i_path.pop() i_path.reverse() sdag_dict[s_node][i_node] = (i_prob, i_path) if i_node in dag_dict: for ii_node in dag_dict[i_node]: # -- not yet find MIP from source_node to ii_node -- if ii_node not in sdag_dict[s_node]: ii_prob = round(i_prob * dag_dict[i_node][ii_node], 4) if ii_prob >= self.prob_threshold: # -- if ii_node is in heap -- if ii_node in source_dict: ii_prob_d = source_dict[ii_node][0] if ii_prob > ii_prob_d: source_dict[ii_node] = (ii_prob, i_node) source_heap.remove( (ii_prob_d, ii_node)) source_heap.append((ii_prob, ii_node)) heap.heapify_max(source_heap) # -- if ii_node is not in heap -- else: source_dict[ii_node] = (ii_prob, i_node) heap.heappush_max(source_heap, (ii_prob, ii_node)) return sdag_dict
def generateDAG1(self, s_set_k): node_rank_dict = {} source_dict = {s_node: 1.0 for s_node in s_set_k} source_heap = [(1.0, s_node) for s_node in s_set_k] # -- it will not find a better path than the existing MIP -- # -- because if this path exists, it should be pop earlier from the heap. -- while source_heap: (i_prob, i_node) = heap.heappop_max(source_heap) node_rank_dict[i_node] = i_prob if i_node in self.graph_dict: for ii_node in self.graph_dict[i_node]: # -- not yet find MIP from source_node to ii_node -- if ii_node not in node_rank_dict: ii_prob = round( i_prob * self.graph_dict[i_node][ii_node], 4) if ii_prob >= self.prob_threshold: # -- if ii_node is in heap -- if ii_node in source_dict: ii_prob_d = source_dict[ii_node] if ii_prob > ii_prob_d: source_dict[ii_node] = ii_prob source_heap.remove((ii_prob_d, ii_node)) source_heap.append((ii_prob, ii_node)) heap.heapify_max(source_heap) # -- if ii_node is not in heap -- else: source_dict[ii_node] = ii_prob heap.heappush_max(source_heap, (ii_prob, ii_node)) dag_dict = {i: {} for i in self.graph_dict} i_set = set(i for i in self.graph_dict if i in node_rank_dict) for i in i_set: j_set = set(j for j in self.graph_dict[i] if j in node_rank_dict and node_rank_dict[i] > node_rank_dict[j]) for j in j_set: dag_dict[i][j] = self.graph_dict[i][j] if not dag_dict[i]: del dag_dict[i] return dag_dict
def generateMIA(self): mioa_dict, miia_dict = {}, {} for source_node in self.graph_dict: ### source_dict[i_node] = (prob, in-neighbor) mioa_dict[source_node] = {} source_dict = {source_node: 1.0} source_heap = [] for i in self.graph_dict[source_node]: source_dict[i] = self.graph_dict[source_node][i] heap.heappush_max(source_heap, (self.graph_dict[source_node][i], i)) # -- it will not find a better path than the existing MIP -- # -- because if this path exists, it should be pop earlier from the heap. -- while source_heap: (i_prob, i_node) = heap.heappop_max(source_heap) mioa_dict[source_node][i_node] = i_prob if i_node not in miia_dict: miia_dict[i_node] = {source_node} else: miia_dict[i_node].add(source_node) if i_node in self.graph_dict: for ii_node in self.graph_dict[i_node]: # -- not yet find MIP from source_node to ii_node -- if ii_node not in mioa_dict[source_node]: ii_prob = round(i_prob * self.graph_dict[i_node][ii_node], 4) if ii_prob >= self.prob_threshold: # -- if ii_node is in heap -- if ii_node in source_dict: ii_prob_d = source_dict[ii_node][0] if ii_prob > ii_prob_d: source_dict[ii_node] = ii_prob source_heap.remove((ii_prob_d, ii_node)) source_heap.append((ii_prob, ii_node)) heap.heapify_max(source_heap) # -- if ii_node is not in heap -- else: source_dict[ii_node] = ii_prob heap.heappush_max(source_heap, (ii_prob, ii_node)) return mioa_dict, miia_dict
def generateMIOG(self): miog_dict = {s: [] for s in self.graph_dict} for source_node in self.graph_dict: ### source_dict[i_node] = (prob, in-neighbor) source_dict = {i: self.graph_dict[source_node][i] for i in self.graph_dict[source_node]} source_dict[source_node] = 1.0 source_heap = [(self.graph_dict[source_node][i], i, [i]) for i in self.graph_dict[source_node]] heap.heapify_max(source_heap) # -- it will not find a better path than the existing MIP -- # -- because if this path exists, it should be pop earlier from the heap. -- while source_heap: (i_prob, i_node, i_path) = heap.heappop_max(source_heap) if i_prob >= source_dict[i_node]: miog_dict[source_node].append((i_prob, i_path)) # if i_prev not in miog_dict[source_node]: # miog_dict[source_node][i_prev] = [i_node] # else: # miog_dict[source_node][i_prev].append(i_node) if i_node in self.graph_dict: for ii_node in self.graph_dict[i_node]: # -- not yet find MIP from source_node to ii_node -- ii_prob = round(i_prob * self.graph_dict[i_node][ii_node], 4) if ii_prob >= self.prob_threshold: if ii_node in source_dict: if ii_prob >= source_dict[ii_node]: source_dict[ii_node] = ii_prob heap.heappush_max(source_heap, (ii_prob, ii_node, i_path + [ii_node])) else: source_dict[ii_node] = ii_prob heap.heappush_max(source_heap, (ii_prob, ii_node, i_path + [ii_node])) return miog_dict
def generateMIOA(self): ### mioa_dict[source_node][i_node]: (prob., MIP) mioa_dict = {} for source_node in self.graph_dict: ### source_dict: the node in heap which may update its activated probability -- ### source_dict[i_node] = (prob, in-neighbor) mioa_dict[source_node] = {} source_dict = { i: (self.graph_dict[source_node][i], source_node) for i in self.graph_dict[source_node] } source_dict[source_node] = (1.0, source_node) source_heap = [(self.graph_dict[source_node][i], i) for i in self.graph_dict[source_node]] heap.heapify_max(source_heap) # -- it will not find a better path than the existing MIP -- # -- because if this path exists, it should be pop earlier from the heap. -- while source_heap: (i_prob, i_node) = heap.heappop_max(source_heap) i_prev = source_dict[i_node][1] # -- find MIP from source_node to i_node -- i_path = [i_node, i_prev] while i_prev != source_node: i_prev = source_dict[i_prev][1] i_path.append(i_prev) i_path.pop() i_path.reverse() mioa_dict[source_node][i_node] = (i_prob, i_path) if i_node in self.graph_dict: for ii_node in self.graph_dict[i_node]: # -- not yet find MIP from source_node to ii_node -- if ii_node not in mioa_dict[source_node]: ii_prob = round( i_prob * self.graph_dict[i_node][ii_node], 4) if ii_prob >= self.prob_threshold: # -- if ii_node is in heap -- if ii_node in source_dict: ii_prob_d = source_dict[ii_node][0] if ii_prob > ii_prob_d: source_dict[ii_node] = (ii_prob, i_node) source_heap.remove( (ii_prob_d, ii_node)) source_heap.append((ii_prob, ii_node)) heap.heapify_max(source_heap) # -- if ii_node is not in heap -- else: source_dict[ii_node] = (ii_prob, i_node) heap.heappush_max(source_heap, (ii_prob, ii_node)) mioa_dict = [mioa_dict] * self.num_product if self.epw_flag: # -- update node's activated probability by product weight -- mioa_dict = [{ i: { j: (round( mioa_dict[k][i][j][0] * self.product_weight_list[k]** len(mioa_dict[k][i][j][1]), 4), mioa_dict[k][i][j][1]) for j in mioa_dict[k][i] } for i in mioa_dict[k] } for k in range(self.num_product)] # -- remove influenced nodes which are over diffusion threshold -- mioa_dict = [{ i: { j: mioa_dict[k][i][j] for j in mioa_dict[k][i] if mioa_dict[k][i][j][0] >= self.prob_threshold } for i in mioa_dict[k] } for k in range(self.num_product)] # -- remove empty mioa -- mioa_dict = [{ i: mioa_dict[k][i] for i in mioa_dict[k] if mioa_dict[k][i] } for k in range(self.num_product)] return mioa_dict
def generateSeedExpectedInfDictUsingDAG1(self, k_prod, s_set): node_rank_dict = {} source_dict = {s_node: 1.0 for s_node in s_set[k_prod]} source_heap = [(1.0, s_node) for s_node in s_set[k_prod]] # -- it will not find a better path than the existing MIP -- # -- because if this path exists, it should be pop earlier from the heap. -- while source_heap: (i_prob, i_node) = heap.heappop_max(source_heap) node_rank_dict[i_node] = i_prob if i_node in self.graph_dict: for ii_node in self.graph_dict[i_node]: # -- not yet find MIP from source_node to ii_node -- if ii_node not in node_rank_dict: ii_prob = round( i_prob * self.graph_dict[i_node][ii_node], 4) if ii_prob >= self.prob_threshold: # -- if ii_node is in heap -- if ii_node in source_dict: ii_prob_d = source_dict[ii_node] if ii_prob > ii_prob_d: source_dict[ii_node] = ii_prob source_heap.remove((ii_prob_d, ii_node)) source_heap.append((ii_prob, ii_node)) heap.heapify_max(source_heap) # -- if ii_node is not in heap -- else: source_dict[ii_node] = ii_prob heap.heappush_max(source_heap, (ii_prob, ii_node)) s_total_set = set(s for k in range(self.num_product) for s in s_set[k]) dag_dict = {} for i in self.graph_dict: if i in node_rank_dict: for j in self.graph_dict[i]: if j in node_rank_dict and j not in s_total_set: if node_rank_dict[i] > node_rank_dict[j]: if i not in dag_dict: dag_dict[i] = {} dag_dict[i][j] = self.graph_dict[i][j] seed_exp_inf_dict = {} for s_node in s_set[k_prod]: if s_node in dag_dict: ### source_dict[i_node] = (prob, in-neighbor) mioa_set = set() s_node_dict = {s_node: 1.0} source_heap = [] for i in dag_dict[s_node]: s_node_dict[i] = dag_dict[s_node][i] heap.heappush_max(source_heap, (dag_dict[s_node][i], i)) # -- it will not find a better path than the existing MIP -- # -- because if this path exists, it should be pop earlier from the heap. -- while source_heap: (i_prob, i_node) = heap.heappop_max(source_heap) mioa_set.add(i_node) if i_node in seed_exp_inf_dict: seed_exp_inf_dict[i_node].append(i_prob) else: seed_exp_inf_dict[i_node] = [i_prob] if i_node in dag_dict: for ii_node in dag_dict[i_node]: # -- not yet find MIP from source_node to ii_node -- if ii_node not in mioa_set: ii_prob = round( i_prob * dag_dict[i_node][ii_node], 4) if ii_prob >= self.prob_threshold: # -- if ii_node is in heap -- if ii_node in s_node_dict: ii_prob_d = s_node_dict[ii_node] if ii_prob > ii_prob_d: s_node_dict[ii_node] = ii_prob source_heap.remove( (ii_prob_d, ii_node)) source_heap.append( (ii_prob, ii_node)) heap.heapify_max(source_heap) # -- if ii_node is not in heap -- else: s_node_dict[ii_node] = ii_prob heap.heappush_max( source_heap, (ii_prob, ii_node)) return seed_exp_inf_dict
def updatePMIIA(self, s_set, pmiia_dict): s_list = s_set.copy() s_node = '' sub_graph = self.graph_dict.copy() while s_list: if len(s_list) != len(s_set): induceGraph(sub_graph, s_node) s_node = s_list.pop(0) pmiia_dict[s_node] = {} mioa_dict, miia_dict = {}, {s_node: {}} source_in_dict = {s_node: (1.0, s_node)} source_in_heap = [] s_node_in_neighbor = [ i for i in sub_graph if s_node in sub_graph[i] ] for i in s_node_in_neighbor: source_in_dict[i] = (sub_graph[i][s_node], s_node) heap.heappush_max(source_in_heap, (sub_graph[i][s_node], i)) while source_in_heap: (i_prob, i_node) = heap.heappop_max(source_in_heap) i_subs = source_in_dict[i_node][1] # -- find MIP from source_node to i_node -- i_path = [i_node, i_subs] while i_subs != source_in_dict[i_subs][1]: i_subs = source_in_dict[i_subs][1] i_path.append(i_subs) if i_node not in mioa_dict: mioa_dict[i_node] = {s_node: (i_prob, i_path)} else: mioa_dict[i_node][s_node] = (i_prob, i_path) miia_dict[s_node][i_node] = (i_prob, i_path) i_node_in_neighbor = [ i for i in sub_graph if i_node in sub_graph[i] ] if i_node_in_neighbor: for ii_node in i_node_in_neighbor: # -- not yet find MIP from source_node to ii_node -- if ii_node not in mioa_dict[i_node]: ii_prob = round( i_prob * sub_graph[ii_node][i_node], 4) if ii_prob >= self.prob_threshold: # -- if ii_node is in heap -- if ii_node in source_in_dict: ii_prob_d = source_in_dict[ii_node][0] if ii_prob > ii_prob_d: source_in_dict[ii_node] = (ii_prob, i_node) source_in_heap.remove( (ii_prob_d, ii_node)) source_in_heap.append( (ii_prob, ii_node)) heap.heapify_max(source_in_heap) # -- if ii_node is not in heap -- else: source_in_dict[ii_node] = (ii_prob, i_node) heap.heappush_max(source_in_heap, (ii_prob, ii_node)) for i_node_v in miia_dict[s_node]: is_set = { i for i in miia_dict[s_node][i_node_v][1] if i in s_list } if not is_set: pmiia_dict[s_node][i_node_v] = miia_dict[s_node][i_node_v] del_list = [i for i in pmiia_dict if not pmiia_dict[i]] while del_list: del_node = del_list.pop() del pmiia_dict[del_node] return pmiia_dict
def updatePMIIA(self, s_set, pmiia_dict): s_list = s_set.copy() s_node = '' sub_graph = copy.deepcopy(self.graph_dict) mioa_dict = {} while s_list: if len(s_list) != len(s_set): induceGraph(sub_graph, s_node) s_node = s_list.pop(0) mioa_dict[s_node] = {} source_dict = {s_node: (1.0, s_node)} source_heap = [] if s_node in sub_graph: for i in sub_graph[s_node]: source_dict[i] = (sub_graph[s_node][i], s_node) heap.heappush_max(source_heap, (sub_graph[s_node][i], i)) while source_heap: (i_prob, i_node) = heap.heappop_max(source_heap) i_prev = source_dict[i_node][1] # -- find MIP from source_node to i_node -- i_path = [i_node, i_prev] while i_prev != source_dict[i_prev][1]: i_prev = source_dict[i_prev][1] i_path.append(i_prev) i_path.reverse() mioa_dict[s_node][i_node] = (i_prob, i_path) if i_node in sub_graph: for ii_node in sub_graph[i_node]: # -- not yet find MIP from source_node to ii_node -- if ii_node not in mioa_dict[s_node]: ii_prob = round( i_prob * sub_graph[i_node][ii_node], 4) if ii_prob >= self.prob_threshold: # -- if ii_node is in heap -- if ii_node in source_dict: ii_prob_d = source_dict[ii_node][0] if ii_prob > ii_prob_d: source_dict[ii_node] = (ii_prob, i_node) source_heap.remove( (ii_prob_d, ii_node)) source_heap.append((ii_prob, ii_node)) heap.heapify_max(source_heap) # -- if ii_node is not in heap -- else: source_dict[ii_node] = (ii_prob, i_node) heap.heappush_max(source_heap, (ii_prob, ii_node)) is_dict = { i_node_v: set() for s_node in mioa_dict for i_node_v in mioa_dict[s_node] } s_list = s_set.copy() s_node = s_list.pop(0) while s_list: for i_node_v in mioa_dict[s_node]: # --- if there is a seed j > i s.t. sj is in MIP_G(Si) (si, v), si will be added into IS(v) --- is_condition = { i for i in mioa_dict[s_node][i_node_v][1] if i in s_list } if is_condition: is_dict[i_node_v].add(s_node) s_node = s_list.pop(0) for i_node_v in is_dict: s_set_reduce_is_set = set(s_set).difference(is_dict[i_node_v]) for s_node in s_set_reduce_is_set: if i_node_v in mioa_dict[s_node]: if i_node_v not in pmiia_dict: pmiia_dict[i_node_v] = {} pmiia_dict[i_node_v][s_node] = mioa_dict[s_node][i_node_v] return pmiia_dict