Skip to content

feature: floyd warshall added #52

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 1 commit into from
Jan 21, 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
24 changes: 24 additions & 0 deletions Headers/0003_Graph/0013_AllPairsShortestPathsFloydWarshall.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include<vector>
using namespace std;

namespace AllPairsShortestPathsFloydWarshall
{
class Graph
{
private:
int _noOfVertices;
vector<vector<int>> _adjMatrix;
vector<vector<int>> _shortestPathMatrixFloydWarshall;
vector<vector<int>> _predecessorMatrix;
void InitializeDistanceAndPredecessors();
void GetShortestPath(int source, int destination, vector<int>& path);

public:
void CreateGraph(int noOfVertices);
void PushDirectedEdge(int valueU, int valueV, int weight);
void FindAllPairsShortestPathsFloydWarshallSolution();
vector<vector<int>> GetFloydWarshallShortestPath();
};
}
104 changes: 104 additions & 0 deletions SourceCodes/0003_Graph/0013_AllPairsShortestPathsFloydWarshall.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#include "../Headers/0003_Graph/0013_AllPairsShortestPathsFloydWarshall.h"
#include<climits>
using namespace std;

namespace AllPairsShortestPathsFloydWarshall
{
// Graph Private Member Methods
void Graph::InitializeDistanceAndPredecessors()
{
this->_shortestPathMatrixFloydWarshall = this->_adjMatrix;

for (int i = 0; i < this->_noOfVertices; i++)
{
for (int j = 0; j < this->_noOfVertices; j++)
{
if ((i == j) || this->_adjMatrix[i][j] == INT_MAX)
{
this->_predecessorMatrix[i][j] = -1;
}
else
{
this->_predecessorMatrix[i][j] = i + 1;
}
}
}
}

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::CreateGraph(int noOfVertices)
{
this->_noOfVertices = noOfVertices;
this->_adjMatrix = vector<vector<int>>(this->_noOfVertices, vector<int>(this->_noOfVertices, INT_MAX));
this->_shortestPathMatrixFloydWarshall = vector<vector<int>>(this->_noOfVertices, vector<int>(this->_noOfVertices, INT_MAX));
this->_predecessorMatrix = vector<vector<int>>(this->_noOfVertices, vector<int>(this->_noOfVertices, INT_MAX));

for (int i = 0; i < this->_noOfVertices; i++)
{
for (int j = 0; j < this->_noOfVertices; j++)
{
if (i == j)
{
this->_adjMatrix[i][j] = 0;
}
}
}
}

void Graph::PushDirectedEdge(int valueU, int valueV, int weight)
{
this->_adjMatrix[valueU - 1][valueV - 1] = weight;
}

void Graph::FindAllPairsShortestPathsFloydWarshallSolution()
{
this->InitializeDistanceAndPredecessors();

for (int k = 0; k < this->_noOfVertices; k++)
{
for (int i = 0; i < this->_noOfVertices; i++)
{
for (int j = 0; j < this->_noOfVertices; j++)
{
if ((this->_shortestPathMatrixFloydWarshall[i][j] > (this->_shortestPathMatrixFloydWarshall[i][k] + this->_shortestPathMatrixFloydWarshall[k][j]))
&&
(this->_shortestPathMatrixFloydWarshall[i][k] != INT_MAX && this->_shortestPathMatrixFloydWarshall[k][j] != INT_MAX))
{
this->_shortestPathMatrixFloydWarshall[i][j] = this->_shortestPathMatrixFloydWarshall[i][k] + this->_shortestPathMatrixFloydWarshall[k][j];
this->_predecessorMatrix[i][j] = this->_predecessorMatrix[k][j];
}
}
}
}
}

vector<vector<int>> Graph::GetFloydWarshallShortestPath()
{
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 @@ -12,6 +12,7 @@ set(0003GRAPH_SOURCES
0010_DirectedAcyclicGraphShortestPath.cc
0011_SingleSourceShortestPathDijkstra.cc
0012_DifferenceConstraintsShortestPaths.cc
0013_AllPairsShortestPathsFloydWarshall.cc

)

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

namespace AllPairsShortestPathsFloydWarshall
{
UnitTestHelper unitTestHelper;

TEST(FloydWarshall, SimpleGraph)
{
// Arrange
Graph graph;
int noOfVertices = 5;
string expectedResult = "[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.CreateGraph(noOfVertices);

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);

graph.FindAllPairsShortestPathsFloydWarshallSolution();
string actualResult = unitTestHelper.SerializeVectorToString(graph.GetFloydWarshallShortestPath());

// Assert
ASSERT_EQ(actualResult, expectedResult);
}
}
1 change: 1 addition & 0 deletions Tests/0003_Graph/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ add_executable(
0010_DirectedAcyclicGraphShortestPathTest.cc
0011_SingleSourceShortestPathDijkstraTest.cc
0012_DifferenceConstraintsShortestPathsTest.cc
0013_AllPairsShortestPathsFloydWarshallTest.cc
)

target_link_libraries(
Expand Down
Loading