← All problemsFree · Python · 60 min

Algorithm design / guarantees under an adversary

The Fibre Run

Join a few sites with fibre under the cheapest streets, using any junction on the way. Beat the shortest-path heuristic by hand, build a town where it does badly, then write the dynamic program over sets of sites that is exponential only in their number.

0 / 3 graded parts passSaved in this browser · loading saved work…
Jump to your work ↓

A town is laying fibre to join a few sites: here a school, a clinic, a library and a pool. Fibre runs under streets, and digging up each street has a cost. The fibre may pass any junction on the way, whether it is a site or not. Which streets should be dug so that every site is connected, at the lowest total cost?

7 4 6 6 6 4 8 2 7 2 8 2 9 2 3 8 School 0 Clinic 1 Library 2 Pool 3 4 5 6 7 8

The cheapest such set of streets is a tree, a minimum Steiner tree. With every junction a site, it is a minimum spanning tree, and with two sites it is a shortest path. In between, the problem is NP-hard. Choosing which other junctions to use is the hard part, and trying every set of them takes 2n−k attempts for n junctions and k sites.

Few sites change that. A town may have thousands of junctions and only five sites, and dynamic programming over sets of sites rather than sets of junctions gives a running time that is exponential only in k.

In code, junctions are numbered 0 to n−1, a street is (u, v, cost), and sites is a list of junctions.

Background: Dynamic Programming II.

easy · Part A

The cheapest fibre run

What is the lowest total cost of streets that connects the School, the Clinic, the Library and the Pool in the figure?

Explore in the sandbox

Experiment here, then copy a rule into the editor and test its guarantees.