Traveling Salesperson

I’ve fixed several bugs in my code so far but can’t figure this one out. Please help.

Traceback (most recent call last):
File “”, line 76, in
File “”, line 56, in traveling_salesperson
current_vertex_edge_weights[edge] = graph.graph_dict[current_vertex].get_edge_weight()
AttributeError: ‘Vertex’ object has no attribute ‘get_edge_weight’

Please see:

1 Like

As @lisalisaj’s reply is alluding to, we can’t really be of much help with the information provided. A link to the exercise would be very helpful. Your code would also be helpful. What I can tell you from the error message and the vague memory I have of having done the exercise myself some time ago, is that vertexes are intended to be points on the graph, and as the error message says would have no edge weight let alone a method to retrieve it. Edges have edge weight in a weighted graph, so you’ll need to call your get_edge_weight() method on an edge. If you can’t figure it out, read through the article that @lisalisaj posted, and ask again.

1 Like

Have you added the get_edge_weight method in the file?

Here it is:

class Vertex:
def init(self, value):
self.value = value
self.edges = {}

def add_edge(self, vertex, weight=0):
self.edges[vertex] = weight

def get_edges(self):
return list(self.edges.keys())

def get_edge_weight(self, edge):
return self.edges[edge]

Here is the block of code that includes the error. It is identical to the hint in step 8 of the project.
while not visited_all_vertices:
current_vertex_edges = graph.graph_dict[current_vertex].get_edges()
current_vertex_edge_weights = {}
for edge in current_vertex_edges:
current_vertex_edge_weights[edge] = graph.graph_dict[current_vertex].get_edge_weight(edge)

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.