
Matthew B. answered 03/16/20
Founder, CEO, and Lead Developer: Apteryx Labs | Stanford, 2015
Sounds like a job for either the Maximum Flow algorithm, or the Stable Marriage algorithm. This really depends on the specifics, though. So I'd need you to provide the following clarifications:
1) Define "route" - can A transport directly to D, or are there intervening stops?
2) If the latter is true, do these stops have an associated cost?
3) Are supply and demand indicated in terms of quantity possessed/desired, or by dollar amount?
4) Can multiple vendors sell to the same buyer, or are vender-buyer partnerships exclusive?
5) Do buyers have price ceilings?
If you assume 1) is direct (no stops between), 3) is in $s, 4) is "exclusive", and 5) is true - you can solve with the stable marriage algorithm. In fact, if just one iteration of trading will take place, you can simply sort buyers by buying price (descending), vendors with selling price (ascending), and pair indexwise.
Else, you could turn the vendor - buyer data into a graph, calculate some metric for weights between nodes, and solve for maximum flow.
Do provide the additional information requested above, and perhaps I can help further!
Max Flow:
https://en.wikipedia.org/wiki/Minimum-cost_flow_problem
Stable Marriage: