Skip to content

Commit

Permalink
Optimization ( Partial ) (#201)
Browse files Browse the repository at this point in the history
* Converted std::list to std::deque for edgeList

* Enhanced add Edge to the graph

converted std::list to std::deque for edgeSet and nodeSet

* Created BFS benchmark

Enhanced getAdjMatrix function

* Removed Old Implementation of addElementToAdjMatrix

Converted to a lambda function

* Introduced Set, for node and edge of the graph

* Created first benchmark with real dataset.

* Added some benchmarks, partially closed Optimization

* Introduced Citations for Graph Dataset
  • Loading branch information
ZigRazor authored Apr 4, 2022
1 parent 982a3d3 commit b382914
Show file tree
Hide file tree
Showing 36 changed files with 448,577 additions and 833 deletions.
37 changes: 37 additions & 0 deletions benchmark/BFS_BM.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <benchmark/benchmark.h>
#include "CXXGraph.hpp"
#include "Utilities.hpp"

static auto nodes = generateRandomNodes(100000, 2);
static auto edges = generateRandomEdges(100000, nodes);
static auto graph_ptr = readGraph("CitHepPh");

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 << 16);

static void BFS_FromReadedCitHep(benchmark::State &state)
{
auto edgeSet = graph_ptr->getEdgeSet();
for (auto _ : state)
{

auto &result = graph_ptr->breadth_first_search(*((*(edgeSet.begin()))->getNodePair().first));
}
}

BENCHMARK(BFS_FromReadedCitHep);
39 changes: 39 additions & 0 deletions benchmark/DFS_BM.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <benchmark/benchmark.h>
#include "CXXGraph.hpp"
#include "Utilities.hpp"

static auto nodes = generateRandomNodes(100000, 2);
static auto edges = generateRandomEdges(100000, nodes);
static auto graph_ptr = readGraph("CitHepPh");

static void DFS_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.depth_first_search(*(range_start->second->getNodePair().first));
}
}


BENCHMARK(DFS_X)->RangeMultiplier(16)->Range((unsigned long)1, (unsigned long)1 << 16);

static void DFS_FromReadedCitHep(benchmark::State &state)
{
auto edgeSet = graph_ptr->getEdgeSet();
for (auto _ : state)
{

auto &result = graph_ptr->depth_first_search(*((*(edgeSet.begin()))->getNodePair().first));
}
}

BENCHMARK(DFS_FromReadedCitHep);
36 changes: 18 additions & 18 deletions benchmark/FloydWarshall_BM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ static void BM_FWDirected(benchmark::State &state)
CXXGRAPH::DirectedWeightedEdge<int> edge4(4, node4, node2, -1);
CXXGRAPH::DirectedWeightedEdge<int> edge5(3, node2, node3, 3);

std::list<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.push_back(&edge1);
edgeSet.push_back(&edge2);
edgeSet.push_back(&edge3);
edgeSet.push_back(&edge4);
edgeSet.push_back(&edge5);
std::set<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.insert(&edge1);
edgeSet.insert(&edge2);
edgeSet.insert(&edge3);
edgeSet.insert(&edge4);
edgeSet.insert(&edge5);

