LeetCode [Hard] Bus Routes | Algorithm - BFS
Question
Attempts
Classic DFS
At first, I came oup with a classic DFS solution.
|
|
Which failed at case like [[24],[3,6,11,14,22],[1,23,24],[0,6,14],[1,3,8,11,20]], 20, 8
where you need to know which bus you’re at i.e. stop-1.
Because the part I used i == 0
to determine if that’s a transfer doesn’t quite fit for case like above.
|
|
Now you need to look closer to the problem and draw the routes. For example [[24],[3,6,11,14,22],[1,23,24],[0,6,14],[1,3,8,11,20]]
the first 4 rounds should be:
20 -> 1 -> 3 -> 8
1 -> 23 -> 24
3 -> 6
We need to utilize queue
to achieve the transfer record like. How? By traversing the same route first then other branches. So that queue would be like [20,1,3,8,11,23,6...]
.
What if we have multiple stop to transfer at the first stop? Let’s say [[24,20],[3,6,11,14,22],[1,23,24],[0,6,14],[1,3,8,11,20]]
. Then you need to know where they are by tagging their bus_num
in the adj_list
.
Remember The Bus Number
|
|
Failed at Input:
[[1,9,12,20,23,24,35,38],[10,21,24,31,32,34,37,38,43],[10,19,28,37],[8],[14,19],[11,17,23,31,41,43,44],[21,26,29,33],[5,11,33,41],[4,5,8,9,24,44]]
,source=37
, target=28
Dry run the code you will find the issue here: How can we find the shortest route (in terms of number of transfer needed)? (Since the output was 4 and expected is 1.)
Find The Shortest Route (Transfer)
You will need to divide the searching route and record it and find the min
.
|
|
Then I proposed the new solution with num of transfers recorded while failed at case like [[10,21,24,31,32,34,37],[10,19,28,37],[11,17,23,31,41,43,44],[21,26,29,33],[5,11,33,41],[4,5,8,9,24,44]]
, output=2
, expected=1
. You need to be careful of adapting 1D visited record or 2D.
2D Visited Matrix
|
|
With the 2-d visited matrix, still I got the wrong output. After a further examination, I’ve found I’ve might been using the wrong technique. I should have adopted BFS instead of DFS which made my visited matrix useless here (sine it needs to be updated/re-initialized on each branch).
You need to do BFS simultaniously.To record the visited-matrix in order to determine whether to stop or not.
|
|
Still failed, at
|
|
We need to re-think the problem, how do we walk through the routes? Or in other words, how do we travel from a to b by bus if we have no maps?
Why BFS
In order to record the right process. Imagine you’re taking a bus, you have tried all the bus on that specific station. Would you come back again before you reach the end given the condition that you have travelled all the possible routes starts from this station? (Inspired by zippysphinx’s Solution.)
|
|