ProgrammingHardGATE CS & IT · Infosys

What is printed?

What is printed?
from collections import deque
def bip(g):
    col = {}
    for s in g:
        if s in col:
            continue
        col[s] = 0
        q = deque([s])
        while q:
            u = q.popleft()
            for v in g[u]:
                if v not in col:
                    col[v] = 1 - col[u]
                    q.append(v)
                elif col[v] == col[u]:
                    return False
    return True
print(bip({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]}), bip({0: [1, 2], 1: [0, 2], 2: [0, 1]}))
  1. A. False False
  2. B. True False
  3. C. True True
  4. D. False True

Correct answer: B. True False

A 4-cycle can be coloured with two colours, so it is bipartite. A triangle needs three colours, so the BFS finds a conflict and returns False.

More Python Programming questions

Exams that ask this topic