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

Implement best first search #254

Merged
merged 4 commits into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
75 changes: 75 additions & 0 deletions include/Graph/Graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ namespace CXXGRAPH
* search.
*
*/
virtual BestFirstSearchResult<T> best_first_search(const Node<T> &source, const Node<T> &target) const;
virtual const std::vector<Node<T>> breadth_first_search(const Node<T> &start) const;
/**
* \brief
Expand Down Expand Up @@ -1587,6 +1588,80 @@ namespace CXXGRAPH
return result;
}

template <typename T>
BestFirstSearchResult<T> Graph<T>::best_first_search(const Node<T> &source, const Node<T> &target) const
{
BestFirstSearchResult<T> result;
auto &nodeSet = Graph<T>::getNodeSet();
using pq_type = std::pair<double, const Node<T> *>;

if(std::find(nodeSet.begin(), nodeSet.end(), &source) == nodeSet.end())
{
result.errorMessage = ERR_SOURCE_NODE_NOT_IN_GRAPH;
return result;
}

if(std::find(nodeSet.begin(), nodeSet.end(), &target) == nodeSet.end())
{
result.errorMessage = ERR_TARGET_NODE_NOT_IN_GRAPH;
return result;
}

auto adj = Graph<T>::getAdjMatrix();
std::priority_queue<pq_type, std::vector<pq_type>, std::greater<pq_type>> pq;

std::vector<Node<T>> visited;
visited.push_back(source);
pq.push(std::make_pair(static_cast<T>(0), &source));

while (!pq.empty())
{
const Node<T> *currentNode = pq.top().second;
pq.pop();
result.nodesInBestSearchOrder.push_back(*currentNode);

if (*currentNode == target)
{
break;
}
if (adj.find(currentNode) != adj.end())
{
for (const auto &elem : adj.at(currentNode))
{
if (elem.second->isWeighted().has_value())
{
if (elem.second->isDirected().has_value())
{
const DirectedWeightedEdge<T> *dw_edge = static_cast<const DirectedWeightedEdge<T> *>(elem.second);
if (std::find(visited.begin(), visited.end(), *(elem.first)) == visited.end())
{
visited.push_back(*(elem.first));
pq.push(std::make_pair(dw_edge->getWeight(), elem.first));
}
}
else
{
const UndirectedWeightedEdge<T> *dw_edge = static_cast<const UndirectedWeightedEdge<T> *>(elem.second);
if (std::find(visited.begin(), visited.end(), *(elem.first)) == visited.end())
{
visited.push_back(*(elem.first));
pq.push(std::make_pair(dw_edge->getWeight(), elem.first));
}
}
}
else
{
result.errorMessage = ERR_NO_WEIGHTED_EDGE;
return result;
}
}
}
}

result.success = true;
return result;
}

template <typename T>
const std::vector<Node<T>> Graph<T>::breadth_first_search(const Node<T> &start) const
{
Expand Down
10 changes: 10 additions & 0 deletions include/Utility/Typedef.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@ namespace CXXGRAPH
template <typename T>
using TopoSortResult = TopoSortResult_struct<T>;

template <typename T>
struct BestFirstSearchResult_struct
{
bool success = false;
std::string errorMessage = "";
std::vector<Node<T>> nodesInBestSearchOrder = {};
};
template <typename T>
using BestFirstSearchResult = BestFirstSearchResult_struct<T>;

/// Struct that contains the information about the partitioning statistics


Expand Down
136 changes: 136 additions & 0 deletions test/BestFirstSearchTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#include "gtest/gtest.h"
#include "CXXGraph.hpp"
#include <vector>

TEST(BestFirstSearchTest, source_node_missing)
{
CXXGRAPH::Node<int> node1("1", 1);
CXXGRAPH::Node<int> node2("2", 2);
CXXGRAPH::Node<int> node3("3", 3);
CXXGRAPH::Node<int> node4("4", 4);
std::pair<const CXXGRAPH::Node<int> *, const CXXGRAPH::Node<int> *> pairNode(&node1, &node2);
CXXGRAPH::DirectedWeightedEdge<int> edge1(1, pairNode, 1);
CXXGRAPH::DirectedWeightedEdge<int> edge2(2, node2, node3, 3);
CXXGRAPH::DirectedWeightedEdge<int> edge3(3, node1, node3, 6);
CXXGRAPH::T_EdgeSet<int> edgeSet;
edgeSet.insert(&edge1);
edgeSet.insert(&edge2);
edgeSet.insert(&edge3);
CXXGRAPH::Graph<int> graph(edgeSet);
CXXGRAPH::BestFirstSearchResult<int> && res = graph.best_first_search(node4, node1);
ASSERT_EQ(res.success, false);
ASSERT_EQ(res.nodesInBestSearchOrder.size(), 0);
ASSERT_EQ(res.errorMessage, CXXGRAPH::ERR_SOURCE_NODE_NOT_IN_GRAPH);
}

TEST(BestFirstSearchTest, target_node_missing)
{
CXXGRAPH::Node<int> node1("1", 1);
CXXGRAPH::Node<int> node2("2", 2);
CXXGRAPH::Node<int> node3("3", 3);
CXXGRAPH::Node<int> node4("4", 4);
std::pair<const CXXGRAPH::Node<int> *, const CXXGRAPH::Node<int> *> pairNode(&node1, &node2);
CXXGRAPH::DirectedWeightedEdge<int> edge1(1, pairNode, 1);
CXXGRAPH::DirectedWeightedEdge<int> edge2(2, node2, node3, 3);
CXXGRAPH::DirectedWeightedEdge<int> edge3(3, node1, node3, 6);
CXXGRAPH::T_EdgeSet<int> edgeSet;
edgeSet.insert(&edge1);
edgeSet.insert(&edge2);
edgeSet.insert(&edge3);
CXXGRAPH::Graph<int> graph(edgeSet);
CXXGRAPH::BestFirstSearchResult<int> && res = graph.best_first_search(node1, node4);
ASSERT_EQ(res.success, false);
ASSERT_EQ(res.nodesInBestSearchOrder.size(), 0);
ASSERT_EQ(res.errorMessage, CXXGRAPH::ERR_TARGET_NODE_NOT_IN_GRAPH);
}

TEST(BestFirstSearchTest, correct_example_small)
{
CXXGRAPH::Node<int> node1("1", 1);
CXXGRAPH::Node<int> node2("2", 2);
CXXGRAPH::Node<int> node3("3", 3);
std::pair<const CXXGRAPH::Node<int> *, const CXXGRAPH::Node<int> *> pairNode(&node1, &node2);
CXXGRAPH::DirectedWeightedEdge<int> edge1(1, pairNode, 1);
CXXGRAPH::DirectedWeightedEdge<int> edge2(2, node2, node3, 3);
CXXGRAPH::DirectedWeightedEdge<int> edge3(3, node1, node3, 6);
CXXGRAPH::T_EdgeSet<int> edgeSet;
edgeSet.insert(&edge1);
edgeSet.insert(&edge2);
edgeSet.insert(&edge3);
CXXGRAPH::Graph<int> graph(edgeSet);
CXXGRAPH::BestFirstSearchResult<int> && res = graph.best_first_search(node1, node2);
ASSERT_EQ(res.success, true);
ASSERT_EQ(res.nodesInBestSearchOrder.size(), 2);
ASSERT_EQ(res.errorMessage, "");
}

TEST(BestFirstSearchTest, source_target_same)
{
CXXGRAPH::Node<int> node1("1", 1);
CXXGRAPH::Node<int> node2("2", 2);
CXXGRAPH::Node<int> node3("3", 3);
std::pair<const CXXGRAPH::Node<int> *, const CXXGRAPH::Node<int> *> pairNode(&node1, &node2);
CXXGRAPH::DirectedWeightedEdge<int> edge1(1, pairNode, 1);
CXXGRAPH::DirectedWeightedEdge<int> edge2(2, node2, node3, 3);
CXXGRAPH::DirectedWeightedEdge<int> edge3(3, node1, node3, 6);
CXXGRAPH::T_EdgeSet<int> edgeSet;
edgeSet.insert(&edge1);
edgeSet.insert(&edge2);
edgeSet.insert(&edge3);
CXXGRAPH::Graph<int> graph(edgeSet);
CXXGRAPH::BestFirstSearchResult<int> && res = graph.best_first_search(node1, node1);
ASSERT_EQ(res.success, true);
ASSERT_EQ(res.nodesInBestSearchOrder.size(), 1);
ASSERT_EQ(res.errorMessage, "");
}

TEST(BestFirstSearchTest, correct_example_big)
{
CXXGRAPH::Node<int> node1("1", 1);
CXXGRAPH::Node<int> node2("2", 2);
CXXGRAPH::Node<int> node3("3", 3);
CXXGRAPH::Node<int> node4("4", 4);
CXXGRAPH::Node<int> node5("5", 5);
CXXGRAPH::Node<int> node6("6", 6);
CXXGRAPH::Node<int> node7("7", 7);
CXXGRAPH::Node<int> node8("8", 8);
CXXGRAPH::Node<int> node9("9", 9);
CXXGRAPH::Node<int> node10("10", 10);
CXXGRAPH::Node<int> node11("11", 11);
CXXGRAPH::Node<int> node12("12", 12);
CXXGRAPH::Node<int> node13("13", 13);
CXXGRAPH::Node<int> node14("14", 14);
std::pair<const CXXGRAPH::Node<int> *, const CXXGRAPH::Node<int> *> pairNode(&node1, &node2);
CXXGRAPH::DirectedWeightedEdge<int> edge1(1, node1, node2, 3);
CXXGRAPH::DirectedWeightedEdge<int> edge2(2, node1, node3, 6);
CXXGRAPH::DirectedWeightedEdge<int> edge3(3, node1, node4, 5);
CXXGRAPH::DirectedWeightedEdge<int> edge4(4, node2, node5, 9);
CXXGRAPH::DirectedWeightedEdge<int> edge5(5, node2, node6, 8);
CXXGRAPH::DirectedWeightedEdge<int> edge6(6, node3, node7, 12);
CXXGRAPH::DirectedWeightedEdge<int> edge7(7, node3, node8, 14);
CXXGRAPH::DirectedWeightedEdge<int> edge8(8, node4, node9, 7);
CXXGRAPH::DirectedWeightedEdge<int> edge9(9, node9, node10, 5);
CXXGRAPH::DirectedWeightedEdge<int> edge10(10, node9, node11, 6);
CXXGRAPH::DirectedWeightedEdge<int> edge11(10, node10, node12, 1);
CXXGRAPH::DirectedWeightedEdge<int> edge12(10, node10, node13, 10);
CXXGRAPH::DirectedWeightedEdge<int> edge13(10, node10, node14, 2);
CXXGRAPH::T_EdgeSet<int> edgeSet;
edgeSet.insert(&edge1);
edgeSet.insert(&edge2);
edgeSet.insert(&edge3);
edgeSet.insert(&edge4);
edgeSet.insert(&edge5);
edgeSet.insert(&edge6);
edgeSet.insert(&edge7);
edgeSet.insert(&edge8);
edgeSet.insert(&edge9);
edgeSet.insert(&edge10);
edgeSet.insert(&edge11);
edgeSet.insert(&edge12);
edgeSet.insert(&edge13);
CXXGRAPH::Graph<int> graph(edgeSet);
CXXGRAPH::BestFirstSearchResult<int> && res = graph.best_first_search(node1, node10);
ASSERT_EQ(res.success, true);
ASSERT_EQ(res.nodesInBestSearchOrder.size(), 6);
ASSERT_EQ(res.errorMessage, "");
}