# Pathfinding Algorithm

The core intelligence of InfiRouter lies in its on-chain pathfinding algorithm, implemented primarily within the internal \_findBestPath function. This recursive algorithm systematically explores potential swap routes to identify the one offering the maximum output amount, potentially adjusted for gas costs. It utilizes a recursive depth-first search strategy.

The algorithm operates recursively as follows:

1. **Base Case** - Direct Path Evaluation: The function first considers all possible single-step path. It  iterates through every registered adapter.&#x20;
2.  **Recursive Step - Multi-Hop Exploration**:\


    – The algorithm checks if further exploration is warranted based on the maxSteps constraint. If the current path being built has length ( l ) and ( l <  - 1 ), it proceeds to explore two-step (or longer) paths originating from the current state. \
    \
    – It iterates through each potential intermediary token in the TRUSTED\_TOKENS list.

