Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Open
Sameeksha-12 wants to merge 2 commits into TheAlgorithms:master
base: master
Choose a base branch
Loading
from Sameeksha-12:add-ford-fulkerson-algorithm

Conversation

Copy link

@Sameeksha-12 Sameeksha-12 commented Oct 2, 2024
edited
Loading

Open in Gitpod know more

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new JavaScript files are placed inside an existing directory.
  • All filenames should use the UpperCamelCase (PascalCase) style. There should be no spaces in filenames.
    Example:UserProfile.js is allowed but userprofile.js,Userprofile.js,user-Profile.js,userProfile.js are not
  • All new algorithms have a URL in their comments that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

Copy link

codecov-commenter commented Oct 2, 2024
edited
Loading

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 84.69%. Comparing base (9010481) to head (ea28f4b).

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.
📢 Have feedback on the report? Share it here.

@@ -0,0 +1,52 @@
// https://en.wikipedia.org/wiki/Ford%E2%80%93Fulkerson_algorithm
function fordFulkerson(capacity, source, sink) {
Copy link
Collaborator

@appgurueu appgurueu Oct 2, 2024

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) {
Copy link
Collaborator

@appgurueu appgurueu Oct 2, 2024

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;
Copy link
Collaborator

@appgurueu appgurueu Oct 2, 2024

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);
Copy link
Collaborator

@appgurueu appgurueu Oct 2, 2024

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();
Copy link
Collaborator

@appgurueu appgurueu Oct 2, 2024

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', () => {
Copy link
Collaborator

@appgurueu appgurueu Oct 2, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers

@appgurueu appgurueu appgurueu requested changes

@raklaptudirm raklaptudirm Awaiting requested review from raklaptudirm raklaptudirm is a code owner

Requested changes must be addressed to merge this pull request.

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /