Build a weighted oriented graph with each vertex labeled with a currency name. If you have a rate for AB currency, add an edge (A, B) with the exchange rate as weight.
If you have an edge (A, B) but not an edge (B, A), add an edge (B, A) with a weight of 1 divided by weight (A, B).
To convert the currency C to D, use the shortest path algorithm to find the lowest weight path from peak C to peak D. If there is no such path, then conversion is not possible. See a directed graph with non-negative weights .
==================================================== =======================================
It will not necessarily find the best exchange rate because the rates are multiplying. This can be done to find the best speed using the logarithms of exchange rates as edge weights and using the shortest path algorithm that can handle negative edge weights.
To find the exchange path with the least number of exchanges, give each edge that corresponds to a direct exchange weighing 1.
source share