Marking vertices when queued or when removed

A network crawler keeps a frontier, the queue of records awaiting processing. It runs on a connected, undirected graph with VV vertices and EE edges, with no self-loops or parallel edges. Each edge counts as one hop, and the graph is stored using adjacency lists.


Compare two breadth-first search variants. Both use a first-in, first-out queue. Early marks each vertex seen when it is enqueued, records its distance then, and enqueues only unseen neighbours. Late instead runs the following code:


Python
from collections import deque

queue = deque([(start, 0)])
seen = set()
distance = {}
while queue:
    vertex, hops = queue.popleft()
    if vertex in seen:
        continue
    seen.add(vertex)
    distance[vertex] = hops
    for neighbour in adjacency[vertex]:
        if neighbour not in seen:
            queue.append((neighbour, hops + 1))


Assume the adjacency lists already exist and each set or queue operation shown takes constant time. Which statements are true? Select every true statement.

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Graph Traversals Questions: