You're on a plane due to land at Heathrow airport in the next hours. You have to get to London as fast as possible; your rich uncle is dying and you want to be the first there to claim dibs on his estate.--book
There are two roads going from Heathrow to London and a bunch of smaller streets linking them together. Because of speed limits and usual traffic, some parts of the roads and smaller streets take longer to drive on than others. Before you land, you decide to maximize your chances by finding the optimal path to his house. Here's the map you've found on your laptop--book
50 10 30 5 90 20 40 2 25 10 8 0
The road is laid in the pattern: A1, B1, X1, A2, B2, X2, ..., An, Bn, Xn, where X is one of the roads joining the A side to the B side of the map. We insert a 0 as the last X segment, because no matter what we do we're at our destination already. Data can probably be organized in tuples of 3 elements (triples) of the form {A,B,X}.--book
Writing a Recursive Function
When writing a recursive function, the first thing to do is to find our base case. For our problem at hand, this would be if we had only one tuple to analyze, that is, if we only had to choose between A, B (and crossing X, which in this case is useless because we're at destination)--book
A ---10 min-- | Destination | B ---23 min---
In base case scenario you got two options, or rather two roads that leads to your destination. Either you pick road A which will take you 10 minutes to get to your destination or road B which takes 23 minutes.
Then the choice is only between picking which of path A or path B is the shortest. If you've learned your recursion right, you know that we ought to try and converge towards the base case. This means that on each step we'll take, we'll want to reduce the problem to choosing between A and B for the next step.--book
Let's extend our map and start over:
Ah! It gets interesting! How can we reduce the triple {5,1,3} to a strict choice between A and B?--book
A1 Pt A A2 A --- 5 ----*---------- 10 ------------*-- | | 3 | X1 0 | X2 | | B --- 1 ----*---------- 15 ------------*-- B1 B2
Let's see how many options are possible for A [(*)]. To get to the intersection of A1 and A2 (I'll call this the point A1), I can either take road A1 directly (5), ...
A1 A2 A --- 5 ----*---------- 10 ------------*-- | | 3 | X1 0 | X2 | | B --- 1 ----*---------- 15 ------------*-- B1 B2
or come from B1 (1) and then cross over X1 (3).
A1 A2 A --- 5 ----*---------- 10 ------------*-- | | 3 | X1 0 | X2 | | B --- 1 ----*---------- 15 ------------*-- B1 B2
In this case, The first option (5) is longer than the second one (4). For the option A, the shortest path is [B, X].
So what are the options for B?
A1 A2 A --- 5 ----*---------- 10 ------------*-- | | 3 | X1 0 | X2 | | B --- 1 ----*---------- 15 ------------*-- B1 Pt B B2
You can either proceed from A1 (5) then cross over X1 (3),
A1 A2 A --- 5 ----*---------- 10 ------------*-- | | 3 | X1 0 | X2 | | B --- 1 ----*---------- 15 ------------*-- B1 Pt B B2
or strictly take the path B1 (1).--book
A1 A2 A --- 5 ----*---------- 10 ------------*-- | | 3 | X1 0 | X2 | | B --- 1 ----*---------- 15 ------------*-- B1 Pt B B2
Alright! What we've got is a length 4 with the path [B, X] towards the first intersection A and a length 1 with the path [B] towards the intersection of B1 and B2. We then have to decide what to pick between going to the second point A (the intersection of A2 and the endpoint or X2) and the second point B (intersection of B2 and X2). To make a decision, I suggest we do the same as before. Now you don't have much choice but to obey, given I'm the guy writing this text. Here we go!--book
A1 A2 Pt A2 A --- 5 ----*---------- 10 ------------*-- | | 3 | X1 0 | X2 | | B --- 1 ----*---------- 15 ------------*-- B1 B2 Pt B2
All possible paths to take in this case can be found in the same way as the previous one. We can get to the next point A [Pt A2] by either taking the path A2 from [B, X], which gives us a length of 14 (14 = 4 + 10),
A1 A2 Pt A2 A --- 5 ----*---------- 10 ------------*-- | | 3 | X1 0 | X2 | | B --- 1 ----*---------- 15 ------------*-- B1 B2
or by taking B2 then X2 from [B], which gives us a length of 16 (16 = 1 + 15 + 0).
A1 A2 Pt A2 A --- 5 ----*---------- 10 ------------*-- | | 3 | X1 0 | X2 | | B --- 1 ----*---------- 15 ------------*-- B1 B2
In this case, the path [B, X, A] is better than [B, B, X].--book
We can also get to the next point B by either taking the path A2 from [B, X] and then crossing over X2 for a length of 14 (14 = 4 + 10 + 0),
A1 A2 A --- 5 ----*---------- 10 ------------*-- | | 3 | X1 0 | X2 | | B --- 1 ----*---------- 15 ------------*-- B1 B2 Pt B2
or by taking the road B2 from [B] for a length of 16 (16 = 1 + 15). Here, the best path is to pick the first option, [B, X, A, X].--book
A1 A2 A --- 5 ----*---------- 10 ------------*-- | | 3 | X1 0 | X2 | | B --- 1 ----*---------- 15 ------------*-- B1 B2 Pt B2
So when this whole process is done, we're left with two paths, A [[B, X, A]] or B [[B, X, A, X]], both of length 14.