-
Notifications
You must be signed in to change notification settings - Fork 65
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
Rebeca Muniz #Cedar #45
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,56 @@ | ||
# Can be used for BFS | ||
from collections import deque | ||
class Graph(): | ||
|
||
def possible_bipartition(dislikes): | ||
def __init__(self, V): | ||
self.V = V | ||
self.graph = [[0 for column in range(V)] \ | ||
for row in range(V)] | ||
|
||
def possible_bipartition(self, dislikes): | ||
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. Your code won't run in part because this line is indented incorrectly. If you intend this to be a method of your class Graph, you need to tab it over one level. I think you may be slightly confused about what For example in Then Node 0 (aka dog 0) has no neighbors/edges. Node 1 has neighbors 2 and 3 (meaning dog 1 dislikes dogs 2 and 3), Node 2 has neighbors 1 and 4, and so forth. This should be sufficient for you to complete the problem. While you can create a Graph class, it shouldn't be necessary. |
||
""" Will return True or False if the given graph | ||
can be bipartitioned without neighboring nodes put | ||
into the same partition. | ||
Time Complexity: ? | ||
Space Complexity: ? | ||
Time Complexity: O(N+E) | ||
Space Complexity: O(N) | ||
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. ✨ Your time and space complexity are correct for a BFS implementation |
||
""" | ||
pass | ||
|
||
colorArr = [-1] * self.V | ||
|
||
# Assign first color to source | ||
colorArr[dislikes] = 1 | ||
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. Again, |
||
|
||
# Create a queue (FIFO) of vertex numbers and | ||
# enqueue source vertex for BFS traversal | ||
queue = [] | ||
queue.append(dislikes) | ||
|
||
# Run while there are vertices in queue | ||
# (Similar to BFS) | ||
while queue: | ||
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. Your approach looks largely correct for an adjacency matrix, but I may be missing something without the tests |
||
|
||
u = queue.pop() | ||
|
||
# Return false if there is a self-loop | ||
if self.graph[u][u] == 1: | ||
return False; | ||
|
||
for v in range(self.V): | ||
|
||
# An edge from u to v exists and destination | ||
# v is not colored | ||
if self.graph[u][v] == 1 and colorArr[v] == -1: | ||
|
||
# Assign alternate color to this | ||
# adjacent v of u | ||
colorArr[v] = 1 - colorArr[u] | ||
queue.append(v) | ||
|
||
# An edge from u to v exists and destination | ||
# v is colored with same color as u | ||
elif self.graph[u][v] == 1 and colorArr[v] == colorArr[u]: | ||
return False | ||
|
||
# If we reach here, then all adjacent | ||
# vertices can be colored with alternate | ||
# color | ||
return True |
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.
🤔 It looks like you're creating a graph here as an adjacency matrix with no edges initially.