Skip to content
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

Earth - Jasmine #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion lib/possible_bipartition.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,36 @@

# Time complexity: O(V + E), where V is the number of vertices and E is the number of adjecent neighbors.
# Space complexity: O(V) where v is the number of vertices/dogs because of the dog_group array. The recursive stack will be at most O(n), as well.
def possible_bipartition(dislikes)
Comment on lines +2 to 4

Choose a reason for hiding this comment

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

👍

raise NotImplementedError, "possible_bipartition isn't implemented yet"
# 0 is no color
# 1 is one color
# -1 is the other


# keep track of the colors
# if a color exists for one, we return false?
dog_group = Array.new(dislikes.length, 0)
p dislikes
dislikes.length.times do |dog|
if dog_group[dog] == 0 && !dfs(dislikes, dog_group, dog, 1)
return false
end
end
return true
end

def dfs(dislikes, dog_group, current_dog, group)

dog_group[current_dog] = group

dislikes[current_dog].each do |disliked_dog|
if dog_group[disliked_dog] == group
return false
end

if dog_group[disliked_dog] == 0 && !dfs(dislikes, dog_group, disliked_dog, group * -1)
return false
end
end
return true
end