简体   繁体   English

为什么我的来自 a.gv 文件的叶节点之间的距离矩阵会导致一个空字典?

[英]Why does my distance matrix between leaf nodes from a .gv file result in an empty dictionary?

How to get a distance matrix between leaf nodes from a.gv file?如何从 a.gv 文件中获取叶节点之间的距离矩阵? I'm trying to obtain the shortest distance between leaves in an unweighted graph.我正在尝试获得未加权图中叶子之间的最短距离。

I have tried this:我试过这个:

def compute_distance_matrix(graph):
    leaf_nodes = [node for node in graph.nodes() if graph.degree(node) == 1]
    leaf_labels = [node.split('s')[1] for node in leaf_nodes]
    distance_matrix = {}
    for i, node1 in enumerate(leaf_nodes):
        for j, node2 in enumerate(leaf_nodes[i+1:], i+1):
            if nx.has_path(graph, node1, node2):
                distance = nx.shortest_path_length(graph, node1, node2)
                distance_matrix[(leaf_labels[i], leaf_labels[j])] = distance
            elif graph.has_edge(node1, node2):
                distance_matrix[(leaf_labels[i], leaf_labels[j])] = 1
    return distance_matrix

but it returns an empty dict.但它返回一个空的字典。

def compute_distance_matrix(nw):
    tree = Tree(nw)
    leaves = tree.get_leaves()
    leaf_labels = [leaf.name for leaf in leaves]
    distance_matrix = {}
    for i, node1 in enumerate(leaves):
        for j, node2 in enumerate(leaves[i+1:], i+1):
            distance = node1.get_distance(node2)
            distance_matrix[(leaf_labels[i], leaf_labels[j])] = distance
    return distance_matrix

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

相关问题 为什么我的* args结果在Python 3中以空列表开头? - Why does my result from *args begin with an empty list in Python 3? 为什么`pip freeze`会导致一个空文件? - Why does `pip freeze` result in an empty file? python从字典数据计算距离矩阵 - python compute distance matrix from dictionary data 如何计算 PDB 文件中所有原子之间的距离并从中创建距离矩阵 - How to calculate the distance between all atoms in a PDB file and create a distance matrix from that 为什么我的networkx图表在节点之间没有显示边缘? - why does my networkx graph display no edges between the nodes? 如果边存在于 2 个节点之间则取值为 1 的矩阵,否则取自邻接字典的值为 0 - matrix which takes as value 1 if an edge exist between 2 nodes and 0 otherwise from an adjacency dictionary 为什么我的抓取脚本返回空结果 - Why does my scraping script returns empty result 不明白为什么结果是空字典 - Don't understand why the result is an empty dictionary 如何从python字典中找到特定节点到叶节点的所有路径以及前面的节点? - How to find all the paths to a particular node to leaf node and also the preceding nodes from python dictionary? 为什么从字典中替换字符串会产生空文件 - Why replacing strings from dictionary produce empty file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM