Skip to content

Feature graph implementation #38

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

Merged
merged 4 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
43 changes: 43 additions & 0 deletions Headers/0003_Graph/0012_DifferenceConstraintsShortestPaths.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once

#include<map>
#include<string>
#include<vector>
using namespace std;

namespace DifferenceConstraintsShortestPaths
{
class Node
{
public:
string data;
int distance;
Node(string data);
};

class Edge
{
public:
Node* nodeU;
Node* nodeV;
int weight;
Edge(Node* nodeU, Node* nodeV, int weight);
};

class Graph
{
private:
map<Node*, vector<Node*>> _adjlist;
map<string, Node*> _nodeMap;
vector<Edge*> _edgeList;
Node* MakeOrFindNode(string data);
void PushDirectedEdge(string valueU, string valueV, int weight);
void InitializeSingleSource(Node* sourceNode);
void Relax(Edge* edge);

public:
void PushAllDirectedEdges(vector<vector<int>> vectorA, vector<string> vectorX, vector<int> vectorB);
bool FindDifferenceConstraintsSolutionBellmanFord();
vector<pair<string, int>> GetDifferenceConstrtaintsSolution();
};
}
133 changes: 133 additions & 0 deletions SourceCodes/0003_Graph/0012_DifferenceConstraintsShortestPaths.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#include"../Headers/0003_Graph/0012_DifferenceConstraintsShortestPaths.h"
#include<climits>
using namespace std;

namespace DifferenceConstraintsShortestPaths
{
Node::Node(string data)
{
this->data = data;
this->distance = INT_MAX;
}

Edge::Edge(Node* nodeU, Node* nodeV, int weight)
{
this->nodeU = nodeU;
this->nodeV = nodeV;
this->weight = weight;
}

// Graph Private Member Methods
Node* Graph::MakeOrFindNode(string data)
{
Node* node = nullptr;
if (this->_nodeMap.find(data) == this->_nodeMap.end())
{
node = new Node(data);
this->_nodeMap[data] = node;
}
else
{
node = this->_nodeMap[data];
}
return node;
}

void Graph::PushDirectedEdge(string dataU, string dataV, int weight)
{
Node* nodeU = this->MakeOrFindNode(dataU);
Node* nodeV = this->MakeOrFindNode(dataV);

this->_adjlist[nodeU].push_back(nodeV);
this->_edgeList.push_back(new Edge(nodeU, nodeV, weight));
}

void Graph::InitializeSingleSource(Node* sourceNode)
{
for (auto& iterator : this->_nodeMap)
{
iterator.second->distance = INT_MAX;
}
sourceNode->distance = 0;
}

void Graph::Relax(Edge* edge)
{
if (edge->nodeU->distance != INT_MAX && (edge->nodeV->distance > (edge->nodeU->distance + edge->weight)))
{
edge->nodeV->distance = edge->nodeU->distance + edge->weight;
}
}

// Graph Public Member Methods
void Graph::PushAllDirectedEdges(vector<vector<int>> vectorA, vector<string> vectorX, vector<int> vectorB)
{
// Creating the Actual Graph
string valueU = "";
string valueV = "";
int weight = 0;
for (int i = 0; i < vectorA.size(); i++)
{
for (int j = 0; j < vectorX.size(); j++)
{
if (vectorA[i][j] == 1)
{
valueV= vectorX[j];
}
if (vectorA[i][j] == -1)
{
valueU = vectorX[j];
}
}
weight = vectorB[i];
this->PushDirectedEdge(valueU, valueV, weight);
}

// Creating all the edges from the additional vertex
valueU = "";
valueV = "";
weight = 0;
for (int i = 0; i < vectorX.size(); i++)
{
valueV = vectorX[i];
this->PushDirectedEdge(valueU, valueV, weight);
}
}

bool Graph::FindDifferenceConstraintsSolutionBellmanFord()
{
Node* source = this->_nodeMap[""];

this->InitializeSingleSource(source);

for (int i = 0; i < this->_nodeMap.size(); i++)
{
for (auto& edge : this->_edgeList)
{
this->Relax(edge);
}
}

for (auto& edge : this->_edgeList)
{
if (edge->nodeV->distance > (edge->nodeU->distance + edge->weight))
{
return false;
}
}
return true;
}

vector<pair<string, int>> Graph::GetDifferenceConstrtaintsSolution()
{
vector<pair<string, int>> result;
for (auto& node : this->_nodeMap)
{
if (node.second->data != "")
{
result.push_back({ node.second->data, node.second->distance });
}
}
return result;
}
}
1 change: 1 addition & 0 deletions SourceCodes/0003_Graph/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ set(0003GRAPH_SOURCES
0009_SingleSourceShortestPathBellmanFord.cc
0010_DirectedAcyclicGraphShortestPath.cc
0011_SingleSourceShortestPathDijkstra.cc
0012_DifferenceConstraintsShortestPaths.cc

)

