-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.cpp
200 lines (164 loc) · 4.14 KB
/
graph.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <algorithm>
#include <iostream>
#include <list>
#include <vector>
#include <queue>
#include <map>
#include <cmath>
#include "graph.h"
using namespace std;
void Graph::printGraph() {
for (const auto e : edgeLabels) {
cout << get<0>(e.first) << " " << get<1>(e.first) << " " << e.second << endl;
}
}
void Graph::printSimpleCycles() {
// Copy
priority_queue<vector<int>, vector<vector<int>>, CompareVectSize> sc(simpleCycles);
cout << "\nSimple Cycles:" << endl;
while (!sc.empty()) {
vector<int> c = sc.top();
sc.pop();
for (const auto i : c) {
cout << toNodes[i] << "->";
}
cout << endl;
}
}
// Johnson's unblock() procedure
void Graph::unblock(int u)
{
blocked[u] = false;
while (!B[u].empty()) {
int w = B[u].front();
B[u].pop_front();
if (blocked[w]) {
unblock(w);
}
}
}
// Johnson's output-circuit procedure
void Graph::outputCycle()
{
vector<int> cycle;
for (auto i = stack.begin(); i != stack.end(); i++) {
cycle.push_back(*i);
}
cycle.push_back(*stack.begin());
simpleCycles.push(cycle);
}
// Johnson's circuit() procedure
bool Graph::cycle(int v)
{
blocked[v] = true;
bool F = false;
stack.push_back(v);
for (int w : AK[v]) {
if (w == S) {
outputCycle();
F = true;
} else if (w > S && !blocked[w]) {
F = cycle(w);
}
}
if (F) {
unblock(v);
} else {
for (int w : AK[v]) {
if (find(B[w].begin(), B[w].end(), v) == B[w].end()) {
B[w].push_back(v);
}
}
}
stack.pop_back();
return F;
}
// Start of Johnson's Circuit-Finding algorithm
void Graph::getJohnsonSimpleCycles()
{
S = 0;
stack.clear();
while (S < N) {
for (int i = S; i < N; i++) {
B[i].clear();
blocked[i] = false;
}
cycle(S);
S++;
}
simpleCyclesUpdate = false; // Flag to update
}
// Reverse graph and update AK and edgeLabels objects
void Graph::reverseGraph() {
vector<vector<int>> newAK;
map<pair<int,int>,double> newEdgeLabels;
newAK.resize(AK.size());
for (auto e : edgeLabels) {
int src = get<0>(e.first);
int dest = get<1>(e.first);
double label = e.second;
newEdgeLabels[{dest,src}] = label;
newAK[toIndex[dest]].push_back(toIndex[src]);
}
this->AK = newAK;
this->edgeLabels = newEdgeLabels;
simpleCyclesUpdate = true;
}
// Find and print the length of the longest cycle that each node belongs to
void Graph::getLongestCycleLengthPerNode() {
vector<bool> hasCycle(N,false);
vector<int> cycleLengths(N,0);
if (simpleCyclesUpdate) {
getJohnsonSimpleCycles();
}
// Copy priority queue of cycles
priority_queue<vector<int>, vector<vector<int>>, CompareVectSize> sc(simpleCycles);
// Get lengths of cycles and assign to nodes
while (!sc.empty()) {
vector<int> c = sc.top();
sc.pop();
for (auto &node : c) {
if (!hasCycle[node]) {
cycleLengths[node] = c.size() - 1;
hasCycle[node] = true;
}
}
}
for (pair<int, int> element : toIndex) {
cout << element.first << " " << cycleLengths[element.second] << endl;
}
}
// Find and print best arbitrage opportunity via
// smallest negative log-sum cycle
void Graph::getMaxArbitrage()
{
vector<int> bestCycle;
double minCycleSum = 0;
if (simpleCyclesUpdate) {
getJohnsonSimpleCycles();
}
// Determine smallest negative log-sum cycle
while (!simpleCycles.empty()) {
vector<int> cycle = simpleCycles.top();
simpleCycles.pop();
double cycleSum = 0;
for (int i = 0; i < cycle.size() - 1; i++) {
pair<int,int> edge({toNodes[cycle[i]],toNodes[cycle[i+1]]});
// Use -log(label) to minimize sum of cycle labels
cycleSum += -log(edgeLabels[edge]);
}
if (cycleSum < minCycleSum) {
minCycleSum = cycleSum;
bestCycle = cycle;
}
}
if (minCycleSum == 0) {
cout << "No arbitrage opportunity." << endl;
return;
}
// Print minimal subgraph of best arbitrage opportunity
for (int i = 0; i < bestCycle.size() - 1; i++) {
pair<int,int> edge({toNodes[bestCycle[i]],toNodes[bestCycle[i+1]]});
cout << toNodes[bestCycle[i]] << " " << toNodes[bestCycle[i+1]] << " " << edgeLabels[edge] << endl;
}
}