按照如下定义给G=Graph()添加边举例子class Graph: def __init__(self): self.vertices = {} self.numVertices = 0 # 初始图 顶点0 def addVertex(self, key): # add顶点 self.numVertices = self.numVertices + 1 newVertex = Vertex(key) self.vertices[key] = newVertex # 顶点对应属性 return newVertex def getVertex(self, key): if key in self.vertices: return self.vertices[key] else: return None def __contains__(self, key): # check判断是否存在 return key in self.vertices def addEdge(self, f, t, cost): # 添加边 if f not in self.vertices: nv = self.addVertex(f) # 顶点未在图的顶点集先加入 if t not in self.vertices: nv = self.addVertex(t) self.vertices[f].addNeighbor(self.vertices[t], cost) # 添加邻接点
时间: 2024-02-14 08:04:57 浏览: 60
GPAC.rar_graph image_graph 分割_image segmentation_site:www.pudn.c
好的,假设我们要给这个图添加一条从顶点0到顶点1的边,边权为2,代码如下:
```
G = Graph()
G.addVertex(0)
G.addVertex(1)
G.addEdge(0, 1, 2)
```
这样就在图中添加了一条从顶点0到顶点1的边,边权为2。
阅读全文