Expand Down
26 changes: 24 additions & 2 deletions Tests/0000_CommonUtilities/UnitTestHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ class UnitTestHelper
return result;
}


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

template<typename T>
bool NormalizeCyclesAnCompare(vector<T> data1, vector<T> data2)
bool NormalizeCyclesAndCompare(vector<T> data1, vector<T> data2)
{
if (data1.size() != data2.size())
{
Expand Down Expand Up @@ -158,4 +157,27 @@ class UnitTestHelper
});
return data;
}

template<typename T1, typename T2>
string SortVectorOfPairAndSerialize(vector<pair<T1, T2>> data)
{
// Sorting the vector in non-decreasing order on typename T1
sort(data.begin(), data.end(), [](const pair<T1, T2>& pair1, const pair<T1, T2>& pair2)
{
return pair1.first < pair2.first;
});

// Serializing the vector to string
string result = "";
for (auto& iterator : data)
{
result += iterator.first + "(" + to_string(iterator.second) + ")" + " ";
}

if (!result.empty())
{
result.pop_back();
}
return result;
}
};
2 changes: 1 addition & 1 deletion Tests/0003_Graph/0005_HamiltonianPathAndCycleTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ namespace HamiltonianPathAndCycle

ASSERT_TRUE(isHamiltonianCyclePresent);
ASSERT_TRUE(isHamiltonianPathPresent);
ASSERT_TRUE(unitTestHelper.NormalizeCyclesAnCompare(hamiltonianPathActualResult, hamiltonianPathExpectedResult));
ASSERT_TRUE(unitTestHelper.NormalizeCyclesAndCompare(hamiltonianPathActualResult, hamiltonianPathExpectedResult));
}
}
40 changes: 40 additions & 0 deletions Tests/0003_Graph/0012_DifferenceConstraintsShortestPathsTest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include<gtest/gtest.h>
#include"../Headers/0003_Graph/0012_DifferenceConstraintsShortestPaths.h"
#include"../0000_CommonUtilities/UnitTestHelper.h"
using namespace std;

namespace DifferenceConstraintsShortestPaths
{
UnitTestHelper unitTestHelper;

TEST(DifferenceConstraints, SimpleGraph)
{
Graph graph;

vector<vector<int>> vectorA =
{
{1, -1, 0, 0, 0},
{1, 0, 0, 0, -1},
{0, 1, 0, 0, -1},
{-1, 0, 1, 0, 0},
{-1, 0, 0, 1, 0},
{0, 0, -1, 1, 0},
{0, 0, -1, 0, 1},
{0, 0, 0, -1, 1},
};
vector<string> vectorX = { "v1", "v2", "v3", "v4", "v5" };
vector<int> vectorB = {0, -1, 1, 5, 4, -1, -3, -3};
vector<pair<string, int>> expectedSolution =
{
{"v2", -3},
{"v5", -4},
{"v1", -5},
{"v4", -1},
{"v3", 0},
};
graph.PushAllDirectedEdges(vectorA, vectorX, vectorB);

ASSERT_TRUE(graph.FindDifferenceConstraintsSolutionBellmanFord());
ASSERT_EQ(unitTestHelper.SortVectorOfPairAndSerialize(graph.GetDifferenceConstrtaintsSolution()), unitTestHelper.SortVectorOfPairAndSerialize(expectedSolution));
}
}
1 change: 1 addition & 0 deletions Tests/0003_Graph/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ add_executable(
0009_SingleSourceShortestPathBellmanFordTest.cc
0010_DirectedAcyclicGraphShortestPathTest.cc
0011_SingleSourceShortestPathDijkstraTest.cc
0012_DifferenceConstraintsShortestPathsTest.cc
)

target_link_libraries(
Expand Down
Loading