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
Pauline - Fire #1
Open
ghostfruitleaf
wants to merge
2
commits into
Ada-C14:master
Choose a base branch
from
ghostfruitleaf: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
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 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,67 @@ | ||
|
||
# Time Complexity - O(v + e) Using BFS, the function checks each vertex through the queue once, and all edges | ||
# associated with that queue once or twice at most. Checking for non biparition-able graphs via edges beforehand | ||
# and only adding to the queue any edges with vertices that haven't been visited as a vertex prevents | ||
# repeat vertices from being visited. | ||
|
||
# Space Complexity - O(v) - A hash, and set, and a queue are created where the max number of elements added will never exceed | ||
# the number of vertices (v) in the graph. group_hash will always have two keys between which the vertices are divided into based on | ||
# edges, to_visit is a set of all vertices to delete from (in O(1) time), while bfs at worst will have all vertices in the Queue, | ||
# as the function doesn't add any edges that have already been visited. to_visit mostly exists to track any vertices | ||
# in a graph that aren't reached by a group of other vertices in the graph, which group_hash and bfs_q can't account for. | ||
def possible_bipartition(dislikes) | ||
raise NotImplementedError, "possible_bipartition isn't implemented yet" | ||
return true if dislikes.empty? | ||
|
||
# at this point will have at least one vertex | ||
group_hash = {1 => Set.new, 2 => Set.new} | ||
to_visit = Set.new(0...dislikes.length) | ||
bfs_q = Queue.new | ||
|
||
# set current group for leading vertex | ||
v_group = 1 | ||
|
||
# set current group for edges | ||
e_group = 2 | ||
Comment on lines
+20
to
+24
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 works to label the groups, I just used the symbols |
||
|
||
# start with first vertex | ||
group_hash[v_group].add(0) | ||
to_visit.delete(0) | ||
|
||
dislikes[0].each do |e| | ||
group_hash[e_group].add(e) unless e == 0 | ||
bfs_q << e | ||
end | ||
|
||
until to_visit.empty? | ||
until bfs_q.empty? | ||
# figure out where to put cur_v | ||
cur_v = bfs_q.pop | ||
to_visit.delete(cur_v) # mark visited | ||
|
||
if group_hash[e_group].include?(cur_v) | ||
v_group, e_group = e_group, v_group | ||
else | ||
group_hash[v_group].add(cur_v) | ||
end | ||
|
||
dislikes[cur_v].each do |e| | ||
return false if group_hash[v_group].include?(e) && e != cur_v # account for self cycle | ||
# at this point there are only edges in e_group or edges that haven't been added | ||
if !group_hash[e_group].include?(e) # skip and don't queue any that are already checked | ||
group_hash[e_group].add(e) | ||
bfs_q << e | ||
end | ||
# do nothing if already added | ||
end | ||
end | ||
|
||
# bfs ensures that any vertices left in to_visit won't interact with those currently in groups | ||
# we can just grab the first element in the to_visit set | ||
if !to_visit.empty? | ||
bfs_q << to_visit.first # for a general set, we can assume that set pulls any number it wants out, we don't care which one | ||
end | ||
|
||
end | ||
|
||
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.
Nice implementation of Breadth-first-search. Well done!