Skip to content
Open
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
68 changes: 68 additions & 0 deletions fifo_push_relabel_algorithm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <limits.h>

#define N 6

int min(int a, int b) {
return a < b ? a : b;
}

bool bfs(int rGraph[N][N], int s, int t, int parent[]) {
bool visited[N];
memset(visited, 0, sizeof(visited));
visited[s] = true;
parent[s] = -1;
int queue[N];
int front = 0, rear = 0;
queue[rear++] = s;
while (front < rear) {
int u = queue[front++];
for (int v = 0; v < N; v++) {
if (!visited[v] && rGraph[u][v] > 0) {
visited[v] = true;
parent[v] = u;
queue[rear++] = v;
}
}
}
return visited[t];
}

int fordFulkerson(int graph[N][N], int s, int t) {
int rGraph[N][N];
for (int u = 0; u < N; u++)
for (int v = 0; v < N; v++)
rGraph[u][v] = graph[u][v];
int parent[N];
int max_flow = 0;
while (bfs(rGraph, s, t, parent)) {
int path_flow = INT_MAX;
for (int v = t; v != s; v = parent[v]) {
int u = parent[v];
path_flow = min(path_flow, rGraph[u][v]);
}
for (int v = t; v != s; v = parent[v]) {
int u = parent[v];
rGraph[u][v] -= path_flow;
rGraph[v][u] += path_flow;
}
max_flow += path_flow;
}
return max_flow;
}

int main() {
int graph[N][N] = {
{0, 16, 13, 0, 0, 0},
{0, 0, 10, 12, 0, 0},
{0, 4, 0, 0, 14, 0},
{0, 0, 9, 0, 0, 20},
{0, 0, 0, 7, 0, 4},
{0, 0, 0, 0, 0, 0}
};
int s = 0, t = 5;
printf("Max Flow: %d\n", fordFulkerson(graph, s, t));
return 0;
}
104 changes: 104 additions & 0 deletions johnson_algorithm_for_sparse_graph.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#include <bits/stdc++.h>
#define INF 99999
using namespace std;

// Number of vertices in the graph
#define V 4

// A utility function to find the vertex with minimum
// distance value, from the set of vertices not yet included
// in shortest path tree
int minDistance(int dist[], bool sptSet[])
{
// Initialize min value
int min = INT_MAX, min_index;

for (int v = 0; v < V; v++)
if (sptSet[v] == false && dist[v] <= min)
min = dist[v], min_index = v;

return min_index;
}

// A utility function to print the constructed distance
// array
void printSolution(int dist[][V])
{
printf("Following matrix shows the shortest distances"
" between every pair of vertices \n");
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (dist[i][j] == INF)
printf("%7s", "INF");
else
printf("%7d", dist[i][j]);
}
printf("\n");
}
}

// Solves the all-pairs shortest path problem using
// Johnson's algorithm
void floydWarshall(int graph[][V])
{
int dist[V][V], i, j, k;

/* Initialize the solution matrix same as input graph
matrix. Or we can say the initial values of shortest
distances are based
on shortest paths considering no intermediate vertex.
*/
for (i = 0; i < V; i++)
for (j = 0; j < V; j++)
dist[i][j] = graph[i][j];

/* Add all vertices one by one to the set of
intermediate vertices.
---> Before start of a iteration, we have shortest
distances between all pairs of vertices such that the
shortest distances consider only the vertices in set
{0, 1, 2, .. k-1} as intermediate vertices.
----> After the end of a iteration, vertex no. k is
added to the set of
intermediate vertices and the set becomes {0, 1, 2, ..
k} */
for (k = 0; k < V; k++) {
// Pick all vertices as source one by one
for (i = 0; i < V; i++) {
// Pick all vertices as destination for the
// above picked source
for (j = 0; j < V; j++) {
// If vertex k is on the shortest path from
// i to j, then update the value of
// dist[i][j]
if (dist[i][k] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}

// Print the shortest distance matrix
printSolution(dist);
}

// driver program to test above function
int main()
{
/* Let us create the following weighted graph
10
(0)------->(3)
| /|\
5 | |
| | 1
\|/ |
(1)------->(2)
3 */
int graph[V][V] = { { 0, 5, INF, 10 },
{ INF, 0, 3, INF },
{ INF, INF, 0, 1 },
{ INF, INF, INF, 0 } };

// Print the solution
floydWarshall(graph);
return 0;
}
Loading