You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Numpy生成可变节点数特定邻接矩阵的简便方法

通用生成特定结构邻接矩阵的方法

需求说明

我需要生成如下结构的邻接矩阵:

节点数 = 3

A   B   C   AB  AC  BC
A   0   1   1   0   0   1
B   1   0   1   0   1   0
C   1   1   0   1   0   0
AB  0   0   1   0   0   0
AC  0   1   0   0   0   0
BC  1   0   0   0   0   0

针对3节点的情况,我可以用这段代码实现:

out = np.block([
    [1 - np.eye(3), np.eye(3)       ],
    [    np.eye(3), np.zeros((3, 3))]
]).astype(int)

但这段代码无法适配不同节点数的场景,比如节点数为5时的邻接矩阵如下:

节点数 = 5

A   B   C   D   E   AB  AC  AD  AE  BC  BD  BE  CD  CE  DE
A   0   1   1   1   1   0   0   0   0   1   1   1   1   1   1
B   1   0   1   1   1   0   1   1   1   0   0   0   1   1   1
C   1   1   0   1   1   1   0   1   1   0   1   1   0   0   1
D   1   1   1   0   1   1   1   0   1   1   0   1   0   1   0
E   1   1   1   1   0   1   1   1   0   1   1   0   1   0   0
AB  0   0   1   1   1   0   0   0   0   0   0   0   0   0   0
AC  0   1   0   1   1   0   0   0   0   0   0   0   0   0   0
AD  0   1   1   0   1   0   0   0   0   0   0   0   0   0   0
AE  0   1   1   1   0   0   0   0   0   0   0   0   0   0   0
BC  1   0   0   1   1   0   0   0   0   0   0   0   0   0   0
BD  1   0   1   0   1   0   0   0   0   0   0   0   0   0   0
BE  1   0   1   1   0   0   0   0   0   0   0   0   0   0   0
CD  1   1   0   0   1   0   0   0   0   0   0   0   0   0   0
CE  1   1   0   1   0   0   0   0   0   0   0   0   0   0   0
DE  1   1   1   0   0   0   0   0   0   0   0   0   0   0   0

请问是否存在简单易用的通用方法来实现这类邻接矩阵?


通用实现方案

规律拆解

先明确这类邻接矩阵的四个区域规则:

  • 左上角(单节点→单节点):主对角线为0(自身无连接),其余位置全为1(任意两个不同单节点相连)
  • 右上角(单节点→组合节点):单节点不在组合节点的元素中时为1,否则为0(比如单节点A和组合节点BC相连,和AB不相连)
  • 左下角(组合节点→单节点):是右上角的转置矩阵,规则和右上角一致
  • 右下角(组合节点→组合节点):全为0(任意两个组合节点之间无连接)

通用代码

import numpy as np
from itertools import combinations

def build_adjacency_matrix(n):
    # 计算单节点和组合节点的数量
    single_count = n
    pair_count = n * (n - 1) // 2
    total_size = single_count + pair_count
    
    # 初始化全0邻接矩阵
    adj_matrix = np.zeros((total_size, total_size), dtype=int)
    
    # 填充单节点间的连接区域
    adj_matrix[:single_count, :single_count] = 1 - np.eye(single_count, dtype=int)
    
    # 生成所有两两节点组合的索引对
    pair_indices = list(combinations(range(single_count), 2))
    
    # 填充单节点到组合节点的区域
    for single_idx in range(single_count):
        for pair_idx, (a, b) in enumerate(pair_indices):
            if single_idx != a and single_idx != b:
                adj_matrix[single_idx, single_count + pair_idx] = 1
    
    # 填充组合节点到单节点的区域(转置右上角区域)
    adj_matrix[single_count:, :single_count] = adj_matrix[:single_count, single_count:].T
    
    return adj_matrix, pair_indices

# 示例:生成3节点邻接矩阵
adj_3, pairs_3 = build_adjacency_matrix(3)
print("3节点邻接矩阵:")
print(adj_3)

# 示例:生成5节点邻接矩阵
adj_5, pairs_5 = build_adjacency_matrix(5)
print("\n5节点邻接矩阵:")
print(adj_5)

代码说明

  • 使用combinations生成任意n的两两节点组合,确保适配所有节点数场景
  • 通过索引判断单节点是否属于组合节点,逻辑直接对应邻接矩阵规则
  • 矩阵区域分块处理,结构清晰,便于维护和修改

内容的提问来源于stack exchange,提问作者Ahmad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 02:30:59