如何在networkx中列出特定的节点/边缘? [英] How to list specific node/edge in networkx?

查看:2552
本文介绍了如何在networkx中列出特定的节点/边缘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设一个在 networkx 图中的树状结构下面:

  n ----- n1 ---- n11 
| | ---- n12
| | ---- n13
| | ---- n131
| ---- n2 |
| | ----- n21 X
| | ----- n22 |
| | ---- n221
| ---- n3


n4 ------ n41
n5

  • 如何列出没有子节点的所有节点,在这里:n11,n12,n21,n41,n5

  • 节点,这里:n5和如何列出孤立边缘,不属于根n边缘,这里n4-n41,

  • 如何列出具有多于2个子节点的节点,这里n,n1

  • 如何处理n131,n221在遍历节点中是否存在边界,会发生无穷循环?

    谢谢。

    解决方案

    图表构造:

     >>> import networkx as nx 
    >>> G = nx.DiGraph()
    >>> G.add_edges_from([('n','n1'),('n','n2'),('n','n3')])
    >>> G.add_edges_from([''n4','n41'),('n1','n11'),('n1','n12'),('n1','n13')])
    >>> G.add_edges_from([('n2','n21'),('n2','n22')])
    >>> G.add_edges_from([('n13','n131'),('n22','n221')])
    >>> G.add_edges_from([('n131','n221'),('n221','n131')]
    >>> G.add_node('n5')




    1. 使用 out_degree 函数来查找所有包含子节点的节点:



      <如果v> 0]
      ['n13','n,pre $ >>> [k for k,v in G.out_degree()。​​iteritems ','n131','n1','n22','n2','n221','n4']
  • 请注意,n131和n221也会出现在这里,因为它们彼此都有边缘,如果需要的话,可以将它们过滤掉。 >没有孩子的所有节点:$ b​​
    $ b

     >>> [k for k,v in G.out_degree()。​​iteritems ()如果v == 0] 
    ['n12','n11','n3','n41','n21','n5']
    pre>
  • 所有孤儿节点,即度数为0的节点:

     >>> [k for k,v in G.degree()。​​iteritems()if v == 0] $为了得到所有孤儿的边缘,你可以得到这个列表。为了得到所有的孤儿边缘,你可以得到列表的图中的组件,过滤掉那些不包含 n ,然后只保留有边的那些:

     >>> [G.ed_undirected())中的组件的G.edges(组件)如果len(G.edges(component))> 0和'n'不在组件中] 
    [[('n4','n41')]]



  • $ b

     >>> $ b 
  • 超过2个子节点的节点: [k for k,v in G.out_degree()。​​iteritems()if v> 2]
    ['n','n1']


  • 树,你不会得到一个无限循环。 NetworkX有遍历算法,对此很有帮助。



  • Suppose one below tree-like structure in networkx graph:

    n-----n1----n11
     |     |----n12
     |     |----n13
     |           |----n131 
     |----n2             | 
     |     |-----n21     X
     |     |-----n22     |
     |            |----n221 
     |----n3
    
    
          n4------n41
          n5
    

    1. How to list all nodes with "subnode" and its depth, here: n,n1,n13,n2,n22,n4
    2. How to list all nodes without "subnode", here: n11,n12,n21,n41,n5
    3. How to list orphan node, here: n5 and how to list "orphan" edge, not belongs to root n edge, here n4-n41,
    4. How to list node with more than 2 "subnode", here n,n1
    5. How to deal with if n131,n221 have an edge exists in nodes traversal, will infinity loop happen?

    Thanks.

    解决方案

    Graph construction:

    >>> import networkx as nx
    >>> G = nx.DiGraph()
    >>> G.add_edges_from([('n', 'n1'), ('n', 'n2'), ('n', 'n3')])
    >>> G.add_edges_from([('n4', 'n41'), ('n1', 'n11'), ('n1', 'n12'), ('n1', 'n13')])
    >>> G.add_edges_from([('n2', 'n21'), ('n2', 'n22')])
    >>> G.add_edges_from([('n13', 'n131'), ('n22', 'n221')])
    >>> G.add_edges_from([('n131', 'n221'), ('n221', 'n131')]
    >>> G.add_node('n5')
    

    1. Using the out_degree function to find all the nodes with children:

      >>> [k for k,v in G.out_degree().iteritems() if v > 0]
      ['n13', 'n', 'n131', 'n1', 'n22', 'n2', 'n221', 'n4']
      

      Note that n131 and n221 also show up here since they both have an edge to each other. You could filter these out if you want.

    2. All nodes without children:

      >>> [k for k,v in G.out_degree().iteritems() if v == 0]
      ['n12', 'n11', 'n3', 'n41', 'n21', 'n5']
      

    3. All orphan nodes, i.e. nodes with degree 0:

      >>> [k for k,v in G.degree().iteritems() if v == 0]
      ['n5']
      

      To get all orphan "edges", you can get the list of components of the graph, filter out the ones that don't contain n and then keep only the ones that have edges:

      >>> [G.edges(component) for component in nx.connected_components(G.to_undirected()) if len(G.edges(component)) > 0 and 'n' not in component]
      [[('n4', 'n41')]]
      

    4. Nodes with more than 2 children:

      >>> [k for k,v in G.out_degree().iteritems() if v > 2]
      ['n', 'n1']
      

    5. If you traverse the tree, you will not get an infinite loop. NetworkX has traversal algorithms that are robust to this.

    这篇关于如何在networkx中列出特定的节点/边缘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