Skip to content

Commit b554a3e

Browse files
authored
Merge pull request #38 from Debashis08/feature-graph-implementation
Feature graph implementation
2 parents 8f87e66 + 7e2e3bf commit b554a3e

File tree

7 files changed

+243
-3
lines changed

7 files changed

+243
-3
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#pragma once
2+
3+
#include<map>
4+
#include<string>
5+
#include<vector>
6+
using namespace std;
7+
8+
namespace DifferenceConstraintsShortestPaths
9+
{
10+
class Node
11+
{
12+
public:
13+
string data;
14+
int distance;
15+
Node(string data);
16+
};
17+
18+
class Edge
19+
{
20+
public:
21+
Node* nodeU;
22+
Node* nodeV;
23+
int weight;
24+
Edge(Node* nodeU, Node* nodeV, int weight);
25+
};
26+
27+
class Graph
28+
{
29+
private:
30+
map<Node*, vector<Node*>> _adjlist;
31+
map<string, Node*> _nodeMap;
32+
vector<Edge*> _edgeList;
33+
Node* MakeOrFindNode(string data);
34+
void PushDirectedEdge(string valueU, string valueV, int weight);
35+
void InitializeSingleSource(Node* sourceNode);
36+
void Relax(Edge* edge);
37+
38+
public:
39+
void PushAllDirectedEdges(vector<vector<int>> vectorA, vector<string> vectorX, vector<int> vectorB);
40+
bool FindDifferenceConstraintsSolutionBellmanFord();
41+
vector<pair<string, int>> GetDifferenceConstrtaintsSolution();
42+
};
43+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#include"../Headers/0003_Graph/0012_DifferenceConstraintsShortestPaths.h"
2+
#include<climits>
3+
using namespace std;
4+
5+
namespace DifferenceConstraintsShortestPaths
6+
{
7+
Node::Node(string data)
8+
{
9+
this->data = data;
10+
this->distance = INT_MAX;
11+
}
12+
13+
Edge::Edge(Node* nodeU, Node* nodeV, int weight)
14+
{
15+
this->nodeU = nodeU;
16+
this->nodeV = nodeV;
17+
this->weight = weight;
18+
}
19+
20+
// Graph Private Member Methods
21+
Node* Graph::MakeOrFindNode(string data)
22+
{
23+
Node* node = nullptr;
24+
if (this->_nodeMap.find(data) == this->_nodeMap.end())
25+
{
26+
node = new Node(data);
27+
this->_nodeMap[data] = node;
28+
}
29+
else
30+
{
31+
node = this->_nodeMap[data];
32+
}
33+
return node;
34+
}
35+
36+
void Graph::PushDirectedEdge(string dataU, string dataV, int weight)
37+
{
38+
Node* nodeU = this->MakeOrFindNode(dataU);
39+
Node* nodeV = this->MakeOrFindNode(dataV);
40+
41+
this->_adjlist[nodeU].push_back(nodeV);
42+
this->_edgeList.push_back(new Edge(nodeU, nodeV, weight));
43+
}
44+
45+
void Graph::InitializeSingleSource(Node* sourceNode)
46+
{
47+
for (auto& iterator : this->_nodeMap)
48+
{
49+
iterator.second->distance = INT_MAX;
50+
}
51+
sourceNode->distance = 0;
52+
}
53+
54+
void Graph::Relax(Edge* edge)
55+
{
56+
if (edge->nodeU->distance != INT_MAX && (edge->nodeV->distance > (edge->nodeU->distance + edge->weight)))
57+
{
58+
edge->nodeV->distance = edge->nodeU->distance + edge->weight;
59+
}
60+
}
61+
62+
// Graph Public Member Methods
63+
void Graph::PushAllDirectedEdges(vector<vector<int>> vectorA, vector<string> vectorX, vector<int> vectorB)
64+
{
65+
// Creating the Actual Graph
66+
string valueU = "";
67+
string valueV = "";
68+
int weight = 0;
69+
for (int i = 0; i < vectorA.size(); i++)
70+
{
71+
for (int j = 0; j < vectorX.size(); j++)
72+
{
73+
if (vectorA[i][j] == 1)
74+
{
75+
valueV= vectorX[j];
76+
}
77+
if (vectorA[i][j] == -1)
78+
{
79+
valueU = vectorX[j];
80+
}
81+
}
82+
weight = vectorB[i];
83+
this->PushDirectedEdge(valueU, valueV, weight);
84+
}
85+
86+
// Creating all the edges from the additional vertex
87+
valueU = "";
88+
valueV = "";
89+
weight = 0;
90+
for (int i = 0; i < vectorX.size(); i++)
91+
{
92+
valueV = vectorX[i];
93+
this->PushDirectedEdge(valueU, valueV, weight);
94+
}
95+
}
96+
97+
bool Graph::FindDifferenceConstraintsSolutionBellmanFord()
98+
{
99+
Node* source = this->_nodeMap[""];
100+
101+
this->InitializeSingleSource(source);
102+
103+
for (int i = 0; i < this->_nodeMap.size(); i++)
104+
{
105+
for (auto& edge : this->_edgeList)
106+
{
107+
this->Relax(edge);
108+
}
109+
}
110+
111+
for (auto& edge : this->_edgeList)
112+
{
113+
if (edge->nodeV->distance > (edge->nodeU->distance + edge->weight))
114+
{
115+
return false;
116+
}
117+
}
118+
return true;
119+
}
120+
121+
vector<pair<string, int>> Graph::GetDifferenceConstrtaintsSolution()
122+
{
123+
vector<pair<string, int>> result;
124+
for (auto& node : this->_nodeMap)
125+
{
126+
if (node.second->data != "")
127+
{
128+
result.push_back({ node.second->data, node.second->distance });
129+
}
130+
}
131+
return result;
132+
}
133+
}

SourceCodes/0003_Graph/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ set(0003GRAPH_SOURCES
1111
0009_SingleSourceShortestPathBellmanFord.cc
1212
0010_DirectedAcyclicGraphShortestPath.cc
1313
0011_SingleSourceShortestPathDijkstra.cc
14+
0012_DifferenceConstraintsShortestPaths.cc
1415

1516
)
1617

Tests/0000_CommonUtilities/UnitTestHelper.h

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ class UnitTestHelper
7373
return result;
7474
}
7575

