Skip to content

Feature graph implementation #57

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
Mar 11, 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
29 changes: 29 additions & 0 deletions Headers/0003_Graph/0015_MaximumFlowFordFulkerson.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

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

namespace MaximumFlowFordFulkerson
{
class Graph
{
private:
int _noOfVertices;
int _source;
int _sink;
int _maximumFlow;
bool _flagParallelEdges;
vector<vector<int>> _adjMatrix;
vector<vector<int>> _residualGraph;
vector<int> _parent;
vector<bool> _visited;
void ResolveAntiParallelEdges();
void DepthFirstSearchVisit(int nodeU);
bool DepthFirstSearch();
public:
void CreateGraph(int noOfVertices);
void PushDirectedEdge(int valueU, int valueV, int capacity);
int FindMaximumFlowFordFulkerson();
};
}
139 changes: 139 additions & 0 deletions SourceCodes/0003_Graph/0015_MaximumFlowFordFulkerson.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#include "../Headers/0003_Graph/0015_MaximumFlowFordFulkerson.h"
#include<climits>
using namespace std;

namespace MaximumFlowFordFulkerson
{
// Graph Private Member Methods
void Graph::ResolveAntiParallelEdges()
{
int countParallelEdges = 0;
for (int i = 0; i < this->_noOfVertices; i++)
{
for (int j = 0; j < this->_noOfVertices; j++)
{
if (this->_adjMatrix[i][j] > 0 && this->_adjMatrix[j][i] > 0)
{
countParallelEdges++;
}
}
}

// As i->j and j->i both edges has been counted, actual count is count = count / 2
countParallelEdges /= 2;

this->_flagParallelEdges = countParallelEdges > 0;

// If there are no anti-parallel edges, no need to modify the adjMatrix
if (!this->_flagParallelEdges)
{
return;
}

int newNoOfVertices = this->_noOfVertices + countParallelEdges;

// Modifying the adjMatrix
for (auto& edge : this->_adjMatrix)
{
edge.resize(newNoOfVertices, 0);
}
int k = this->_noOfVertices;
this->_visited.resize(newNoOfVertices, false);
this->_parent.resize(newNoOfVertices, -1);
this->_adjMatrix.resize(newNoOfVertices, vector<int>(newNoOfVertices, 0));

// Removing the anti-parallel edges by adding new nodes
for (int i = 0; i < this->_noOfVertices; i++)
{
for (int j = 0; j < this->_noOfVertices; j++)
{
if (this->_adjMatrix[i][j] > 0 && this->_adjMatrix[j][i] > 0)
{
this->_adjMatrix[i][k] = this->_adjMatrix[i][j];
this->_adjMatrix[k][j] = this->_adjMatrix[i][j];
this->_adjMatrix[i][j] = 0;
k++;
}
}
}

// Updating the total no of vertices after modifying the adjMatrix
this->_noOfVertices = newNoOfVertices;
}

void Graph::DepthFirstSearchVisit(int nodeU)
{
this->_visited[nodeU] = true;
for (int nodeV = 0; nodeV < this->_noOfVertices; nodeV++)
{
if (!this->_visited[nodeV] && this->_residualGraph[nodeU][nodeV] > 0)
{
this->_parent[nodeV] = nodeU;
this->DepthFirstSearchVisit(nodeV);
}
}
}

bool Graph::DepthFirstSearch()
{
// Resetting the visited values
fill(this->_visited.begin(), this->_visited.end(), false);

// Resetting the parent values
fill(this->_parent.begin(), this->_parent.end(), -1);

// Starting the DepthFirstSearch from the source vertex
this->DepthFirstSearchVisit(this->_source);

// Returning the visited value of the sink vertex, initially it was set to false
return this->_visited[this->_sink];
}

// Graph Public Member Methods
void Graph::CreateGraph(int noOfVertices)
{
this->_noOfVertices = noOfVertices;
this->_source = 0;
this->_sink = this->_noOfVertices - 1;
this->_maximumFlow = 0;
this->_flagParallelEdges = false;
this->_adjMatrix = vector<vector<int>>(this->_noOfVertices, vector<int>(this->_noOfVertices, 0));
this->_parent = vector<int>(this->_noOfVertices, -1);
this->_visited = vector<bool>(this->_noOfVertices, false);
}

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

int Graph::FindMaximumFlowFordFulkerson()
{
// Resolving all the parallel edges if present
this->ResolveAntiParallelEdges();
this->_residualGraph = this->_adjMatrix;

// While there exists a path p from source to sink in the residual network G'
while (this->DepthFirstSearch())
{
int augmentedPathFlow = INT_MAX;

// Calculating c'(p) = min{ c'(u,v) : (u,v) is in p }
for (int nodeV = this->_sink; nodeV > this->_source; nodeV = this->_parent[nodeV])
{
int nodeU = this->_parent[nodeV];
augmentedPathFlow = min(augmentedPathFlow, this->_residualGraph[nodeU][nodeV]);
}

for (int nodeV = this->_sink; nodeV > this->_source; nodeV = this->_parent[nodeV])
{
int nodeU = this->_parent[nodeV];
this->_residualGraph[nodeU][nodeV] -= augmentedPathFlow;
this->_residualGraph[nodeV][nodeU] += augmentedPathFlow;
}
this->_maximumFlow += augmentedPathFlow;
}

return this->_maximumFlow;
}
}
1 change: 1 addition & 0 deletions SourceCodes/0003_Graph/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ set(0003GRAPH_SOURCES
0012_DifferenceConstraintsShortestPaths.cc
0013_AllPairsShortestPathsFloydWarshall.cc
0014_AllPairsShortestPathsJohnson.cc
0015_MaximumFlowFordFulkerson.cc

)

