-
Notifications
You must be signed in to change notification settings - Fork 7
[feat] Prim-Jarnik's algorithm for Minimum Spanning Tree #33
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
Open
whois-pk
wants to merge
6
commits into
meshtag:testing
Choose a base branch
from
whois-pk:feat/mst
base: testing
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b09daab
feat(mst): initial implementation
whois-pk c530a2c
chore(mst): delete test.mlir
whois-pk c6e64c7
feat(mst): add benchmark
whois-pk 69e2e81
chore(mst): cleanup comments
whois-pk 347a2de
fix(mst): max weight limit bug
whois-pk ca9825c
refactor: rename variables
whois-pk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| #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"; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.