Advanced Graphs
119 / 170Advanced GraphsLesson 4 of 6About 50 min
A different kind of path problem, with a traversal that looks wrong until you see why it works.
Each node's destinations are stored so that removing the last one always hands back the smallest. Type the three lines it prints.
from collections import defaultdict pairs = [("A", "C"), ("A", "B"), ("B", "C")] graph = defaultdict(list)for origin, destination in sorted(pairs, reverse=True): graph[origin].append(destination) print(dict(graph))print("next from A:", graph["A"].pop())print("then from A:", graph["A"].pop())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.