Open gryumov opened 3 months ago
Q1. Have you seen/heard of an implementation of something like this? I mean, with all restrictions of task :smiling_face_with_tear:
Tough task. Especially compared to what you have now, it's a very big leap forward I have some ideas for this thing
@7uperior hi, yes, we have a solution π
graph LR; X --> |42| Y; Y --> |24| X; classDef julia_blue fill:#4063D8,stroke:#333,stroke-width:2px; classDef julia_green fill:#389826,stroke:#333,stroke-width:2px; classDef julia_red fill:#CB3C33,stroke:#333,stroke-width:2px; classDef julia_purple fill:#9558B2,stroke:#333,stroke-width:2px; classDef def_color fill:#eee,stroke:#ccc,stroke-width:2px; class A,F julia_blue; class B,E julia_red; class C,X julia_green; class D,Y julia_purple;
Just out of curiosity, if X-->Y
has weight w but Y-->X
doesn't have 1/w, but some weight w2 > 1/w, shouldn't the best rate to buy X from Y be w2 itself, rather than using a rate of 1/w, which reflects the consideration of direction of path X-->Y in the question? Or is it implicitly implied that w2 < 1/w, given all the gas/extra fees?
In this case wouldn't it be effective to only consider maximum weight. Since when you consider buying X from Y, instead of considering minimum weight X-->Y, you shall calculate maximum of Y-->X instead?
If the assumption of 2w<1/w is true, and we formulate the problem this way naturally we shall bypass the difficult of dead loop.
Hi, @RongkunWang!
I didn't quite understand what you mean, so I would be very grateful for a more specific example
However, as I understood it, the question is what to do if the path A --> B
(with weight W1
) has an explicitly defined reverse path B --> A
(with weight W2
). In this case, we assume that there is no implicit path B --> A
with weight 1/W1
(only W2
)
So we need a different mechanism to handle dead loops
Hi @artememelin ,
I understood the original question formation. I'm just making a connection to the real-world problem and try to understand if the formation of the question makes sense.
If I can exchange crypto A
to B
with price W1
, but I can change from B
to A
with price W2
. If W1*W2 > 1
, in real world we should simply just buy B
with A
and buy A
back with B
indefinitely until W1*W2 < 1
, so as to make money. So in this case it actually encourages dead loops, not discourages it.. And if W1*W2< 1
is always true, we have the maximum gain we don't need to consider minimum weight path, we only need to consider maximum weight path. For example to from A
to B
, the most econimic price is by maximizing W_AB
, or W_AC * W_CB
, from B
to A
, we should maximize W_BA
or W_BC * W_CA
, but not minimize W_AB
, or W_AC * W_CB
?
@RongkunWang, of course, what you suggest is not far from the truth.
Indeed, if we assume that in the real world we are unlikely to see a similar situation, then the task becomes quite simplified. Perhaps, for cases where we know for sure that there are no dead loops in the graph, we can implement the version of the algorithm you propose. In case of such a limitation, we can implement a more optimized algorithm for such graphs.
However, we also need an algorithm that will be able to work with dead loops in the graph
Description
Add a new algorithm to the package that is capable of finding the most advantageous chain of currency conversions between two specified currencies.
The "most advantageous chain of currency conversions" is defined as a sequence of exchanges from currency A to currency B such that the product of the exchange rates (the weights of the edges in the currency exchange graph) is optimized. If the goal is to sell currency A, the algorithm should maximize this product; if the goal is to buy currency A, the algorithm should minimize it.
Tasks
Tests
The algorithm must pass the following tests:
Testset
Basic rules of the algorithm
The final conversion coefficient between two currencies
A
andB
is determined by multiplying the weights of the edges corresponding to the laid path.If there is an edge from
A
toB
with weightW
in the graph, then it is assumed that an inverse edge fromB
toA
with weight1/W
is implicitly specified unless otherwise stated (likeB -> C
andC -> B
).Thus, the algorithm must search for a path in all directions of the graph, even if the edges are explicitly specified only in one direction.
Algorithm requirements
The algorithm must be able to find a path in which the final conversion coefficient will be maximum (We want to sell the base currency and get as much of the quoted currency as possible). Note that such a path may be longer than the path that would be found using the A* algorithm.
The algorithm must also be able to find a path in which the final conversion rate will be minimal (We want to buy as much of the base currency as possible and spend the minimum amount of the quoted currency). Note that such a path may be longer than the path that would be found using the A* algorithm.
For simplicity, you can break this algorithm into two separate ones to find the maximum and minimum. Or you can implement algorithm selection using keywords
Possible difficulties
Situations may arise with so-called βnegative cyclesβ - a closed path, following which we can endlessly improve the final conversion coefficient. You need to figure out how to avoid this behavior. Here
A -> B -> C -> A -> ...
is a negative cycle.