A tree spreads both ways
transcript
A search app marks one page of its index stale. A minute later, every linked page is marked too. How long until all are stale?
The input lists only the children of each page. But the mark also goes up to the parent page. So add the parent to every list.
Start at page one. Its distance is zero. Pages zero, three and four are next. Distance one for all three. Page two follows, one step past page zero. Distance two. Pages five and six follow at distance three. That is the last step.
Follow only the children, and the search stops early. It never goes up. It never reaches page zero above the start. With the parent, the answer is three.
A tree has exactly one path between any two nodes. So the first visit gives the distance. Each node is visited once. The cost is n.
Build neighbour lists that go both ways. Then use one queue from the start. Full breakdown in the lecture.