简体   繁体   English

如何在 Python 中构建图(节点和边)?

[英]How to construct a graph (nodes and edges) in Python?

I have a txt file has coordinates for multiple rectangles (X center , Y center , width, height).我有一个 txt 文件,其中包含多个矩形的坐标(X中心、Y中心、宽度、高度)。 I would like to create an undirected graph network where the nodes of which stand for the rectangle and the edges of which represent distance between the nodes.我想创建一个无向图网络,其中节点代表矩形,边代表节点之间的距离。

txt file: txt文件:

 Xcenter    Ycenter     width    height
0.568396   0.394130   0.176887  0.345912
0.391509   0.393082   0.172170  0.327044
0.717571   0.377358   0.119104  0.320755
0.254717   0.373166   0.103774  0.299790

In graph, you can represent rectangles as a 'list' of nodes with their x,y,height and weight as their properties, and represent their edges in an adjacency matrix, with distance between them marked in each cell.在图中,您可以将矩形表示为节点的“列表”,其中 x、y、高度和权重作为它们的属性,并在邻接矩阵中表示它们的边缘,并在每个单元格中标记它们之间的距离。
Below is a sample program, that creates a complete-graph out of given data.下面是一个示例程序,它根据给定的数据创建一个完整的图表。

import math
class node:
    def __init__(self,x,y,height,width):
        self.x=x
        self.y=y
        self.height=height
        self.width=width

class graph:
    def __init__(self,txt):
        self.node_list=[]
        for line in txt.split("\n"):
            x,y,width,height=[i for i in line.strip().split(' ') if i!='']
            new_node=node(x,y,height,width)
            self.node_list.append(new_node)

        self.adjacency_matrix= self.get_adjacenty_matrix()

    def get_distance(self,x1,y1,x2,y2):
        x1=float(x1)
        y1=float(y1)
        x2=float(x2)
        y2=float(y2)
        
        dis= math.sqrt(((x2-x1)**2)+((y2-y1)**2))
        return dis

    def get_adjacenty_matrix(self):
        matrix=[]
        for i in range(len(self.node_list)):
            row=[0]*len(self.node_list)
            for j in range(len(self.node_list)):
                row[j]=self.get_distance(self.node_list[i].x, self.node_list[i].y, self.node_list[j].x, self.node_list[j].y)
            print(row)
            matrix.append(row)
        return matrix

txt="""0.568396   0.394130   0.176887  0.345912
0.391509   0.393082   0.172170  0.327044
0.717571   0.377358   0.119104  0.320755
0.254717   0.373166   0.103774  0.299790"""

print(graph(txt).adjacency_matrix)

Output (Adjacency matrix):输出(邻接矩阵):

[[0.0, 0.17689010450842071, 0.15011489136324876, 0.314378759360425],
 [0.17689010450842071, 0.0, 0.3264409165836905, 0.13823421544610434], 
 [0.15011489136324876, 0.3264409165836905, 0.0, 0.46287298277173183],
 [0.314378759360425, 0.13823421544610434, 0.46287298277173183, 0.0]]

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

相关问题 python igraph 边作为节点图 - python igraph edges as nodes graph Python:如何从csv文件创建图形节点和边? - Python: How to create graph nodes and edges from csv file? 如何使用 python 中的节点和边在图形中绘制形状? - How to draw shapes in a graph using nodes and edges in python? 如何将带有(新节点,新边)的子图添加到 python 中的现有图 - how to add subgraph with (new nodes, new edges) to an existed graph in python Python:从带括号的边创建图节点 - python: creating graph nodes from a parenthesized edges 在 Python 中使用邻接表构造节点图 - Construct a graph of nodes using adjacency list in Python 如何在 Graph Networkx 中找到具有公共节点的边? - How to find edges with common nodes in Graph Networkx? 如何使用python可视化节点分为2个集群的图形,边缘应该存在于集群之间和集群内? - How to visualize a graph with nodes divided in 2 clusters, edges should be present between the clusters and within the clusters , using python? 在networkx python中,如何将一组新的节点和边连接到图中的每个节点? - In networkx python, how to connect a new bunch of nodes and edges to each node in a graph? 通过networkx并使用python在图中添加节点和边 - Add nodes & edges in graph via networkx and using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM