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]}))- A. False False
- B. True False
- C. True True
- 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
- What is printed? python def mst(n, edges): p = list(range(n)) def f(x): while p[x] != x: x = p[x] return x total = 0 for w, u, v in…
- What is printed? python def candy(r): n = len(r) c = [1] n for i in range(1, n): if r[i] > r[i - 1]: c[i] = c[i - 1] + 1 for i in range(n…
- What is printed? python def mat(a, b, m): return [[sum(a[i][k] b[k][j] for k in range(2)) % m for j in range(2)] for i in range(2)] def…
- What is printed? python class A: x = 1 def get(self): return self.x a = A() a.x = 5 A.x = 9 b = A() print(a.get(), b.get(), A.x)
- What is printed? python class A: def f(self): return ['A'] class B(A): def f(self): return ['B'] + super().f() class C(A): def f(self)…
- What is printed? python from itertools import product print(len(list(product('01', repeat=3))), list(product('ab', 'x'))[-1], sum(1 for in…