Skip to content

Commit 3822fb3

Browse files
authored
Merge pull request #52 from Debashis08/feature-graph-implementation
feature: floyd warshall added
2 parents 1bc853a + 262410a commit 3822fb3

File tree

5 files changed

+166
-0
lines changed

5 files changed

+166
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#pragma once
2+
3+
#include<vector>
4+
using namespace std;
5+
6+
namespace AllPairsShortestPathsFloydWarshall
7+
{
8+
class Graph
9+
{
10+
private:
11+
int _noOfVertices;
12+
vector<vector<int>> _adjMatrix;
13+
vector<vector<int>> _shortestPathMatrixFloydWarshall;
14+
vector<vector<int>> _predecessorMatrix;
15+
void InitializeDistanceAndPredecessors();
16+
void GetShortestPath(int source, int destination, vector<int>& path);
17+
18+
public:
19+
void CreateGraph(int noOfVertices);
20+
void PushDirectedEdge(int valueU, int valueV, int weight);
21+
void FindAllPairsShortestPathsFloydWarshallSolution();
22+
vector<vector<int>> GetFloydWarshallShortestPath();
23+
};
24+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#include "../Headers/0003_Graph/0013_AllPairsShortestPathsFloydWarshall.h"
2+
#include<climits>
3+
using namespace std;
4+
5+
namespace AllPairsShortestPathsFloydWarshall
6+
{
7+
// Graph Private Member Methods
8+
void Graph::InitializeDistanceAndPredecessors()
9+
{
10+
this->_shortestPathMatrixFloydWarshall = this->_adjMatrix;
11+
12+
for (int i = 0; i < this->_noOfVertices; i++)
13+
{
14+
for (int j = 0; j < this->_noOfVertices; j++)
15+
{
16+
if ((i == j) || this->_adjMatrix[i][j] == INT_MAX)
17+
{
18+
this->_predecessorMatrix[i][j] = -1;
19+
}
20+
else
21+
{
22+
this->_predecessorMatrix[i][j] = i + 1;
23+
}
24+
}
25+
}
26+
}
27+
28+
void Graph::GetShortestPath(int source, int destination, vector<int>& path)
29+
{
30+
if (this->_predecessorMatrix[source - 1][destination - 1] != source)
31+
{
32+
int predecessor = this->_predecessorMatrix[source - 1][destination - 1];
33+
this->GetShortestPath(source, predecessor, path);
34+
path.push_back(predecessor);
35+
}
36+
}
37+
38+
// Graph Public Member Methods
39+
void Graph::CreateGraph(int noOfVertices)
40+
{
41+
this->_noOfVertices = noOfVertices;
42+
this->_adjMatrix = vector<vector<int>>(this->_noOfVertices, vector<int>(this->_noOfVertices, INT_MAX));
43+
this->_shortestPathMatrixFloydWarshall = vector<vector<int>>(this->_noOfVertices, vector<int>(this->_noOfVertices, INT_MAX));
44+
this->_predecessorMatrix = vector<vector<int>>(this->_noOfVertices, vector<int>(this->_noOfVertices, INT_MAX));
45+
46+
for (int i = 0; i < this->_noOfVertices; i++)
47+
{
48+
for (int j = 0; j < this->_noOfVertices; j++)
49+
{
50+
if (i == j)
51+
{
52+
this->_adjMatrix[i][j] = 0;
53+
}
54+
}
55+
}
56+
}
57+
58+
void Graph::PushDirectedEdge(int valueU, int valueV, int weight)
59+
{
60+
this->_adjMatrix[valueU - 1][valueV - 1] = weight;
61+
}
62+
63+
void Graph::FindAllPairsShortestPathsFloydWarshallSolution()
64+
{
65+
this->InitializeDistanceAndPredecessors();
66+
67+
for (int k = 0; k < this->_noOfVertices; k++)
68+
{
69+
for (int i = 0; i < this->_noOfVertices; i++)
70+
{
71+
for (int j = 0; j < this->_noOfVertices; j++)
72+
{
73+
if ((this->_shortestPathMatrixFloydWarshall[i][j] > (this->_shortestPathMatrixFloydWarshall[i][k] + this->_shortestPathMatrixFloydWarshall[k][j]))
74+
&&
75+
(this->_shortestPathMatrixFloydWarshall[i][k] != INT_MAX && this->_shortestPathMatrixFloydWarshall[k][j] != INT_MAX))
76+
{
77+
this->_shortestPathMatrixFloydWarshall[i][j] = this->_shortestPathMatrixFloydWarshall[i][k] + this->_shortestPathMatrixFloydWarshall[k][j];
78+
this->_predecessorMatrix[i][j] = this->_predecessorMatrix[k][j];
79+
}
80+
}
81+
}
82+
}
83+
}
84+
85+
vector<vector<int>> Graph::GetFloydWarshallShortestPath()
86+
{
87+
vector<vector<int>> result;
88+
for (int i = 0; i < this->_noOfVertices; i++)
89+
{
90+
for (int j = 0; j < this->_noOfVertices; j++)
91+
{
92+
if (i != j)
93+
{
94+
vector<int> path = {};
95+
path.push_back(i + 1);
96+
this->GetShortestPath(i + 1, j + 1, path);
97+
path.push_back(j + 1);
98+
result.push_back(path);
99+
}
100+
}
101+
}
102+
return result;
103+
}
104+
}

SourceCodes/0003_Graph/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ set(0003GRAPH_SOURCES
1212
0010_DirectedAcyclicGraphShortestPath.cc
1313
0011_SingleSourceShortestPathDijkstra.cc
1414
0012_DifferenceConstraintsShortestPaths.cc
15+
0013_AllPairsShortestPathsFloydWarshall.cc
1516

1617
)
1718

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include<gtest/gtest.h>
2+
#include "../Headers/0003_Graph/0013_AllPairsShortestPathsFloydWarshall.h"
3+
#include"../0000_CommonUtilities/UnitTestHelper.h"
4+
using namespace std;
5+
6+
namespace AllPairsShortestPathsFloydWarshall
7+
{
8+
UnitTestHelper unitTestHelper;
9+
10+
TEST(FloydWarshall, SimpleGraph)
11+
{
12+
// Arrange
13+
Graph graph;
14+
int noOfVertices = 5;
15+
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]";
16+
17+
// Act
18+
graph.CreateGraph(noOfVertices);
19+
20+
graph.PushDirectedEdge(1, 2, 3);
21+
graph.PushDirectedEdge(1, 3, 8);
22+
graph.PushDirectedEdge(1, 5, -4);
23+
graph.PushDirectedEdge(2, 4, 1);
24+
graph.PushDirectedEdge(2, 5, 7);
25+
graph.PushDirectedEdge(3, 2, 4);
26+
graph.PushDirectedEdge(4, 3, -5);
27+
graph.PushDirectedEdge(4, 1, 2);
28+
graph.PushDirectedEdge(5, 4, 6);
29+
30+
graph.FindAllPairsShortestPathsFloydWarshallSolution();
31+
string actualResult = unitTestHelper.SerializeVectorToString(graph.GetFloydWarshallShortestPath());
32+
33+
// Assert
34+
ASSERT_EQ(actualResult, expectedResult);
35+
}
36+
}

Tests/0003_Graph/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ add_executable(
2424
0010_DirectedAcyclicGraphShortestPathTest.cc
2525
0011_SingleSourceShortestPathDijkstraTest.cc
2626
0012_DifferenceConstraintsShortestPathsTest.cc
27+
0013_AllPairsShortestPathsFloydWarshallTest.cc
2728
)
2829

2930
target_link_libraries(

0 commit comments

Comments
 (0)