算法模块
基础类
- class pynetim.algorithms.base_algorithm.BaseAlgorithm(graph, diffusion_model=None)[source]
Bases:
object影响力最大化算法基类。
为各种影响力最大化算法实现提供基础框架,包含图结构和扩散模型, 以及需要子类实现的核心方法。
- graph
输入图对象。
- seeds
种子节点集合。
- diffusion_model
扩散模型类。
Example
>>> from pynetim import IMGraph >>> from pynetim.algorithms import BaseAlgorithm >>> >>> class MyAlgorithm(BaseAlgorithm): ... def run(self, k): ... return set(range(k)) ... >>> graph = IMGraph(edges, weights=0.3) >>> algo = MyAlgorithm(graph, diffusion_model='IC') >>> seeds = algo.run(k=10)
- __init__(graph, diffusion_model=None)[source]
初始化算法基类。
- Parameters:
- Raises:
ValueError – 当 diffusion_model 不是 ‘IC’ 或 ‘LT’ 时抛出。
- run(k)[source]
执行算法选择种子节点。
子类必须实现此方法来定义具体的算法逻辑。
- Parameters:
k (int) – 需要选择的种子节点数量。
- Returns:
选中的种子节点集合。
- Return type:
Set[int]
- Raises:
NotImplementedError – 子类未实现此方法时抛出。
启发式算法
- class pynetim.algorithms.heuristic.BetweennessCentralityAlgorithm(graph, diffusion_model=None, normalized=True, sample_size=None)[source]
Bases:
BaseAlgorithm介数中心性启发式算法。
选择在网络中作为最短路径”桥梁”的节点。介数中心性高的节点 控制着网络中的信息流动,是关键的传播节点。
时间复杂度: O(n * m) 或 O(n^2) 对于稀疏图
- graph
输入图对象。
- seeds
种子节点集合。
References
Freeman, L. C. (1977). A set of measures of centrality based on betweenness. Sociometry, 35-41.
Example
>>> from pynetim import IMGraph >>> from pynetim.algorithms import BetweennessCentralityAlgorithm >>> >>> graph = IMGraph(edges, weights=0.3) >>> algo = BetweennessCentralityAlgorithm(graph) >>> seeds = algo.run(k=10)
- run(k)[source]
执行算法选择种子节点。
子类必须实现此方法来定义具体的算法逻辑。
- Parameters:
k (int) – 需要选择的种子节点数量。
- Returns:
选中的种子节点集合。
- Return type:
Set[int]
- Raises:
NotImplementedError – 子类未实现此方法时抛出。
- class pynetim.algorithms.heuristic.ClosenessCentralityAlgorithm(graph, diffusion_model=None)[source]
Bases:
BaseAlgorithm接近中心性启发式算法。
选择距离网络中其他节点最近的节点。接近中心性高的节点 可以快速将信息传播到整个网络。
时间复杂度: O(n * m)
- graph
输入图对象。
- seeds
种子节点集合。
References
Sabidussi, G. (1966). The centrality index of a graph. Psychometrika, 31(4), 581-603.
Example
>>> from pynetim import IMGraph >>> from pynetim.algorithms import ClosenessCentralityAlgorithm >>> >>> graph = IMGraph(edges, weights=0.3) >>> algo = ClosenessCentralityAlgorithm(graph) >>> seeds = algo.run(k=10)
- __init__(graph, diffusion_model=None)[source]
初始化算法基类。
- Parameters:
- Raises:
ValueError – 当 diffusion_model 不是 ‘IC’ 或 ‘LT’ 时抛出。
- run(k)[source]
执行算法选择种子节点。
子类必须实现此方法来定义具体的算法逻辑。
- Parameters:
k (int) – 需要选择的种子节点数量。
- Returns:
选中的种子节点集合。
- Return type:
Set[int]
- Raises:
NotImplementedError – 子类未实现此方法时抛出。
- class pynetim.algorithms.heuristic.DegreeCentralityAlgorithm(graph, diffusion_model=None)[source]
Bases:
BaseAlgorithm度中心性启发式算法。
选择出度最大的节点作为种子节点,是最简单快速的启发式方法。 适合作为基准算法进行比较。
时间复杂度: O(n log n)
- graph
输入图对象。
- seeds
种子节点集合。
Example
>>> from pynetim import IMGraph >>> from pynetim.algorithms import DegreeCentralityAlgorithm >>> >>> graph = IMGraph(edges, weights=0.3) >>> algo = DegreeCentralityAlgorithm(graph) >>> seeds = algo.run(k=10)
- __init__(graph, diffusion_model=None)[source]
初始化算法基类。
- Parameters:
- Raises:
ValueError – 当 diffusion_model 不是 ‘IC’ 或 ‘LT’ 时抛出。
- run(k)[source]
执行算法选择种子节点。
子类必须实现此方法来定义具体的算法逻辑。
- Parameters:
k (int) – 需要选择的种子节点数量。
- Returns:
选中的种子节点集合。
- Return type:
Set[int]
- Raises:
NotImplementedError – 子类未实现此方法时抛出。
- class pynetim.algorithms.heuristic.DegreeDiscountAlgorithm(graph, diffusion_model='IC')[source]
Bases:
BaseAlgorithm度折扣启发式算法。
是 SingleDiscountAlgorithm 的改进版本,考虑了邻居节点之间的影响关系, 使用更复杂的折扣公式来更好地评估节点的边际影响力。
该算法速度快且效果较好,适合大规模图的种子选择。
- graph
输入图对象。
- seeds
种子节点集合。
References
Chen, W., Wang, Y., & Yang, S. (2009). Efficient influence maximization in social networks. KDD, 199-208.
Example
>>> from pynetim import IMGraph >>> from pynetim.algorithms import DegreeDiscountAlgorithm >>> >>> graph = IMGraph(edges, weights=0.3) >>> algo = DegreeDiscountAlgorithm(graph, diffusion_model='IC') >>> seeds = algo.run(k=10)
- __init__(graph, diffusion_model='IC')[source]
初始化算法基类。
- Parameters:
- Raises:
ValueError – 当 diffusion_model 不是 ‘IC’ 或 ‘LT’ 时抛出。
- run(k)[source]
执行算法选择种子节点。
子类必须实现此方法来定义具体的算法逻辑。
- Parameters:
k (int) – 需要选择的种子节点数量。
- Returns:
选中的种子节点集合。
- Return type:
Set[int]
- Raises:
NotImplementedError – 子类未实现此方法时抛出。
- class pynetim.algorithms.heuristic.EigenvectorCentralityAlgorithm(graph, diffusion_model=None, max_iter=100, tol=1e-06)[source]
Bases:
BaseAlgorithm特征向量中心性启发式算法。
节点的重要性取决于其邻居的重要性。一个节点如果连接了许多 重要的节点,那么它自己也更重要。
时间复杂度: O(n * max_iter)
- graph
输入图对象。
- seeds
种子节点集合。
References
Bonacich, P. (1972). Factoring and weighting approaches to status scores and clique identification. Journal of Mathematical Sociology, 2(1), 113-120.
Example
>>> from pynetim import IMGraph >>> from pynetim.algorithms import EigenvectorCentralityAlgorithm >>> >>> graph = IMGraph(edges, weights=0.3) >>> algo = EigenvectorCentralityAlgorithm(graph) >>> seeds = algo.run(k=10)
- run(k)[source]
执行算法选择种子节点。
子类必须实现此方法来定义具体的算法逻辑。
- Parameters:
k (int) – 需要选择的种子节点数量。
- Returns:
选中的种子节点集合。
- Return type:
Set[int]
- Raises:
NotImplementedError – 子类未实现此方法时抛出。
- class pynetim.algorithms.heuristic.KShellDecompositionAlgorithm(graph, diffusion_model=None)[source]
Bases:
BaseAlgorithmK-shell 分解启发式算法。
通过迭代移除度数最小的节点,将网络分层。位于核心层(高 k-shell 值) 的节点被认为具有更大的影响力。
时间复杂度: O(m),m 为边数
- graph
输入图对象。
- seeds
种子节点集合。
References
Kitsak, M., Gallos, L. K., Havlin, S., Liljeros, F., Muchnik, L., Stanley, H. E., & Makse, H. A. (2010). Identification of influential spreaders in complex networks. Nature physics, 6(11), 888-893.
Example
>>> from pynetim import IMGraph >>> from pynetim.algorithms import KShellDecompositionAlgorithm >>> >>> graph = IMGraph(edges, weights=0.3) >>> algo = KShellDecompositionAlgorithm(graph) >>> seeds = algo.run(k=10)
- __init__(graph, diffusion_model=None)[source]
初始化算法基类。
- Parameters:
- Raises:
ValueError – 当 diffusion_model 不是 ‘IC’ 或 ‘LT’ 时抛出。
- static compute_k_shell_values(graph)[source]
计算图中所有节点的 K-shell 值。
使用 Batagelj-Zaversnik 算法,时间复杂度 O(m)。
References
Batagelj, V., & Zaversnik, M. (2003). An O(m) algorithm for cores decomposition of networks. arXiv preprint cs/0310049.
Note
此方法是 pynetim.graph.compute_k_shell_values 的便捷包装。
Example
>>> from pynetim import IMGraph >>> from pynetim.algorithms import KShellDecompositionAlgorithm >>> graph = IMGraph(edges, directed=True) >>> k_shell = KShellDecompositionAlgorithm.compute_k_shell_values(graph)
- run(k)[source]
执行算法选择种子节点。
子类必须实现此方法来定义具体的算法逻辑。
- Parameters:
k (int) – 需要选择的种子节点数量。
- Returns:
选中的种子节点集合。
- Return type:
Set[int]
- Raises:
NotImplementedError – 子类未实现此方法时抛出。
- class pynetim.algorithms.heuristic.PageRankAlgorithm(graph, diffusion_model=None, damping=0.85, max_iter=100, tol=1e-06)[source]
Bases:
BaseAlgorithmPageRank 启发式算法。
基于 Google 的 PageRank 算法,通过模拟随机游走来评估节点的重要性。 节点的重要性取决于指向它的节点的重要性。
时间复杂度: O(n * max_iter)
- graph
输入图对象。
- seeds
种子节点集合。
References
Brin, S., & Page, L. (1998). The anatomy of a large-scale hypertextual Web search engine. Computer networks and ISDN systems, 30(1-7), 107-117.
Example
>>> from pynetim import IMGraph >>> from pynetim.algorithms import PageRankAlgorithm >>> >>> graph = IMGraph(edges, weights=0.3) >>> algo = PageRankAlgorithm(graph) >>> seeds = algo.run(k=10)
- __init__(graph, diffusion_model=None, damping=0.85, max_iter=100, tol=1e-06)[source]
初始化 PageRank 算法。
- run(k)[source]
执行算法选择种子节点。
子类必须实现此方法来定义具体的算法逻辑。
- Parameters:
k (int) – 需要选择的种子节点数量。
- Returns:
选中的种子节点集合。
- Return type:
Set[int]
- Raises:
NotImplementedError – 子类未实现此方法时抛出。
- class pynetim.algorithms.heuristic.SingleDiscountAlgorithm(graph, diffusion_model=None)[source]
Bases:
BaseAlgorithm简单度折扣启发式算法。
通过逐步选择具有最高度数的节点作为种子,并对其邻居节点的度数进行折扣, 以避免选择过多相互连接的节点。
该算法速度快,适合大规模图的快速种子选择。
- graph
输入图对象。
- seeds
种子节点集合。
References
Chen, W., Wang, Y., & Yang, S. (2009). Efficient influence maximization in social networks. KDD, 199-208.
Example
>>> from pynetim import IMGraph >>> from pynetim.algorithms import SingleDiscountAlgorithm >>> >>> graph = IMGraph(edges, weights=0.3) >>> algo = SingleDiscountAlgorithm(graph) >>> seeds = algo.run(k=10)
- __init__(graph, diffusion_model=None)[source]
初始化算法基类。
- Parameters:
- Raises:
ValueError – 当 diffusion_model 不是 ‘IC’ 或 ‘LT’ 时抛出。
- run(k)[source]
执行算法选择种子节点。
子类必须实现此方法来定义具体的算法逻辑。
- Parameters:
k (int) – 需要选择的种子节点数量。
- Returns:
选中的种子节点集合。
- Return type:
Set[int]
- Raises:
NotImplementedError – 子类未实现此方法时抛出。
- class pynetim.algorithms.heuristic.VoteRankAlgorithm(graph, diffusion_model=None)[source]
Bases:
BaseAlgorithmVoteRank 启发式算法。
通过投票机制选择分散的影响力节点。每个节点为其邻居投票, 得票最高的节点被选为种子,然后其邻居的投票能力被削弱。 这样可以避免选择过于聚集的种子节点。
时间复杂度: O(n * k)
- graph
输入图对象。
- seeds
种子节点集合。
References
Zhang, J. X., Chen, D. B., Dong, Q., & Zhao, Z. D. (2016). Identifying a set of influential spreaders in complex networks by VoteRank. Physica A: Statistical Mechanics and its Applications, 461, 171-182.
Example
>>> from pynetim import IMGraph >>> from pynetim.algorithms import VoteRankAlgorithm >>> >>> graph = IMGraph(edges, weights=0.3) >>> algo = VoteRankAlgorithm(graph) >>> seeds = algo.run(k=10)
- __init__(graph, diffusion_model=None)[source]
初始化算法基类。
- Parameters:
- Raises:
ValueError – 当 diffusion_model 不是 ‘IC’ 或 ‘LT’ 时抛出。
- run(k)[source]
执行算法选择种子节点。
子类必须实现此方法来定义具体的算法逻辑。
- Parameters:
k (int) – 需要选择的种子节点数量。
- Returns:
选中的种子节点集合。
- Return type:
Set[int]
- Raises:
NotImplementedError – 子类未实现此方法时抛出。
模拟类算法
- class pynetim.algorithms.simulation.CELFAlgorithm(graph, diffusion_model='IC')[source]
Bases:
BaseAlgorithmCELF 算法。
CELF (Cost-Effective Lazy Forward) 是贪婪算法的优化版本, 利用边际增益的子模特性减少计算量,通过优先队列维护节点的边际增益, 避免重复计算已失效的边际增益。
该算法精度高,比贪婪算法快 2-7 倍,适合中等规模图。
- graph
输入图对象。
- seeds
种子节点集合。
- diffusion_model
扩散模型类。
References
Leskovec, J., Krause, A., Guestrin, C., Faloutsos, C., VanBriesen, J., & Glance, N. (2007). Cost-effective outbreak detection in networks. KDD, 420-429.
Example
>>> from pynetim import IMGraph >>> from pynetim.algorithms import CELFAlgorithm >>> >>> graph = IMGraph(edges, weights=0.3) >>> algo = CELFAlgorithm(graph, diffusion_model='IC') >>> seeds = algo.run(k=10, mc_rounds=1000, use_multithread=True, num_threads=4)
- run(k, mc_rounds=1000, use_multithread=False, num_threads=4, show_progress=True, random_seed=None)[source]
运行 CELF 算法选择种子节点。
- Parameters:
- Returns:
选中的种子节点集合。
- Return type:
Set[int]
- Raises:
ValueError – 当 use_multithread=True 但 num_threads <= 0 时抛出。
Note
利用子模特性,CELF 比贪婪算法快 2-7 倍,同时保证相同的近似比。
- class pynetim.algorithms.simulation.CELFPlusAlgorithm(graph, diffusion_model='IC')[source]
Bases:
BaseAlgorithmCELF++ 算法。
CELF++ 是 CELF 算法的改进版本,进一步利用子模特性减少计算量。 在更新节点边际增益时,同时考虑当前最佳候选节点,如果该节点增益仍然最大, 则直接选择它,避免额外的重新计算。
该算法精度高,比 CELF 更快,适合中等规模图。
- graph
输入图对象。
- seeds
种子节点集合。
- diffusion_model
扩散模型类。
References
Goyal, A., Lu, W., & Lakshmanan, L. V. (2011). CELF++: Optimizing the greedy algorithm for influence maximization in social networks. WWW, 47-48.
Example
>>> from pynetim import IMGraph >>> from pynetim.algorithms import CELFPlusAlgorithm >>> >>> graph = IMGraph(edges, weights=0.3) >>> algo = CELFPlusAlgorithm(graph, diffusion_model='IC') >>> seeds = algo.run(k=10, mc_rounds=1000, use_multithread=True, num_threads=4)
- run(k, mc_rounds=1000, use_multithread=False, num_threads=4, show_progress=True, random_seed=None)[source]
运行 CELF++ 算法选择种子节点。
- Parameters:
- Returns:
选中的种子节点集合。
- Return type:
Set[int]
- Raises:
ValueError – 当 use_multithread=True 但 num_threads <= 0 时抛出。
Note
CELF++ 比 CELF 更快,同时保证相同的近似比。
- class pynetim.algorithms.simulation.GreedyAlgorithm(graph, diffusion_model='IC')[source]
Bases:
BaseAlgorithm贪婪算法。
通过迭代地选择具有最大边际影响力的节点作为种子节点, 直到选够 k 个种子节点。每次选择都需要计算所有候选节点的边际增益。
该算法精度高,但速度较慢,适合小规模图或需要高精度的场景。
- graph
输入图对象。
- seeds
种子节点集合。
- diffusion_model
扩散模型类。
References
Kempe, D., Kleinberg, J., & Tardos, É. (2003). Maximizing the spread of influence through a social network. KDD, 137-146.
Example
>>> from pynetim import IMGraph >>> from pynetim.algorithms import GreedyAlgorithm >>> >>> graph = IMGraph(edges, weights=0.3) >>> algo = GreedyAlgorithm(graph, diffusion_model='IC') >>> seeds = algo.run(k=10, mc_rounds=1000, use_multithread=True, num_threads=4)
- run(k, mc_rounds=1000, use_multithread=False, num_threads=4, show_progress=True, random_seed=None)[source]
运行贪婪算法选择种子节点。
- Parameters:
- Returns:
选中的种子节点集合。
- Return type:
Set[int]
- Raises:
ValueError – 当 use_multithread=True 但 num_threads <= 0 时抛出。
Note
时间复杂度为 O(k * n * mc_rounds),其中 n 为节点数。 对于大规模图,建议使用 CELFAlgorithm 或 IMMAlgorithm。
RIS 类算法
- class pynetim.algorithms.ris.BaseRISAlgorithm
Bases:
pybind11_object
- class pynetim.algorithms.ris.IMMAlgorithm
Bases:
BaseRISAlgorithm- __init__(graph: IMGraph, model: str, epsilon: float = 0.5, l: int = 1, random_seed: int | None = None, verbose: bool = False) None
初始化IMM算法。
- Parameters:
graph – 图对象。
model – 扩散模型,支持 ‘IC’ 或 ‘LT’。
epsilon – 近似参数ε,默认为 0.5。
l – 失败概率参数,默认为 1。
random_seed – 随机种子,默认为 None(每次随机)。
verbose – 是否显示关键过程日志,默认为 False。
- class pynetim.algorithms.ris.OPIMAlgorithm
Bases:
BaseRISAlgorithm- __init__(graph: IMGraph, model: str, random_seed: int | None = None, verbose: bool = False) None
初始化OPIM算法。
OPIM (Online Processing for Influence Maximization) 使用两组独立的RR集合: - R1用于贪心选择种子节点 - R2用于验证选中种子的影响力
- 参考文献:
Jing Tang, Xueyan Tang, Xiaokui Xiao, Junsong Yuan, “Online Processing Algorithms for Influence Maximization,” in Proc. ACM SIGMOD, 2018.
- Parameters:
graph – 图对象。
model – 扩散模型,支持 ‘IC’ 或 ‘LT’。
random_seed – 随机种子,默认为 None(每次随机)。
verbose – 是否显示关键过程日志,默认为 False。
- class pynetim.algorithms.ris.OPIMCAlgorithm
Bases:
OPIMAlgorithm- __init__(graph: IMGraph, model: str, random_seed: int | None = None, verbose: bool = False) None
初始化OPIM-C算法。
OPIM-C是OPIM的自适应版本,迭代增加RR集合数量, 直到达到(1-1/e-ε)近似保证。
- 参考文献:
Jing Tang, Xueyan Tang, Xiaokui Xiao, Junsong Yuan, “Online Processing Algorithms for Influence Maximization,” in Proc. ACM SIGMOD, 2018.
- Parameters:
graph – 图对象。
model – 扩散模型,支持 ‘IC’ 或 ‘LT’。
random_seed – 随机种子,默认为 None(每次随机)。
verbose – 是否显示关键过程日志,默认为 False。
- class pynetim.algorithms.ris.TIMAlgorithm
Bases:
BaseRISAlgorithm- __init__(graph: IMGraph, model: str, epsilon: float = 0.5, l: int = 1, random_seed: int | None = None, verbose: bool = False) None
初始化TIM算法。
- Parameters:
graph – 图对象。
model – 扩散模型,支持 ‘IC’ 或 ‘LT’。
epsilon – 近似参数ε,默认为 0.5。
l – 失败概率参数,默认为 1。
random_seed – 随机种子,默认为 None(每次随机)。
verbose – 是否显示关键过程日志,默认为 False。
- class pynetim.algorithms.ris.TIMPlusAlgorithm
Bases:
BaseRISAlgorithm- __init__(graph: IMGraph, model: str, epsilon: float = 0.5, l: int = 1, random_seed: int | None = None, verbose: bool = False) None
初始化TIM+算法。
TIM+改进了TIM的采样策略,通常比TIM更快。
- Parameters:
graph – 图对象。
model – 扩散模型,支持 ‘IC’ 或 ‘LT’。
epsilon – 近似参数ε,默认为 0.5。
l – 失败概率参数,默认为 1。
random_seed – 随机种子,默认为 None(每次随机)。
verbose – 是否显示关键过程日志,默认为 False。
深度学习算法
深度学习影响力最大化算法模块。
此模块包含纯深度学习算法(非强化学习)。 深度强化学习算法已移动到 reinforcement_learning.deep 模块。
- class pynetim.algorithms.deep_learning.BaseDLAlgorithm(graph, pretrained=True, weights_path=None, device='auto', diffusion_model=None)[source]
Bases:
BaseAlgorithm深度学习影响力最大化算法基类。
为深度学习算法提供基础框架,包含设备管理、权重加载等功能。
- Parameters:
- device
计算设备 (CPU/GPU)。
- model
神经网络模型。
Example
>>> from pynetim import IMGraph >>> from pynetim.algorithms import ToupleGDDAlgorithm >>> >>> graph = IMGraph(edges, weights=0.3) >>> algo = ToupleGDDAlgorithm(graph, pretrained=True) >>> seeds = algo.run(k=10)
- __init__(graph, pretrained=True, weights_path=None, device='auto', diffusion_model=None)[source]
初始化深度学习算法基类。
- run(k)
执行算法选择种子节点。
子类必须实现此方法来定义具体的算法逻辑。
- Parameters:
k (int) – 需要选择的种子节点数量。
- Returns:
选中的种子节点集合。
- Return type:
Set[int]
- Raises:
NotImplementedError – 子类未实现此方法时抛出。