Find shortest-path code is failing with graph not found error

When running unittests.test_shortest_path(GraphShortestPath), I get the following errors:

Process shortest_path:
Traceback (most recent call last):
  File "/opt/conda/lib/python3.11/multiprocessing/process.py", line 314, in _bootstrap
    self.run()
  File "/opt/conda/lib/python3.11/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/home/jovyan/work/unittests.py", line 51, in helper
    out = func(*arg)
          ^^^^^^^^^^
  File "/tmp/ipykernel_80/1059960311.py", line 24, in shortest_path
    distances = {node: float('inf') for node in graph}
                                                ^^^^^
NameError: name 'graph' is not defined

If I look at the file unittests.py file, one statement implies that ‘graph’ is read from the file shortest_path_data.joblib. I am assuming that I do not have to call the generate_graph method.. Maybe I’m wrong. Please confirm.

The Graph object doesn’t hold its nodes directly, they’re all stored inside its internal data structure. Your shortest-path code needs to read from that structure because that’s where the vertices actually are. If you reference something else instead, Python won’t find it, which is why you’re getting a “name not defined” error.

Check in your function implementation, probably shortetst_path, do you have a variable called graph?

for node in graph, but it says no graph found. If the code was generated by an LLM you can always come back to it and ask more questions. Also check the function definition sometimes the code produced by the LLM changes the variables inside the function’s definition or inside the function itself.

1 Like

Thanks for the feedback. Got around the reported error and now get the following:

Process shortest_path:
Traceback (most recent call last):
  File "/opt/conda/lib/python3.11/multiprocessing/process.py", line 314, in _bootstrap
    self.run()
  File "/opt/conda/lib/python3.11/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/home/jovyan/work/unittests.py", line 51, in helper
    out = func(*arg)
          ^^^^^^^^^^
  File "/tmp/ipykernel_2178/2908448105.py", line 27, in shortest_path
    distances = {node: float('inf') for node in graph}
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'GraphShortestPath' object is not iterable

This is the reply I got from chatgpt , what you are actually supposed to do as well when building these codes, as well as use you own understanding in the meantime:

The error message you’re seeing:

TypeError: 'GraphShortestPath' object is not iterable

is happening because you’re trying to iterate over graph here:

distances = {node: float('inf') for node in graph}

But graph is a GraphShortestPath object, and Python doesn’t know how to iterate over it. Only objects like lists, dictionaries, sets, or anything implementing __iter__ are iterable.

How to fix it

  1. Check what graph actually contains. If it has a property or method that returns the nodes, use that instead. For example, if graph.nodes is a list of nodes:
distances = {node: float('inf') for node in graph.nodes}

  1. Implement __iter__ in your class (optional, if you want GraphShortestPath itself to be iterable):
class GraphShortestPath:
    def __init__(self, ...):
        ...
    def __iter__(self):
        return iter(self.nodes)  # assuming self.nodes is a list of nodes

  1. Double-check your function signature and make sure graph is what you expect (a collection of nodes, not a wrapper object).

One way we define the distances matrix when solving exercises similar to this one is: dist = [float(‘inf’)]*V which means all distances are infinite but then you have to set up the starting node with distance 0. The LLM will guide you through this as well.

1 Like