-
-
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?
Added Ford Fulkerson algorithm for Hacktoberfest contribution #1708
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #1708 +/- ##
==========================================
+ Coverage 84.65% 84.69% +0.03%
==========================================
Files 378 379 +1
Lines 19744 19795 +51
Branches 2951 2965 +14
==========================================
+ Hits 16715 16766 +51
Misses 3029 3029 ☔ View full report in Codecov by Sentry. |
@@ -0,0 +1,52 @@ | |||
// https://en.wikipedia.org/wiki/Ford%E2%80%93Fulkerson_algorithm | |||
function fordFulkerson(capacity, source, sink) { |
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.)
const parent = Array(V).fill(-1); | ||
let maxFlow = 0; | ||
|
||
function bfs(source, sink) { |
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.
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).
@@ -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 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.
function fordFulkerson(capacity, source, sink) { | ||
const V = capacity.length; | ||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Some comments would help clarity here.
parent[source] = -1; | ||
|
||
while (queue.length > 0) { | ||
const u = queue.shift(); |
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.
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.
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
- These should all be in one big
describe("Edmonds-Karp", ...)
. - These test case names should be descriptive and useful, or be omitted altogether.
- 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.
Describe your change:
Checklist:
Example:
UserProfile.js
is allowed butuserprofile.js
,Userprofile.js
,user-Profile.js
,userProfile.js
are notFixes: #{$ISSUE_NO}
.