影响力最大化——最常用的模型IC和LT模型以及python实现

一、IC模型(独立级联模型)

        红色为处于激活态的种子节点,每条边都有一个传播概率p,激活的节点会以p的概率去激活邻居节点,一个节点有且只有一次机会去激活另一个节点,如果失败,则不再去尝试激活这个节点。

def preprocess(G):
    p = 0
    directedGraph = nx.DiGraph()
    for u in G.nodes():
        for v in G.neighbors(u):
            if (v != u):
                #propProb = G.number_of_edges(u, v) / G.in_degree(v)
                propProb = G.number_of_edges(u, v) / G.degree(v)
                directedGraph.add_edge(u, v, pp=propProb)
                #p += propProb
                #print(propProb)
    #print('平均阈值:', p/2939)
    return directedGraph
def simulate(G, seedNode, propProbability):
    newActive = True
    currentActiveNodes = copy.deepcopy(seedNode)
    newActiveNodes = set()
    activatedNodes = copy.deepcopy(seedNode)  # Biar ga keaktivasi 2 kali
    influenceSpread = len(seedNode)

    while (newActive):
        for node in currentActiveNodes:
            for neighbor in G.neighbors(node):  # Harus dicek udah aktif apa belom, jangan sampe ngaktifin yang udah aktif
                if (neighbor not in activatedNodes):
                    if (G[node][neighbor]['pp']>propProbability): #flipCoin(propProbability)
                        newActiveNodes.add(neighbor)
                        activatedNodes.append(neighbor)
        influenceSpread += len(newActiveNodes)
        if newActiveNodes:
            currentActiveNodes = list(newActiveNodes)
            newActiveNodes = set()
        else:
            newActive = False
    # print("activatedNodes",len(activatedNodes),activatedNodes)
    return influenceSpread


def flipCoin(probability):
    return random.random() < probability

二、LT模型(线性阈值模型)

        红色节点为处于激活状态的节点,每个节点会有一个激活阈值,如果该节点的处于激活态的邻居节点的激活概率相加大于这个节点的激活阈值,则该节点被激活,每个节点会有多次机会被激活。

def weight(G, u, v):
    if G.has_edge(u, v):
        return G[u][v]['weight']
    else:
        return 0

# LT传播模型
def simulate(G, seedNode,threshold_active):
    # Set Random threshold for every node ~ [0,1]
    # nodeThresholds = {}
    # for node in G.nodes():
    #     nodeThresholds[node] = random.uniform(0, 1)
    # Set predefined threshold for every node ~ threshold_active [0,1]
    nodeThresholds = {}
    for node in G.nodes():
        nodeThresholds[node] = threshold_active

    nodeValues = {}
    for node in G.nodes():
        nodeValues[node] = 0

    newActive = True
    currentActiveNodes = copy.deepcopy(seedNode)
    newActiveNodes = set()
    activatedNodes = copy.deepcopy(seedNode)  # Prevent from activating node twice
    influenceSpread = len(seedNode)

    while (newActive):
        for node in currentActiveNodes:
            for neighbor in G.neighbors(node):
                if (neighbor not in activatedNodes):
                    nodeValues[neighbor] += weight(G, node, neighbor)
                    if (nodeValues[neighbor] >= nodeThresholds[neighbor]):
                        newActiveNodes.add(neighbor)
                        activatedNodes.append(neighbor)
        influenceSpread += len(newActiveNodes)
        if newActiveNodes:
            currentActiveNodes = list(newActiveNodes)
            newActiveNodes = set()
        else:
            newActive = False
    return influenceSpread

  • 11
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
由于题目要求使用LT模型和贪心算法求解社交网络影响力最大化问题,我们需要先了解一下这两个概念。 LT模型是一种基于阈值的传播模型,它假设每个节点有一个阈值,只有当它的邻居中被激活的节点数量超过了这个阈值时,它才会被激活。贪心算法是一种近似算法,它通过对问题进行局部最优选择,来得到近似的全局最优解。 现在我们可以开始编写代码了。在这里,我们假设社交网络是一个有向图,节点用数字表示,种子节点用列表seed表示。我们的目标是找到种子节点集合使得被激活节点数量最大。 ```python import networkx as nx import random def LT_model(G, seed): """ LT模型传播 """ # 初始化所有节点状态为未激活 active_nodes = set(seed) activated = set(seed) while seed: # 找到所有未被激活的邻居节点 next_seed = set() for node in seed: for neighbor in G.successors(node): if neighbor not in activated: next_seed.add(neighbor) # 计算邻居节点中激活的节点数量 for node in next_seed: # 计算阈值 threshold = 0 for neighbor in G.predecessors(node): if neighbor in active_nodes: threshold += G[neighbor][node]['weight'] # 如果激活阈值满足条件则激活该节点 if random.random() &lt; threshold: activated.add(node) # 更新种子节点集合 seed = next_seed.copy() active_nodes |= seed return activated def greedy_LT(G, k): """ 贪心算法求解社交网络影响力最大化 """ seed = [] for i in range(k): max_gain = 0 best_node = None # 遍历所有节点找到对影响力增益最大的节点 for node in G.nodes(): if node not in seed: # 使用LT模型计算种子节点集合加上当前节点后的被激活节点数量 activated = LT_model(G, seed + [node]) gain = len(activated) - len(seed) if gain > max_gain: max_gain = gain best_node = node # 将影响力增益最大的节点加入种子节点集合 seed.append(best_node) # 使用LT模型计算最终的被激活节点集合 activated = LT_model(G, seed) return seed, activated ``` 在以上代码中,我们首先定义了一个LT_model函数,用于计算LT模型传播结果。具体地,该函数接受一个有向图G和一个种子节点列表seed作为输入,返回被激活的节点集合。该函数的实现过程与LT模型的定义相符合,具体地,我们首先将所有节点状态初始化为未激活状态,然后按照种子节点集合seed进行传播,直到种子节点集合为空为止。在每次传播中,我们找到所有未被激活的邻居节点,计算它们的激活阈值,如果满足条件则将其激活,并将其加入被激活节点集合中。 接下来,我们定义了一个greedy_LT函数,用于采用贪心算法求解社交网络影响力最大化问题。具体地,该函数接受一个有向图G和一个整数k作为输入,返回一个种子节点列表和一个被激活节点列表。在函数实现过程中,我们首先将种子节点集合seed初始化为空,然后进行k次迭代,每次迭代找到对影响力增益最大的节点加入种子节点集合中。在每次迭代中,我们使用LT模型计算种子节点集合加上当前节点后的被激活节点数量,选择增益最大的节点加入种子节点集合中。最终,我们使用LT模型计算最终的被激活节点集合,并返回种子节点列表和被激活节点列表。 注意,以上代码仅仅是一个简单的实现,实际上在实际应用中,我们需要考虑很多优化问题,如何加速LT模型传播、如何对节点进行排序等等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值