小编sjh*_*e14的帖子

为什么 NetworkX find_cycle 函数不产生涉及指定源顶点的输出?

在 NetworkX 中采用以下无向图对象。

G_3 = nx.Graph()
G_3.add_nodes_from([1,2,3,4,5])
G_3.add_weighted_edges_from([(1,2,1), (2,1,1), (2,4,1), (4,2,1), (2,3,1), (3,2,1), (3,4,1), (4,3,1), (1,4,1), (4,1,1),(5,2,1),(5,1,1)])
Run Code Online (Sandbox Code Playgroud)

该图看起来像这样。有三个基本循环,我想使用这些函数nx.cycle_basisnx.find_cycle获取组成每个循环的节点和边的列表。

在此输入图像描述

当我使用 时nx.cycle_basis,我得到以下结果:

cycles_3 = [c for c in nx.cycle_basis(G_3)]
print(cycles_3)
>>> [[2, 5, 1], [2, 4, 1], [2, 3, 4]] 
Run Code Online (Sandbox Code Playgroud)

这是正确的输出。然而,当我使用该nx.find_cycle函数时,我最终得到以下输出:

num_nodes = len(G_3.nodes())
edges_in_cycle = {}

for i in range(1,num_nodes+1):
  edges_in_cycle[i] = nx.find_cycle(G_3,i)`

edges_in_cycle
>>>
{1: [(2, 4), (4, 3), (3, 2)],
 2: [(2, 1), (1, 4), (4, 2)],
 3: [(2, 1), (1, …
Run Code Online (Sandbox Code Playgroud)

python graph-theory networkx python-3.x jupyter

7
推荐指数
0
解决办法
409
查看次数

矩阵与标量数组的 Numpy 乘法,无需 for 循环

假设我有以下代码:

# define a 3x3 array
A = np.array([[-2, 1, 1], [1,-2,1], [1,1,-2]])
# define a 1D array of scalars to multiply A with
test = np.arange(10)

for i in range(len(test)):
    matrix = scipy.linalg.expm(A*test[i])
Run Code Online (Sandbox Code Playgroud)

我想看看是否有一种方法可以在不使用 for 循环的情况下执行此乘法。我不想使用矩阵乘法将两个数组相乘。我将测试数组视为一组标量值,我想将它们与 A 逐一相乘。必须有某种偷偷摸摸的麻木的方法来做到这一点。有任何想法吗?

python numpy scipy python-3.x jupyter-notebook

3
推荐指数
1
解决办法
1327
查看次数