问题描述
伙计们,请帮助我编写主类,每当我执行它时都会抛出错误,我不认为有什么问题..在这个程序中,我使用 DFS 和 BFS 从图中打印最短路径,但是 class Graph(object):请检查主类是否有错误..
def __init__(self,graph_dict=None):
""" initializes a graph object
If no dictionary or None is given,an empty dictionary will be used
"""
if graph_dict == None:
graph_dict = {}
self.__graph_dict = graph_dict
def vertices(self):
""" returns the vertices of a graph """
return list(self.__graph_dict.keys())
def edges(self):
""" returns the edges of a graph """
return self.__generate_edges()
def add_vertex(self,vertex):
""" If the vertex "vertex" is not in
self.__graph_dict,a key "vertex" with an empty
list as a value is added to the dictionary.
Otherwise nothing has to be done.
"""
if vertex not in self.__graph_dict:
self.__graph_dict[vertex] = []
def add_edge(self,edge):
""" assumes that edge is of type set,tuple or list;
between two vertices can be multiple edges!
"""
edge = set(edge)
(vertex1,vertex2) = tuple(edge)
if vertex1 in self.__graph_dict:
self.__graph_dict[vertex1].append(vertex2)
else:
self.__graph_dict[vertex1] = [vertex2]
def __generate_edges(self):
""" A static method generating the edges of the
graph "graph". Edges are represented as sets
with one (a loop back to the vertex) or two
vertices
"""
edges = []
for vertex in self.__graph_dict:
for neighbour in self.__graph_dict[vertex]:
if {neighbour,vertex} not in edges:
edges.append({vertex,neighbour})
return edges
def __str__(self):
res = "vertices: "
for k in self.__graph_dict:
res += str(k) + " "
res += "\nedges: "
for edge in self.__generate_edges():
res += str(edge) + " "
return res
def Dfsutil(self,v,visited):
# Mark the current node as visited
# and print it
visited.add(v)
print(v,end=' ')
# Recur for all the vertices
# adjacent to this vertex
for neighbour in self.graph[v]:
if neighbour not in visited:
self.Dfsutil(neighbour,visited)
# The function to do DFS traversal. It uses
# recursive Dfsutil()
def DFS(self,v):
# Create a set to store visited vertices
visited = set()
# Call the recursive helper function
# to print DFS traversal
self.Dfsutil(v,visited)
打印图形 BFS 的函数
def BFS(self,s):
# Mark all the vertices as not visited
visited = [False] * (max(self.graph) + 1)
# Create a queue for BFS
queue = []
# Mark the source node as
# visited and enqueue it
queue.append(s)
visited[s] = True
while queue:
# Dequeue a vertex from
# queue and print it
s = queue.pop(0)
print (s,end = " ")
# Get all adjacent vertices of the
# dequeued vertex s. If a adjacent
# has not been visited,then mark it
# visited and enqueue it
for i in self.graph[s]:
if visited[i] == False:
queue.append(i)
visited[i] = True
if name == "main":
g = { "a" : ["b","c"],"b" : ["a","d","e"],"c" : ["a","h"],"d" : ["b","c","e","g","f"],"e" : ["b","g"],"f" : ["d","g" : ["e","h" : ["c","f","g"]
}
graph = Graph(g)
print("Vertices of graph:")
print(graph.vertices())
print("Edges of graph:")
print(graph.edges())
print("Add vertex:")
graph.add_vertex("z")
#call both implemented function here
graph.DFS(graph.vertices(0)) graph.BFS(graph.vertices(0))
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)