Advanced Graphs
116 / 170Advanced GraphsLesson 1 of 6About 111 min
Breadth-first search with a heap instead of a queue, and a precise reason why that works.
Run the algorithm from A on edges A to C costing 10, A to B costing 1, B to C costing 1. For each pop give the node finalized and its final cost. Ignore stale pops that get skipped.
| pop | node | final cost |
|---|---|---|
| 1 | ||
| 2 | ||
| 3 |
cheapest_total returns the minimum total cost from a start to every reachable node. lowest_ceiling returns, for every reachable node, the smallest possible value of the largest edge on a path to it. Both take an adjacency map of node to a list of (neighbor, weight).
Tests
graph = { "A": [("B", 1), ("C", 10)], "B": [("C", 1)], "C": [], "D": [],}print(cheapest_total(graph, "A"))print(lowest_ceiling(graph, "A"))print(cheapest_total(graph, "D"))Output
Run tests to see output.
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.
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.