简体   繁体   English

如何检查图是否为无向图?

[英]How to check whether a graph is an undirected graph?

Currently, I am creating a function to check whether a graph is un-directed.目前,我正在创建一个 function 来检查图形是否是无向的。 The way, my graphs are stored are in this way.方式,我的图表是这样存储的。 This is a un-directed graph of 3 nodes, 1, 2, 3.这是 3 个节点 1、2、3 的无向图。

graph = {1: {2:{...}, 3:{...}}, 2: {1:{...}, 3:{...}}, 3: {1:{...}, 2:{...}}}

the {...} represents alternating layers of the dictionaries for the connections in each of the nodes. {...} 表示每个节点中连接的字典的交替层。 It is infinitely recurring, since it is nested in each other.它是无限循环的,因为它是相互嵌套的。

More details about graph:有关图表的更多详细信息:

  1. the keys refer to the node, and it's values refer to a dict, with the nodes that are connected to the key.键是指节点,它的值是指一个字典,节点连接到键。
  2. Example: two nodes (1, 2) with an undirected edge: graph = {1: {2: {1: {...}}}, 2: {1: {2: {...}}}}示例:具有无向边的两个节点 (1, 2): graph = {1: {2: {1: {...}}}, 2: {1: {2: {...}}}}
  3. Example2: two nodes (1, 2) with a directed edge from 1 to 2: graph = {1: {2: {}}, 2: {}}示例 2:两个节点 (1, 2) 的有向边从 1 到 2: graph = {1: {2: {}}, 2: {}}

My current way of figuring out whether a graph is un-directed or not, is by checking whether the number of edges in the graph is equal to (n*(n-1))/2 (n represents the number of nodes), but this cannot differentiate between 15 directed edges and 15 un-directed edges, so what other way can i confirm that my graph is undirected?我目前确定图是否无向的方法是检查图中的边数是否等于 (n*(n-1))/2(n 表示节点数),但这不能区分 15 条有向边和 15 条无向边,那么我还有什么其他方法可以确认我的图是无向的?

First off, I think you're abusing terminology by calling a graph with edges in both directions "undirected".首先,我认为您通过调用具有两个方向的边的图“无向”来滥用术语。 In a real undirected graph, there is no notion of direction to an edge, which often means you don't need redundant direction information in the graph's representation in a computer program.在真正的无向图中,没有边的方向概念,这通常意味着您不需要计算机程序中图形表示中的冗余方向信息。 What you have is a directed graph, and you want to see if it could be represented by an undirected graph, even though you're not doing so yet.你有一个有向图,你想看看它是否可以用无向图表示,即使你还没有这样做。

I'm not sure there's any easier way to do this than by checking every edge in the graph to see if the reversed edge also exists.我不确定是否有比检查图中的每条边以查看反向边是否也存在更简单的方法来做到这一点。 This is pretty easy with your graph structure, just loop over the verticies and check if there is a returning edge for every outgoing edge:这对于您的图形结构非常简单,只需遍历顶点并检查每个传出边是否有返回边:

def undirected_compatible(graph):
    for src, edges in graph.items():          # edges is dict of outgoing edges from src
        for dst, dst_edges in edges.items():  # dst_edges is dict of outgoing edges from dst
            if src not in dst_edges:
                return False
    return True

I'd note that a more typical way of describing a graph like yours would be to omit the nested dictionaries and just give a list of destinations for the edges.我会注意到,描述像您这样的图形的更典型方法是省略嵌套字典,而只给出边缘的目的地列表。 A fully connected 3-node graph would be:一个完全连接的 3 节点图将是:

{1: [2, 3], 2: [1, 3], 3: [1, 2]}

You can get the same information from this graph as your current one, you'd just need an extra indirection to look up the destination node in the top level graph dict, rather than having it be the value of the corresponding key in the edge container already.您可以从此图表中获得与当前图表相同的信息,您只需要额外的间接来查找顶级图表字典中的目标节点,而不是让它成为边缘容器中相应键的值已经。 A version of my function above for this more conventional structure would be:对于这种更传统的结构,我上面的 function 的一个版本是:

def undirected_compatible(graph):
    for src, edges in graph.items():
        for dst in edges:
            if src not in graph[dst]:
                return False
    return True

The not in test may make this slower for large graphs, since searching a list for an item is less asymptotically efficient than checking if a key is in a dictionary. not in test 可能会使大图的速度变慢,因为在列表中搜索项目的渐近效率低于检查键是否在字典中。 If you needed the higher performance, you could use sets instead of lists, to speed up the membership tests.如果您需要更高的性能,您可以使用集合而不是列表来加快成员资格测试。

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

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