Skip to content
Open
Show file tree
Hide file tree
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
46 changes: 45 additions & 1 deletion lib/possible_bipartition.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,48 @@
# I looked at the leetcode solution in Python
# (https://leetcode.com/problems/possible-bipartition/solution/)

Choose a reason for hiding this comment

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

Ok great, it's been a while since you saw graphs in class anyway.

# to get an idea of how to turn the list of dislikes into a graph,
# but chose to implement a breadth-first solution instead

def possible_bipartition(dislikes)
raise NotImplementedError, "possible_bipartition isn't implemented yet"
return true if dislikes.empty?
graph = build_graph(dislikes)
# start with an arbitrary node of the graph,
# assuming all nodes have at least one edge
grouped = {graph.keys[0] => true}
queue = [graph.keys[0]]
# using a bfs approach, set all neighbor nodes to the opposite label
# and add them to the queue if they haven't been visited
# return false if a neighbor has the same label as the current node
while !queue.empty?
node = queue.shift()
graph[node].each do |neighbor|
if grouped[neighbor] == nil
grouped[neighbor] = !grouped[node]
queue.push(neighbor)
elsif grouped[neighbor] == grouped[node]
return false
end
end
if queue.empty?
graph.keys.each do |key|
if grouped[key].nil?
queue.push(key)
break
end
end
end
end
return true
end


def build_graph(dislikes)
nodes = Hash.new {|h,k| h[k] = [] } #had to look up how to do this on StackOverflow
dislikes.each do |dislike|
dislike.each do |node|
nodes[node] += dislike - [node]
end
end
return nodes
end

41 changes: 38 additions & 3 deletions test/possible_bipartition_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
expect(answer).must_equal true
end

it "will work for example 2" do
it "will work for example 2" do
# Arrange
dislikes = [ [],
[2, 3],
Expand Down Expand Up @@ -49,7 +49,7 @@
expect(answer).must_equal false
end

it "will return true for a graph which can be bipartitioned" do
it "will return true for a graph which can be bipartitioned" do
# Arrange
dislikes = [ [3, 6],
[2, 5],
Expand All @@ -68,7 +68,7 @@
end


it "will return false for a graph which cannot be bipartitioned" do
it "will return false for a graph which cannot be bipartitioned" do
# Arrange
dislikes = [ [3, 6],
[2, 5],
Expand All @@ -89,4 +89,39 @@
it "will work for an empty graph" do
expect(possible_bipartition([])).must_equal true
end
end

describe "edge cases" do
it "will work for a graph where all nodes are not interconnected" do
# Arrange
dislikes = [
[1,2],
[3,4],
[4,5],
[3,5]]

# Act
answer = possible_bipartition(dislikes)

# Assert
expect(answer).must_equal false

end

it "will work for a graph where all nodes don't have dislikes" do
# Arrange
dislikes = [
[1,2],
[4,5],
[5,6],
[4,6]]

# Act
answer = possible_bipartition(dislikes)

# Assert
expect(answer).must_equal false

end

end