Added Ford Fulkerson algorithm for Hacktoberfest contribution#1708
Added Ford Fulkerson algorithm for Hacktoberfest contribution#1708Sameeksha-12 wants to merge 2 commits intoTheAlgorithms:masterfrom
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.
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.
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.
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.
Some comments would help clarity here.
| parent[source] = -1; | ||
|
|
||
| while (queue.length > 0) { | ||
| const u = queue.shift(); |
There was a problem hiding this comment.
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.
- 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.jsis allowed butuserprofile.js,Userprofile.js,user-Profile.js,userProfile.jsare notFixes: #{$ISSUE_NO}.