在networkx中按规则查找不合适的节点?

2024-04-28 08:51:09 发布

您现在位置:Python中文网/ 问答频道 /正文

所有人

考虑下面的示例“树状”图。在

在纵向上,它是一个基于节点“0”的层次结构。 从水平上讲,它是一个从1级开始的基于组的结构,组意味着从一个根节点继承的节点

    '''                         
                              +---+       
                              | 0 |                                    Level 0
                              +---+       
                                |
                 +--------------+---------------+         
                 |              |               |           
               +---+          +---+           +---+      
               | 1 |          | 2 |           | 3 |                    Level 1      
               +---+          +---+           +---+      
     +-----+----+        +-----+-----+        +|---+-----+            
     |     |     |        |     |     |        |    |     |           
   +---+ +---+ +---+    +---+ +---+ +---+   +---+ +---+ +---+  
   |11 | |12 | |13 |    |21 | |22 | |23 |   |31 | |32 | |33 |          Level 2 
   +---+ +---+ +---+    +---+ +---+ +---+   +---+ +---+ +---+  
     |     |   / |    /   |      |            |                 
     |     | /   | /      |      |            |  
     |   +---+ +---+    +---+ +---+           |
     |   |121|-|131|    |211| |221|           |                        Level 3                                 
     |   +---+ +---+    +---+ +---+           |
     |           |--------|------|            |
     |-----------|----------------------------|     


 |     Group 0       |       group 1     |      group 2       |
'''

在Networkx中创建:

^{pr2}$

问题:

如何在图中找出节点和边,以下面的节点为例:

  1. “121”,在同一组中有多个链接指向更高级别?(节点边缘类型为“不确定”,可能在度或度外,或两者兼而有之,在以下问题中相同)

  2. “131”,有多个链接到另一个组的高级别节点?

  3. “131”,链接到同一组中的同一级别节点

  4. “131”,具有指向同一级别节点但在其他组中的链接

  5. “21”,带有指向不同组中较低级别节点的链接

新的“图形”和尝试获得示例代码图如何使用networkx深入挖掘。在

非常感谢。在


Tags: networkx示例节点层次结构链接group水平级别
2条回答

像这样的事情也许会奏效。它使用节点的长度作为级别(必须在代码中注释掉0级节点才能正常工作),节点字符串的第一个元素作为组。我认为这正是您对数据结构的预期。在

# create it in networkx
import networkx as nx
G = nx.DiGraph()
#G.add_edges_from([('0', '1'), ('0', '2'), ('0', '3')])
G.add_edges_from([('1', '11'), ('1', '12'), ('1', '13')])
G.add_edges_from([('2', '21'), ('2', '22'), ('2', '23')])
G.add_edges_from([('3', '31'), ('3', '32'), ('3', '33')])
#
G.add_edges_from([('12', '121'), ('13', '131')])
G.add_edges_from([('12', '121'), ('13', '131')])
G.add_edges_from([('21', '211'), ('22', '221')])
#
G.add_edges_from([('13', '121')])
G.add_edges_from([('21', '131')])
G.add_edges_from([('131', '211')])
G.add_edges_from([('131', '221')])

#
G.add_edges_from([('121', '13')])            # node may not with "in_degree" link only
G.add_edges_from([('131', '21')])            # ditto

#
G.add_edges_from([('131', '31')])
G.add_edges_from([('131', '11')])
G.add_edges_from([('11', '131')])
G.add_edges_from([('121', '131')])

# "121", with more than one link to higher level in same group? (node edge type "unsure", may in_degree or out_degree or both, same in following question)

print "more than one link to higher level in same group"
for node in G:
    l = len(node)-1 # higher level
    others = [n for n in G.successors(node)+G.predecessors(node)
              if len(n)==l and n[0]==node[0]]
    if len(others) > 1:
        print node

# "131", with more than one link to higner level nodes to other group?
print "more than one link to higher level in same group"
for node in G:
    l = len(node)-1 # higher level
    others = [n for n in G.successors(node)+G.predecessors(node)
              if len(n)==l and n[0]!=node[0]]
    if len(others) > 1:
        print node


# "131", with links to same level nodes in same group
print "same level, same group"
for u,v in G.edges():
    if len(u) == len(v):
        if u[0] == v[0]:
            print u

# "131", with links to same level nodes but in other group
print "same level, other group"
for u,v in G.edges():
    if len(u) == len(v):
        if u[0] != v[0]:
            print u


# "21", with links to lower level nodes in different group
print "same level, other group"
for u,v in G.edges():
    if len(u) == len(v)-1:
        if u[0] != v[0]:
            print u

首先,您应该以某种方式定义节点所在的组或级别,因为它没有在图istelf中定义,因为附加的边破坏了树结构。 我只是按照您的命名模式编写了以下helper函数,将给定的名称转换为级别/组:

def get_group(node):
    if node == '0':
        return 1
    return int(node[0])-1

def get_level(node):
    if node == '0':
        return 0
    return len(node)

def equal_group(a,b):
    return get_group(a) == get_group(b)

def lower_level(a,b):
    return get_level(a) < get_level(b)

def equal_level(a,b):
    return get_level(a) == get_level(b)

然后,可以根据您的规范筛选节点:

^{pr2}$

结果如下:

>>>Q1: ['131', '121']
>>>Q2: ['131', '121']
>>>Q3: ['121']
>>>Q4: ['131', '121']
>>>Q5: ['21', '131', '0']

通常,您可以找到不需要的边,如下所示:

def is_bad_edge(edge):
    a, b = edge
    if not equal_group(a, b):
        return True
    if not lower_level(a, b):
        return True
    return False
bad_edges = filter(is_bad_edge, G.edges_iter())
print 'Bad edges:', bad_edges 

因此:

>>>Bad edges: [('21', '131'), ('131', '11'), ('131', '31'), ('131', '21'), ('131', '221'), ('131', '211'), ('121', '13'), ('121', '131'), ('0', '1'), ('0', '3')]

如您所见,源于0的边在那里,因为它被分类为组1,但是节点1和节点3不是。根据对节点的分类方式,0可以调整函数以包含或排除根节点。在

相关问题 更多 >