Skip to content
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

Added Ford Fulkerson algorithm for Hacktoberfest contribution #1708

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
52 changes: 52 additions & 0 deletions Graphs/FordFulkerson.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// https://en.wikipedia.org/wiki/Ford%E2%80%93Fulkerson_algorithm
function fordFulkerson(capacity, source, sink) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It needs to be documented what format this is in. (Side note: I would prefer an adjacency list representation because it yields a better time complexity.)

const V = capacity.length;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This violates our naming conventions. It should be called n, n_vertices or similar.

const residualCapacity = capacity.map((arr) => arr.slice());
const parent = Array(V).fill(-1);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments would help clarity here.

let maxFlow = 0;

function bfs(source, sink) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're using BFS to find augmenting paths, this isn't just any Ford-Fulkerson algorithm, it's the Edmonds-Karp variant to be specific (which has a runtime bound independent from the specific capacities).

const visited = Array(V).fill(false);
const queue = [source];
visited[source] = true;
parent[source] = -1;

while (queue.length > 0) {
const u = queue.shift();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This unnecessarily wrecks the time complexity (assuming you decide to go with an adjacency list). This should use an actual queue. Or alternatively, implement the BFS as "levelorder" via level / next level arrays you populate and swap.


for (let v = 0; v < V; v++) {
if (!visited[v] && residualCapacity[u][v] > 0) {
if (v === sink) {
parent[v] = u;
return true;
}
queue.push(v);
parent[v] = u;
visited[v] = true;
}
}
}
return false;
}

while (bfs(source, sink)) {
let pathFlow = Infinity;
for (let v = sink; v !== source; v = parent[v]) {
const u = parent[v];
pathFlow = Math.min(pathFlow, residualCapacity[u][v]);
}

for (let v = sink; v !== source; v = parent[v]) {
const u = parent[v];
residualCapacity[u][v] -= pathFlow;
residualCapacity[v][u] += pathFlow;
}

maxFlow += pathFlow;
}

return maxFlow;
}

export { fordFulkerson };

46 changes: 46 additions & 0 deletions Graphs/test/FordFulkerson.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { fordFulkerson } from '../FordFulkerson.js'

test('Test Case 1', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. These should all be in one big describe("Edmonds-Karp", ...).
  2. These test case names should be descriptive and useful, or be omitted altogether.
  3. How would I easily verify these tests? I think some visualizations of the graphs might be helpful, perhaps as ASCII art, or if these are from an external source, a link to where you got them from.

const capacity = [
[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],
]
const source = 0
const sink = 5
const maxFlow = fordFulkerson(capacity, source, sink)
expect(maxFlow).toBe(23)
})

test('Test Case 2', () => {
const capacity = [
[0, 10, 0, 10, 0, 0],
[0, 0, 5, 0, 15, 0],
[0, 0, 0, 0, 10, 10],
[0, 0, 10, 0, 0, 10],
[0, 0, 0, 0, 0, 10],
[0, 0, 0, 0, 0, 0],
]
const source = 0
const sink = 5
const maxFlow = fordFulkerson(capacity, source, sink)
expect(maxFlow).toBe(20)
})

test('Test Case 3', () => {
const capacity = [
[0, 7, 0, 0, 3, 0],
[0, 0, 5, 0, 2, 0],
[0, 0, 0, 8, 0, 7],
[0, 0, 0, 0, 0, 5],
[0, 0, 0, 5, 0, 0],
[0, 0, 0, 0, 0, 0],
]
const source = 0
const sink = 5
const maxFlow = fordFulkerson(capacity, source, sink)
expect(maxFlow).toBe(10)
})
Loading