def is_hom(G, H, hom):
  """
  Check whether hom is a homomorphism from G to H.
  Works for both directed and undirected.
  """
  assert set(G.vertices()) == set(hom.keys())
  assert set(H.vertices()).issuperset(set(hom.values()))
  for e in G.edges():
    u, v = e[0], e[1]
    if not H.has_edge(hom[u], hom[v]):
      return False
  return True