Does the network contain a loop?

graph is an adjacency dictionary for a two-way graph with every node as a key and no edge listed twice. Write has_cycle(graph) that returns whether any cycle exists.

has_cycle({1: [2], 2: [1, 3], 3: [2]})           ->  False     (a line)
has_cycle({1: [2, 3], 2: [1, 3], 3: [1, 2]})     ->  True      (a triangle)
has_cycle({1: [2], 2: [1], 3: [4], 4: [3]})      ->  False

The point is to practise DFS that remembers where it came from: in an undirected graph, seeing a visited neighbour that is not your parent means a cycle.

def has_cycle(graph):
    ...