graph.py 文件源码

python
阅读 46 收藏 0 点赞 0 评论 0

项目:py-graphart 作者: dandydarcy 项目源码 文件源码
def connect_graphs(self, sets_orig, edges_orig):
        '''
        Returns the edges needed to connect unconnected graphs (sets of nodes)
        given a set of sets of nodes, select the master_graph (the biggest) one
        and search the shortest edges to connect the other sets of nodes
        '''
        master_graph = max(sets_orig, key=len)
        sets = sets_orig.copy()
        edges = np.array([], dtype=object)
        sets.remove(master_graph)
        master_tree = cKDTree(self.nodes[list(master_graph)])
        for s in sets:
            x = np.array(list(s))
            nearests = np.array([master_tree.query(self.nodes[v]) for v in x])
            tails = nearests[
                nearests[:, 0].argsort()][:, 1][:self.max_neighbours]
            heads = x[nearests[:, 0].argsort()][:self.max_neighbours]
            for head, tail in zip(heads, tails):
                edges = np.append(edges, None)
                edges[-1] = (head, tail)
                edges = np.append(edges, None)
                edges[-1] = (tail, head)
        return edges
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号