Repository: brean/python-pathfinding

Issue:
Error when running pathfinding multiple times in graph
**Describe the bug**
Error when running pathfinding multiple times in graph

**To Reproduce**

```python
from pathfinding.core.graph import Graph
from pathfinding.finder.dijkstra import DijkstraFinder

graph = Graph(edges=[[0, 1, 1], [1, 0, 1]], bi_directional=False)
finder = DijkstraFinder()
path, _ = finder.find_path(graph.node(0), graph.node(1), graph)
path, _ = finder.find_path(graph.node(0), graph.node(1), graph)
```

Error:

```
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../python-pathfinding/pathfinding/finder/a_star.py", line 96, in find_path
    return super(AStarFinder, self).find_path(start, end, graph)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../python-pathfinding/pathfinding/finder/finder.py", line 180, in find_path
    self.clean_grid(grid)
  File ".../python-pathfinding/pathfinding/finder/finder.py", line 167, in clean_grid
    grid.cleanup()
    ^^^^^^^^^^^^
AttributeError: 'Graph' object has no attribute 'cleanup'
```

**Expected behavior**
Calling pathfinding multiple times should return the correct path each time without errors. Each call should be independent and not affected by previous runs.

**Screenshots** / **Map** / **Log**
 .

**Environment (please complete the following information):**
 - Environment: Ubuntu 24.04
 - Python version: 3.12
 - Pathfinding Version: 1.0.16 

**Additional context**
Apparently Graph doesn't have a cleanup method.

Hints from issue comments:
@brean: fixed in 1.0.17

Solution patch:
diff --git a/pathfinding/core/graph.py b/pathfinding/core/graph.py
index cf71b0e..1226edd 100644
--- a/pathfinding/core/graph.py
+++ b/pathfinding/core/graph.py
@@ -62,3 +62,7 @@ class Graph:
 
     def node(self, node_id):
         return self.nodes[node_id]
+
+    def cleanup(self):
+        for node in self.nodes.values():
+            node.cleanup()


Test patch:
diff --git a/test/test_graph.py b/test/test_graph.py
index 6ef71b9..a0d5442 100644
--- a/test/test_graph.py
+++ b/test/test_graph.py
@@ -1,3 +1,5 @@
+from unittest.mock import patch
+
 from pathfinding.core.graph import Graph
 from pathfinding.finder.dijkstra import DijkstraFinder
 
@@ -33,3 +35,19 @@ def test_connected_end():
     finder = DijkstraFinder()
     path, _ = finder.find_path(graph.node(0), graph.node(2), graph)
     assert [n.node_id for n in path] == [0, 1, 2]
+
+
+def test_graph_cleanup():
+    graph = Graph(edges=[[0, 1, 1], [1, 0, 1]], bi_directional=False)
+    assert not graph.dirty
+
+    finder = DijkstraFinder()
+    path, _ = finder.find_path(graph.node(0), graph.node(1), graph)
+    assert [n.node_id for n in path] == [0, 1]
+
+    assert graph.dirty
+
+    with patch.object(graph, "cleanup", wraps=graph.cleanup) as mock_cleanup:
+        path, _ = finder.find_path(graph.node(1), graph.node(0), graph)
+        assert [n.node_id for n in path] == [1, 0]
+        mock_cleanup.assert_called_once()