Python implements a given weighted directed graph, find the number of edges and vertices

The information of a given graph is stored in a txt file in the following format

 

  def length(file_path):#file_path为txt文件路径
        node = set()
        edge = 0
        f = open(file_path)
        lines = [l.split() for l in f.readlines() if l.strip()]
        #print(lines)
        edge = len(lines)
        for i in lines:
            node.add(eval(i[0]))
            node.add(eval(i[1]))
        return len(node), edge

Returns the number of vertices and edges

Guess you like

Origin blog.csdn.net/weixin_63676550/article/details/130183735