Movatterモバイル変換


[0]ホーム

URL:


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

Implemented Partition Problem, Recursive problem#1582

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

vedas-dixit
Copy link
Contributor

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 readCONTRIBUTING.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 butuserprofile.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 containsFixes: #{$ISSUE_NO}.

@vedas-dixit
Copy link
ContributorAuthor

Hello@appgurueu &@raklaptudirm, Can you guys please review "Tug of war-backtracking problem" that I committed recently and leave a comment if any change or modification is required :)

Copy link
Collaborator

@appgurueuappgurueu left a comment
edited
Loading

Choose a reason for hiding this comment

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

I think this should be categorized as "Recursive" rather than "Backtracking". One definition ofBacktracking is:

Backtracking is a class of algorithms for finding solutions to some computational problems, notably constraint satisfaction problems, that incrementally builds candidates to the solutions, andabandons a candidate as soon as it determines that the candidate cannot possibly be completed to a valid solution.

I don't see the emphasized part here. To me, this is just a recursive algorithm which tries all possible ways to partition the set into two subsets, evaluating them and picking the best, solving anoptimization problem.

@vedas-dixit
Copy link
ContributorAuthor

I was also thinking of putting it in the "Recursive" Questions but this question is labelled as "Backtracking" so I just followed the trend.
Thanks for the review, I'll make the necessary changes :)

appgurueu reacted with thumbs up emoji

@appgurueuappgurueu added the hacktoberfest-acceptedAccepted to be counted towards Hacktoberfest labelOct 29, 2023
@vedas-dixitvedas-dixit changed the titleImplemented Tug of war, backtracking problemImplemented Tug of war, Recursive problemOct 30, 2023
@vedas-dixit
Copy link
ContributorAuthor

I've made some necessary changes@appgurueu &@raklaptudirm

Changed File Location of "TugOfWar.js" under "Recursive" from "Backtracking"
Removed unused variable n for clarity.
Initialized result with the entire array in one subset and an empty second subset, setting minimumDiff to totalSum.
Changed totalSum to const for immutability.
Deleted the redundant isSafe function and its associated checks.

Copy link
Collaborator

@appgurueuappgurueu left a comment

Choose a reason for hiding this comment

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

Ah, I see the issue now. This does not solve the "tug of war" problem; you are missing the side condition that both sets should have size n/2.

This does solve the "partition" problem (which can be reduced to the 0-1-knapsack problem) however.

I suggest either renaming this topartition - and removing the GfG reference - or fixing the implementation s.t. the subsets each have size n/2 (note: if n is odd, the sizes may differ by one).

vedas-dixit reacted with thumbs up emoji
@vedas-dixitvedas-dixit changed the titleImplemented Tug of war, Recursive problemImplemented Partition Problem, Recursive problemNov 5, 2023
@vedas-dixit
Copy link
ContributorAuthor

I appreciate your feedback@appgurueu
I've made the suggested changes to the code. It's now the "Partition Problem," and I've added error handling for invalid input. You can Check out the updated code and tests :)

Copy link
Collaborator

@appgurueuappgurueu left a comment

Choose a reason for hiding this comment

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

(Note: Calculating the sum of nums in each recursive call is wasteful. But the implementation is correct, and really "efficiently" solving the problem isn't possible anyways (unless P = NP), so I don't mind terribly much. Ideally you should have an outer function, which takes justnums as a parameter, and an inner function, which takes justindex andtarget.)

vedas-dixit reacted with thumbs up emoji
@raklaptudirmraklaptudirm merged commite9e3ea4 intoTheAlgorithms:masterNov 15, 2023
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@appgurueuappgurueuappgurueu approved these changes

@raklaptudirmraklaptudirmraklaptudirm approved these changes

Assignees
No one assigned
Labels
hacktoberfest-acceptedAccepted to be counted towards Hacktoberfest
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@vedas-dixit@appgurueu@raklaptudirm

[8]ページ先頭

©2009-2025 Movatter.jp