简体   繁体   English

Python:检测networkX中的周期

[英]python: detecting a cycle in networkX

As the title implies, I'm trying to write a function that will calculate the number of cycles any inputted node is part of. 顾名思义,我正在尝试编写一个函数,该函数将计算任何输入节点所属的循环数。 I found a helpful video which explains the theory behind an algorithm to find cycles, but I'm having trouble understanding how to implement it using networkX rather than the data structure that site is using. 我找到了一个有用的视频 ,它解释了找到循环的算法背后的理论,但是我在理解如何使用networkX而不是站点正在使用的数据结构实现方面遇到了麻烦。 I couldn't understand the white/grey/etc set concept as well to traverse the network and find cycles. 我也无法理解白色/灰色/等设置概念来遍历网络并查找周期。

My function parameters/structure: 我的函数参数/结构:

def feedback_loop_counter(G, node):
    c = 0
    calculate all cycles in the network
    for every cycle node is in, increment c by 1
    return c

The network has input and output nodes too, and I'm unclear how those play into calculating cycles 网络也有输入和输出节点,我不清楚它们如何影响计算周期

This is my input network: 这是我的输入网络:

import networkx as nx
import matplotlib.pyplot as plt
G=nx.DiGraph()
molecules = ["CD40L", "CD40", "NF-kB", "XBP1", "Pax5", "Bach2", "Irf4", "IL-4", "IL-4R", "STAT6", "AID", "Blimp1", "Bcl6", "ERK", "BCR", "STAT3", "Ag", "STAT5", "IL-21R", "IL-21", "IL-2", "IL-2R"]
Bcl6 = [("Bcl6", "Bcl6"), ("Bcl6", "Blimp1"), ("Bcl6", "Irf4")]
STAT5 = [("STAT5", "Bcl6")]
IL_2R = [("IL-2R", "STAT5")]
IL_2 = [("IL-22", "IL-2R")]
BCR = [("BCR", "ERK")]
Ag = [("Ag", "BCR")]
CD40L = [("CD40L", "CD40")]
CD40 = [("CD40", "NF-B")]
NF_B = [("NF-B", "Irf4"), ("NF-B", "AID")]
Irf4 = [("Irf4", "Bcl6"), ("Irf4", "Pax5"), ("Irf4", "Irf4"), ("Irf4", "Blimp1")]
ERK = [("ERK", "Bcl6"), ("ERK", "Blimp1"), ("ERK", "Pax5")]
STAT3 = [("STAT3", "Blimp1")]
IL_21 = [("IL-21", "IL-21R")]
IL_21R = [("IL-21R", "STAT3")]
IL_4R = [("IL-4R", "STAT6")]
STAT6 = [("STAT6", "AID"), ("STAT6", "Bcl6")]
Bach2 = [("Bach2", "Blimp1")]
IL_4 = [("IL-4", "IL-4R")]
Blimp1 = [("Blimp1", "Bcl6"), ("Blimp1", "Bach2"), ("Blimp1", "Pax5"), ("Blimp1", "AID"), ("Blimp1", "Irf4")]
Pax5 = [("Pax5", "Pax5"), ("Pax5", "AID"), ("Pax5", "Bcl6"), ("Pax5", "Bach2"), ("Pax5", "XBP1"), ("Pax5", "ERK"), ("Pax5", "Blimp1")]
edges = Bcl6 + STAT5 + IL_2R + IL_2 + BCR + Ag + CD40L + CD40 + NF_B + Irf4 + 
ERK + STAT3 + IL_21 + IL_21R + IL_4R + STAT6 + Bach2 + IL_4 + Blimp1 + Pax5
G.add_nodes_from(molecules)
G.add_edges_from(edges)
sources = ["Ag", "CD40L", "IL-2", "IL-21", "IL-4"]
targets = ["XBP1", "AID"]

The idea to find cycles is to do a Depth-first search and while you do it, remember which nodes you already saw and the path to them. 查找周期的想法是进行深度优先搜索,并且在进行搜索时,请记住已经看到的节点以及它们的路径。 If you happen to visit a node you already saw, then there is a cycle, and you can find it by concatenating paths. 如果您碰巧访问了已经看到的节点,则存在一个循环,您可以通过串联路径找到它。

Try writing some code to do that, and open a new question with that code if you get stuck 尝试编写一些代码来执行此操作,如果遇到问题,请对该代码打开一个新问题

I'm going to write my answer under the assumption you are interested in "simple cycles", that is, cycles whose only repeated node is the first/last node. 我将在您对“简单周期”(即唯一重复的节点是第一个/最后一个节点)感兴趣的假设下编写答案。

Take those nodes that have edges to a node u (the "input nodes"). 取那些具有到节点u边缘的节点(“输入节点”)。 Then use the networkx command all_simple_paths to find all simple paths from u to each of the input nodes. 然后使用networkx命令all_simple_paths查找从u到每个输入节点的所有简单路径。 Each of these becomes a simple cycle. 这些每个都变成一个简单的周期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM