Java实现线性阈值模型(Linear Threshold Model)

影响力传播的线性阈值模型:

网络中连接任意两个节点u,v之间的边都有权重,任意一个节点它的各个邻居节点的边的权重之和为1,即

N(v):neighbors of v.

网络中的节点分为已激活节点和未激活节点,每个节点都有一个自己的激活阈值Θ(每个节点的激活阈值可以不同,且现实情况下社交网络的各个用户的激活阈值一般不相同,有的用户活跃,阈值低,容易受人影响,而有的用户较沉默,阈值高)。未被激活的节点v受所有与之相邻且已被激活的节点u的影响。当未激活节点v与所有已被激活的邻居节点的边的权重之和达到或超过激活阈值Θ时,节点v就会被激活。

即当满足条件:

Na(v):active neighbors of v.

v被激活之后,它也会以同样的方式影响它自己的未被激活的邻居节点。这样会有越来越多的,满足激活条件的节点被激活,直到最后再也没有新的节点被激活了,激活过程才会停止。

上述过程被称为影响力传播的线性阈值模型(Linear Threshold Model),传播过程停止时最终被激活的节点的数量被称为影响力的传播范围(Influence Spread)。

无向无权图的线性阈值模型的Java实现:

public int beginDiffusionProcess(ArrayList<Node> graph,ArrayList<Integer> activeNodeIds,int lastInfSpread)
    {
        //Mark the active neighbors of each node.
        for(Node nd:graph)
        {
            for(Node n:nd.neighbors)
            {
                if(activeNodeIds.contains(n.nodeId))
                {
                    n.setActive(true);
                }
            }
        }
        
        //Determine whether each node is activated or not.
        for(Node nd:graph)
        {
            int activeNeighbor_Num=0;
            for(Node n:nd.neighbors)
            {
                if(n.isActive())
                {
                    activeNeighbor_Num++;
                }
            }
            if (activeNeighbor_Num/(nd.neighbors.size()*1.0)>=nd.getThreshold())//如果是带权图,这里要修改
            {
                nd.setActive(true);
                activeNodeIds.add(nd.nodeId);
            }
        }
        //Get the influence spread of the current step.
        int infSpread=0;
        for(Node n:graph)
        {
            if(n.isActive())
            {
                infSpread++;
            }
        }
        //If it converges,stop the diffusion process,else continue the next step. 
        if(lastInfSpread==infSpread)
            return infSpread;
        else
            return beginDiffusionProcess(graph,activeNodeIds,infSpread); 
    }

下面的代码调用上述方法,获取最终的Influence Spread:

public int GetInfSpread(ArrayList<Node> graph)
    {
        ArrayList<Integer> activeNodeIds=new ArrayList<Integer>();
     //this.dimensions是已经被激活的种子节点,是某一个类的静态属性,类型为ArrayList<Node>,这些节点会尝试激活它们的邻居节点。 for(Node n:this.dimensions) { activeNodeIds.add(n.nodeId); } int lastInfSpread=0; return beginDiffusionProcess(graph, activeNodeIds,lastInfSpread); }

 其他相关的代码:

Node.java

import java.util.ArrayList;

public class Node implements Comparable<Node>
{
    public int nodeId;
    public ArrayList<Node> neighbors = new ArrayList<Node>();
    private boolean b_isActive = false;
    private double threshold = 0.0;

    public Node(int nodeId, double threshold)
    {
        this.nodeId = nodeId;
        this.threshold = threshold;
    }

    public int neighbors_num()
    {
        return this.neighbors.size();
    }

    public void setActive(boolean isActive)
    {
        this.b_isActive = isActive;
    }

    public boolean isActive(){
        return this.b_isActive;
    }
    public double getThreshold(){
        return this.threshold;
    }
    // Sort nodes by (out)degree
    public int compareTo(Node anotherNode)
    {
        if (this.neighbors != null && anotherNode.neighbors != null)
        {
            // reverse order
            return anotherNode.neighbors_num() - this.neighbors_num();
            // positive order
            // return this.neighbors_num()-anotherNode.neighbors_num();
        }
        return 0;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
 本次课程会带着大家学习Hash算法,从源码的角度去学习算法,更加容易理解的方式去学习,能够更高效的吸收学到的内容,也能培养出能够独自看源码,分析源码的能力。Hash,一般翻译做“散列”,也有直接音译为“哈希”的,就是把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值。这种转换是一种压缩映射,也就是,散列值的空间通常远小于输入的空间,不同的输入可能会散列成相同的输出,而不可能从散列值来唯一的确定输入值。简单的说就是一种将任意长度的消息压缩到某一固定长度的消息摘要的函数。  哈希表是根据设定的哈希函数H(key)和处理冲突方法将一组关键字映射到一个有限的地址区间上,并以关键字在地址区间中的象作为记录在表中的存储位置,这种表称为哈希表或散列,所得存储位置称为哈希地址或散列地址。作为线性数据结构与表格和队列等相比,哈希表无疑是查找速度比较快的一种。  通过将单向数学函数(有时称为“哈希算法”)应用到任意数量的数据所得到的固定大小的结果。如果输入数据中有变化,则哈希也会发生变化。哈希可用于许多操作,包括身份验证和数字签名。也称为“消息摘要”。  简单解释:哈希(Hash)算法,即散列函数。它是一种单向密码体制,即它是一个从明文到密文的不可逆的映射,只有加密过程,没有解密过程。同时,哈希函数可以将任意长度的输入经过变化以后得到固定长度的输出。哈希函数的这种单向特征和输出数据长度固定的特征使得它可以生成消息或者数据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值