76-
7776
// This helper method is used to sort the vector of vectors of a particular typename.
7877
// Each inner vector is sorted first.
7978
// Then each of them are sorted by their first element, in increasing order.
@@ -109,7 +108,7 @@ class UnitTestHelper
109108
}
110109

111110
template<typename T>
112-
bool NormalizeCyclesAnCompare(vector<T> data1, vector<T> data2)
111+
bool NormalizeCyclesAndCompare(vector<T> data1, vector<T> data2)
113112
{
114113
if (data1.size() != data2.size())
115114
{
@@ -158,4 +157,27 @@ class UnitTestHelper
158157
});
159158
return data;
160159
}
160+
161+
template<typename T1, typename T2>
162+
string SortVectorOfPairAndSerialize(vector<pair<T1, T2>> data)
163+
{
164+
// Sorting the vector in non-decreasing order on typename T1
165+
sort(data.begin(), data.end(), [](const pair<T1, T2>& pair1, const pair<T1, T2>& pair2)
166+
{
167+
return pair1.first < pair2.first;
168+
});
169+
170+
// Serializing the vector to string
171+
string result = "";
172+
for (auto& iterator : data)
173+
{
174+
result += iterator.first + "(" + to_string(iterator.second) + ")" + " ";
175+
}
176+
177+
if (!result.empty())
178+
{
179+
result.pop_back();
180+
}
181+
return result;
182+
}
161183
};

Tests/0003_Graph/0005_HamiltonianPathAndCycleTest.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ namespace HamiltonianPathAndCycle
2828

2929
ASSERT_TRUE(isHamiltonianCyclePresent);
3030
ASSERT_TRUE(isHamiltonianPathPresent);
31-
ASSERT_TRUE(unitTestHelper.NormalizeCyclesAnCompare(hamiltonianPathActualResult, hamiltonianPathExpectedResult));
31+
ASSERT_TRUE(unitTestHelper.NormalizeCyclesAndCompare(hamiltonianPathActualResult, hamiltonianPathExpectedResult));
3232
}
3333
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include<gtest/gtest.h>
2+
#include"../Headers/0003_Graph/0012_DifferenceConstraintsShortestPaths.h"
3+
#include"../0000_CommonUtilities/UnitTestHelper.h"
4+
using namespace std;
5+
6+
namespace DifferenceConstraintsShortestPaths
7+
{
8+
UnitTestHelper unitTestHelper;
9+
10+
TEST(DifferenceConstraints, SimpleGraph)
11+
{
12+
Graph graph;
13+
14+
vector<vector<int>> vectorA =
15+
{
16+
{1, -1, 0, 0, 0},
17+
{1, 0, 0, 0, -1},
18+
{0, 1, 0, 0, -1},
19+
{-1, 0, 1, 0, 0},
20+
{-1, 0, 0, 1, 0},
21+
{0, 0, -1, 1, 0},
22+
{0, 0, -1, 0, 1},
23+
{0, 0, 0, -1, 1},
24+
};
25+
vector<string> vectorX = { "v1", "v2", "v3", "v4", "v5" };
26+
vector<int> vectorB = {0, -1, 1, 5, 4, -1, -3, -3};
27+
vector<pair<string, int>> expectedSolution =
28+
{
29+
{"v2", -3},
30+
{"v5", -4},
31+
{"v1", -5},
32+
{"v4", -1},
33+
{"v3", 0},
34+
};
35+
graph.PushAllDirectedEdges(vectorA, vectorX, vectorB);
36+
37+
ASSERT_TRUE(graph.FindDifferenceConstraintsSolutionBellmanFord());
38+
ASSERT_EQ(unitTestHelper.SortVectorOfPairAndSerialize(graph.GetDifferenceConstrtaintsSolution()), unitTestHelper.SortVectorOfPairAndSerialize(expectedSolution));
39+
}
40+
}

Tests/0003_Graph/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ add_executable(
2323
0009_SingleSourceShortestPathBellmanFordTest.cc
2424
0010_DirectedAcyclicGraphShortestPathTest.cc
2525
0011_SingleSourceShortestPathDijkstraTest.cc
26+
0012_DifferenceConstraintsShortestPathsTest.cc
2627
)
2728

2829
target_link_libraries(

0 commit comments

Comments
 (0)