Skip to content

Feature graph implementation #53

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 2 commits into from
Jan 26, 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
66 changes: 66 additions & 0 deletions Headers/0003_Graph/0014_AllPairsShortestPathsJohnson.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#pragma once

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

namespace AllPairsShortestPathsJohnson
{
class Node
{
public:
int data;
int distance;
Node* parent;
int potentialWeight;
Node(int data);
};

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

class CompareNodeDistance
{
public:
bool operator()(const Node* nodeU, const Node* nodeV) const
{
return nodeU->distance < nodeV->distance;
}
};

class Graph
{
private:
int _noOfVertices=0;
map<Node*, vector<Node*>> _adjlist;
map<int, Node*> _nodeMap;
vector<Edge*> _edgeList;
map<Node*, vector<Edge*>> _edgeMap;
map<Node*, vector<Node*>> _augmentedAdjlist;
vector<Edge*> _augmentedEdgeList;
multiset<Node*, CompareNodeDistance> _operationalSet;
vector<vector<int>> _shortestPathMatrix;
vector<vector<int>> _predecessorMatrix;
Node* MakeOrFindNode(int data);
void PushAugmentedDirectedEdges(Node* sourceNode, Node* nodeV, int weight);
void InitializeSingleSource(Node* sourceNode);
void RelaxBellmanFord(Edge* edge);
bool BellmanFord(Node* source);
void RelaxDijkstra(Edge* edge);
void Dijkstra(Node* source);
void GetShortestPath(int source, int destination, vector<int>& path);

public:
void PushDirectedEdge(int dataU, int dataV, int weight);
bool FindAllPairsShortestPathsJohnsonAlgorithm();
vector<vector<int>> GetAllPairsShortestPathsDistanceMatrix();
vector<vector<int>> GetAllPairsShortestPathsPathMatrix();
};
}
213 changes: 213 additions & 0 deletions SourceCodes/0003_Graph/0014_AllPairsShortestPathsJohnson.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
#include "../Headers/0003_Graph/0014_AllPairsShortestPathsJohnson.h"
#include<climits>
using namespace std;

