简体   繁体   English

如何从无向图上删除一些边缘?

[英]How to remove some edges from an undirected graph?

Let say, I have an undirected graph with 7 nodes. 假设我有一个有7个节点的无向​​图。

g = {1:[2,3], 2:[1,4], 3:[1,4,6], 4:[2,3,5,7], 5:[4,6], 6:[3,5], 7:[4]} g = {1:[2,3],2:[1,4],3:[1,4,6],4:[2,3,5,7],5:[4,6],6 :[3,5],7:[4]}

I want to delete some edges from the graph, specifically, a node with more than degree 2. I have implemented the following code which can explore each node in the graph by DFS manner, checks their degree and remove edges based on the node's degree. 我想从图中删除一些边缘,尤其是删除一个度数大于2的节点。我实现了以下代码,该代码可以通过DFS方式探索图中的每个节点,检查它们的程度并根据该节点的程度删除边缘。

def Remove_edges(graph, start):
stack = [start]
visited = []
while stack:
    vertex = stack.pop()
    if vertex not in visited:
        visited.append(vertex)
        check_node_degree = node_degree(graph,vertex)

        if check_node_degree > 2:
            compute_edges = get_edges(graph, vertex)

            while len(compute_edges)!=2:
                compute_edges.pop()


        for neighbour in graph[vertex]:
            stack.append(neighbour)

graph.update(g.edges())
new_g = graph.copy()
return new_g

I got the result like this, new_g = {1: 3, 2: 4, 3: 6, 4: 7, 5: 6, 6: [3, 5], 7: [4]}. 我得到了这样的结果,new_g = {1:3,2:4,3:3,6,4:7,5:6,6:[3,5],7:[4]}。

Can any one help me to solve the problem? 谁能帮助我解决问题?

It seems like you want to create a graph with maximum degree of 1 (ie, you are building a matching). 似乎您想创建最大度数为1的图形(即,您正在构建匹配项)。 If this is not the problem you are trying to solve, then you need to be more clear with your question. 如果这不是您要解决的问题,那么您需要更清楚地了解问题。

Additionally, you call custom functions which you do not include in the question. 此外,您调用不包含在问题中的自定义函数。 I believe that your error is coming from one such function, but I cannot be sure: 我相信您的错误是由这样的功能之一引起的,但是我不确定:

graph.update(g.edges())

After the while loop, any nodes that were visited will have one neighbor (its last neighbor); 在while循环之后,所有被访问的节点将有一个邻居(它的最后一个邻居)。 specifically, your graph should be: new_g = {1: 3, 2: 4, 3: 6, 4: 7, 5: 6, 6: 5, 7: 4}. 具体来说,您的图表应为:new_g = {1:3,2:4,4,3:6,6,4:7,5:6,6:5,7:4}。

However, you have an undirected graph, so you call graph.update() to repair broken edges, which restrings 6 to 3 (since 3 has an edge to 6). 但是,您有一个无向图,因此您调用graph.update()来修复折断的边缘,该边缘将字符串重新排列为6到3(因为3的边为6)。 (Again, you did not include the code for graph.update(), so I am making an assumption that it is repairing broken edges.) You need to call a function that does the opposite: instead of making directed edges undirected, they should be removed. (同样,您没有包括graph.update()的代码,因此我假设它正在修复折断的边缘。)您需要调用一个相反的函数:而不是使有向边变为无向,它们应该被删除。


Furthermore, might I suggest that you change your algorithm? 此外,我是否建议您更改算法? I see two problems. 我看到两个问题。 First, if the graph is well connected you will visit nodes multiple times, which is why you need to compare against the visited array. 首先,如果图形连接良好,您将多次访问节点,这就是为什么需要与访问的数组进行比较的原因。 On the other hand, if the graph is not one connected component, you will not visit every node. 另一方面,如果图形不是一个连接的组件,则不会访问每个节点。 Both of these problems can be solved by looping over the nodes instead of doing a DFS; 这两个问题都可以通过遍历节点而不是执行DFS来解决。 this will save you time and resources and improve the correctness of your algorithm. 这将节省您的时间和资源,并提高算法的正确性。

Also, if you are interested in maximizing the number of edges remaining in your graph, I suggest you look at http://en.wikipedia.org/wiki/Matching_%28graph_theory%29 另外,如果您有兴趣最大限度地增加图中剩余的边数,建议您访问http://en.wikipedia.org/wiki/Matching_%28graph_theory%29

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

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