Embercode
✕
GraphsQuick quiz

0-1 BFS and bipartite checks in Python

7 questions. Covers Bipartite check by two-coloring in Python.

Questions 1 to 5 of 7

  1. 1. What does this program print? ``python from collections import deque dq = deque() dq.append("cost1-a") dq.appendleft("cost0-b") dq.append("cost1-c") dq.appendleft("cost0-d") out = "" while dq: out += dq.popleft() + " " print(out.strip()) ``
  2. 2. What does this program print? ``python from collections import deque n = 6 g = [[] for _ in range(n)] for a, b in [(0, 1), (1, 2), (2, 3), (3, 4)]: g[a].append(b) g[b].append(a) colour = [-1] * n for s in range(n): if colour[s] != -1: continue colour[s] = 0 q = deque([s]) while q: u = q.popleft() for v in g[u]: if colour[v] == -1: colour[v] = 1 - colour[u] q.append(v) print(colour) ``
  3. 3. In the 0-1 BFS, a neighbour reached by a cost 0 road goes to the front of the deque. Why?
  4. 4. Why does two_colour start a new walk from every vertex that is still uncoloured?
  5. 5. True or false, with a reason: the proof that the deque stays in order (every distance is d or d + 1) still holds when one road costs 2.

5 question(s) left on this page.