Skip to content

Commit c6fbd34

Browse files
committed
General performance improvement and BFS optimizzation
1 parent e368b4b commit c6fbd34

2 files changed

Lines changed: 40 additions & 27 deletions

File tree

include/CXXGraph/Graph/Algorithm/BreadthFirstSearch_impl.hpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,33 @@ const std::vector<Node<T>> Graph<T>::breadth_first_search(
3333
const Node<T> &start) const {
3434
// vector to keep track of visited nodes
3535
std::vector<Node<T>> visited;
36-
auto &nodeSet = Graph<T>::getNodeSet();
36+
// std::unordered_set<CXXGraph::id_t> visited_ids;
37+
3738
// check is exist node in the graph
38-
auto start_node_it = std::find_if(
39-
nodeSet.begin(), nodeSet.end(),
40-
[&start](auto &node) { return node->getUserId() == start.getUserId(); });
41-
if (start_node_it == nodeSet.end()) {
39+
40+
auto start_node = Graph<T>::getNode(start.getId());
41+
if (!start_node.has_value()) {
4242
return visited;
4343
}
4444
// queue that stores vertices that need to be further explored
4545
std::queue<shared<const Node<T>>> tracker;
4646

4747
// mark the starting node as visited
4848
visited.push_back(start);
49-
tracker.push(*start_node_it);
49+
// visited_ids.insert(start.getId());
50+
tracker.push(start_node.value());
5051
while (!tracker.empty()) {
5152
shared<const Node<T>> node = tracker.front();
5253
tracker.pop();
5354
if (cachedAdjListOut->find(node) != cachedAdjListOut->end()) {
5455
for (const auto &elem : cachedAdjListOut->at(node)) {
55-
// if the node is not visited then mark it as visited
56-
// and push it to the queue
57-
if (std::find(visited.begin(), visited.end(), *(elem.first)) ==
58-
visited.end()) {
56+
if (std::find_if(visited.begin(), visited.end(),
57+
[&elem](const auto &node) {
58+
return node.getId() == elem.first->getId();
59+
}) == visited.end()) {
60+
// if (visited_ids.find(elem.first->getId()) == visited_ids.end()) {
5961
visited.push_back(*(elem.first));
62+
// visited_ids.insert(elem.first->getId());
6063
tracker.push(elem.first);
6164
}
6265
}

include/CXXGraph/Graph/Graph_impl.hpp

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -481,10 +481,13 @@ void Graph<T>::setNodeData(std::map<std::string, T> &dataMap) {
481481
template <typename T>
482482
const std::optional<shared<const Edge<T>>> Graph<T>::getEdge(
483483
const std::string &edgeUserId) const {
484-
for (const auto &it : edgeSet) {
485-
if (it->getUserId() == edgeUserId) {
486-
return it;
487-
}
484+
const auto &edgeSet = this->getEdgeSet();
485+
auto it = std::find_if(edgeSet.begin(), edgeSet.end(),
486+
[&edgeUserId](const auto &edge) {
487+
return edge->getUserId() == edgeUserId;
488+
});
489+
if (it != edgeSet.end()) {
490+
return *it;
488491
}
489492

490493
return std::nullopt;
@@ -493,10 +496,12 @@ const std::optional<shared<const Edge<T>>> Graph<T>::getEdge(
493496
template <typename T>
494497
const std::optional<shared<const Edge<T>>> Graph<T>::getEdge(
495498
const CXXGraph::id_t edgeId) const {
496-
for (const auto &it : edgeSet) {
497-
if (it->getId() == edgeId) {
498-
return it;
499-
}
499+
const auto &edgeSet = this->getEdgeSet();
500+
auto it = std::find_if(
501+
edgeSet.begin(), edgeSet.end(),
502+
[edgeId](const auto &edge) { return edge->getId() == edgeId; });
503+
if (it != edgeSet.end()) {
504+
return *it;
500505
}
501506

502507
return std::nullopt;
@@ -505,10 +510,13 @@ const std::optional<shared<const Edge<T>>> Graph<T>::getEdge(
505510
template <typename T>
506511
const std::optional<shared<const Node<T>>> Graph<T>::getNode(
507512
const std::string &nodeUserId) const {
508-
for (const auto &it : getNodeSet()) {
509-
if (it->getUserId() == nodeUserId) {
510-
return it;
511-
}
513+
const auto &nodeSet = this->getNodeSet();
514+
auto it = std::find_if(nodeSet.begin(), nodeSet.end(),
515+
[&nodeUserId](const auto &node) {
516+
return node->getUserId() == nodeUserId;
517+
});
518+
if (it != nodeSet.end()) {
519+
return *it;
512520
}
513521

514522
return std::nullopt;
@@ -517,10 +525,12 @@ const std::optional<shared<const Node<T>>> Graph<T>::getNode(
517525
template <typename T>
518526
const std::optional<shared<const Node<T>>> Graph<T>::getNode(
519527
const CXXGraph::id_t nodeId) const {
520-
for (const auto &it : getNodeSet()) {
521-
if (it->getId() == nodeId) {
522-
return it;
523-
}
528+
const auto &nodeSet = this->getNodeSet();
529+
auto it = std::find_if(
530+
nodeSet.begin(), nodeSet.end(),
531+
[nodeId](const auto &node) { return node->getId() == nodeId; });
532+
if (it != nodeSet.end()) {
533+
return *it;
524534
}
525535

526536
return std::nullopt;
@@ -538,7 +548,7 @@ std::unordered_set<shared<Node<T>>, nodeHash<T>> Graph<T>::nodeSet() {
538548
nodeSet.insert(std::const_pointer_cast<Node<T>>(adjListOutIt.first));
539549
}
540550

541-
for (auto &isNodeIt : isolatedNodesSet) {
551+
for (const auto &isNodeIt : isolatedNodesSet) {
542552
nodeSet.insert(std::const_pointer_cast<Node<T>>(isNodeIt));
543553
}
544554

0 commit comments

Comments
 (0)