forked from dilsonpereira/Minimum-Cost-Perfect-Matching
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample.cpp
More file actions
202 lines (166 loc) · 3.88 KB
/
Example.cpp
File metadata and controls
202 lines (166 loc) · 3.88 KB
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
#include "Matching.h"
#include <fstream>
#include "Graph.h"
#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
using namespace MWPM;
pair< Graph, vector<double> > CreateRandomGraph()
{
//random seed
int x;
cin >> x;
srand( x );
//Please see Graph.h for a description of the interface
int n = 50;
Graph G(n);
vector<double> cost;
for(int i = 0; i < n; i++)
for(int j = i+1; j < n; j++)
if(rand()%10 == 0)
{
G.AddEdge(i, j);
cost.push_back(rand()%1000);
}
return make_pair(G, cost);
}
Graph ReadGraph(string filename)
{
//Please see Graph.h for a description of the interface
ifstream file;
file.open(filename.c_str());
string s;
getline(file, s);
stringstream ss(s);
int n;
ss >> n;
getline(file, s);
ss.str(s);
ss.clear();
int m;
ss >> m;
Graph G(n);
for(int i = 0; i < m; i++)
{
getline(file, s);
ss.str(s);
ss.clear();
int u, v;
ss >> u >> v;
G.AddEdge(u, v);
}
file.close();
return G;
}
pair< Graph, vector<double> > ReadWeightedGraph(string filename)
{
//Please see Graph.h for a description of the interface
ifstream file;
file.open(filename.c_str());
string s;
getline(file, s);
stringstream ss(s);
int n;
ss >> n;
getline(file, s);
ss.str(s);
ss.clear();
int m;
ss >> m;
Graph G(n);
vector<double> cost(m);
for(int i = 0; i < m; i++)
{
getline(file, s);
ss.str(s);
ss.clear();
int u, v;
double c;
ss >> u >> v >> c;
G.AddEdge(u, v);
cost[G.GetEdgeIndex(u, v)] = c;
}
file.close();
return make_pair(G, cost);
}
void MinimumCostPerfectMatchingExample(string filename)
{
Graph G;
vector<double> cost;
//Read the graph
pair< Graph, vector<double> > p = ReadWeightedGraph(filename);
//pair< Graph, vector<double> > p = CreateRandomGraph();
G = p.first;
cost = p.second;
//Create a Matching instance passing the graph
Matching M(G);
//Pass the costs to solve the problem
pair< list<int>, double > solution = M.SolveMinimumCostPerfectMatching(cost);
list<int> matching = solution.first;
double obj = solution.second;
cout << "Optimal matching cost: " << obj << endl;
cout << "Edges in the matching:" << endl;
for(list<int>::iterator it = matching.begin(); it != matching.end(); it++)
{
pair<int, int> e = G.GetEdge( *it );
cout << e.first << " " << e.second << endl;
}
}
void MaximumMatchingExample(string filename)
{
Graph G = ReadGraph(filename);
Matching M(G);
list<int> matching;
matching = M.SolveMaximumMatching();
cout << "Number of edges in the maximum matching: " << matching.size() << endl;
cout << "Edges in the matching:" << endl;
for(list<int>::iterator it = matching.begin(); it != matching.end(); it++)
{
pair<int, int> e = G.GetEdge( *it );
cout << e.first << " " << e.second << endl;
}
}
int main(int argc, char* argv[])
{
string filename = "";
string algorithm = "";
int i = 1;
while(i < argc)
{
string a(argv[i]);
if(a == "-f")
filename = argv[++i];
else if(a == "--minweight")
algorithm = "minweight";
else if(a == "--max")
algorithm = "max";
i++;
}
if(filename == "" || algorithm == "")
{
cout << "usage: ./example -f <filename> <--minweight | --max>" << endl;
cout << "--minweight for minimum weight perfect matching" << endl;
cout << "--max for maximum cardinality matching" << endl;
cout << "file format:" << endl;
cout << "the first two lines give n (number of vertices) and m (number of edges)," << endl;
cout << "followed by m lines, each with a tuple (u, v [, c]) representing the edges," << endl;
cout << "where u and v are the endpoints (0-based indexing) of the edge and c is its cost" << endl;
cout << "the cost is optional if --max is specified" << endl;
return 1;
}
try
{
if(algorithm == "minweight")
MinimumCostPerfectMatchingExample(filename);
else
MaximumMatchingExample(filename);
}
catch(const char * msg)
{
cout << msg << endl;
return 1;
}
return 0;
}