CXXGRAPH::Graph<int> graph(edgeSet);
for (auto _ : state)
Expand All @@ -40,10 +40,10 @@ static void BM_FWNegCycle(benchmark::State &state)
CXXGRAPH::DirectedWeightedEdge<int> edge1(1, node0, node1, 2);
CXXGRAPH::DirectedWeightedEdge<int> edge2(2, node1, node2, 3);
CXXGRAPH::DirectedWeightedEdge<int> edge3(3, node2, node0, -7);
std::list<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.push_back(&edge1);
edgeSet.push_back(&edge2);
edgeSet.push_back(&edge3);
std::set<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.insert(&edge1);
edgeSet.insert(&edge2);
edgeSet.insert(&edge3);
CXXGRAPH::Graph<int> graph(edgeSet);
for (auto _ : state)
{
Expand All @@ -61,10 +61,10 @@ static void BM_FWUndirectedWeighted(benchmark::State &state)
CXXGRAPH::DirectedWeightedEdge<int> edge1(1, pairNode, 1);
CXXGRAPH::DirectedWeightedEdge<int> edge2(2, node2, node3, 1);
CXXGRAPH::UndirectedWeightedEdge<int> edge3(3, node1, node3, 6);
std::list<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.push_back(&edge1);
edgeSet.push_back(&edge2);
edgeSet.push_back(&edge3);
std::set<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.insert(&edge1);
edgeSet.insert(&edge2);
edgeSet.insert(&edge3);
CXXGRAPH::Graph<int> graph(edgeSet);
for (auto _ : state)
{
Expand All @@ -82,10 +82,10 @@ static void BM_FWNoWeighted(benchmark::State &state)
CXXGRAPH::DirectedWeightedEdge<int> edge1(1, pairNode, 1);
CXXGRAPH::DirectedWeightedEdge<int> edge2(2, node2, node3, 1);
CXXGRAPH::DirectedEdge<int> edge3(3, node1, node3);
std::list<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.push_back(&edge1);
edgeSet.push_back(&edge2);
edgeSet.push_back(&edge3);
std::set<const CXXGRAPH::Edge<int> *> edgeSet;
edgeSet.insert(&edge1);
edgeSet.insert(&edge2);
edgeSet.insert(&edge3);
CXXGRAPH::Graph<int> graph(edgeSet);
for (auto _ : state)
{
Expand Down
105 changes: 104 additions & 1 deletion benchmark/Graph_BM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@

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

static CXXGRAPH::Graph<int> *graph;

static void GraphCreation(benchmark::State &state)
{
for (auto _ : state){
for (auto _ : state)
{
CXXGRAPH::Graph<int> g;
}
}
Expand Down Expand Up @@ -46,3 +48,104 @@ static void AddEdgeX(benchmark::State &state)
}
}
BENCHMARK(AddEdgeX)->RangeMultiplier(16)->Range((unsigned long)1, (unsigned long)1 << 16);

static void ReadGraphCitHep(benchmark::State &state)
{
for (auto _ : state)
{
auto g = readGraph("CitHepPh");
}
}

BENCHMARK(ReadGraphCitHep);

static void getEdgeSetX(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 edgeSet = g.getEdgeSet();
}
}

BENCHMARK(getEdgeSetX)->RangeMultiplier(16)->Range((unsigned long)1, (unsigned long)1 << 16);

static void getNodeSetX(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 nodeSet = g.getNodeSet();
}
}

BENCHMARK(getNodeSetX)->RangeMultiplier(16)->Range((unsigned long)1, (unsigned long)1 << 16);

static void getEdgeSetCitHep(benchmark::State &state)
{
auto g = readGraph("CitHepPh");
for (auto _ : state)
{
auto edgeSet = g->getEdgeSet();
}
}

BENCHMARK(getEdgeSetCitHep);

static void getNodeSetCitHep(benchmark::State &state)
{
auto g = readGraph("CitHepPh");
for (auto _ : state)
{
auto nodeSet = g->getNodeSet();
}
}

BENCHMARK(getNodeSetCitHep);


static void getAdjMatrixX(benchmark::State &state)
{
CXXGRAPH::Graph<int> g;
auto range_start = undirectedEdges.begin();
auto range_end = undirectedEdges.find(state.range(0));
std::unordered_map<unsigned long, CXXGRAPH::UndirectedEdge<int> *> edgesX;
edgesX.insert(range_start, range_end);
for (auto e : edgesX)
{
g.addEdge(&(*e.second));
}
for (auto _ : state)
{
auto adjMatrix = g.getAdjMatrix();
}
}

BENCHMARK(getAdjMatrixX)->RangeMultiplier(16)->Range((unsigned long)1, (unsigned long)1 << 16);

static void getAdjMatrixCitHep(benchmark::State &state)
{
auto g = readGraph("CitHepPh");
for (auto _ : state)
{
auto adjMatrix = g->getAdjMatrix();
}
}

BENCHMARK(getAdjMatrixCitHep);
24 changes: 24 additions & 0 deletions benchmark/Utilities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,28 @@ static std::map<unsigned long, CXXGRAPH::Edge<int> *> generateRandomEdges(unsign
return edges;
}

static std::map<unsigned long, CXXGRAPH::UndirectedEdge<int> *> generateRandomUndirectedEdges(unsigned long numberOfEdges, std::map<unsigned long, CXXGRAPH::Node<int> *> nodes)
{
std::map<unsigned long, CXXGRAPH::UndirectedEdge<int> *> edges;
srand((unsigned)time(NULL));
int randomNumber1;
int randomNumber2;
auto MaxValue = nodes.size();
for (auto index = 0; index < numberOfEdges; index++)
{
randomNumber1 = (rand() % MaxValue);
randomNumber2 = (rand() % MaxValue);
CXXGRAPH::UndirectedEdge<int> *newEdge = new CXXGRAPH::UndirectedEdge<int>(index, *(nodes.at(randomNumber1)), *(nodes.at(randomNumber2)));
edges[index] = newEdge;
}
return edges;
}

static CXXGRAPH::Graph<int> *readGraph(const std::string &filename)
{
CXXGRAPH::Graph<int> *graph_ptr = new CXXGRAPH::Graph<int>();
auto result = graph_ptr->readFromFile(CXXGRAPH::InputOutputFormat::STANDARD_CSV, "../benchmark/dataset", filename);
return graph_ptr;
}

#endif // __UTILITIES_H__
Loading

0 comments on commit b382914

Please sign in to comment.