FAQ: Graphs: Python - Refactoring Path-Finding

This community-built FAQ covers the “Refactoring Path-Finding” exercise from the lesson “Graphs: Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

Complex Data Structures

FAQs on the exercise Refactoring Path-Finding

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Assignment 2 says:

Replace the commented code after we declare current_vertex . Set the key of the current_vertex as True in our seen dictionary.

Should that be: “Set the value of the current_vertex as True in our seen dictionary.”? Or am I missing something here?

2 Likes

You have it correct. The dictionary is seen, a key is current_vertex, and the associated value is True.

5 Likes

It is February 29, 2020 and the text still erroneously reads as follows.
Replace the commented code after we declare current_vertex . Set the key of the current_vertex as True in our seen dictionary.

Was there any reason for using a dictionary and not a list for the ‘seen’ variable? I tried it with the list and it gave me the same result, and I can’t think of any reasons it wouldn’t in other circumstances. It seems a lot easier to just use a list, am I missing something?

1 Like

Why is

Visiting callan

printed twice when the script.py is run?

This is the command

railway.add_vertex(callan)
railway.add_vertex(peel)
railway.add_vertex(harwick)
railway.add_vertex(ulfstead)

railway.add_edge(peel, harwick)
railway.add_edge(harwick, callan)
railway.add_edge(callan, peel)

peel_to_ulfstead_path_exists = railway.find_path('peel', 'ulfstead')

that runs this piece of code in graph.py

  def find_path(self, start_vertex, end_vertex):
    start = [start_vertex]
    seen = {}
    while len(start) > 0:
      current_vertex = start.pop(0)
      seen[current_vertex] = True
      print("Visiting " + current_vertex)
      if current_vertex == end_vertex:
        return True
      else:
        vertex = self.graph_dict[current_vertex]
        next_vertices = vertex.get_edges()
        next_vertices = [vertex for vertex in next_vertices if vertex not in seen]
        start.extend(next_vertices)
    return False

I would have assumed that after pop(0) removed “callan” from the “start” list that the while loop would end and return False. There would be no more next_vertices with which to extend the start list.

However, it runs one more time for the following results:

Visiting peel
Visiting harwick
Visiting callan
Visiting callan

Thanks!

These courses are poorly made sometimes. They just ask you to follow the step like a robot. If you do the same, another way, you get an error even if it is correct…

Instead of doing:

next_veritcles = vertex.get_edges()
        next_vertices = [vertex for vertex in next_vertices if vertex not in seen]

I did:

next_vertices = [edge for edgein vertex.get_edges() if edge not in seen]

So I tried:

next_veritcles = vertex.get_edges()
next_vertices = [edge for edge in next_vertices if edge not in seen]

So just because I don’t have the same name they want me to have for a temporary local variable, I get an error?

2 Likes

In step 3, we remove the vertices we’ve already visited by checking with the seen dictionary. Here’s the code they consider correct:
next_vertices = [vertex for vertex in next_vertices if vertex not in seen]
How come we’re able to compare the elements in a list with elements in a dictionary? next_vertices is a list, and seen is a dictionary. Do they automatically assume we’re comparing with the keys?

If you’re checking for membership a dictionary or iterating through it (with for for example) it defaults to using the keys.

I have the same question…

I have the same question. Did anyone figure out why Callan prints twice?

Hi guys,

I am having a hard time understanding the below, can someone please help ?

seen is a dictionary made of str as keys and True as values: e.g. {“callan”: True, “peel”:True} if we’ve visited this Vertex already

That’s how find_path is called and defined (I just put the first few lines of the def function:

harwick_to_peel_path_exists = railway.find_path('harwick', 'peel')

  def find_path(self, start_vertex, end_vertex):
    start = [start_vertex]
    seen = {}
    while len(start) > 0:
      current_vertex = start.pop(0)

so the argument start_vertex and end_vertex of find_path are strings.
and that’s how seen is being amended as a dictionary (with current_vertex being equal a string too.

seen[current_vertex] = True

So current_vertex is a STRING! Not a Vertex object.
How come the below works then ?

next_vertices = [vertex for vertex in next_vertices if vertex not in seen]

Shouldn’t it be

next_vertices = [vertex for vertex in next_vertices if vertex.value not in seen]

instead ?

thanks a lot in advance

Callan prints twice because it is put into the list the first time as an edge of peel and then again as an edge of harwick. After the first while loop:

seen = {peel: True}
start = [harwick, callan]

then the second while loop executes start.pop(0), harwick’s edges (peel and callan) are put into start. Peel doesn’t make it in because of the conditional but callan makes it in again. Hence:

seen = {peel: True, harwick: True}
start = [callan, callan]

while loop three pops callan, and then loop four pops another callan.

Also checkpoint two is still written incorrectly… it’s September 2023 now…

next_vertices is a list of strings not vertex object.

next_vertices = vertex.get_edges()
# in vertex.py
def get_edges(self):
    return list(self.edges.keys())

the list method makes the self.edges.keys() into a list of strings of the keys in self.edges