Country Chains

What Your Code Should Do

Write a function CountryChainLength that takes two country entity inputs and returns the length of the shortest chain of bordering countries that connects them. If no such path exists, return ∞.

CountryChainLength[(Entity["France", Entity["Country","France"], "Entity[\"Country\", \"France\"]", "country"]),(Entity["Ukraine", Entity["Country","Ukraine"], "Entity[\"Country\", \"Ukraine\"]", "country"])]

An example of such a chain is France -> Germany -> Poland -> Ukraine:

Adjacent countries only need one step:

CountryChainLength[(Entity["China", Entity["Country","China"], "Entity[\"Country\", \"China\"]", "country"]),(Entity["Mongolia", Entity["Country","Mongolia"], "Entity[\"Country\", \"Mongolia\"]", "country"])]

The bordering countries:

Some need many steps:

CountryChainLength[(Entity["Canada", Entity["Country","Canada"], "Entity[\"Country\", \"Canada\"]", "country"]),(Entity["Argentina", Entity["Country","Argentina"], "Entity[\"Country\", \"Argentina\"]", "country"])]

Visualize:

More Examples

A long chain connecting countries:

CountryChainLength[(Entity["Lesotho", Entity["Country","Lesotho"], "Entity[\"Country\", \"Lesotho\"]", "country"]),(Entity["South Korea", Entity["Country","SouthKorea"], "Entity[\"Country\", \"SouthKorea\"]", "country"])]

Here is the chain:

A country needs no chain to get to itself:

CountryChainLength[(Entity["Italy", Entity["Country","Italy"], "Entity[\"Country\", \"Italy\"]", "country"]),(Entity["Italy", Entity["Country","Italy"], "Entity[\"Country\", \"Italy\"]", "country"])]

Some countries have no such chain:

CountryChainLength[(Entity["New Zealand", Entity["Country","NewZealand"], "Entity[\"Country\", \"NewZealand\"]", "country"]),(Entity["Fiji", Entity["Country","Fiji"], "Entity[\"Country\", \"Fiji\"]", "country"])]

CountryChainLength[c1_,c2_]:=