需求
A currency exchange graph is a graph whose n nodes represent different curriencies, and whose directed edges represent exchange rates ruv between those
currencies (for example re$
≈ 1.78 for exchanging Euro to New Zealand dollars).
For convenience the edges can be weighted using wuv = log 1
ruv
(for example
we$
≈ −0.58), allowing the weight of adjacent edges to be added together to
find combined exchange rates, and shorter paths result in higher exchange rates.
The purpose of this assignment is to develop useful graph tools for currency
trading.
The assignment should include the following components, each with some
suitable test examples:
Best Conversion Finder which accepts an n × n connectivity table of exchange rates (where 0 denotes no direct exchange rate between two currencies), and can calculate the best conversion rate between any two specified
currencies (both ways), and how that rate can be obtained both ways as
sequences of exchanges. (10 marks)
Arbitrage Finder which accepts an n × n table of exchange rates and efficently finds whether there is arbitrage in the system, a closed loop of
exchanges that results in a profit. If so then all the occurrences of arbitrage, currencies v0, v1, v2, . . . , vk−1 for which rv0v1
· rv1v2
· . . . · rvk−1v0 > 1
should be found (allowing a currency trader to exploit a price differential
to make a profit). (20 marks)


运行截图:

部分代码:
main.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
String filePath = "c.txt";
Scanner scanner = new Scanner(new File(filePath));
// Question 1 Best Conversion Finder
StdOut.println("Question 1 Best Conversion Finder");
// V currencies
int V = scanner.nextInt();
String[] name = new String[V];
// create network
EdgeWeightedDigraph G = new EdgeWeightedDigraph(V);
for (int v = 0; v < V; v++) {
name[v] = scanner.next();
for (int w = 0; w < V; w++) {
double rate = scanner.nextDouble();
DirectedEdge e = new DirectedEdge(v, w, -Math.log(rate));
G.addEdge(e);
}
}
// Question 1 Best Conversion Finder
StdOut.println("Input a index of currency: ");
for (int i = 0; i < V; i++) {
StdOut.printf("%s - %d \n", name[i], i);
}
Scanner sc = new Scanner(System.in);
int s = sc.nextInt();
// compute shortest paths
ArbitrageFinder AF = new ArbitrageFinder(G, s);
// print negative cycle
if (AF.hasNegativeCycle()) {
StdOut.println("Has Negative cycle, can not compute");
}
// print shortest paths
else {
for (int v = 0; v < G.V(); v++) {
if (AF.hasPathTo(v)) {
StdOut.printf("%s to %s (%5.2f): ", name[s], name[v], 1 / Math.pow(Math.E, AF.distTo(v)));
for (DirectedEdge e : AF.pathTo(v)) {
StdOut.printf("%s -> %s (%4.2f) ", name[e.from()], name[e.to()], 1 / Math.pow(Math.E, e.weight()));
}
StdOut.println();
}
else {
StdOut.printf("%d to %d no path\n", name[s], name[v]);
}
}
}
// Question 2 Arbitrage Finder
StdOut.println("\nQuestion 2 Arbitrage Finder");
ArbitrageFinder AF1 = new ArbitrageFinder(G, 0);
if (AF.hasNegativeCycle()) {
StdOut.println("Find arbitrage opportunity");
double temp = 1.0;
for (DirectedEdge e : AF1.negativeCycle()) {
StdOut.printf("%6.2f %s ", temp, name[e.from()]);
temp *= Math.exp(-e.weight());
StdOut.printf("= %6.2f %s\n", temp, name[e.to()]);
}
} else {
StdOut.println("No arbitrage opportunity");
}
// Question 3 Bridge Exchange Finder
StdOut.println("\nQuestion 3 Bridge Exchange Finder");
EdgeWeightedGraph G1 = new EdgeWeightedGraph(V);
Scanner scanner1 = new Scanner(new File(filePath));
V = scanner1.nextInt();
// create network
for (int v = 0; v < V; v++) {
name[v] = scanner1.next();
for (int w = 0; w < V; w++) {
double rate = scanner1.nextDouble();
Edge e = new Edge(v, w, -Math.log(rate));
G1.addEdge(e);
}
}
Prim prim = new Prim(G1);
for (Edge e : prim.edges()) {
StdOut.printf("%s - %s \n", name[e.getW()], name[e.either()]);
}
}
}
项目截图

该文章涉及一个关于货币兑换图的任务,要求开发用于货币交易的图形工具。BestConversionFinder组件计算最佳兑换路径和汇率,而ArbitrageFinder检测系统中是否存在套利机会。如果存在,它会找出能带来利润的交易循环。此外,还提到了使用Prim算法查找桥接交换。
609

被折叠的 条评论
为什么被折叠?



