Documentation Index
Fetch the complete documentation index at: https://mintlify.com/tutosrive/Constellations/llms.txt
Use this file to discover all available pages before exploring further.
LongestPathWithDonkey in src/controllers/LongestPathWithDonkey.py calculates the maximum-distance path the donkey can walk before its energy, health, or grass runs out. It uses recursive depth-first search (DFS) with copy.deepcopy of the donkey’s state at every branching point so that each branch has its own independent resource pool. When a branch exhausts the donkey’s resources, that branch is pruned; the algorithm continues exploring remaining branches and keeps track of the globally longest viable route encountered.
Constructor
A loaded
Graph instance whose vertex_list will be traversed during the DFS.A
Donkey instance carrying the initial resource values (energy, health, grass) to use as the baseline state at the start of every traversal.self.best_path = [] and self.max_distance = 0, which are overwritten by each call to find_longest_path.
find_longest_path
The public entry point. Resets internal state, deep-copies the donkey to protect the caller’s instance, and launches the recursive DFS.Label of the vertex from which the DFS begins.
Ordered list of vertex labels representing the longest viable route found.
Total accumulated distance of
best_path. Remains 0 if no step is possible from start._dfs Internal Algorithm
_dfs is the recursive core. It appends the current vertex to the working path, updates the global best if a new maximum distance has been reached, and then tries to extend the path through every unvisited neighbour that the donkey can still reach.
- Visited check —
if neighbor not in pathensures the traversal stays acyclic without a separate visited set; the current workingpathlist acts as the visited tracker. - Deep copy per branch —
d_copy = deepcopy(donkey)creates a fully independent donkey state for each candidate neighbour. This means backtracking is implicit: when the recursive call returns, the caller’sdonkeyobject is untouched. - Travel confirmation —
d_copy.travel(distance)is called only after_can_travelreturnsTrue. The branch is extended only whentravel_result.okisTrueor the donkey has not yet died. - Backtrack —
path.pop()removes the current vertex before returning, restoring the path for sibling branches.
_can_travel
Checks whether the donkey has enough resources to cover the given distance, using the cost breakdown fromdonkey.get_travel_cost(distance).
0 also counts as insufficient.
Resource Pruning
The resource check in_can_travel acts as the primary pruning mechanism. When any of energy, health, or grass would drop to zero or below after a move, the entire subtree rooted at that neighbour is skipped. This can dramatically reduce the search space on dense graphs where most paths eventually exhaust the donkey before reaching distant vertices.
LongestPathWithDonkey explores all simple paths from the start vertex, which is O(V!) in the worst case. Resource pruning reduces this significantly in practice, but the algorithm is designed for small constellation graphs with fewer than 20 vertices. Avoid using it on large graphs — consider a heuristic or beam-search approach for graphs with 20 or more vertices.