Skip to content

Commit

Permalink
Created BFS benchmark
Browse files Browse the repository at this point in the history
Enhanced getAdjMatrix function
  • Loading branch information
ZigRazor authored Mar 25, 2022
1 parent f1957be commit 2a30303
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
24 changes: 24 additions & 0 deletions benchmark/BFS_BM.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <benchmark/benchmark.h>
#include "CXXGraph.hpp"
#include "Utilities.hpp"

static auto nodes = generateRandomNodes(100000, 2);
static auto edges = generateRandomEdges(100000, nodes);

static void BFS_X(benchmark::State &state)
{
CXXGRAPH::Graph<int> g;
auto range_start = edges.begin();
auto range_end = edges.find(state.range(0));
std::unordered_map<unsigned long, CXXGRAPH::Edge<int> *> edgesX;
edgesX.insert(range_start, range_end);
for (auto e : edgesX)
{
g.addEdge(&(*e.second));
}
for (auto _ : state)
{
auto &result = g.breadth_first_search(*(range_start->second->getNodePair().first));
}
}
BENCHMARK(BFS_X)->RangeMultiplier(16)->Range((unsigned long)1, (unsigned long)1 << 12);
17 changes: 17 additions & 0 deletions benchmark/results/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# BENCHMARK RESULTS

- To create Json file with results, run:

```bash
./benchmark --benchmark_out=<filename> --benchmark_out_format=json --benchmark_repetitions=20
```

- To compare results, run:

```bash
./compare.py benchmarks /workspaces/CXXGraph/benchmark/results/file1.json /workspaces/CXXGraph/benchmark/results/file2.json
```

BFS_X/256_mean 2376804 ns 2370768 ns 20
BFS_X/16_mean 14632 ns 14623 ns 20
BFS_X/1_mean 1504 ns 1503 ns 20
17 changes: 11 additions & 6 deletions include/Graph/Graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ namespace CXXGRAPH
{
private:
std::deque<const Edge<T> *> edgeSet = {};
void addElementToAdjMatrix(AdjacencyMatrix<T> &adjMatrix, const Node<T> *nodeFrom, const Node<T> *nodeTo, const Edge<T> *edge) const;
void _addElementToAdjMatrix(AdjacencyMatrix<T> &adjMatrix, const Node<T> *nodeFrom, const Node<T> *nodeTo, const Edge<T> *edge) const;
std::optional<std::pair<std::string, char>> getExtenstionAndSeparator(InputOutputFormat format) const;
int writeToStandardFile(const std::string &workingDir, const std::string &OFileName, bool compress, bool writeNodeFeat, bool writeEdgeWeight, InputOutputFormat format) const;
int readFromStandardFile(const std::string &workingDir, const std::string &OFileName, bool compress, bool readNodeFeat, bool readEdgeWeight, InputOutputFormat format);
Expand Down Expand Up @@ -530,7 +530,7 @@ namespace CXXGRAPH
}

template <typename T>
void Graph<T>::addElementToAdjMatrix(AdjacencyMatrix<T> &adjMatrix, const Node<T> *nodeFrom, const Node<T> *nodeTo, const Edge<T> *edge) const
void Graph<T>::_addElementToAdjMatrix(AdjacencyMatrix<T> &adjMatrix, const Node<T> *nodeFrom, const Node<T> *nodeTo, const Edge<T> *edge) const
{
std::pair<const Node<T> *, const Edge<T> *> elem = {nodeTo, edge};
adjMatrix[nodeFrom].push_back(elem);
Expand Down Expand Up @@ -938,20 +938,25 @@ namespace CXXGRAPH
template <typename T>
const AdjacencyMatrix<T> Graph<T>::getAdjMatrix() const
{

AdjacencyMatrix<T> adj;
auto addElementToAdjMatrix = [&adj](const Node<T> *nodeFrom, const Node<T> *nodeTo, const Edge<T> *edge){
std::pair<const Node<T> *, const Edge<T> *> elem = {nodeTo, edge};
adj[nodeFrom].push_back(std::move(elem));
};
for (const auto &edgeSetIt : edgeSet)
{
if (edgeSetIt->isDirected().has_value() && edgeSetIt->isDirected().value())
{
const DirectedEdge<T> *d_edge = dynamic_cast<const DirectedEdge<T> *>(edgeSetIt);
addElementToAdjMatrix(adj, &(d_edge->getFrom()), &(d_edge->getTo()), d_edge);
addElementToAdjMatrix(&(d_edge->getFrom()), &(d_edge->getTo()), d_edge);
}
else if (edgeSetIt->isDirected().has_value() && !edgeSetIt->isDirected().value())
{
const UndirectedEdge<T> *ud_edge = dynamic_cast<const UndirectedEdge<T> *>(edgeSetIt);
;
addElementToAdjMatrix(adj, &(ud_edge->getNode1()), &(ud_edge->getNode2()), ud_edge);
addElementToAdjMatrix(adj, &(ud_edge->getNode2()), &(ud_edge->getNode1()), ud_edge);
addElementToAdjMatrix(&(ud_edge->getNode1()), &(ud_edge->getNode2()), ud_edge);
addElementToAdjMatrix(&(ud_edge->getNode2()), &(ud_edge->getNode1()), ud_edge);
}
else
{ // is a simple edge we cannot create adj matrix
Expand Down Expand Up @@ -1535,7 +1540,7 @@ namespace CXXGRAPH
{
return visited;
}
const AdjacencyMatrix<T> adj = Graph<T>::getAdjMatrix();
const AdjacencyMatrix<T> &adj = Graph<T>::getAdjMatrix();
// queue that stores vertices that need to be further explored
std::queue<const Node<T> *> tracker;

Expand Down

0 comments on commit 2a30303

Please sign in to comment.