# Wolfram Language™

## Connectivity of US Counties

Construct the connectivity graph between neighboring US counties.

Take the list of all counties in the contiguous continental US states.

In[1]:=
```Length[counties = Flatten[EntityClass["AdministrativeDivision", "ContinentalUSStates"]["Subdivisions"]]]```
Out[1]=

Add the District of Columbia, which is not a subdivision of any of the 48 contiguous states.

In[2]:=
```AppendTo[counties, Entity["AdministrativeDivision", {"DistrictOfColumbia", "DistrictOfColumbia", "UnitedStates"}]];```

Compute the list of counties bordering each county.

In[3]:=
`bordering = EntityValue[counties, "BorderingCounties"];`

Discard some cases for which bordering information is not available.

In[4]:=
`Length[missingpos = Position[bordering, _Missing]]`
Out[4]=
In[5]:=
```counties = Delete[counties, missingpos]; bordering = Delete[bordering, missingpos];```

Construct the edges of the connectivity graph of the US counties.

In[6]:=
```Length[edges = DeleteDuplicates[ Sort /@ Flatten[ MapThread[Thread[UndirectedEdge[##]] &, {counties, bordering}]]]]```
Out[6]=
In[7]:=
`Graph[counties, edges]`
Out[7]=

To construct a more familiar embedding, download the center position of all counties.

In[8]:=
`pos = GeoPosition[EntityValue[counties, "Position"]]`
Out[8]=

Use the following cartographic projection.

In[9]:=
```proj = {"LambertAzimuthal", "Centering" -> Entity["City", {"Topeka", "Kansas", "UnitedStates"}]};```
In[10]:=
`projpos = First@GeoGridPosition[pos, proj];`

Then you have the following graph embedding.

In[11]:=
`graph = Graph[counties, edges, VertexCoordinates -> projpos]`
Out[11]=

To go from the county of San Francisco to Manhattan, you need to visit at least 67 counties, including the initial and final ones.

In[12]:=
```Length[path = FindShortestPath[graph, Entity[ "AdministrativeDivision", {"SanFranciscoCounty", "California", "UnitedStates"}], Entity[ "AdministrativeDivision", {"NewYorkCounty", "NewYork", "UnitedStates"}]]]```
Out[12]=
In[13]:=
`HighlightGraph[graph, PathGraph[path]]`
Out[13]=