forked from AdaGold/Bipartition-Graph
-
Notifications
You must be signed in to change notification settings - Fork 41
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
Gloria - Scissors #18
Open
ggrossvi
wants to merge
1
commit into
Ada-C15:master
Choose a base branch
from
ggrossvi: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
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,12 +1,67 @@ | ||
# Can be used for BFS | ||
from collections import deque | ||
|
||
# does breadth first search(BFS) | ||
def partition_helper(visited, dislikes, starting_dog): | ||
""" Will return True or False if the given graph | ||
can be bipartitioned without neighboring nodes put | ||
into the same partition. | ||
Time Complexity: O(N + E) | ||
Since you will visit each node once, and loop through each of the edges in each node the Big-O of this algorithm is O(N + E) where N is the number of nodes in the graph and E is the number of edges since each node and each edge will be explored. | ||
Space Complexity: O(n) | ||
In the worst-case you will need to add each node to the Queue, so the space complexity is O(N) where N is the number of nodes in the graph. | ||
""" | ||
queue = deque() | ||
queue.append(starting_dog) | ||
# visited holds all the nodes. It is set to false at beginning indicating it has not been visited | ||
visited[starting_dog] = "group_1" | ||
if not dislikes[starting_dog]: | ||
return True | ||
while queue: | ||
|
||
current_dog = queue.popleft() | ||
# dislikes[current_dog] has value of dogs that the current dog doesn't like | ||
for enemy_dog in dislikes[current_dog]: | ||
# don't want to compare current dog to itself | ||
#assign other dogs to other group if not assigned | ||
# if not assigned/visited/they have value false | ||
# append the dogs that have not been visited to the queue | ||
if visited[enemy_dog] == False: | ||
queue.append(enemy_dog) | ||
# check current dog's group and assign the enemy dog the opposite group | ||
if visited[current_dog] == "group_1": | ||
visited[enemy_dog] = "group_2" | ||
elif visited[current_dog] == "group_2": | ||
visited[enemy_dog] = "group_1" | ||
#check if current dogs group matches the enemy dog's group | ||
elif visited[current_dog] == visited[enemy_dog]: | ||
return False | ||
return True | ||
|
||
|
||
def possible_bipartition(dislikes): | ||
""" Will return True or False if the given graph | ||
can be bipartitioned without neighboring nodes put | ||
into the same partition. | ||
Time Complexity: ? | ||
Space Complexity: ? | ||
""" | ||
pass | ||
Time Complexity: O(N) | ||
Space Complexity: O(N) | ||
""" | ||
# if empty array return True | ||
if not dislikes: | ||
return True | ||
|
||
|
||
visited = [False] * ((len(dislikes)+1)) | ||
# return partition_helper(visited,dislikes, 0) | ||
|
||
for i in range(len(dislikes)): | ||
#dislike[i] i is not connected to any other node empty list for adjacency 7. [] means it has no adjacencey. dog i does not dislike any other dog or you have visited dog you skip it. | ||
if not visited[i]: | ||
#continue means you skip.return flow to the start of the loop while break is exit the loop | ||
# continue | ||
# i index of the dogs, breadth first search (done with queue) i nodes of the dog | ||
result = partition_helper(visited,dislikes, i) | ||
if not result: | ||
return False | ||
return True | ||
|
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 BFS!