Say x / y = 4.0, y / z = 2.0, and z / w = 0.5. Your graph has edges: x→y (4.0), y→x (0.25), y→z (2.0), z→y (0.5), z→w (0.5), w→z (2.0).
Query x / w: BFS from x. Visit y with product . Visit z with product . Visit w with product . Return .
Query w / x: BFS from w. Path is w→z→y→x, products: , then , then . Return .
For queries over a graph with variables and edges, each BFS takes . Total time: . Space is for the adjacency list.