Python 图形
python 图形
图表是一组对象通过链接连接的一组对象的图形表示。互连对象由称为顶点的点表示,连接顶点的链接称为边。我们的教程在这里详细描述了与图形相关的各种术语和功能。在本章中,我们将看到如何使用python程序创建图形并向其添加各种数据元素。以下是我们在图表上执行的基本操作。
- 显示图形顶点
- 显示图形边缘
- 添加一个顶点
- 添加边缘
- 创建一个图
可以使用python字典数据类型轻松呈现图形。我们将顶点表示为字典的关键字,顶点之间的连接也称为边界,作为字典中的值。
看看下面的图表 -
在上面的图中
v = {a, b, c, d, e} e = {ab, ac, bd, cd, de}
我们可以在下面的python程序中展示这个图。
# create the dictionary with graph elements graph = { "a" : ["b","c"], "b" : ["a", "d"], "c" : ["a", "d"], "d" : ["e"], "e" : ["d"] } # print the graph print(graph)
当上面的代码被执行时,它会产生以下结果 -
{'c': ['a', 'd'], 'a': ['b', 'c'], 'e': ['d'], 'd': ['e'], 'b': ['a', 'd']}
显示图形顶点
为了显示图形顶点,我们简单地找到图形字典的关键字。我们使用keys()方法。
class graph: def __init__(self,gdict=none): if gdict is none: gdict = [] self.gdict = gdict # get the keys of the dictionary def getvertices(self): return list(self.gdict.keys()) # create the dictionary with graph elements graph_elements = { "a" : ["b","c"], "b" : ["a", "d"], "c" : ["a", "d"], "d" : ["e"], "e" : ["d"] } g = graph(graph_elements) print(g.getvertices())
当上面的代码被执行时,它会产生以下结果 -
['d', 'b', 'e', 'c', 'a']
显示图形边缘
寻找图形边缘比顶点少一些,因为我们必须找到每对顶点之间的边缘。因此,我们创建一个空边列表,然后迭代与每个顶点关联的边值。一个列表形成了包含从顶点找到的不同组的边。
class graph: def __init__(self,gdict=none): if gdict is none: gdict = {} self.gdict = gdict def edges(self): return self.findedges() # find the distinct list of edges def findedges(self): edgename = [] for vrtx in self.gdict: for nxtvrtx in self.gdict[vrtx]: if {nxtvrtx, vrtx} not in edgename: edgename.append({vrtx, nxtvrtx}) return edgename # create the dictionary with graph elements graph_elements = { "a" : ["b","c"], "b" : ["a", "d"], "c" : ["a", "d"], "d" : ["e"], "e" : ["d"] } g = graph(graph_elements) print(g.edges())
当上面的代码被执行时,它会产生以下结果 -
[{'b', 'a'}, {'b', 'd'}, {'e', 'd'}, {'a', 'c'}, {'c', 'd'}]
添加一个顶点
添加一个顶点是直接向我们添加另一个关键字到图形字典。
class graph: def __init__(self,gdict=none): if gdict is none: gdict = {} self.gdict = gdict def getvertices(self): return list(self.gdict.keys()) # add the vertex as a key def addvertex(self, vrtx): if vrtx not in self.gdict: self.gdict[vrtx] = [] # create the dictionary with graph elements graph_elements = { "a" : ["b","c"], "b" : ["a", "d"], "c" : ["a", "d"], "d" : ["e"], "e" : ["d"] } g = graph(graph_elements) g.addvertex("f") print(g.getvertices())
当上面的代码被执行时,它会产生以下结果 -
['f', 'e', 'b', 'a', 'c','d']
添加边缘
将边添加到现有图涉及将新顶点视为元组并验证边是否已经存在。如果不是,则添加边缘。
class graph: def __init__(self,gdict=none): if gdict is none: gdict = {} self.gdict = gdict def edges(self): return self.findedges() # add the new edge def addedge(self, edge): edge = set(edge) (vrtx1, vrtx2) = tuple(edge) if vrtx1 in self.gdict: self.gdict[vrtx1].append(vrtx2) else: self.gdict[vrtx1] = [vrtx2] # list the edge names def findedges(self): edgename = [] for vrtx in self.gdict: for nxtvrtx in self.gdict[vrtx]: if {nxtvrtx, vrtx} not in edgename: edgename.append({vrtx, nxtvrtx}) return edgename # create the dictionary with graph elements graph_elements = { "a" : ["b","c"], "b" : ["a", "d"], "c" : ["a", "d"], "d" : ["e"], "e" : ["d"] } g = graph(graph_elements) g.addedge({'a','e'}) g.addedge({'a','c'}) print(g.edges())
当上面的代码被执行时,它会产生以下结果 -
[{'e', 'd'}, {'b', 'a'}, {'b', 'd'}, {'a', 'c'}, {'a', 'e'}, {'c', 'd'}]