-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Added Ford Fulkerson algorithm for Hacktoberfest contribution #1708
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) { | ||
const V = capacity.length; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This violates our naming conventions. It should be called |
||
const residualCapacity = capacity.map((arr) => arr.slice()); | ||
const parent = Array(V).fill(-1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 }; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { fordFulkerson } from '../FordFulkerson.js' | ||
|
||
test('Test Case 1', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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) | ||
}) |
There was a problem hiding this comment.
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.)