Expand Down
64 changes: 64 additions & 0 deletions Tests/0003_Graph/0015_MaximumFlowFordFulkersonTest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include<gtest/gtest.h>
#include "../Headers/0003_Graph/0015_MaximumFlowFordFulkerson.h"
#include "../0000_CommonUtilities/UnitTestHelper.h"

namespace MaximumFlowFordFulkerson
{
UnitTestHelper unitTestHelper;

TEST(MaximumFlowFordFulkerson, GraphWithNoParallelEdges)
{
// Arrange
Graph graph;
int noOfVertices = 6;
int expectedMaximumFlow = 23;


// Act
graph.CreateGraph(noOfVertices);

graph.PushDirectedEdge(0, 1, 16);
graph.PushDirectedEdge(0, 2, 13);
graph.PushDirectedEdge(1, 3, 12);
graph.PushDirectedEdge(2, 1, 4);
graph.PushDirectedEdge(2, 4, 14);
graph.PushDirectedEdge(3, 2, 9);
graph.PushDirectedEdge(3, 5, 20);
graph.PushDirectedEdge(4, 3, 7);
graph.PushDirectedEdge(4, 5, 4);

int actualMaximumFlow = graph.FindMaximumFlowFordFulkerson();

// Assert
ASSERT_EQ(expectedMaximumFlow, actualMaximumFlow);
}

TEST(MaximumFlowFordFulkerson, GraphWithParallelEdges)
{
// Arrange
Graph graph;
int noOfVertices = 6;
int expectedMaximumFlow = 24;


// Act
graph.CreateGraph(noOfVertices);

graph.PushDirectedEdge(0, 1, 16);
graph.PushDirectedEdge(0, 2, 13);
graph.PushDirectedEdge(1, 3, 12);
graph.PushDirectedEdge(1, 2, 6);
graph.PushDirectedEdge(2, 1, 10);
graph.PushDirectedEdge(2, 4, 14);
graph.PushDirectedEdge(2, 3, 2);
graph.PushDirectedEdge(3, 2, 11);
graph.PushDirectedEdge(3, 5, 20);
graph.PushDirectedEdge(4, 3, 7);
graph.PushDirectedEdge(4, 5, 4);

int actualMaximumFlow = graph.FindMaximumFlowFordFulkerson();

// Assert
ASSERT_EQ(expectedMaximumFlow, actualMaximumFlow);
}
}
1 change: 1 addition & 0 deletions Tests/0003_Graph/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ add_executable(
0012_DifferenceConstraintsShortestPathsTest.cc
0013_AllPairsShortestPathsFloydWarshallTest.cc
0014_AllPairsShortestPathsJohnsonTest.cc
0015_MaximumFlowFordFulkersonTest.cc
)

target_link_libraries(
Expand Down