← Back to DSA Animator
Evaluate Division LC #399 Medium Graph · Weighted BFS
Problem

Equations a/b=k. Queries c/d. Build graph a→b(k), b→a(1/k). BFS from src, multiply weights. Return product if path exists else -1.

Example
Input: equations=[a/b=2,b/c=3], queries=[a/c,b/a]
Output: [6.0, 0.5]
Try Examples
Approach
Weighted Graph + BFS
a/b=v → edge a→b(v), b→a(1/v). Query x/y: BFS from x, multiply edge weights. Product = answer.
Graph & Results
Load an example.
Variables
Query
Curr
Acc
Result
Step Logic
Press ▶ Play or Next Step.
Ready
0/0
Select example and Play.
Algorithm
1
Build graph: a→b(v), b→a(1/v)
2
BFS from src, multiply weights
3
Product = answer or -1
Time
O((V+E)·Q)
Space
O(V+E)
Why Product

a/b=2, b/c=3 → a/c = (a/b)*(b/c) = 2*3 = 6. BFS multiplies edge weights along path.