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

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

matlab 复制代码
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 = (A²)_{xy} + α(A³)_{xy}
            A = obj.adjMatrix;
            A2 = A^2;
            A3 = A2 * A;
            scores = A2 + alpha * A3;
        end
        
        function [sortedPairs, sortedScores] = predictLinks(obj, topK)
            % 预测最可能的链接
            % 输入: topK - 返回前K个预测结果
            % 输出: 
            %   sortedPairs - 排序后的节点对 (N×2矩阵)
            %   sortedScores - 对应的相似度分数 (N×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 - 测试集中的正样本边 (M×2矩阵)
            %   nonEdges - 测试集中的负样本边 (N×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)
matlab 复制代码
function scores = commonNeighbors(obj)
    scores = obj.adjMatrix^2;  % 矩阵乘法计算共同邻居数
end
  • 计算每对节点的共同邻居数量

  • 使用矩阵乘法高效实现

(2) Jaccard Coefficient (JC)
matlab 复制代码
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)
matlab 复制代码
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)
matlab 复制代码
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
matlab 复制代码
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)
matlab 复制代码
function scores = localPathIndex(obj, alpha)
    A = obj.adjMatrix;
    A2 = A^2;
    A3 = A2 * A;
    scores = A2 + alpha * A3;
end
  • 结合长度为2和3的路径

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

2. 辅助功能

(1) 链接预测
matlab 复制代码
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) 性能评估
matlab 复制代码
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/contentcss/97310.html

使用示例

1. 基本使用

matlab 复制代码
% 创建邻接矩阵 (示例)
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. 运行演示

matlab 复制代码
% 运行内置演示
LinkPredictor.demo();

3. 完整工作流程

matlab 复制代码
% 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. 动态网络支持

    matlab 复制代码
    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. 加权网络支持

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

    matlab 复制代码
    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. 并行计算优化

    matlab 复制代码
    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. 扩展性强:易于添加新算法和功能

相关推荐
喵喵蒻葉睦2 小时前
力扣 hot100 最小覆盖子串 哈希表 滑动窗口 Java 题解
java·算法·leetcode·哈希算法·散列表·滑动窗口
oem1102 小时前
C++与Docker集成开发
开发语言·c++·算法
自信150413057592 小时前
数据结构初阶之二叉树收尾
c语言·数据结构·算法
季远迩2 小时前
240. 搜索二维矩阵 II(中等)
人工智能·算法·矩阵
xushichao19892 小时前
C++中的享元模式
开发语言·c++·算法
tankeven2 小时前
HJ133 隐匿社交网络
c++·算法
m0_716667072 小时前
C++中的访问者模式高级应用
开发语言·c++·算法
Oueii2 小时前
C++中的访问者模式变体
开发语言·c++·算法
一叶落4382 小时前
【LeetCode 12】整数转罗马数字(C语言)| 贪心算法详解
c语言·数据结构·c++·算法·leetcode·贪心算法