forked from AdaGold/Bipartition-Graph
-
Notifications
You must be signed in to change notification settings - Fork 29
Water - Beatrice #13
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
Beatress
wants to merge
2
commits into
Ada-C14:master
Choose a base branch
from
Beatress:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Water - Beatrice #13
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,40 @@ | ||
|
|
||
| # Time complexity: O(N + E) for BFS * O(N) for each node - O(N^2)? | ||
| # Space complexity: O(N) for dog_groups and queue | ||
| def possible_bipartition(dislikes) | ||
| raise NotImplementedError, "possible_bipartition isn't implemented yet" | ||
| # The goal is to find out if it is possible to put the dogs into two groups | ||
| # We are going to denote these as 1 & -1, with 0 as "unvisited" | ||
| # We are going to do BFS starting from each vertices | ||
|
|
||
| size = dislikes.length | ||
| dog_groups = Hash.new(0) # hash of dog groups | ||
| queue = Queue.new | ||
| size.times do |node| | ||
| # If we haven't put this dog in a group yet | ||
| if dog_groups[node].zero? | ||
| dog_groups[node] = 1 # Start with group 1 | ||
| queue << node # Put the node into the BFS queue | ||
|
|
||
| # Process the queue | ||
| until queue.empty? | ||
| neighbor = queue.pop | ||
| this_group = dog_groups[neighbor] # Group that the parent belongs to | ||
|
|
||
| dislikes[neighbor].each do |node| # Traverse all the children | ||
| # Visit each connected node and assign to opposite group | ||
| if dog_groups[node].zero? # Node has not been visited | ||
| dog_groups[node] = this_group * -1 # Assign opposite group and add to queue | ||
| queue << node | ||
| elsif dog_groups[node] == this_group | ||
| return false # We have an existing connection in the same group - bipartition not possible | ||
| end | ||
| end | ||
|
|
||
| end | ||
|
|
||
| end | ||
| end | ||
|
|
||
| # If we're here - we have sorted the dogs into two groups! | ||
| return true | ||
|
|
||
| end | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
👍