Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Prim-Jarnik's algorithm for Minimum Spanning Tree #33

Open
wants to merge 6 commits into
base: testing
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions benchmarks/BoostMinSpanningTree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,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 V 10
pk-218 marked this conversation as resolved.
Show resolved Hide resolved
#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 = V;
int num_edges = V * (V -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";
}
8 changes: 5 additions & 3 deletions benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ COMMAND ${GraphMLIR_BINARY_DIR}/graph-opt ${GraphMLIR_EXAMPLES_DIR}/graph.mlir
-test-vector-multi-reduction-lowering-patterns
-convert-vector-to-llvm
-convert-memref-to-llvm
-llvm-request-c-wrappers
-convert-func-to-llvm
-convert-func-to-llvm='emit-c-wrappers=1'
-reconcile-unrealized-casts |
${LLVM_MLIR_BINARY_DIR}/mlir-translate --mlir-to-llvmir |
${LLVM_MLIR_BINARY_DIR}/llc -mtriple=${GraphMLIR_OPT_TRIPLE} -mattr=${GraphMLIR_OPT_ATTR}
Expand All @@ -39,8 +38,11 @@ add_executable(graph-processing-benchmark
BoostFloydWarshall.cpp
GraphMlirFloydWarshallBenchmark.cpp
LemonBFS.cpp
MinSpanningTree.cpp
LemonMinSpanningTree.cpp
BoostMinSpanningTree.cpp
GraphMlirMinSpanningTree.cpp
Main.cpp

)


Expand Down
79 changes: 79 additions & 0 deletions benchmarks/GraphMlirMinSpanningTree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//===- GraphMlirMinSpanningTreeBenchmark.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 GraphMLIR Minimum Spanning Tree.
//
//===----------------------------------------------------------------------===//

#include <Interface/GraphContainer.h>
#include <Interface/graph.h>
#include <benchmark/benchmark.h>
#include <Utility/Utils.h>

#define V 10
#define MAX_WEIGHT 1000

using namespace std;

namespace {
Graph<int, 2> g(graph::detail::GRAPH_ADJ_MATRIX_UNDIRECTED_WEIGHTED, V);
MemRef<int, 2> *input;
intptr_t size[1];
} // namespace

void initializeGraphMlirMinSpanningTree() {
graph::generateRandomGraphI(&g, V);
input = &g.get_Memref();
size[0] = V;

MemRef<int, 1> cost = MemRef<int, 1>(size, MAX_WEIGHT);
MemRef<int, 1> visited = MemRef<int, 1>(size, 0);
MemRef<int, 1> output = MemRef<int, 1>(size, -1);
}

// Benchmarking function.
static void GraphMlirMinSpanningTree(benchmark::State &state) {
for (auto _ : state) {
MemRef<int, 1> output = MemRef<int, 1>(size, -1);
MemRef<int, 1> visited = MemRef<int, 1>(size, 0);
MemRef<int, 1> cost = MemRef<int, 1>(size, MAX_WEIGHT);
for (int i = 0; i < state.range(0); ++i) {
graph::min_spanning_tree(input, &output, &visited, &cost);
}
}
}

// Register benchmarking function.
BENCHMARK(GraphMlirMinSpanningTree)->Arg(1);

void generateResultGraphMlirMinSpanningTree() {
initializeGraphMlirMinSpanningTree();
MemRef<int, 1> output(size, -1);
MemRef<int, 1> visited(size, 0);
MemRef<int, 1> cost(size, MAX_WEIGHT);

std::cout << "-------------------------------------------------------\n";
std::cout << "[ GraphMLIR Minimum Spanning Tree Result Information ]\n";
graph::min_spanning_tree(input, &output, &visited, &cost);

auto parent = output.getData();
for (int i = 0; i < V; i++) {
std::cout << "p[" << i << "] = " << parent[i] << ", ";
}

std::cout << "GraphMLIR Minimum Spanning Tree operation finished!\n";
}
109 changes: 109 additions & 0 deletions benchmarks/LemonMinSpanningTree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//===- LemonMinSpanningTree.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 LEMON Minimum Spanning Tree.
//
//===----------------------------------------------------------------------===//

#include <benchmark/benchmark.h>
#include <bits/stdc++.h>
#include <lemon/maps.h>
#include <lemon/kruskal.h>
#include <lemon/list_graph.h>

