Skip to content

Commit 449ab2a

Browse files
committed
feature-test: dijkstra logic, test added
1 parent 601924d commit 449ab2a

File tree

5 files changed

+200
-0
lines changed

5 files changed

+200
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#pragma once
2+
3+
#include<map>
4+
#include<vector>
5+
#include<set>
6+
using namespace std;
7+
8+
namespace SingleSourceShortestPathDijkstra
9+
{
10+
class Node
11+
{
12+
public:
13+
int data;
14+
int distance;
15+
Node* parent;
16+
Node(int data);
17+
};
18+
19+
class Edge
20+
{
21+
public:
22+
Node* nodeU;
23+
Node* nodeV;
24+
int weight;
25+
Edge(Node* nodeU, Node* nodeV, int weight);
26+
};
27+
28+
class CompareNodeDistance
29+
{
30+
public:
31+
bool operator()(const Node* nodeU, const Node* nodeV) const
32+
{
33+
return nodeU->distance < nodeV->distance;
34+
}
35+
};
36+
37+
class Graph
38+
{
39+
private:
40+
map<Node*, vector<Node*>> _adjlist;
41+
map<int, Node*> _nodeMap;
42+
map<Node*, vector<Edge*>> _edgeMap;
43+
multiset<Node*, CompareNodeDistance> _operationalSet;
44+
Node* MakeOrFindNode(int data);
45+
void InitializeSingleSource(Node* sourceNode);
46+
void Relax(Edge* edge);
47+
void Dijkstra(Node* source);
48+
void GetShortestPath(Node* node, vector<int>& path);
49+
50+
public:
51+
void PushDirectedEdge(int valueU, int valueV, int weight);
52+
void FindShortestPathDijkstra(int data);
53+
vector<int> GetDijkstraShortestPath(int data);
54+
};
55+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#include "../Headers/0003_Graph/0011_SingleSourceShortestPathDijkstra.h"
2+
#include<climits>
3+
using namespace std;
4+
5+
namespace SingleSourceShortestPathDijkstra
6+
{
7+
Node::Node(int data)
8+
{
9+
this->data = data;
10+
this->distance = INT_MAX;
11+
this->parent = nullptr;
12+
}
13+
14+
Edge::Edge(Node* nodeU, Node* nodeV, int weight)
15+
{
16+
this->nodeU = nodeU;
17+
this->nodeV = nodeV;
18+
this->weight = weight;
19+
}
20+
21+
// Graph Private Member Methods
22+
Node* Graph::MakeOrFindNode(int data)
23+
{
24+
Node* node = nullptr;
25+
if (this->_nodeMap.find(data) == this->_nodeMap.end())
26+
{
27+
node = new Node(data);
28+
this->_nodeMap[data] = node;
29+
}
30+
else
31+
{
32+
node = this->_nodeMap[data];
33+
}
34+
return node;
35+
}
36+
37+
void Graph::InitializeSingleSource(Node* sourceNode)
38+
{
39+
for (auto& iterator : this->_nodeMap)
40+
{
41+
iterator.second->distance = INT_MAX;
42+
iterator.second->parent = nullptr;
43+
}
44+
sourceNode->distance = 0;
45+
}
46+
47+
void Graph::Relax(Edge* edge)
48+
{
49+
if (edge->nodeU->distance != INT_MAX && (edge->nodeV->distance > (edge->nodeU->distance + edge->weight)))
50+
{
51+
this->_operationalSet.erase(edge->nodeV);
52+
edge->nodeV->distance = edge->nodeU->distance + edge->weight;
53+
edge->nodeV->parent = edge->nodeU;
54+
this->_operationalSet.insert(edge->nodeV);
55+
}
56+
}
57+
58+
void Graph::Dijkstra(Node* source)
59+
{
60+
this->InitializeSingleSource(source);
61+
62+
for (auto& node : this->_nodeMap)
63+
{
64+
this->_operationalSet.insert(node.second);
65+
}
66+
67+
while (!this->_operationalSet.empty())
68+
{
69+
Node* nodeU = *(this->_operationalSet.begin());
70+
this->_operationalSet.erase(nodeU);
71+
72+
for (auto& edge : this->_edgeMap[nodeU])
73+
{
74+
this->Relax(edge);
75+
}
76+
}
77+
}
78+
79+
void Graph::GetShortestPath(Node* node, vector<int>& path)
80+
{
81+
path.push_back(node->data);
82+
if (node->parent != nullptr)
83+
{
84+
this->GetShortestPath(node->parent, path);
85+
}
86+
}
87+
88+
// Graph Public Member Methods
89+
void Graph::PushDirectedEdge(int dataU, int dataV, int weight)
90+
{
91+
Node* nodeU = this->MakeOrFindNode(dataU);
92+
Node* nodeV = this->MakeOrFindNode(dataV);
93+
94+
this->_adjlist[nodeU].push_back(nodeV);
95+
this->_edgeMap[nodeU].push_back(new Edge(nodeU, nodeV, weight));
96+
}
97+
98+
void Graph::FindShortestPathDijkstra(int data)
99+
{
100+
Node* source = this->_nodeMap[data];
101+
this->Dijkstra(source);
102+
}
103+
104+
vector<int> Graph::GetDijkstraShortestPath(int data)
105+
{
106+
vector<int> path = {};
107+
Node* node = this->_nodeMap[data];
108+
this->GetShortestPath(node, path);
109+
reverse(path.begin(), path.end());
110+
return path;
111+
}
112+
}

SourceCodes/0003_Graph/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ set(0003GRAPH_SOURCES
1010
0008_MinimumSpanningTreePrimAlgorithm.cc
1111
0009_SingleSourceShortestPathBellmanFord.cc
1212
0010_DirectedAcyclicGraphShortestPath.cc
13+
0011_SingleSourceShortestPathDijkstra.cc
1314

1415
)
1516

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include<gtest/gtest.h>
2+
#include "../Headers/0003_Graph/0011_SingleSourceShortestPathDijkstra.h"
3+
#include "../0000_CommonUtilities/UnitTestHelper.h"
4+
5+
namespace SingleSourceShortestPathDijkstra
6+
{
7+
UnitTestHelper unitTestHelper;
8+
9+
// Test for Simple Graph
10+
TEST(DijkstraTest, SimpleGraph)
11+
{
12+
Graph graph;
13+
14+
graph.PushDirectedEdge(0, 1, 10);
15+
graph.PushDirectedEdge(0, 3, 5);
16+
graph.PushDirectedEdge(1, 2, 1);
17+
graph.PushDirectedEdge(1, 3, 2);
18+
graph.PushDirectedEdge(2, 4, 4);
19+
graph.PushDirectedEdge(3, 1, 3);
20+
graph.PushDirectedEdge(3, 2, 9);
21+
graph.PushDirectedEdge(3, 4, 2);
22+
graph.PushDirectedEdge(4, 2, 6);
23+
graph.PushDirectedEdge(4, 0, 7);
24+
25+
graph.FindShortestPathDijkstra(0);
26+
27+
string expectedPath = "0 3 1 2";
28+
29+
ASSERT_EQ(unitTestHelper.SerializeVectorToString(graph.GetDijkstraShortestPath(2)), expectedPath);
30+
}
31+
}

Tests/0003_Graph/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ add_executable(
2222
0008_MinimumSpanningTreePrimAlgorithmTest.cc
2323
0009_SingleSourceShortestPathBellmanFordTest.cc
2424
0010_DirectedAcyclicGraphShortestPathTest.cc
25+
0011_SingleSourceShortestPathDijkstraTest.cc
2526
)
2627

2728
target_link_libraries(

0 commit comments

Comments
 (0)