Graphs
109 / 170GraphsLesson 2 of 8About 42 min
An adjacency map, and traversing a structure where the nodes are objects rather than coordinates.
A graph whose two nodes point at each other is copied. The map from original to copy is written before any recursion happens. Type the two lines it prints.
graph = {1: [2], 2: [1]} copies = {}order = [] def clone(label): if label in copies: return copies[label] # already made, and this is what stops the cycle copies[label] = [] # recorded before descending, not after order.append(label) for neighbor in graph[label]: copies[label].append(clone(neighbor)) return copies[label] clone(1)print(order)print(sorted(copies))Read the constraints first and let them tell you what complexity is expected. Derive the approach, implement it, run the tests, and submit when it passes.