Skip to content

Commit

Permalink
Added changes to save nodes in Dijkstra shortest path. Modified tests. (
Browse files Browse the repository at this point in the history
  • Loading branch information
guru2396 committed Aug 28, 2023
1 parent 530be52 commit 7c03e1a
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 1 deletion.
13 changes: 12 additions & 1 deletion include/Graph/Graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1514,10 +1514,12 @@ const DijkstraResult Graph<T>::dijkstra(const Node<T> &source,
// marking the distance of source as 0
dist[*source_node_it] = 0;

std::unordered_map<std::string, std::string> parent;
parent[source.getUserId()] = "";

while (!pq.empty()) {
// second element of pair denotes the node / vertex
shared<const Node<T>> currentNode = pq.top().second;

// first element of pair denotes the distance
double currentDist = pq.top().first;

Expand All @@ -1541,6 +1543,7 @@ const DijkstraResult Graph<T>::dijkstra(const Node<T> &source,
} else if (currentDist + dw_edge->getWeight() < dist[elem.first]) {
dist[elem.first] = currentDist + dw_edge->getWeight();
pq.push(std::make_pair(dist[elem.first], elem.first));
parent[elem.first.get()->getUserId()] = currentNode.get()->getUserId();
}
} else if (elem.second->isDirected().has_value() &&
!elem.second->isDirected().value()) {
Expand All @@ -1553,6 +1556,7 @@ const DijkstraResult Graph<T>::dijkstra(const Node<T> &source,
} else if (currentDist + udw_edge->getWeight() < dist[elem.first]) {
dist[elem.first] = currentDist + udw_edge->getWeight();
pq.push(std::make_pair(dist[elem.first], elem.first));
parent[elem.first.get()->getUserId()] = currentNode.get()->getUserId();
}
} else {
// ERROR it shouldn't never returned ( does not exist a Node
Expand All @@ -1572,6 +1576,13 @@ const DijkstraResult Graph<T>::dijkstra(const Node<T> &source,
result.success = true;
result.errorMessage = "";
result.result = dist[*target_node_it];
std::string id = target.getUserId();
while(parent[id] != ""){
result.path.push_back(id);
id = parent[id];
}
result.path.push_back(source.getUserId());
std::reverse(result.path.begin(),result.path.end());
return result;
}
result.errorMessage = ERR_TARGET_NODE_NOT_REACHABLE;
Expand Down
1 change: 1 addition & 0 deletions include/Utility/Typedef.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ struct DijkstraResult_struct {
false; // TRUE if the function does not return error, FALSE otherwise
std::string errorMessage = ""; // message of error
double result = INF_DOUBLE; // result (valid only if success is TRUE)
std::vector<std::string> path;
};
typedef DijkstraResult_struct DijkstraResult;

Expand Down
132 changes: 132 additions & 0 deletions test/DijkstraTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,19 @@ TEST(DijkstraTest, correct_example_1) {

CXXGraph::Graph<int> graph(edgeSet);
CXXGraph::DijkstraResult res = graph.dijkstra(node1, node3);
std::vector<std::string> expected;
expected.push_back("1");
expected.push_back("2");
expected.push_back("3");
int index=0;
ASSERT_TRUE(res.success);
ASSERT_EQ(res.errorMessage, "");
ASSERT_EQ(res.result, 2);
ASSERT_EQ(res.path.size(), expected.size());
for(auto elem:res.path){
ASSERT_EQ(elem,expected[index]);
index++;
}
}

TEST(DijkstraTest, correct_example_2) {
Expand All @@ -52,9 +62,18 @@ TEST(DijkstraTest, correct_example_2) {

CXXGraph::Graph<int> graph(edgeSet);
CXXGraph::DijkstraResult res = graph.dijkstra(node1, node3);
std::vector<std::string> expected;
expected.push_back("1");
expected.push_back("3");
int index=0;
ASSERT_TRUE(res.success);
ASSERT_EQ(res.errorMessage, "");
ASSERT_EQ(res.result, 6);
ASSERT_EQ(res.path.size(), expected.size());
for(auto elem:res.path){
ASSERT_EQ(elem,expected[index]);
index++;
}
}

TEST(DijkstraTest, correct_example_3) {
Expand Down Expand Up @@ -83,23 +102,62 @@ TEST(DijkstraTest, correct_example_3) {
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge6));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge7));

std::vector<std::string> expected;
expected.push_back("C");
expected.push_back("A");
expected.push_back("B");
expected.push_back("E");
int index=0;
CXXGraph::Graph<int> graph(edgeSet);
CXXGraph::DijkstraResult res = graph.dijkstra(nodeC, nodeE);
ASSERT_TRUE(res.success);
ASSERT_EQ(res.errorMessage, "");
ASSERT_EQ(res.result, 5);
ASSERT_EQ(res.path.size(), expected.size());
for(auto elem:res.path){
ASSERT_EQ(elem,expected[index]);
index++;
}
expected.clear();
expected.push_back("C");
expected.push_back("A");
index=0;
res = graph.dijkstra(nodeC, nodeA);
ASSERT_TRUE(res.success);
ASSERT_EQ(res.errorMessage, "");
ASSERT_EQ(res.result, 1);
ASSERT_EQ(res.path.size(), expected.size());
for(auto elem:res.path){
ASSERT_EQ(elem,expected[index]);
index++;
}
expected.clear();
expected.push_back("C");
expected.push_back("A");
expected.push_back("B");
index=0;
res = graph.dijkstra(nodeC, nodeB);
ASSERT_TRUE(res.success);
ASSERT_EQ(res.errorMessage, "");
ASSERT_EQ(res.result, 4);
ASSERT_EQ(res.path.size(), expected.size());
for(auto elem:res.path){
ASSERT_EQ(elem,expected[index]);
index++;
}
expected.clear();
expected.push_back("C");
expected.push_back("D");
index=0;
res = graph.dijkstra(nodeC, nodeD);
ASSERT_TRUE(res.success);
ASSERT_EQ(res.errorMessage, "");
ASSERT_EQ(res.result, 2);
ASSERT_EQ(res.path.size(), expected.size());
for(auto elem:res.path){
ASSERT_EQ(elem,expected[index]);
index++;
}
}

TEST(DijkstraTest, correct_example_4) {
Expand Down Expand Up @@ -134,31 +192,94 @@ TEST(DijkstraTest, correct_example_4) {
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge8));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge9));

