-
Notifications
You must be signed in to change notification settings - Fork 7
/
min_spanning_forest.cpp
221 lines (179 loc) · 6.07 KB
/
min_spanning_forest.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
// min_spanning_forest.cpp
/*===========================================================================
*
* PUBLIC DOMAIN NOTICE
* National Center for Biotechnology Information
*
* This software/database is a "United States Government Work" under the
* terms of the United States Copyright Act. It was written as part of
* the author's official duties as a United States Government employee and
* thus cannot be copyrighted. This software/database is freely available
* to the public for use. The National Library of Medicine and the U.S.
* Government have not placed any restriction on its use or reproduction.
*
* Although all reasonable efforts have been taken to ensure the accuracy
* and reliability of the software and data, the NLM and the U.S.
* Government do not and cannot warrant the performance or results that
* may be obtained by using this software or data. The NLM and the U.S.
* Government disclaim all warranties, express or implied, including
* warranties of performance, merchantability or fitness for any particular
* purpose.
*
* Please cite the author in any work or product based on this material.
*
* ===========================================================================
*
* Author: Vyacheslav Brover
*
* File Description:
* Minimum spanning forest
*
*/
#undef NDEBUG
#include "common.hpp"
#include "graph.hpp"
using namespace Common_sp;
#include "version.inc"
#include "common.inc"
namespace
{
struct Item final : DiGraph::Node
{
const string name;
Item (DiGraph &graph_arg,
const string &name_arg)
: DiGraph::Node (graph_arg)
, name (name_arg)
{}
};
struct Link final : DiGraph::Arc
{
const double weight;
Link (Item* start,
Item* end,
double weight_arg)
: DiGraph::Arc (start, end)
, weight (weight_arg)
{
ASSERT (! isNan (weight));
}
void saveText (ostream &os) const final
{ os << static_cast <const Item*> (node [false]) -> name
<< '\t' << weight
<< '\t' << static_cast <const Item*> (node [true]) -> name
<< '\n';
}
bool operator< (const Link &other) const
{ return weight > other. weight; }
};
struct ThisApplication final : Application
{
ThisApplication ()
: Application ("Print minimum spanning forest per connected component")
{
version = VERSION;
addPositional ("in", "File with lines: <item1> <item2> <weight>. Sorted by <weight> ascending, or descending if -max");
addFlag ("max", "<weight> must be maximized in the forest, otherwise minimized");
addKey ("rename", "File with pairs: <item> <tab> <new name>");
}
void body () const final
{
const string inFName = getArg ("in");
const bool maxP = getFlag ("max");
const string matchFName = getArg ("rename");
KeyValue old2new;
if (! matchFName. empty ())
{
LineInput fIn (matchFName);
Set<string> newNames;
while (fIn. nextLine ())
{
string& newName = fIn. line;
const string oldName (findSplit (newName, '\t'));
QC_ASSERT (! newName. empty ());
if (contains (old2new, oldName))
throw runtime_error ("Duplicate old name: " + strQuote (oldName));
if (newNames. contains (newName))
throw runtime_error ("Duplicate new name: " + strQuote (newName));
old2new [oldName] = newName;
}
}
unordered_map<string/*Item::name*/,Item*> items; items. rehash (100000); // PAR
DiGraph gr; // Of Item, DAG
{
double weight_prev = - numeric_limits<double>::max ();
if (maxP)
weight_prev *= -1;
LineInput fIn (inFName);
Istringstream iss;
while (fIn. nextLine ())
{
string s1;
string s2;
double weight = NaN;
iss. reset (fIn. line);
iss >> s1 >> s2 >> weight;
QC_ASSERT (! s2. empty ());
QC_ASSERT (! isNan (weight));
if ( (maxP && weight > weight_prev)
|| (! maxP && weight < weight_prev)
)
throw runtime_error ("Weight disorder: " + fIn. lineStr ());
trim (s1);
trim (s2);
if (s1 == s2)
continue;
s1 = find (old2new, s1, true);
s2 = find (old2new, s2, true);
// n1, n2
if (! contains (items, s1))
items [s1] = new Item (gr, s1);
if (! contains (items, s2))
items [s2] = new Item (gr, s2);
Item* n1 = items [s1];
Item* n2 = items [s2];
ASSERT (n1);
ASSERT (n2);
ASSERT (n1 != n2);
if (n1->getDisjointCluster () != n2->getDisjointCluster ())
{
new Link (n1, n2, weight);
n1->DisjointCluster::merge (*n2);
}
weight_prev = weight;
}
}
ASSERT (gr. nodes. size () == items. size ());
gr. qc ();
map<const DisjointCluster*,VectorPtr<Item>> clusters;
for (auto& it : items)
clusters [it. second->getDisjointCluster ()] << it. second;
bool first = false;
for (const auto& it : clusters)
{
const VectorPtr<Item>& cluster = it. second;
ASSERT (! cluster . empty ());
VectorPtr<Link> links; links. reserve (cluster. size () * 10); // PAR
for (const Item* item : cluster)
for (const DiGraph::Arc* arc : item->arcs [true])
{
ASSERT (arc);
links << static_cast <const Link*> (arc);
}
links. sortPtr ();
if (first)
first = false;
else
cout << endl;
for (const Link* link : links)
link->saveText (cout);
}
}
};
} // namespace
int main (int argc,
const char* argv[])
{
ThisApplication app;
return app. run (argc, argv);
}