-
Notifications
You must be signed in to change notification settings - Fork 3
/
mst.h
305 lines (277 loc) · 8.93 KB
/
mst.h
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
293
294
295
296
297
298
299
300
301
302
303
304
305
///////////////////////////////////////////////////////////////////
// Copyright (c) 2018 Rohit Sharma. All rights reserved.
// This program is free software; you can redistribute it and/or
// modify it under the terms as GNU General Public License.
///////////////////////////////////////////////////////////////////
//
//
// Minimal Spanning Trees argorithms.
//
#ifndef GRAPH_MST_H
#define GRAPH_MST_H
#include <algorithm>
#include "graph.h"
#include "heap.h"
using namespace std;
namespace MST {
struct treeNode {
public:
const basicGraph::bNode* node_; // to node in graph node
treeNode* parent_; // from node in graph node
size_t iset_; // index of set
size_t weight_; // weight of the edge from parent to this node.
treeNode(const basicGraph::bNode* node, size_t i = 0, size_t wt = 0) :
node_(node), parent_(nullptr), iset_(i), weight_(wt)
{}
void setParent(treeNode* other, size_t wt) {
other->parent_ = this;
other->weight_ = wt;
}
};
struct treeNodeCompare
{
bool operator() (const treeNode* tn1, const treeNode* tn2) const
{
return basicGraph::nodeCmp.operator()(tn1->node_, tn2->node_);
}
};
class minTree {
public:
set<treeNode*, treeNodeCompare> tree_;
virtual void build() = 0;
bool nodePartOfMST(const basicGraph::bNode* gnode)
{
treeNode tmpTreeNode = treeNode(gnode);
return tree_.find(&tmpTreeNode) != tree_.end();
}
treeNode* getTreeNode(const basicGraph::bNode* gnode)
{
treeNode tmpTreeNode = treeNode(gnode);
set<treeNode*, treeNodeCompare>::iterator niter = tree_.find(&tmpTreeNode);
return niter == tree_.end() ? nullptr : *niter;
}
void print(bool edges_only=false)
{
set<treeNode*, treeNodeCompare>::iterator niter = tree_.begin();
for (; niter != tree_.end(); niter++)
{
treeNode* tnode = (*niter);
if (!edges_only || tnode->parent_)
{
cout << (tnode->parent_ ? tnode->parent_->node_->name() : "root") << " "
<< tnode->node_->name() << " "
<< tnode->weight_;
// cout << " (set " << tnode->iset_ << ")";
cout << "\n";
}
}
}
~minTree()
{
set<treeNode*, treeNodeCompare>::iterator niter = tree_.begin();
for (; niter != tree_.end(); niter++)
delete (*niter);
tree_.clear();
}
};
// ####################
// Kruskal's algorithm
// ####################
// Kruskal's algorithm is a minimum-spanning-tree algorithm which finds an edge
// of the least possible weight that connects any two trees in the forest. It
// is a greedy algorithm in graph theory as it finds a minimum spanning tree
// for a connected weighted graph adding increasing cost arcs at each step.
//
// Note: This only works for undirected graphs.
// Pseudo Code:
// KRUSKAL-MST(G, w)
// T: = Ø
// for each vertex u in V
// MAKE - SET(tree_, u)
// end for
// for each edge(u, v) in E (sortedEdges) in order of nondecreasing weight
// if FIND - SET(tree_, u) != FIND - SET(tree_, v)
// UNION - SET(tree_, u, v)
// T : = T U{ (u,v) }
// end for
// return T
class kruskal : public minTree {
private:
const basicGraph::bGraph* graph_;
// unionSet is used to combine two trees to one by adding an
// edge between two trees with nodes in two disjoint sets.
// TODO: complexity is linear. Can be reduced to logarithmic
void unionSet(treeNode* u, treeNode* v, size_t wt, bool directed)
{
size_t v_iset = v->iset_;
set<treeNode*, treeNodeCompare>::iterator niter = tree_.begin();
for (; niter != tree_.end(); niter++)
{
treeNode* tnode = *niter;
if (tnode->iset_ == v_iset)
{
tnode->iset_ = u->iset_;
}
}
// change v's parent-child relationship for undirected graphs.
if (!directed && v->parent_ != nullptr)
{
v->parent_->parent_ = v;
v->parent_->weight_ = v->weight_;
}
// set v's parent to u.
u->setParent(v, wt);
return;
}
public:
kruskal(const basicGraph::bGraph* graph) : minTree(), graph_(graph)
{}
~kruskal()
{}
// build MST.
void build()
{
if (!graph_)
{
cerr << "Error: graph not found.";
return;
} if (graph_->nNodes() == 0)
{
cerr << "Error: no nodes found in the graph.";
return;
}
if (graph_->directed())
cerr << "Warning: Kruskal's algorithm may produce invalid tree or \n"
<< " more than one spanning multitree on directed graphs.\n";
// create an empty tree node and add it to set.
size_t set_index = 1;
set<const basicGraph::bNode*, basicGraph::nodeCompare>::iterator niter;
for (niter = graph_->nodeBegin(); niter != graph_->nodeEnd(); niter++)
{
tree_.insert(new treeNode(*niter, set_index++));
}
vector<const basicGraph::bEdge*> sortedEdges;
set<const basicGraph::bEdge*, basicGraph::edgeCompare>::iterator eiter;
for (eiter = graph_->edgeBegin(); eiter != graph_->edgeEnd(); eiter++)
{
sortedEdges.push_back(*eiter);
}
sort(sortedEdges.begin(), sortedEdges.end(), basicGraph::edgeCmp);
for (size_t i = 0; i < sortedEdges.size(); i++)
{
const basicGraph::bNode* u = sortedEdges[i]->n1();
const basicGraph::bNode* v = sortedEdges[i]->n2();
treeNode tmpUNode = treeNode(u);
treeNode tmpVNode = treeNode(v);
auto uset = tree_.find(&tmpUNode);
auto vset = tree_.find(&tmpVNode);
if (uset != tree_.end() && vset != tree_.end() &&
(*uset)->iset_ != (*vset)->iset_ )
{
treeNode* tuset = *uset;
treeNode* tvset = *vset;
unionSet(tuset, tvset, dynamic_cast<const basicGraph::bWeightedEdge*>(sortedEdges[i])->weight(), graph_->directed());
}
}
}
};
// #################
// Prim's Algorithm
// #################
// procedure prim(G,W)
// for i = 1 to n do
// MinHeap[i] ← i
// WhereInHeap[i] ← i
// d[i] ← ∞
// outside[i] ← true
// π[i] ← NIL
// end for
// d[1] ← 0
// for i = n downto 1 do
// u ← MinHeap[1]
// MinHeap[1] ← MinHeap(n)
// WhereInHeap[MinHeap[1]] ← SiftDown(MinHeap, 1, n - 1, d)
// for each v ∈ adj[u] do
// if v ∈ outside and W[u, v] < d[v] then
// d[v] ← W[u, v]
// π[v] ← u
// WhereInHeap[v] ← SiftUp(MinHeap, WhereInHeap[v], d)
// end if
// end for
// end for
// end procedure
class prim : public minTree {
private:
const basicGraph::bGraph* graph_;
bool nodeNotInMST(treeNode* tn)
{
return tree_.find(tn) == tree_.end();
}
void addEdgesToHeap(const basicGraph::bNode* node,
Heap<const basicGraph::bWeightedEdge*, basicGraph::edgeCompare>& minEdgeHeap)
{
set<const basicGraph::bEdge*, basicGraph::edgeCompare>::iterator eiter = node->edgeBegin();
for (; eiter != node->edgeEnd(); eiter++) {
if (! nodePartOfMST((*eiter)->n1()) || !nodePartOfMST((*eiter)->n2()) )
minEdgeHeap.push(dynamic_cast<const basicGraph::bWeightedEdge*>(*eiter));
}
}
public:
prim(const basicGraph::bGraph* graph) : minTree(), graph_(graph)
{}
// build MST.
void build()
{
if (!graph_)
{
cerr << "Error: graph not found.";
return;
}
if (graph_->nNodes() == 0)
{
cerr << "Error: no nodes found in the graph.";
return;
}
if (graph_->directed())
{
cerr << "Warning: Prim's algorithm may produce invalid tree or \n"
<< " more than one spanning multitree on directed graphs.\n";
}
Heap<const basicGraph::bWeightedEdge*, basicGraph::edgeCompare> minEdgeHeap;
// Initialize a tree with a single vertex, chosen arbitrrily from the graph.
set<const basicGraph::bNode*, basicGraph::nodeCompare>::iterator niter = graph_->nodeBegin();
const basicGraph::bNode* node = *niter;
tree_.insert(new treeNode(node, 0));
// Grow the tree by one edge : of the edges that connect the tree
// to vertices not yet in the tree, find the minimum - weight edge,
// and transfer it to the tree.
addEdgesToHeap(node, minEdgeHeap);
const basicGraph::bWeightedEdge* minEdge = nullptr;
while (minEdgeHeap.pop(minEdge))
{
// get the node, not in MST
MST::treeNode* tn1 = getTreeNode(minEdge->n1());
MST::treeNode* tn2 = getTreeNode(minEdge->n2());
if (tn1 && tn2==nullptr)
{
tn2 = new treeNode(minEdge->n2());
tree_.insert(tn2);
tn1->setParent(tn2, minEdge->weight());
addEdgesToHeap(tn2->node_, minEdgeHeap);
}
else if (tn1==nullptr && tn2)
{
tn1 = new treeNode(minEdge->n1());
tree_.insert(tn1);
tn2->setParent(tn1, minEdge->weight());
addEdgesToHeap(tn1->node_, minEdgeHeap);
}
else
{
continue;
}
}
}
};
}
#endif