-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmin-cash-flow.cpp
292 lines (229 loc) · 9.16 KB
/
min-cash-flow.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include <bits/stdc++.h>
using namespace std;
class bank{
public:
string name;
int netAmount;
set<string> types;
};
int getMinIndex(bank listOfNetAmounts[],int numBanks){
int min=INT_MAX, minIndex=-1;
for(int i=0;i<numBanks;i++){
if(listOfNetAmounts[i].netAmount == 0) continue;
if(listOfNetAmounts[i].netAmount < min){
minIndex = i;
min = listOfNetAmounts[i].netAmount;
}
}
return minIndex;
}
int getSimpleMaxIndex(bank listOfNetAmounts[],int numBanks){
int max=INT_MIN, maxIndex=-1;
for(int i=0;i<numBanks;i++){
if(listOfNetAmounts[i].netAmount == 0) continue;
if(listOfNetAmounts[i].netAmount > max){
maxIndex = i;
max = listOfNetAmounts[i].netAmount;
}
}
return maxIndex;
}
pair<int,string> getMaxIndex(bank listOfNetAmounts[],int numBanks,int minIndex,bank input[],int maxNumTypes){
int max=INT_MIN;
int maxIndex=-1;
string matchingType;
for(int i=0;i<numBanks;i++){
if(listOfNetAmounts[i].netAmount == 0) continue;
if(listOfNetAmounts[i].netAmount < 0) continue;
//TODO
//see complexity of intersection
vector<string> v(maxNumTypes);
vector<string>::iterator ls=set_intersection(listOfNetAmounts[minIndex].types.begin(),listOfNetAmounts[minIndex].types.end(), listOfNetAmounts[i].types.begin(),listOfNetAmounts[i].types.end(), v.begin());
if((ls-v.begin())!=0 && max<listOfNetAmounts[i].netAmount ){
max=listOfNetAmounts[i].netAmount;
maxIndex=i;
matchingType = *(v.begin());
}
}
//if there is NO such max which has a common type with any remaining banks then maxIndex has -1
// also return the common payment type
return make_pair(maxIndex,matchingType);
}
void printAns(vector<vector<pair<int,string>>> ansGraph, int numBanks,bank input[]){
cout<<"\nThe transactions for minimum cash flow are as follows : \n\n";
for(int i=0;i<numBanks;i++){
for(int j=0;j<numBanks;j++){
if(i==j) continue;
if(ansGraph[i][j].first != 0 && ansGraph[j][i].first != 0){
if(ansGraph[i][j].first == ansGraph[j][i].first){
ansGraph[i][j].first=0;
ansGraph[j][i].first=0;
}
else if(ansGraph[i][j].first > ansGraph[j][i].first){
ansGraph[i][j].first -= ansGraph[j][i].first;
ansGraph[j][i].first =0;
cout<<input[i].name<<" pays Rs" << ansGraph[i][j].first<< "to "<<input[j].name<<" via "<<ansGraph[i][j].second<<endl;
}
else{
ansGraph[j][i].first -= ansGraph[i][j].first;
ansGraph[i][j].first = 0;
cout<<input[j].name<<" pays Rs "<< ansGraph[j][i].first<<" to "<<input[i].name<<" via "<<ansGraph[j][i].second<<endl;
}
}
else if(ansGraph[i][j].first != 0){
cout<<input[i].name<<" pays Rs "<<ansGraph[i][j].first<<" to "<<input[j].name<<" via "<<ansGraph[i][j].second<<endl;
}
else if(ansGraph[j][i].first != 0){
cout<<input[j].name<<" pays Rs "<<ansGraph[j][i].first<<" to "<<input[i].name<<" via "<<ansGraph[j][i].second<<endl;
}
ansGraph[i][j].first = 0;
ansGraph[j][i].first = 0;
}
}
cout<<"\n";
}
void minimizeCashFlow(int numBanks,bank input[],unordered_map<string,int>& indexOf,int numTransactions,vector<vector<int>>& graph,int maxNumTypes){
//Find net amount of each bank has
bank listOfNetAmounts[numBanks];
for(int b=0;b<numBanks;b++){
listOfNetAmounts[b].name = input[b].name;
listOfNetAmounts[b].types = input[b].types;
int amount = 0;
//incoming edges
//column travers
for(int i=0;i<numBanks;i++){
amount += (graph[i][b]);
}
//outgoing edges
//row traverse
for(int j=0;j<numBanks;j++){
amount += ((-1) * graph[b][j]);
}
listOfNetAmounts[b].netAmount = amount;
}
vector<vector<pair<int,string>>> ansGraph(numBanks,vector<pair<int,string>>(numBanks,{0,""}));//adjacency matrix
//find min and max net amount
int numZeroNetAmounts=0;
for(int i=0;i<numBanks;i++){
if(listOfNetAmounts[i].netAmount == 0) numZeroNetAmounts++;
}
while(numZeroNetAmounts!=numBanks){
int minIndex=getMinIndex(listOfNetAmounts, numBanks);
pair<int,string> maxAns = getMaxIndex(listOfNetAmounts, numBanks, minIndex,input,maxNumTypes);
int maxIndex = maxAns.first;
if(maxIndex == -1){
(ansGraph[minIndex][0].first) += abs(listOfNetAmounts[minIndex].netAmount);
(ansGraph[minIndex][0].second) = *(input[minIndex].types.begin());
int simpleMaxIndex = getSimpleMaxIndex(listOfNetAmounts, numBanks);
(ansGraph[0][simpleMaxIndex].first) += abs(listOfNetAmounts[minIndex].netAmount);
(ansGraph[0][simpleMaxIndex].second) = *(input[simpleMaxIndex].types.begin());
listOfNetAmounts[simpleMaxIndex].netAmount += listOfNetAmounts[minIndex].netAmount;
listOfNetAmounts[minIndex].netAmount = 0;
if(listOfNetAmounts[minIndex].netAmount == 0) numZeroNetAmounts++;
if(listOfNetAmounts[simpleMaxIndex].netAmount == 0) numZeroNetAmounts++;
}
else{
int transactionAmount = min(abs(listOfNetAmounts[minIndex].netAmount), listOfNetAmounts[maxIndex].netAmount);
(ansGraph[minIndex][maxIndex].first) += (transactionAmount);
(ansGraph[minIndex][maxIndex].second) = maxAns.second;
listOfNetAmounts[minIndex].netAmount += transactionAmount;
listOfNetAmounts[maxIndex].netAmount -= transactionAmount;
if(listOfNetAmounts[minIndex].netAmount == 0) numZeroNetAmounts++;
if(listOfNetAmounts[maxIndex].netAmount == 0) numZeroNetAmounts++;
}
}
printAns(ansGraph,numBanks,input);
// cout<<"HI\n";
}
//correct
int main()
{
cout<<"\n\t\t\t\t********************* Welcome to CASH FLOW MINIMIZER SYSTEM ***********************\n\n\n";
cout<<"This system minimizes the number of transactions among multiple banks in the different corners of the world that use different modes of payment.There is one world bank (with all payment modes) to act as an intermediary between banks that have no common mode of payment. \n\n";
cout<<"Enter the number of banks participating in the transactions.\n";
int numBanks;cin>>numBanks;
bank input[numBanks];
unordered_map<string,int> indexOf;//stores index of a bank
cout<<"Enter the details of the banks and transactions as stated:\n";
cout<<"Bank name ,number of payment modes it has and the payment modes.\n";
cout<<"Bank name and payment modes should not contain spaces\n";
int maxNumTypes;
for(int i=0; i<numBanks;i++){
if(i==0){
cout<<"World Bank : ";
}
else{
cout<<"Bank "<<i<<" : ";
}
cin>>input[i].name;
indexOf[input[i].name] = i;
int numTypes;
cin>>numTypes;
if(i==0) maxNumTypes = numTypes;
string type;
while(numTypes--){
cin>>type;
input[i].types.insert(type);
}
}
cout<<"Enter number of transactions.\n";
int numTransactions;
cin>>numTransactions;
vector<vector<int>> graph(numBanks,vector<int>(numBanks,0));//adjacency matrix
cout<<"Enter the details of each transaction as stated:";
cout<<"Debtor Bank , creditor Bank and amount\n";
cout<<"The transactions can be in any order\n";
for(int i=0;i<numTransactions;i++){
cout<<(i)<<" th transaction : ";
string s1,s2;
int amount;
cin >> s1>>s2>>amount;
graph[indexOf[s1]][indexOf[s2]] = amount;
}
//settle
minimizeCashFlow(numBanks,input,indexOf,numTransactions,graph,maxNumTypes);
return 0;
}
/*
5
A 2 t1 t2
B 1 t1
C 1 t1
D 1 t2
E 1 t2
4
B A 300
C A 700
D B 500
E B 500
--------
5
World_Bank 2 Google_Pay PayTM
Bank_B 1 Google_Pay
Bank_C 1 Google_Pay
Bank_D 1 PayTM
Bank_E 1 PayTM
4
Bank_B World_Bank 300
Bank_C World_Bank 700
Bank_D Bank_B 500
Bank_E Bank_B 500
--------------------
6
B 3 1 2 3
C 2 1 2
D 1 2
E 2 1 3
F 1 3
G 2 2 3
9
G B 30
G D 10
F B 10
F C 30
F D 10
F E 10
B C 40
C D 20
D E 50
*/