Skip to content

Instantly share code, notes, and snippets.

@3-24
Created September 13, 2018 05:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 3-24/d35c38ba058f2093690dea2f2fda8944 to your computer and use it in GitHub Desktop.
Save 3-24/d35c38ba058f2093690dea2f2fda8944 to your computer and use it in GitHub Desktop.
Checking Bipartite Graph
test_nbr = int(input()) # number of test cases
def isBipartite():
# v and e is number of vertexes and edges, respectively.
v,e = map(int,input().split())
# None if not colored
# If colored, there are two bool cases: False and True.
colorArr = [None for _ in range(v+1)]
adjacentPoints = [[] for _ in range(v+1)]
for i in range(e):
v1, v2 = map(int, input().split()) # edge (v1, v2) is expected input
adjacentPoints[v1].append(v2)
adjacentPoints[v2].append(v1)
queue = []
node = set([i for i in range(1,v+1)]) # for non-connected graph
while bool(node): # node is nonempty
c = node.pop()
queue.append(c)
while bool(queue): # queue is nonempty
start = queue[0]
for u in adjacentPoints[start]:
if colorArr[u] is not None:
if colorArr[u] is colorArr[start]:
return False
else:
colorArr[u] = not colorArr[start]
queue.append(u)
del queue[0]
if start != c: node.remove(start)
return True
for _ in range(test_nbr):
print('YES' if isBipartite() else 'NO')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment