简体   繁体   English

试图在Python中找到断开连接的图形

[英]Trying to find disconnected Graphs in Python

I am looking to find disconnected subgraphs in Python 我希望在Python中找到断开连接的子图

Take this graph as an example: Index 0 represents node A , 1 represent B ... etc -1 is just a place holder because this is a simple graph having no edges connecting itself. 以此图为例:索引0代表节点A,1代表B ......等-1只是一个占位符,因为这是一个没有边连接自身的简单图。

The 5 represents the weight of the edges ( will have graphs in the future with different weights ) 5表示边缘的权重(将来会有不同权重的图表)

[[-1  5  5  0  0]
 [ 5 -1  0  0  0]
 [ 5  0 -1  0  5]
 [ 0  0  0 -1  0]
 [ 0  0  5  0 -1]]

To look for disconnected graphs, I first created a True / False on if I have visited the edge or not. 要查找断开连接的图形,我首先创建了一个True / False,如果我已经访问过边缘。 ( 0 and -1 are as default, True) which looks like this: (0和-1默认为True),如下所示:

[[ True False False  True  True]
 [False  True  True  True  True]
 [False  True  True  True False]
 [ True  True  True  True  True]
 [ True  True False  True  True]]

My approach to this problem is to start at any edge with a false value and start at the node represented by the rows, and go through all the possible edges connecting that node and its children's node, and so on. 我解决这个问题的方法是从具有错误值的任何边开始,并从行所代表的节点开始,并遍历连接该节点及其子节点的所有可能边,依此类推。 As I traverse along those vertices, I will mark the Boolean Matrix True as I have "visited" that edge. 当我遍历这些顶点时,我将布尔矩阵标记为True,因为我已“访问”该边缘。 Once I know that I have "visited" all the edges, I know that I will have a connected subgraph. 一旦我知道我已“访问”了所有边缘,我知道我将有一个连接的子图。 Then I will look for another "False" in my True/False matrix and start from there looking for another disconnected graph, and continue until I fill in all the elements as True. 然后我将在我的True / False矩阵中寻找另一个“False”并从那里开始寻找另一个断开连接的图形,并继续直到我将所有元素填充为True。

However, I am stuck on traversing through the edges 但是,我坚持穿越边缘

Here is my algorithm: 这是我的算法:

reducedMatrix = np.load(reducedweightmatrix)
print(reducedMatrix)
boolarray = (reducedMatrix == 0) | (reducedMatrix == -1)
print(boolarray)


def traverse(iy,visited_nodes,looped):
    #Only move to next index if there is no loop
    # already visited node?
    print("I am currently at: "+ str(iy))
    print(visited_nodes)
    print(looped)
    print("-----------------\n")
    if (iy in visited_nodes):
        looped = True
    if(not looped):
        print("I enterred the loop")
        children = []
        #Find connected "children" vertices
        for ix,weight in enumerate(reducedMatrix[iy]):
            if weight != 0 and weight != -1:
                #Collect the index with connected vertices
                children.append(ix)
                #I AM GOING TO VISIT  THESE VERTICES
                boolarray[iy,ix] = True

        print(children)
        visited_nodes.append(iy) 
        for child,children in enumerate(children):
            print(child)
            traverse(child,visited_nodes,looped)
    return visited_nodes

print(traverse(0,[],False))

Using the example shown above, here are the log messages: 使用上面显示的示例,以下是日志消息:

[[-1  5  5  0  0]
 [ 5 -1  0  0  0]
 [ 5  0 -1  0  5]
 [ 0  0  0 -1  0]
 [ 0  0  5  0 -1]]
[[ True False False  True  True]
 [False  True  True  True  True]
 [False  True  True  True False]
 [ True  True  True  True  True]
 [ True  True False  True  True]]
I am currently at: 0
[]
False
-----------------

False
I enterred the loop
[1, 2]
0
I am currently at: 0
[0]
False
-----------------

True
1
I am currently at: 1
[0]
False
-----------------

False
I enterred the loop
[0]
0
I am currently at: 0
[0, 1]
False
-----------------

True
[0, 1]

According to the example above, the algorithm should be showing the following: [0,1,2,4] Please point out to me where I went wrong with the recursion 根据上面的例子,算法应该显示如下:[0,1,2,4]请指出我在哪里出错了递归

I don't quite understand this piece of code, 我不太明白这段代码,

for child,children in enumerate(children):
    print(child)
    traverse(child,visited_nodes,looped)

Just change it to, 只需将其更改为

for child in children:
    print(child)
    traverse(child,visited_nodes,looped)

The answer is right over there. 答案就在那边。

What you want is to visit every child in the children, not the index of children. 你想要的是访问孩子的每个孩子,而不是孩子的索引。 What you saved in children previously is the index number. 您之前在children保存的是索引编号。 You definite do not want to find the index of index number. 你肯定不想找到索引号的索引。

Edit : If you are iterating over an iterable, please don't name your value the same as the iterable itself. 编辑 :如果您正在迭代迭代,请不要将您的值命名为与iterable本身相同。 Guess what happens below? 猜猜下面会发生什么?

children = list(range(10))  
for child,children in enumerate(children):
    pass
print(children)

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

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