美国各郡间的连通性
构建美国各郡间的连通图.
提取美国大陆相连的州的所有郡的列表.
In[1]:=
Length[counties =
Flatten[EntityClass["AdministrativeDivision",
"ContinentalUSStates"]["Subdivisions"]]]
Out[1]=
加上不属于 48 个相连本土州的哥伦比亚特区.
In[2]:=
AppendTo[counties,
Entity["AdministrativeDivision", {"DistrictOfColumbia",
"DistrictOfColumbia", "UnitedStates"}]];
计算与每个郡边界相连的郡的列表.
In[3]:=
bordering = EntityValue[counties, "BorderingCounties"];
除去无法获取边界信息的郡.
In[4]:=
Length[missingpos = Position[bordering, _Missing]]
Out[4]=
In[5]:=
counties = Delete[counties, missingpos];
bordering = Delete[bordering, missingpos];
构建美国各郡的连通图.
In[6]:=
Length[edges =
DeleteDuplicates[
Sort /@ Flatten[
MapThread[Thread[UndirectedEdge[##]] &, {counties, bordering}]]]]
Out[6]=
In[7]:=
Graph[counties, edges]
Out[7]=
为构建更常见的嵌入图,下载所有郡的中心位置.
In[8]:=
pos = GeoPosition[EntityValue[counties, "Position"]]
Out[8]=
使用以下地图投影.
In[9]:=
proj = {"LambertAzimuthal",
"Centering" ->
Entity["City", {"Topeka", "Kansas", "UnitedStates"}]};
In[10]:=
projpos = First@GeoGridPosition[pos, proj];
现在有以下嵌入图.
In[11]:=
graph = Graph[counties, edges, VertexCoordinates -> projpos]
Out[11]=
从旧金山郡到曼哈顿,你需要访问最少 67 个郡,包括起点和终点.
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]=