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())
输出
当上述代码被执行时,它产生了以下结果 –
['a', 'b', 'c', 'd', 'e','f']
添加一个边
在一个现有的图上添加一个边涉及到将新的顶点视为一个元组,并验证该边是否已经存在。如果不是,则添加该边。
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'}]