-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathBoostMinSpanningTree.cpp
More file actions
94 lines (76 loc) · 3.02 KB
/
BoostMinSpanningTree.cpp
File metadata and controls
94 lines (76 loc) · 3.02 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
//===- BoostMinSpanningTree.cpp
//-------------------------------------------------------===//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//===----------------------------------------------------------------------===//
//
// This file implements the benchmark for Boost Minimum Spanning Tree.
//
//===----------------------------------------------------------------------===//
#include <iostream>
#include <benchmark/benchmark.h>
#include <boost/graph/undirected_graph.hpp>
#include <boost/graph/exterior_property.hpp>
#include <boost/graph/prim_minimum_spanning_tree.hpp>
#include <vector>
#include <Utility/Utils.h>
#define VERTICES 10
#define MAX_WEIGHT 1000
using namespace std;
namespace {
typedef int t_weight;
// define the graph type
typedef boost::property<boost::edge_weight_t, t_weight> EdgeWeightProperty;
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS,
boost::no_property, EdgeWeightProperty>
Graph;
typedef boost::graph_traits<Graph>::vertex_descriptor VerticesMap;
Graph g;
} // namespace
void initializeBoostMinSpanningTree() {
const int vertices = VERTICES;
int num_edges = VERTICES * (VERTICES - 1) / 2;
std::vector<int> edges;
std::vector<int> weight;
graph::generateRandomGraph(edges, weight, vertices, MAX_WEIGHT);
for (std::size_t k = 0; k < num_edges; ++k) {
boost::add_edge(edges[k * 2] - 1, edges[k * 2 + 1] - 1, weight[k], g);
}
// set the parent vector to receive the minimum spanning tree output
std::vector<VerticesMap> p(num_vertices(g));
}
// Benchmarking function.
static void BoostMinSpanningTree(benchmark::State &state) {
for (auto _ : state) {
// set the parent vector to receive the minimum spanning tree output
std::vector<VerticesMap> p(num_vertices(g));
for (int i = 0; i < state.range(0); ++i) {
boost::prim_minimum_spanning_tree(g, &p[0]);
}
}
}
// Register benchmarking function.
BENCHMARK(BoostMinSpanningTree)->Arg(1);
void generateResultBoostMinSpanningTree() {
initializeBoostMinSpanningTree();
// set the parent vector to receive the minimum spanning tree output
std::vector<VerticesMap> p(num_vertices(g));
std::cout << "-------------------------------------------------------\n";
std::cout << "[ Boost Minimum Spanning Tree Result Information ]\n";
boost::prim_minimum_spanning_tree(g, &p[0]);
for (int i = 0; i < p.size(); i++) {
std::cout << "p[" << i << "] = " << p[i] << ", ";
}
std::cout << "Boost Minimum Spanning Tree operation finished!\n";
}