简体   繁体   English

测试图形是Edgelist文件中的有向图还是无向图

[英]Testing if a graph is Directed or Undirected Graph from Edgelist file

I have a text document that is an edge list file. 我有一个文本文件,它是边缘列表文件。 I know how to read the file (using Canopy Enthought), but I don't know how to get the information about the graph that I want. 我知道如何读取文件(使用Canopy Enthought),但是我不知道如何获取有关所需图形的信息。

Main question : Is there a way to detect whether this graph (created from the edge list file) is directed or undirected using networkx commands? 主要问题 :是否可以使用networkx命令检测此图(从边缘列表文件创建)是有向的还是无向的? Or just if it is weighted or unweighed? 还是只是称重或称重?

I believe that you have to specify the type of the graph before using the edge list file. 我相信您必须在使用边缘列表文件之前指定图的类型。 Because the edge list file is simply composed of tuples containing nodes to be connected without saying how they are connected. 因为边缘列表文件仅由包含要连接的节点的元组组成,而无需说明它们如何连接。 Thus, for instance if you create a graph G = nx.Graph() , then if the node pairs in the file are repeated, there will still be one edge between them and the order of the nodes does not matter; 因此,例如,如果您创建一个图形G = nx.Graph() ,那么如果文件中的节点对重复,则它们之间仍然会有一条边,并且节点的顺序无关紧要; ((node1,node2) is equivalent to (node2,node1)). ((node1,node2)等效于(node2,node1))。 While if you created the graph as G = nx.DiGraph() the order of nodes makes a difference. 如果您以G = nx.DiGraph()创建图,则节点的顺序会有所不同。 Also, specifying G = nx.MultiGraph() more than one edge will exist in case of repetition. 同样,在重复的情况下,指定G = nx.MultiGraph()会存在多个边。 G = nx.MultiDiGraph() will have a different result when reading the edge list file. 读取边缘列表文件时, G = nx.MultiDiGraph()将具有不同的结果。 So, check the the graph types documentation to know which type you need to have. 因此,请查看图形类型文档以了解所需的类型。

To check if the graph is directed you can use nx.is_directed(G) , you can find the documentation here . 要检查图形是否有向,可以使用 nx.is_directed(G) ,您可以在此处找到文档。

To check if the graph is weighted There is no specific type to say if the graph has weighted edges or not. 检查图形是否加权没有具体类型可以说明图形是否具有加权边。 But a work around can be to check if edges contain an attribute called weight , as mentioned here . 但周围的工作可以检查是否含有边重量所谓的属性,如提到这里 It can be done by 可以通过

'weight' in G[1][2] # Returns true if an attribute called weight exists in the edge connecting nodes 1 and 2.

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

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