namespace AllPairsShortestPathsJohnson
{
Node::Node(int data)
{
this->data = data;
this->distance = INT_MAX;
this->parent = nullptr;
this->potentialWeight = 0;
}

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

// Graph Private Member Methods
Node* Graph::MakeOrFindNode(int 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::PushAugmentedDirectedEdges(Node* sourceNode, Node* nodeV, int weight)
{
this->_augmentedAdjlist[sourceNode].push_back(nodeV);
this->_augmentedEdgeList.push_back(new Edge(sourceNode, nodeV, weight));
}

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

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

bool Graph::BellmanFord(Node* source)
{
this->InitializeSingleSource(source);

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

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

void Graph::RelaxDijkstra(Edge* edge)
{
if (edge->nodeU->distance != INT_MAX && (edge->nodeV->distance > (edge->nodeU->distance + edge->weight)))
{
this->_operationalSet.erase(edge->nodeV);
edge->nodeV->distance = edge->nodeU->distance + edge->weight;
edge->nodeV->parent = edge->nodeU;
this->_operationalSet.insert(edge->nodeV);
}
}

void Graph::Dijkstra(Node* source)
{
this->InitializeSingleSource(source);

for (auto& node : this->_nodeMap)
{
this->_operationalSet.insert(node.second);
}

while (!this->_operationalSet.empty())
{
Node* nodeU = *(this->_operationalSet.begin());
this->_operationalSet.erase(nodeU);

for (auto& edge : this->_edgeMap[nodeU])
{
this->RelaxDijkstra(edge);
}
}
}

void Graph::GetShortestPath(int source, int destination, vector<int>& path)
{
if (this->_predecessorMatrix[source - 1][destination - 1] != source)
{
int predecessor = this->_predecessorMatrix[source - 1][destination - 1];
this->GetShortestPath(source, predecessor, path);
path.push_back(predecessor);
}
}

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

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

bool Graph::FindAllPairsShortestPathsJohnsonAlgorithm()
{
// Creating the graph G'
this->_augmentedAdjlist = this->_adjlist;
this->_augmentedEdgeList = this->_edgeList;
Node* source = new Node(0);
this->_nodeMap[0] = source;
for (auto& node : this->_nodeMap)
{
if (node.second != source)
{
this->PushAugmentedDirectedEdges(source, node.second, 0);
}
}

if (this->BellmanFord(source) == false)
{
return false;
}
else
{
this->_nodeMap.erase(0);
for (auto& node : this->_nodeMap)
{
node.second->potentialWeight = node.second->distance;
}

for (auto& edge : this->_edgeList)
{
edge->weight = edge->weight + edge->nodeU->potentialWeight - edge->nodeV->potentialWeight;
}

this->_noOfVertices = (int)this->_nodeMap.size();
this->_shortestPathMatrix = vector<vector<int>>(this->_noOfVertices, vector<int>(this->_noOfVertices, -1));
this->_predecessorMatrix = vector<vector<int>>(this->_noOfVertices, vector<int>(this->_noOfVertices, -1));
for (auto& iteratorU : this->_nodeMap)
{
Node* nodeU = iteratorU.second;
this->Dijkstra(nodeU);
for (auto& iteratorV : this->_nodeMap)
{
Node* nodeV = iteratorV.second;
this->_shortestPathMatrix[nodeU->data - 1][nodeV->data - 1] = nodeV->distance + nodeV->potentialWeight - nodeU->potentialWeight;
this->_predecessorMatrix[nodeU->data - 1][nodeV->data - 1] = nodeV->parent != nullptr ? nodeV->parent->data : -1;
}
}
return true;
}
}

vector<vector<int>> Graph::GetAllPairsShortestPathsDistanceMatrix()
{
return this->_shortestPathMatrix;
}

vector<vector<int>> Graph::GetAllPairsShortestPathsPathMatrix()
{
vector<vector<int>> result;
for (int i = 0; i < this->_noOfVertices; i++)
{
for (int j = 0; j < this->_noOfVertices; j++)
{
if (i != j)
{
vector<int> path = {};
path.push_back(i + 1);
this->GetShortestPath(i + 1, j + 1, path);
path.push_back(j + 1);
result.push_back(path);
}
}
}
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 @@ -13,6 +13,7 @@ set(0003GRAPH_SOURCES
0011_SingleSourceShortestPathDijkstra.cc
0012_DifferenceConstraintsShortestPaths.cc
0013_AllPairsShortestPathsFloydWarshall.cc
0014_AllPairsShortestPathsJohnson.cc

)

Expand Down
44 changes: 44 additions & 0 deletions Tests/0003_Graph/0014_AllPairsShortestPathsJohnsonTest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include<gtest/gtest.h>
#include"../Headers/0003_Graph/0014_AllPairsShortestPathsJohnson.h"
#include"../0000_CommonUtilities/UnitTestHelper.h"
using namespace std;

namespace AllPairsShortestPathsJohnson
{
UnitTestHelper unitTestHelper;

TEST(JohnsonAlgorithm, SimpleGraph)
{
// Arrange
Graph graph;
vector<vector<int>> expectedDistanceMatrix =
{
{0, 1, -3, 2, -4},
{3, 0, -4, 1, -1},
{7, 4, 0, 5, 3},
{2, -1, -5, 0, -2},
{8, 5, 1, 6, 0},
};
string expectedPredecessorMatrixesult = "[1 5 4 3 2][1 5 4 3][1 5 4][1 5][2 4 1][2 4 3][2 4][2 4 1 5][3 2 4 1][3 2][3 2 4][3 2 4 1 5][4 1][4 3 2][4 3][4 1 5][5 4 1][5 4 3 2][5 4 3][5 4]";

// Act
graph.PushDirectedEdge(1, 2, 3);
graph.PushDirectedEdge(1, 3, 8);
graph.PushDirectedEdge(1, 5, -4);
graph.PushDirectedEdge(2, 4, 1);
graph.PushDirectedEdge(2, 5, 7);
graph.PushDirectedEdge(3, 2, 4);
graph.PushDirectedEdge(4, 3, -5);
graph.PushDirectedEdge(4, 1, 2);
graph.PushDirectedEdge(5, 4, 6);

bool result = graph.FindAllPairsShortestPathsJohnsonAlgorithm();
vector<vector<int>> actualDistanceMatrix = graph.GetAllPairsShortestPathsDistanceMatrix();
string actualPredecessorMatrix = unitTestHelper.SerializeVectorToString(graph.GetAllPairsShortestPathsPathMatrix());

// Assert
ASSERT_TRUE(result);
ASSERT_EQ(expectedDistanceMatrix, actualDistanceMatrix);
ASSERT_EQ(expectedPredecessorMatrixesult, actualPredecessorMatrix);
}
}
1 change: 1 addition & 0 deletions Tests/0003_Graph/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ add_executable(
0011_SingleSourceShortestPathDijkstraTest.cc
0012_DifferenceConstraintsShortestPathsTest.cc
0013_AllPairsShortestPathsFloydWarshallTest.cc
0014_AllPairsShortestPathsJohnsonTest.cc
)

target_link_libraries(
Expand Down
Loading