[Tutor] distance between cities matrix

Alan Gauld alan.gauld at btinternet.com
Thu Feb 21 16:10:46 CET 2013

On 21/02/13 12:16, Mara Kelly wrote:
> Consider the (20x20) array of numbers here

> matrix A of distances (in kilometers) between cities. Note that A is
> symmetric A(i,j) = A(j,i) and all its diagonal elements are zero.

> Suppose Vladimir from city i and Petra from city j want to meet up in
> some third city k (i, j and k are all different). Conscious about their
> carbon footprint (!), they want k to be as near as possible:
> specifically the sum of the distances both has to travel should be
> minimum.

So you can extract a list of distances for i and another for j.
You then want to find the smallest pair of values in those lists.

Here is a smaller example:

i = [2,4,6]
j = [3,2,1]

the pairs are (2,3), (4,2), (6,1)

The sums of pairs are therefore: 5, 6, 7

So the smallest pair is the first one.

So which ever city the first one represents is the city where they 
should meet.

> where Vladimir and Petra live. From your program, calculate the answer for
> 1) i=5, j=11
> 2)i=10, j=13
> I think my program is correct, but I'm not sure what they want us to do
> with the two conditions given above.

You plug those into your program as the values for the two start cities.
So you should wind up with two answers.

> I also don't know what to enter
> when I run the program to test these.

You enter (depending on how you code it) either the city names 
corresponding to this numbers in your matrix or you enter
in the numbers directly (if you used a numbered menu, say).

Alan G
Author of the Learn to Program web site

More information about the Tutor mailing list