class arcnode: def __init__(self, adjvex, weight, link=None): self.adjvex = adjvex self.weight = weight self.link = link class vexnode: def __init__(self, data, first_arc=None): self.data = data self.first_arc = first_arc class Graph: def __init__(self): self.vex_list = [] self.vex_num = 0 self.edge_num = 0 # 请在这里填写答案 def addVertex(self, vex_val): new_vertex = vexnode(vex_val) self.vex_list.append(new_vertex) self.vex_num += 1 def addEdge(self, f, t, cost=0): def print_graph(self): for i in range(self.vex_num): print(self.vex_list[i].data, end="->") cur = self.vex_list[i].first_arc while cur: print("adj:{},weight:{}".format(cur.adjvex, cur.weight), end="->") cur = cur.link print('None') if __name__ == "__main__": g = Graph() s = input() for vertex in s: g.addVertex(vertex) g.addEdge(0, 1, 11) g.addEdge(0, 2, 55) g.addEdge(2, 3, 88) g.addEdge(0, 3, 33) g.addEdge(1, 2, 44) g.print_graph()
时间: 2024-02-14 17:21:33 浏览: 26
这段代码实现了一个图的数据结构,其中包含了三个类:arcnode、vexnode和Graph。arcnode类表示图中的边,vexnode类表示图中的顶点,Graph类则通过vex_list列表来存储所有的顶点,通过edge_num来存储边的数量,通过addVertex方法来添加新的顶点,通过addEdge方法来添加新的边,通过print_graph方法来打印整个图的信息。
在程序的主函数中,首先通过输入获取了顶点的值,然后调用addVertex方法逐一将其添加到图中,再通过addEdge方法逐一添加边的信息,最后通过print_graph方法打印整个图的信息。
相关问题
在图的邻接表存储结构下(基于顶点列表和单链表实现),本题要求图类里实现2个方法函数 def addVertex(self, vex_val): def addEdge(self, f, t, cost=0): 函数接口定义: 在这里描述函数接口。例如: def addVertex(self, vex_val): def addEdge(self, f, t, cost=0): 在这里解释接口参数。例如:其中 f和t分别是构成边的顶点在列表中的序号。 裁判测试程序样例: 在这里给出函数被调用进行测试的例子。例如: class arcnode: def __init__(self,adjvex,weight,link=None): self.adjvex = adjvex self.weight = weight self.link=link class vexnode: def __init__(self,data,first_arc=None): self.data = data self.first_arc = first_arc class Graph: def __init__(self): self.vex_list=[] self.vex_num=0 self.edge_num=0 # 请在这里填写答案 # 请在这里填写答案 def print_graph(self): for i in range(self.vex_num): print(self.vex_list[i].data,end="->") cur = self.vex_list[i].first_arc while cur: print("adj:{},weight:{}".format(cur.adjvex,cur.weight),end="->") cur = cur.link print('None') if __name__ =="__main__": g = Graph() s =input() for vertex in s: g.addVertex(vertex) g.addEdge(0,1,11) g.addEdge(0,2,55) g.addEdge(2,3,88) g.addEdge(0,3,33) g.addEdge(1,2,44) g.print_graph()
class arcnode:
def __init__(self, adjvex, weight, link=None):
self.adjvex = adjvex
self.weight = weight
self.link = link
class vexnode:
def __init__(self, data, first_arc=None):
self.data = data
self.first_arc = first_arc
class Graph:
def __init__(self):
self.vex_list = []
self.vex_num = 0
self.edge_num = 0
def addVertex(self, vex_val):
self.vex_list.append(vexnode(vex_val))
self.vex_num += 1
def addEdge(self, f, t, cost=0):
node = arcnode(t, cost)
node.link = self.vex_list[f].first_arc
self.vex_list[f].first_arc = node
self.edge_num += 1
def print_graph(self):
for i in range(self.vex_num):
print(self.vex_list[i].data, end="->")
cur = self.vex_list[i].first_arc
while cur:
print("adj:{},weight:{}".format(cur.adjvex, cur.weight), end="->")
cur = cur.link
print('None')
if __name__ == "__main__":
g = Graph()
s = input()
for vertex in s:
g.addVertex(vertex)
g.addEdge(0, 1, 11)
g.addEdge(0, 2, 55)
g.addEdge(2, 3, 88)
g.addEdge(0, 3, 33)
g.addEdge(1, 2, 44)
g.print_graph()
修改class arcnode: def __init__(self, adjvex, weight, link=None): self.adjvex = adjvex self.weight = weight self.link = link class vexnode: def __init__(self, data, first_arc=None): self.data = data self.first_arc = first_arc class Graph: def __init__(self): self.vex_list = [] self.vex_num = 0 self.edge_num = 0 # 请在这里填写答案 def addVertex(self, vex_val): new_vertex = vexnode(vex_val) self.vex_list.append(new_vertex) self.vex_num += 1 def addEdge(self, f, t, cost=0): if f not in self.vex_list: nv = self.addVertex(f) # 如果起始顶点不存在,则将其添加到图中 if t not in self.vex_list: nv = self.addVertex(t) # 如果目标顶点不存在,则将其添加到图中 # 无向图添加双向边 self.vex_list[f].addNeighbor(self.vex_list[t], cost) # 将目标顶点及其权重添加到起始顶点的 connectedTo 字典中 self.vex_list[t].addNeighbor(self.vex_list[f], cost) # 有向图只添加一条边 # 请在这里填写答案 def print_graph(self): for i in range(self.vex_num): print(self.vex_list[i].data, end="->") cur = self.vex_list[i].first_arc while cur: print("adj:{},weight:{}".format(cur.adjvex, cur.weight), end="->") cur = cur.link print('None') if __name__ == "__main__": g = Graph() s = input() for vertex in s: g.addVertex(vertex) g.addEdge(0, 1, 11) g.addEdge(0, 2, 55) g.addEdge(2, 3, 88) g.addEdge(0, 3, 33) g.addEdge(1, 2, 44) g.print_graph()
class arcnode:
def __init__(self, adjvex, weight, link=None):
self.adjvex = adjvex
self.weight = weight
self.link = link
class vexnode:
def __init__(self, data, first_arc=None):
self.data = data
self.first_arc = first_arc
class Graph:
def __init__(self):
self.vex_list = []
self.vex_num = 0
self.edge_num = 0
def addVertex(self, vex_val):
new_vertex = vexnode(vex_val)
self.vex_list.append(new_vertex)
self.vex_num += 1
def addEdge(self, f, t, cost=0):
if f not in range(self.vex_num):
raise ValueError("Invalid Starting Vertex")
if t not in range(self.vex_num):
raise ValueError("Invalid Ending Vertex")
new_arc = arcnode(t, cost)
new_arc.link = self.vex_list[f].first_arc
self.vex_list[f].first_arc = new_arc
self.edge_num += 1
def print_graph(self):
for i in range(self.vex_num):
print(self.vex_list[i].data, end="->")
cur = self.vex_list[i].first_arc
while cur:
print("adj:{},weight:{}".format(cur.adjvex, cur.weight), end="->")
cur = cur.link
print('None')
if __name__ == "__main__":
g = Graph()
s = input()
for vertex in s:
g.addVertex(vertex)
g.addEdge(0, 1, 11)
g.addEdge(0, 2, 55)
g.addEdge(2, 3, 88)
g.addEdge(0, 3, 33)
g.addEdge(1, 2, 44)
g.print_graph()