经典链路预测相似性算法的MATLAB实现

简介: 经典链路预测相似性算法的MATLAB实现,包含Common Neighbors、Jaccard Coefficient、Adamic-Adar、Resource Allocation、Katz Index和Local Path Index等算法。

经典链路预测相似性算法的MATLAB实现,包含Common Neighbors、Jaccard Coefficient、Adamic-Adar、Resource Allocation、Katz Index和Local Path Index等算法。

classdef LinkPredictor
    % 链路预测相似性算法实现
    % 包含多种经典相似性指标的计算和比较

    properties
        adjMatrix;      % 邻接矩阵
        nodeCount;      % 节点数量
        similarityMat;  % 相似度矩阵
    end

    methods
        function obj = LinkPredictor(adjMatrix)
            % 构造函数
            % 输入: adjMatrix - 邻接矩阵 (n×n)
            obj.adjMatrix = adjMatrix;
            obj.nodeCount = size(adjMatrix, 1);
            obj.similarityMat = zeros(obj.nodeCount, obj.nodeCount);
        end

        function obj = computeSimilarity(obj, method, varargin)
            % 计算相似度矩阵
            % 输入: 
            %   method - 算法名称 ('CN', 'JC', 'AA', 'RA', 'Katz', 'LP')
            %   varargin - 算法参数 (如Katz的beta, LP的alpha)
            % 输出: 更新相似度矩阵的LinkPredictor对象

            switch lower(method)
                case 'cn'  % Common Neighbors
                    obj.similarityMat = obj.commonNeighbors();

                case 'jc'  % Jaccard Coefficient
                    obj.similarityMat = obj.jaccardCoefficient();

                case 'aa'  % Adamic-Adar
                    obj.similarityMat = obj.adamicAdar();

                case 'ra'  % Resource Allocation
                    obj.similarityMat = obj.resourceAllocation();

                case 'katz'  % Katz Index
                    beta = 0.1;  % 默认衰减因子
                    if ~isempty(varargin)
                        beta = varargin{
   1};
                    end
                    obj.similarityMat = obj.katzIndex(beta);

                case 'lp'  % Local Path Index
                    alpha = 0.01;  % 默认参数
                    if ~isempty(varargin)
                        alpha = varargin{
   1};
                    end
                    obj.similarityMat = obj.localPathIndex(alpha);

                otherwise
                    error('未知算法: %s', method);
            end
        end

        function scores = commonNeighbors(obj)
            % Common Neighbors (CN) 算法
            % 公式: s_xy = |Γ(x) ∩ Γ(y)|
            scores = obj.adjMatrix^2;  % 矩阵乘法计算共同邻居数
        end

        function scores = jaccardCoefficient(obj)
            % Jaccard Coefficient (JC) 算法
            % 公式: s_xy = |Γ(x) ∩ Γ(y)| / |Γ(x) ∪ Γ(y)|
            intersection = obj.adjMatrix^2;  % 共同邻居数
            degree = diag(obj.adjMatrix * ones(obj.nodeCount, 1));  % 节点度数
            union = repmat(degree, 1, obj.nodeCount) + repmat(degree', obj.nodeCount, 1) - intersection;
            scores = intersection ./ (union + eps);  % 避免除以零
        end

        function scores = adamicAdar(obj)
            % Adamic-Adar (AA) 算法
            % 公式: s_xy = Σ_{
   z∈Γ(x)∩Γ(y)} 1/log|Γ(z)|
            scores = zeros(obj.nodeCount, obj.nodeCount);
            degrees = sum(obj.adjMatrix, 2);  % 节点度数

            for i = 1:obj.nodeCount
                neighbors_i = find(obj.adjMatrix(i, :));
                for j = i+1:obj.nodeCount
                    neighbors_j = find(obj.adjMatrix(j, :));
                    common = intersect(neighbors_i, neighbors_j);
                    score = 0;
                    for k = 1:length(common)
                        z = common(k);
                        score = score + 1/log(max(2, degrees(z)));  % 避免log(1)=0
                    end
                    scores(i, j) = score;
                    scores(j, i) = score;
                end
            end
        end

        function scores = resourceAllocation(obj)
            % Resource Allocation (RA) 算法
            % 公式: s_xy = Σ_{
   z∈Γ(x)∩Γ(y)} 1/|Γ(z)|
            scores = zeros(obj.nodeCount, obj.nodeCount);
            degrees = sum(obj.adjMatrix, 2);  % 节点度数

            for i = 1:obj.nodeCount
                neighbors_i = find(obj.adjMatrix(i, :));
                for j = i+1:obj.nodeCount
                    neighbors_j = find(obj.adjMatrix(j, :));
                    common = intersect(neighbors_i, neighbors_j);
                    score = 0;
                    for k = 1:length(common)
                        z = common(k);
                        score = score + 1/max(1, degrees(z));  % 避免除以零
                    end
                    scores(i, j) = score;
                    scores(j, i) = score;
                end
            end
        end

        function scores = katzIndex(obj, beta)
            % Katz Index 算法
            % 公式: s_xy = Σ_{
   l=1}^{
   } β^l * |paths_{
   xy}^{
   (l)}|
            % 使用矩阵幂级数计算
            I = eye(obj.nodeCount);
            A = obj.adjMatrix;
            scores = zeros(obj.nodeCount, obj.nodeCount);

            % 计算矩阵幂级数和: S = A + βA² + β²A³ + ...
            % 使用几何级数公式: S = A(I - βA)^{
   -1}
            try
                S = A * inv(I - beta * A);
                scores = S;
            catch
                warning('Katz计算失败,使用近似方法');
                % 近似计算前几项
                S = zeros(obj.nodeCount, obj.nodeCount);
                term = A;
                for l = 1:10  % 计算前10项
                    S = S + (beta^(l-1)) * term;
                    term = term * A;
                end
                scores = S;
            end
        end

        function scores = localPathIndex(obj, alpha)
            % Local Path Index (LP) 算法
            % 公式: s_xy = ()_{
   xy} + α()_{
   xy}
            A = obj.adjMatrix;
            A2 = A^2;
            A3 = A2 * A;
            scores = A2 + alpha * A3;
        end

        function [sortedPairs, sortedScores] = predictLinks(obj, topK)
            % 预测最可能的链接
            % 输入: topK - 返回前K个预测结果
            % 输出: 
            %   sortedPairs - 排序后的节点对 (2矩阵)
            %   sortedScores - 对应的相似度分数 (1向量)

            % 获取上三角部分(避免重复和自环)
            triu_idx = triu(true(obj.nodeCount), 1);
            scoresVec = obj.similarityMat(triu_idx);
            pairs = [];
            for i = 1:obj.nodeCount
                for j = i+1:obj.nodeCount
                    pairs = [pairs; i, j];
                end
            end

            % 按分数降序排序
            [sortedScores, sortIdx] = sort(scoresVec, 'descend');
            sortedPairs = pairs(sortIdx, :);

            % 返回前K个结果
            if nargin > 1 && topK > 0
                topK = min(topK, length(sortedScores));
                sortedPairs = sortedPairs(1:topK, :);
                sortedScores = sortedScores(1:topK);
            end
        end

        function visualizeSimilarity(obj, nodeIds)
            % 可视化节点相似度
            % 输入: nodeIds - 要可视化的节点ID向量
            if nargin < 2
                nodeIds = 1:min(10, obj.nodeCount);  % 默认显示前10个节点
            end

            figure;
            imagesc(obj.similarityMat(nodeIds, nodeIds));
            colorbar;
            title('节点相似度矩阵');
            xlabel('节点ID');
            ylabel('节点ID');
            set(gca, 'XTick', 1:length(nodeIds), 'XTickLabel', nodeIds);
            set(gca, 'YTick', 1:length(nodeIds), 'YTickLabel', nodeIds);
        end

        function plotTopLinks(obj, topK)
            % 绘制预测的前topK个链接
            [pairs, scores] = obj.predictLinks(topK);

            figure;
            bar(scores);
            xlabel('排名');
            ylabel('相似度分数');
            title(sprintf('前%d个预测链接', topK));
            grid on;

            % 显示前10个预测结果
            fprintf('前%d个预测链接:\n', min(10, topK));
            for i = 1:min(10, topK)
                fprintf('(%d, %d): %.4f\n', pairs(i,1), pairs(i,2), scores(i));
            end
        end

        function evaluate(obj, testEdges, nonEdges)
            % 评估预测性能
            % 输入:
            %   testEdges - 测试集中的正样本边 (2矩阵)
            %   nonEdges - 测试集中的负样本边 (2矩阵)

            % 获取所有边的分数
            allPairs = [testEdges; nonEdges];
            allScores = zeros(size(allPairs, 1), 1);

            for i = 1:size(allPairs, 1)
                u = allPairs(i, 1);
                v = allPairs(i, 2);
                allScores(i) = obj.similarityMat(u, v);
            end

            % 计算AUC
            posScores = allScores(1:size(testEdges, 1));
            negScores = allScores(size(testEdges, 1)+1:end);

            auc = 0;
            for i = 1:length(posScores)
                for j = 1:length(negScores)
                    if posScores(i) > negScores(j)
                        auc = auc + 1;
                    elseif abs(posScores(i) - negScores(j)) < 1e-6
                        auc = auc + 0.5;
                    end
                end
            end
            auc = auc / (length(posScores) * length(negScores));

            fprintf('AUC: %.4f\n', auc);
            return auc;
        end

        function demo()
            % 演示函数
            % 创建一个示例网络 (Zachary's Karate Club)
            adjMatrix = [
                0 1 1 1 1 1 1 1 1 0 1 1 1 0 0 0 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 0;
                1 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0;
                1 1 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
                1 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
                1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
                1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
                1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
                1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
                1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
                0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
                1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
                1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
                1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
                0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
                0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
                0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
                0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
                1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
                0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
                1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
                0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
                1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
                0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
                0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
                0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
                0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
                0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
                0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
                0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
                0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
                0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
                1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
                0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
                0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
            ];

            % 创建链路预测器
            predictor = LinkPredictor(adjMatrix);

            % 计算各种相似度
            algorithms = {
   'CN', 'JC', 'AA', 'RA', 'Katz', 'LP'};
            results = struct();

            for i = 1:length(algorithms)
                algo = algorithms{
   i};
                if strcmp(algo, 'Katz')
                    predictor = predictor.computeSimilarity(algo, 0.05);
                elseif strcmp(algo, 'LP')
                    predictor = predictor.computeSimilarity(algo, 0.01);
                else
                    predictor = predictor.computeSimilarity(algo);
                end
                results.(algo) = predictor.similarityMat;
            end

            % 可视化结果
            figure('Name', '链路预测算法比较', 'Position', [100, 100, 1200, 800]);

            % 显示原始网络
            subplot(2, 3, 1);
            gplot(adjMatrix, rand(obj.nodeCount, 2), 'o-');
            title('原始网络 (Zachary''s Karate Club)');
            axis square;

            % 显示各种算法的相似度矩阵
            for i = 1:length(algorithms)
                subplot(2, 3, i+1);
                imagesc(results.(algorithms{
   i}));
                colorbar;
                title(algorithms{
   i});
                axis square;
            end

            % 预测前10个链接
            predictor = predictor.computeSimilarity('CN');  % 使用CN算法
            [pairs, scores] = predictor.predictLinks(10);

            figure('Name', 'CN算法预测结果', 'Position', [200, 200, 800, 400]);
            bar(scores);
            xlabel('排名');
            ylabel('相似度分数');
            title('CN算法预测的前10个链接');
            grid on;

            fprintf('CN算法预测的前10个链接:\n');
            for i = 1:size(pairs, 1)
                fprintf('(%d, %d): %.4f\n', pairs(i,1), pairs(i,2), scores(i));
            end
        end
    end
end

算法实现说明

1. 核心算法实现

(1) Common Neighbors (CN)

function scores = commonNeighbors(obj)
    scores = obj.adjMatrix^2;  % 矩阵乘法计算共同邻居数
end
  • 计算每对节点的共同邻居数量

  • 使用矩阵乘法高效实现

(2) Jaccard Coefficient (JC)

function scores = jaccardCoefficient(obj)
    intersection = obj.adjMatrix^2;  % 共同邻居数
    degree = diag(obj.adjMatrix * ones(obj.nodeCount, 1));  % 节点度数
    union = repmat(degree, 1, obj.nodeCount) + repmat(degree', obj.nodeCount, 1) - intersection;
    scores = intersection ./ (union + eps);  % 避免除以零
end
  • 计算共同邻居占并集的比例

  • 使用向量化操作提高效率

(3) Adamic-Adar (AA)

function scores = adamicAdar(obj)
    scores = zeros(obj.nodeCount, obj.nodeCount);
    degrees = sum(obj.adjMatrix, 2);  % 节点度数

    for i = 1:obj.nodeCount
        neighbors_i = find(obj.adjMatrix(i, :));
        for j = i+1:obj.nodeCount
            neighbors_j = find(obj.adjMatrix(j, :));
            common = intersect(neighbors_i, neighbors_j);
            score = 0;
            for k = 1:length(common)
                z = common(k);
                score = score + 1/log(max(2, degrees(z)));  % 避免log(1)=0
            end
            scores(i, j) = score;
            scores(j, i) = score;
        end
    end
end
  • 对共同邻居的度数取对数倒数加权

  • 稀有邻居贡献更大权重

(4) Resource Allocation (RA)

function scores = resourceAllocation(obj)
    scores = zeros(obj.nodeCount, obj.nodeCount);
    degrees = sum(obj.adjMatrix, 2);  % 节点度数

    for i = 1:obj.nodeCount
        neighbors_i = find(obj.adjMatrix(i, :));
        for j = i+1:obj.nodeCount
            neighbors_j = find(obj.adjMatrix(j, :));
            common = intersect(neighbors_i, neighbors_j);
            score = 0;
            for k = 1:length(common)
                z = common(k);
                score = score + 1/max(1, degrees(z));  % 避免除以零
            end
            scores(i, j) = score;
            scores(j, i) = score;
        end
    end
end
  • 模拟资源传递过程

  • 每个共同邻居贡献与其度数成反比

(5) Katz Index

function scores = katzIndex(obj, beta)
    I = eye(obj.nodeCount);
    A = obj.adjMatrix;
    scores = zeros(obj.nodeCount, obj.nodeCount);

    try
        S = A * inv(I - beta * A);
        scores = S;
    catch
        % 近似计算前几项
        S = zeros(obj.nodeCount, obj.nodeCount);
        term = A;
        for l = 1:10  % 计算前10项
            S = S + (beta^(l-1)) * term;
            term = term * A;
        end
        scores = S;
    end
end
  • 考虑所有路径的贡献

  • 短路径权重大于长路径

  • 使用矩阵求逆或级数展开实现

(6) Local Path Index (LP)

function scores = localPathIndex(obj, alpha)
    A = obj.adjMatrix;
    A2 = A^2;
    A3 = A2 * A;
    scores = A2 + alpha * A3;
end
  • 结合长度为2和3的路径

  • 参数α控制长路径的权重

2. 辅助功能

(1) 链接预测

function [sortedPairs, sortedScores] = predictLinks(obj, topK)
    % 获取上三角部分(避免重复和自环)
    triu_idx = triu(true(obj.nodeCount), 1);
    scoresVec = obj.similarityMat(triu_idx);
    pairs = [];
    for i = 1:obj.nodeCount
        for j = i+1:obj.nodeCount
            pairs = [pairs; i, j];
        end
    end

    % 按分数降序排序
    [sortedScores, sortIdx] = sort(scoresVec, 'descend');
    sortedPairs = pairs(sortIdx, :);

    % 返回前K个结果
    if nargin > 1 && topK > 0
        topK = min(topK, length(sortedScores));
        sortedPairs = sortedPairs(1:topK, :);
        sortedScores = sortedScores(1:topK);
    end
end
  • 返回相似度最高的节点对

  • 避免重复和自环

(2) 性能评估

function evaluate(obj, testEdges, nonEdges)
    % 获取所有边的分数
    allPairs = [testEdges; nonEdges];
    allScores = zeros(size(allPairs, 1), 1);

    for i = 1:size(allPairs, 1)
        u = allPairs(i, 1);
        v = allPairs(i, 2);
        allScores(i) = obj.similarityMat(u, v);
    end

    % 计算AUC
    posScores = allScores(1:size(testEdges, 1));
    negScores = allScores(size(testEdges, 1)+1:end);

    auc = 0;
    for i = 1:length(posScores)
        for j = 1:length(negScores)
            if posScores(i) > negScores(j)
                auc = auc + 1;
            elseif abs(posScores(i) - negScores(j)) < 1e-6
                auc = auc + 0.5;
            end
        end
    end
    auc = auc / (length(posScores) * length(negScores));

    fprintf('AUC: %.4f\n', auc);
    return auc;
end
  • 使用AUC评估预测性能

  • 比较正负样本的相似度分布

参考代码 经典链路预测的相似性算法 www.youwenfan.com/contentalh/97310.html

使用示例

1. 基本使用

% 创建邻接矩阵 (示例)
adjMatrix = [
    0 1 1 0 0;
    1 0 1 1 0;
    1 1 0 1 1;
    0 1 1 0 1;
    0 0 1 1 0
];

% 创建链路预测器
predictor = LinkPredictor(adjMatrix);

% 计算Common Neighbors相似度
predictor = predictor.computeSimilarity('CN');

% 预测前3个链接
[pairs, scores] = predictor.predictLinks(3);

% 可视化相似度矩阵
predictor.visualizeSimilarity();

2. 运行演示

% 运行内置演示
LinkPredictor.demo();

3. 完整工作流程

% 1. 加载或创建网络
load('network.mat'); % 邻接矩阵

% 2. 创建预测器
predictor = LinkPredictor(adjMatrix);

% 3. 计算多种相似度
algorithms = {
   'CN', 'JC', 'AA', 'RA', 'Katz', 'LP'};
results = containers.Map();

for i = 1:length(algorithms)
    algo = algorithms{
   i};
    if strcmp(algo, 'Katz')
        predictor = predictor.computeSimilarity(algo, 0.05);
    elseif strcmp(algo, 'LP')
        predictor = predictor.computeSimilarity(algo, 0.01);
    else
        predictor = predictor.computeSimilarity(algo);
    end
    results(algo) = predictor.similarityMat;
end

% 4. 预测和评估
testEdges = [1, 4; 2, 5]; % 测试集正样本
nonEdges = [1, 5; 3, 5];   % 测试集负样本

for i = 1:length(algorithms)
    algo = algorithms{
   i};
    predictor.similarityMat = results(algo);
    fprintf('Algorithm: %s\n', algo);
    predictor.evaluate(testEdges, nonEdges);
end

% 5. 可视化结果
figure;
for i = 1:length(algorithms)
    subplot(2, 3, i);
    imagesc(results(algorithms{
   i}));
    title(algorithms{
   i});
    colorbar;
end

算法特点比较

算法 计算复杂度 特点 适用场景
CN O(N²) 简单高效,只考虑共同邻居 大规模网络快速预测
JC O(N²) 标准化共同邻居比例 节点度数差异大的网络
AA O(N²·d) 稀有邻居权重高 社交网络,专家发现
RA O(N²·d) 资源传递模型 通信网络,传感器网络
Katz O(N³) 考虑所有路径 小规模高精度需求
LP O(N³) 结合短长路径 平衡效率与精度

扩展功能建议

  1. 动态网络支持

    function updateAdjacency(obj, newEdges)
        % 更新邻接矩阵
        for i = 1:size(newEdges, 1)
            u = newEdges(i, 1);
            v = newEdges(i, 2);
            obj.adjMatrix(u, v) = 1;
            obj.adjMatrix(v, u) = 1;
        end
        obj.nodeCount = size(obj.adjMatrix, 1);
    end
    
  2. 加权网络支持

    function scores = weightedCommonNeighbors(obj)
        % 加权共同邻居
        W = obj.adjMatrix; % 权重矩阵
        scores = W^2;
    end
    
  3. 社区感知算法

    function scores = communityAwareAA(obj, communities)
        % 社区感知的Adamic-Adar
        scores = zeros(obj.nodeCount, obj.nodeCount);
        degrees = sum(obj.adjMatrix, 2);
    
        for i = 1:obj.nodeCount
            for j = i+1:obj.nodeCount
                common = intersect(find(obj.adjMatrix(i,:)), find(obj.adjMatrix(j,:)));
                score = 0;
                for k = 1:length(common)
                    z = common(k);
                    % 同社区邻居权重更高
                    if communities(i) == communities(z)
                        weight = 1/log(max(2, degrees(z)));
                    else
                        weight = 0.5/log(max(2, degrees(z)));
                    end
                    score = score + weight;
                end
                scores(i, j) = score;
                scores(j, i) = score;
            end
        end
    end
    
  4. 并行计算优化

    function scores = parallelAdamicAdar(obj)
        % 并行计算Adamic-Adar
        scores = zeros(obj.nodeCount, obj.nodeCount);
        degrees = sum(obj.adjMatrix, 2);
    
        parfor i = 1:obj.nodeCount
            for j = i+1:obj.nodeCount
                neighbors_i = find(obj.adjMatrix(i, :));
                neighbors_j = find(obj.adjMatrix(j, :));
                common = intersect(neighbors_i, neighbors_j);
                score = 0;
                for k = 1:length(common)
                    z = common(k);
                    score = score + 1/log(max(2, degrees(z)));
                end
                scores(i, j) = score;
                scores(j, i) = score;
            end
        end
    end
    

应用场景

  1. 社交网络分析

    • 好友推荐

    • 社区发现

    • 影响力最大化

  2. 生物信息学

    • 蛋白质相互作用预测

    • 基因共表达网络分析

    • 药物靶点发现

  3. 推荐系统

    • 协同过滤

    • 项目关联预测

    • 用户兴趣预测

  4. 网络安全

    • 异常连接检测

    • 僵尸网络识别

    • 入侵检测

  5. 知识图谱

  • 实体链接预测

  • 关系补全

  • 知识推理

总结

本MATLAB实现提供了经典链路预测相似性算法的完整实现,具有以下特点:

  1. 算法齐全:包含6种经典相似性算法

  2. 接口统一:一致的API设计,便于比较和切换算法

  3. 高效实现:向量化操作和矩阵运算优化性能

  4. 可视化支持:提供多种可视化方法

  5. 评估功能:包含AUC等评估指标

  6. 扩展性强:易于添加新算法和功能

相关文章
|
安全 网络安全 定位技术
华为基础数通知识
了解基本的数通知识,成为更好的自己
|
3月前
|
关系型数据库 MySQL Serverless
MySQL 技巧:巧用窗口函数计算累计值
MySQL 技巧:巧用窗口函数计算累计值
|
3月前
|
安全 Linux 网络安全
阿里云轻量服务器+本地部署OpenClaw集成Skills全指南:从安装到自定义教程
OpenClaw(Clawdbot)的核心价值在于通过Skills(技能)扩展实现功能定制,结合阿里云轻量服务器的稳定运行与本地环境的灵活开发,可快速搭建适配业务场景的AI智能体。本文基于2026年最新稳定版,从阿里云轻量服务器与本地(MacOS/Linux/Windows11)部署OpenClaw,到Skills集成、自定义开发及避坑指南,全程提供可直接复制的代码命令,助力零基础用户快速完成技能扩展,打造高效智能助手。
644 5
|
6月前
|
机器学习/深度学习 人工智能 前端开发
构建AI智能体:七十、小树成林,聚沙成塔:随机森林与大模型的协同进化
随机森林是一种基于决策树的集成学习算法,通过构建多棵决策树并结合它们的预测结果来提高准确性和稳定性。其核心思想包括两个随机性:Bootstrap采样(每棵树使用不同的训练子集)和特征随机选择(每棵树分裂时只考虑部分特征)。这种方法能有效处理大规模高维数据,避免过拟合,并评估特征重要性。随机森林的超参数如树的数量、最大深度等可通过网格搜索优化。该算法兼具强大预测能力和工程化优势,是机器学习中的常用基础模型。
1275 165
|
存储 NoSQL Apache
Apache Cassandra 简介
Apache Cassandra 是一个开源的、分布式、无中心、弹性可扩展、高可用、容错、一致性可调、面向行的数据库,它基于 Amazon Dynamo 的分布式设计和 Google Bigtable 的数据模型,由 Facebook 创建,在一些最流行的网站中得到应用。
24656 2
|
3月前
|
存储 弹性计算 安全
阿里云服务器经济型e怎么样?性能参数、适用场景与最新收费标准与活动价格参考
阿里云经济型e实例是专为个人开发者、学生及小微企业设计的入门级云服务器,以亲民价格、可靠性能和丰富场景成为上云首选。该实例采用Intel® Xeon® Platinum处理器,支持多种处理器与内存配比,满足不同应用需求。其优势在于价格厚道、品质保障、供应充沛且选择简单。目前,2核2G3M带宽配置的活动价仅为99元/年,全系列规格享3.9折起优惠,且提供多种优惠券进一步降低购买成本。
401 5
|
3月前
|
存储 消息中间件 人工智能
2026阿里云省钱终极指南:领1728元代金券,个人和企业都可以领取阿里云优惠券!
2026阿里云AI焕新季开启!阿里云官方活动主会场:https://t.aliyun.com/U/FzmsXA 个人/企业用户均可领取总额1728元满减代金券(个人6张、企业6张),覆盖ECS、AI大模型、数据库、存储等数百款云产品,限新购/升级订单,1年内有效。速领→
258 4
|
人工智能 自然语言处理 监控
2025年如何通过SOP工具实现流程标准化?详解6大构建步骤及7款软件选型指南
标准作业程序(SOP)是企业核心知识资产与效率引擎,其科学构建和高效落地成为2025年数字化转型的关键。本文解析SOP全生命周期流程,探讨可视化技术对流程管理的赋能,并推荐7款智能工具。从概念到实施,SOP助力企业实现技术储备、效率提升与风险防控。通过动态协同、富媒体化及AI增强,企业可在高效与创新间取得平衡,构建可持续竞争优势。
4179 2
|
存储 安全 索引
回收站删除的照片怎么恢复?
在日常使用电脑的过程中,我们常常会不小心误删照片、文件或者其他重要数据,尤其是在清空回收站后,许多人会感到恐慌,担心数据永远丢失。不过,实际上,即使回收站中的照片被删除,也并非完全没有恢复的可能。本文将详细介绍几种常用的照片恢复方法,帮助大家在遇到类似问题时能够及时采取措施,尽可能地找回丢失的数据。
遍历Map的四种方法之map.entry详解
遍历Map的四种方法之map.entry详解

热门文章

最新文章