Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
depth-first search solution in Clear category for Power Supply by David_Jones
from collections import defaultdict
def power_supply(network, power_plants):
G = defaultdict(set)
for u,v in network:
G[u].add(v)
G[v].add(u)
supplied = set()
queue = list(power_plants.items())
while queue:
u, d = queue.pop()
supplied.add(u)
if d:
for v in G[u]:
queue.append((v, d-1))
return set(G) - set(supplied)
May 17, 2019