std::vector<std::string> expected;
expected.push_back("0");
expected.push_back("1");
int index=0;
CXXGraph::Graph<int> graph(edgeSet);
CXXGraph::DijkstraResult res = graph.dijkstra(node0, node1);
ASSERT_TRUE(res.success);
ASSERT_EQ(res.errorMessage, "");
ASSERT_EQ(res.result, 2);
ASSERT_EQ(res.path.size(), expected.size());
for(auto elem:res.path){
ASSERT_EQ(elem,expected[index]);
index++;
}
expected.clear();
expected.push_back("0");
expected.push_back("2");
index=0;

res = graph.dijkstra(node0, node2);
ASSERT_TRUE(res.success);
ASSERT_EQ(res.errorMessage, "");
ASSERT_EQ(res.result, 6);
ASSERT_EQ(res.path.size(), expected.size());
for(auto elem:res.path){
ASSERT_EQ(elem,expected[index]);
index++;
}
expected.clear();
expected.push_back("0");
expected.push_back("1");
expected.push_back("3");
index=0;
res = graph.dijkstra(node0, node3);
ASSERT_TRUE(res.success);
ASSERT_EQ(res.errorMessage, "");
ASSERT_EQ(res.result, 7);
ASSERT_EQ(res.path.size(), expected.size());
for(auto elem:res.path){
ASSERT_EQ(elem,expected[index]);
index++;
}
expected.clear();
expected.push_back("0");
expected.push_back("1");
expected.push_back("3");
expected.push_back("4");
index=0;
res = graph.dijkstra(node0, node4);
ASSERT_TRUE(res.success);
ASSERT_EQ(res.errorMessage, "");
ASSERT_EQ(res.result, 17);
ASSERT_EQ(res.path.size(), expected.size());
for(auto elem:res.path){
ASSERT_EQ(elem,expected[index]);
index++;
}
expected.clear();
expected.push_back("0");
expected.push_back("1");
expected.push_back("3");
expected.push_back("5");
index=0;
res = graph.dijkstra(node0, node5);
ASSERT_TRUE(res.success);
ASSERT_EQ(res.errorMessage, "");
ASSERT_EQ(res.result, 22);
ASSERT_EQ(res.path.size(), expected.size());
for(auto elem:res.path){
ASSERT_EQ(elem,expected[index]);
index++;
}
expected.clear();
expected.push_back("0");
expected.push_back("1");
expected.push_back("3");
expected.push_back("4");
expected.push_back("6");
index=0;
res = graph.dijkstra(node0, node6);
ASSERT_TRUE(res.success);
ASSERT_EQ(res.errorMessage, "");
ASSERT_EQ(res.result, 19);
ASSERT_EQ(res.path.size(), expected.size());
for(auto elem:res.path){
ASSERT_EQ(elem,expected[index]);
index++;
}
}

TEST(DijkstraTest, correct_example_5) {
Expand Down Expand Up @@ -191,11 +312,22 @@ TEST(DijkstraTest, correct_example_5) {
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge8));
edgeSet.insert(make_shared<CXXGraph::UndirectedWeightedEdge<int>>(edge9));

std::vector<std::string> expected;
int index=0;
expected.push_back("1");
expected.push_back("3");
expected.push_back("6");
expected.push_back("5");
CXXGraph::Graph<int> graph(edgeSet);
CXXGraph::DijkstraResult res = graph.dijkstra(node1, node5);
ASSERT_TRUE(res.success);
ASSERT_EQ(res.errorMessage, "");
ASSERT_EQ(res.result, 20);
ASSERT_EQ(res.path.size(), expected.size());
for(auto elem:res.path){
ASSERT_EQ(elem,expected[index]);
index++;
}
}

TEST(DijkstraTest, non_weigthed_node_test) {
Expand Down

0 comments on commit 7c03e1a

Please sign in to comment.