Algorithms, Mathematics, Python, Computer Science, Programming.
in visited: visited.add(vertex) total += random_value(vertex, a, b, p) for neighbor in graph[vertex]: if neighbor not in visited: queue.append(neighbor) return total % m def random_value(vertex, a, b, p): """Generate a pseudo-random value for a given vertex.""" x = vertex y = (a*x + b) % p return y