Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
In the friends graph of the lesson, find by breadth-first search, on paper, the shortest path from Kabir to Ana, and write the queue and the previous dictionary after each step.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, compare your working with the solution.
Plan. The friends are: Asha knows Ravi and Kabir. Ravi knows Asha, Meera and Kabir. Meera knows Ravi and Ana. Kabir knows Asha and Ravi.
Start: queue [Kabir], previous {Kabir: None}.
[Asha, Ravi].[Ravi].[Meera].[Ana].The path, reversed: Kabir, Ravi, Meera, Ana: 3 steps.
Answer: ['Kabir', 'Ravi', 'Meera', 'Ana'].
Log in to join the discussion.
Loading…