forked from AdaGold/Bipartition-Graph
-
Notifications
You must be signed in to change notification settings - Fork 29
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
FIRE - Tram #3
Open
trambui09
wants to merge
3
commits into
Ada-C14:master
Choose a base branch
from
trambui09: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
FIRE - Tram #3
Changes from all commits
Commits
Show all changes
3 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 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,39 @@ | ||
|
||
# Time complexity:O(n) - visiting each node in the graph once, searching in sets is O(1) | ||
# Space complexity: O(n) - the sets can be filled up based on the content of the dislikes | ||
require 'set' | ||
def possible_bipartition(dislikes) | ||
raise NotImplementedError, "possible_bipartition isn't implemented yet" | ||
return true if dislikes.empty? | ||
group_a, group_b, blocked_a, blocked_b = Set.new, Set.new, Set.new, Set.new | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the uses of |
||
|
||
# if this index is not included in blocked_a, and go to the index in the adjacency list, and check if its dislikes are in not in group_a | ||
# check if this index is not included in blocked_b, proceed | ||
# go to the index and check if its dislikes are not in group_b | ||
# return false if all four checks failed | ||
queue = [ 0 ] | ||
queue << 1 if dislikes[0].empty? | ||
|
||
until queue.empty? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a good BFS implementation. |
||
puppy = queue.shift | ||
if !blocked_a.include?(puppy) && group_a.intersection(dislikes[puppy]).empty? | ||
# add puppy to group_a | ||
group_a << puppy | ||
# add it's dislikes to blocked_a | ||
dislikes[puppy].each { |dislike| blocked_a << dislike } | ||
elsif !blocked_b.include?(puppy) && group_b.intersection(dislikes[puppy]).empty? | ||
group_b << puppy | ||
dislikes[puppy].each { |dislike| blocked_b << dislike } | ||
else | ||
return false | ||
end | ||
|
||
# place each of puppy's dislikes into queue if they're not in group_a or group_b | ||
# if they're in group_a or group_b, it means that node has been visted | ||
dislikes[puppy].each do |dislike| | ||
if !(group_a + group_b).include?(dislike) | ||
queue << dislike | ||
end | ||
end | ||
end | ||
|
||
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.
👍 For time complexity you're also checking every edge for every node so this is O(N + E)