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

def possible_bipartition(dislikes)
raise NotImplementedError, "possible_bipartition isn't implemented yet"
return true if dislikes.empty? || dislikes.length < 2

# group_hash includes visited & queued nodes, key is the node & value is the group
# group can be 1 or 2
group_hash = {}
q = []

# add first, connected node to q & group_hash
count = 0
i = 0
until count == 1 && i < dislikes.length

Choose a reason for hiding this comment

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

What if the graph is unconnected like:

adjacency_list = {
  1 => [2, 3],
  2 => [1, 3],
  3 => [1, 2],
  4 => [5, 6],
  5 => [4, 6],
  6 => [4, 5]
}

In this graph there are two unconnected groups of nodes. I'll update my tests to catch this scenario.

if dislikes[i] != []
q << i
group_hash[i] = 1
count += 1
end
i += 1
end

while !q.empty?
dislikes[q[0]].each do |disliked_dog|
parent_group = group_hash[q[0]]

if group_hash[disliked_dog]
return false if group_hash[disliked_dog] == parent_group
elsif !group_hash[disliked_dog]

Choose a reason for hiding this comment

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

You can just do:

Suggested change
elsif !group_hash[disliked_dog]
else

q << disliked_dog
parent_group == 1 ? group_hash[disliked_dog] = 2 : group_hash[disliked_dog] = 1
end
end
q.shift
end
return true
end