using namespace std;
using namespace lemon;

#define V 10
#define MaxWeight 1000
#define UpperLimit 100
#define LowerLimit 2

typedef ListGraph::Node Node;
typedef ListGraph::Edge Edge;
typedef ListGraph::NodeIt NodeIt;
typedef ListGraph::EdgeIt EdgeIt;
typedef ListGraph::EdgeMap<int> ECostMap;

namespace {
ListGraph g;
ListGraph::Node nodes[V];
ECostMap edge_cost_map(g);
} // namespace

void initializeLemonMinSpanningTree() {
for (int i = 0; i < V; i++) {
nodes[i] = g.addNode();
}
std::set<std::pair<int, int>> container;
std::set<std::pair<int, int>>::iterator it;
srand(time(NULL));

int NUM = V; // Number of Vertices
int MAX_EDGES = V * (V - 1) / 2;
int NUMEDGE = MAX_EDGES; // Number of Edges
for (int j = 1; j <= NUMEDGE; j++) {
int a = rand() % NUM;
int b = rand() % NUM;

std::pair<int, int> p = std::make_pair(a, b);
std::pair<int, int> reverse_p = std::make_pair(b, a);

while (container.find(p) != container.end() ||
container.find(reverse_p) != container.end() || a==b) {
a = rand() % NUM;
b = rand() % NUM;
p = std::make_pair(a, b);
reverse_p = std::make_pair(b, a);
}

container.insert(p);
container.insert(reverse_p);
edge_cost_map.set(g.addEdge(nodes[a], nodes[b]), 1 + rand() % MaxWeight);
}
}

// Benchmarking function.
static void LemonMinSpanningTree(benchmark::State &state) {
for (auto _ : state) {
vector<Edge> tree_edge_vec;
for (int i = 0; i < state.range(0); ++i) {
kruskal(g, edge_cost_map, std::back_inserter(tree_edge_vec));
}
}
}

BENCHMARK(LemonMinSpanningTree)->Arg(1);

void generateResultLemonMinSpanningTree() {
initializeLemonMinSpanningTree();
cout << "-------------------------------------------------------\n";
cout << "[ LEMON Kruskal Result Information ]\n";

vector<Edge> tree_edge_vec;
std::cout << "The weight of the minimum spanning tree is "
<< kruskal(g, edge_cost_map, std::back_inserter(tree_edge_vec))
<< std::endl;

std::cout << "The edges of the tree are: ";
for (int i = tree_edge_vec.size() - 1; i >= 0; i--)
std::cout << g.id(tree_edge_vec[i]) << ";";
std::cout << std::endl;
std::cout << "The size of the tree is: " << tree_edge_vec.size()
<< std::endl;

cout << "Lemon Kruskal Operation Completed!\n";
}
17 changes: 17 additions & 0 deletions benchmarks/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,30 @@ void initializeGraphMLIRFloydWarshall();
void initializeFloydWarshall();
void initializeLemonBFS();
void initializeBoostFLoydWarshall();
void initializeMinSpanningTree();
void initializeBoostMinSpanningTree();
void initializeLemonMinSpanningTree();
void initializeGraphMlirMinSpanningTree();

void generateResultGraphMLIRFloydWarshall();
void generateResultFloydWarshall();
void generateResultLemonBFS();
void generateResultBoostFLoydWarshall();
void generateResultMinSpanningTree();
void generateResultBoostMinSpanningTree();
void generateResultLemonMinSpanningTree();
void generateResultGraphMlirMinSpanningTree();

int main(int argc, char **argv) {

initializeGraphMLIRFloydWarshall();
initializeFloydWarshall();
initializeLemonBFS();
initializeBoostFLoydWarshall();
initializeMinSpanningTree();
initializeBoostMinSpanningTree();
initializeLemonMinSpanningTree();
initializeGraphMlirMinSpanningTree();

::benchmark::Initialize(&argc, argv);
::benchmark::RunSpecifiedBenchmarks();
Expand All @@ -44,5 +56,10 @@ int main(int argc, char **argv) {
generateResultFloydWarshall();
generateResultLemonBFS();
generateResultBoostFLoydWarshall();
generateResultMinSpanningTree();
generateResultBoostMinSpanningTree();
generateResultLemonMinSpanningTree();
generateResultGraphMlirMinSpanningTree();

return 0;
}
Loading