本文整理汇总了Python中sage.graphs.graph.Graph.subgraph方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.subgraph方法的具体用法?Python Graph.subgraph怎么用?Python Graph.subgraph使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sage.graphs.graph.Graph
的用法示例。
在下文中一共展示了Graph.subgraph方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: AfricaMap
# 需要导入模块: from sage.graphs.graph import Graph [as 别名]
# 或者: from sage.graphs.graph.Graph import subgraph [as 别名]
def AfricaMap(continental=False, year=2018):
"""
Return African states as a graph of common border.
"African state" here is defined as an independent
state having the capital city in Africa. The graph
has an edge between those countries that have common
*land* border.
INPUT:
- ``continental``, a Boolean -- if set, only return states in
the continental Africa
- ``year`` -- reserved for future use
EXAMPLES::
sage: Africa = graphs.AfricaMap(); Africa
Africa Map: Graph on 54 vertices
sage: sorted(Africa.neighbors('Libya'))
['Algeria', 'Chad', 'Egypt', 'Niger', 'Sudan', 'Tunisia']
sage: cont_Africa = graphs.AfricaMap(continental=True)
sage: cont_Africa.order()
48
sage: 'Madagaskar' in cont_Africa
False
TESTS::
sage: Africa.plot()
Graphics object consisting of 159 graphics primitives
"""
if year != 2018:
raise ValueError("currently only year 2018 is implemented")
common_border = {
'Algeria': ['Libya', 'Mali', 'Mauritania', 'Morocco', 'Niger', 'Tunisia'],
'Angola': ['Namibia', 'Zambia'],
'Benin': ['Burkina Faso', 'Niger', 'Nigeria', 'Togo'],
'Botswana': ['Namibia', 'South Africa', 'Zimbabwe'],
'Burkina Faso': ['Ghana', 'Ivory Coast', 'Mali', 'Niger', 'Togo'],
'Cameroon': ['Central Africa', 'Chad', 'Equatorial Guinea', 'Gabon', 'Nigeria'],
'Central Africa': ['Chad', 'South Sudan', 'Sudan'],
'Chad': ['Libya', 'Niger', 'Nigeria', 'Sudan'],
'Republic of the Congo': ['Gabon', 'Cameroon', 'Central Africa', 'Angola',
'Democratic Republic of the Congo'],
'Democratic Republic of the Congo': ['Zambia', 'South Sudan', 'Tanzania', 'Burundi',
'Rwanda', 'Uganda', 'Central Africa', 'Angola'],
'Djibouti': ['Eritrea', 'Ethiopia', 'Somalia'],
'Ethiopia': ['Eritrea', 'Kenya', 'Somalia', 'South Sudan', 'Sudan'],
'Gabon': ['Equatorial Guinea'],
'Ghana': ['Ivory Coast', 'Togo'],
'Guinea': ['Guinea-Bissau', 'Ivory Coast', 'Liberia', 'Sierra Leone'],
'Kenya': ['Somalia', 'South Sudan', 'Tanzania', 'Uganda'],
'Liberia': ['Ivory Coast', 'Sierra Leone'],
'Libya': ['Egypt', 'Niger', 'Sudan', 'Tunisia'],
'Mali': ['Guinea', 'Ivory Coast', 'Mauritania', 'Niger', 'Senegal'],
'Mozambique': ['Malawi', 'South Africa', 'Swaziland', 'Zimbabwe'],
'Niger': ['Nigeria'],
'Rwanda': ['Burundi', 'Tanzania', 'Uganda'],
'Senegal': ['Guinea', 'Guinea-Bissau', 'Mauritania', 'Gambia'],
'South Africa': ['Lesotho', 'Namibia', 'Swaziland', 'Zimbabwe'],
'South Sudan': ['Uganda', 'Sudan', 'Democratic Republic of the Congo'],
'Sudan': ['Egypt', 'Eritrea'],
'Tanzania': ['Burundi', 'Malawi', 'Mozambique', 'Uganda', 'Zambia'],
'Zambia': ['Malawi', 'Mozambique', 'Namibia', 'Zimbabwe']
}
no_land_border = ['Cape Verde', 'Seychelles', 'Mauritius', u'São Tomé and Príncipe', 'Madagascar', 'Comoros']
G = Graph(common_border, format='dict_of_lists')
if continental:
G = G.subgraph(G.connected_component_containing_vertex('Central Africa'))
G.name(new="Continental Africa Map")
else:
G.add_vertices(no_land_border)
G.name(new="Africa Map")
return G
示例2: _finite_recognition
# 需要导入模块: from sage.graphs.graph import Graph [as 别名]
# 或者: from sage.graphs.graph.Graph import subgraph [as 别名]
#.........这里部分代码省略.........
# A dihedral group is finite iff there is no `\infty`
# in its Coxeter matrix.
c0, c1 = comp
if coxeter_matrix[c0, c1] > 0:
continue # I2
return False
elif l == 3:
# The `3`-node case. The finite groups to check for
# here are `A_3`, `B_3` and `H_3`.
c0, c1, c2 = comp
s = sorted([coxeter_matrix[c0, c1],
coxeter_matrix[c0, c2],
coxeter_matrix[c1, c2]])
if s[1] == 3 and s[2] in [3, 4, 5]:
continue # A3, B3, H3
return False
elif l == 4:
# The `4`-node case. The finite groups to check for
# here are `A_4`, `B_4`, `D_4`, `F_4` and `H_4`.
c0, c1, c2, c3 = comp
u = [coxeter_matrix[c0, c1],
coxeter_matrix[c0, c2],
coxeter_matrix[c0, c3],
coxeter_matrix[c1, c2],
coxeter_matrix[c1, c3],
coxeter_matrix[c2, c3]]
s = sorted(u)
# ``s`` is the list of all off-diagonal entries of
# the ``comp``-submatrix of the Coxeter matrix,
# sorted in increasing order.
if s[3:5] == [3, 3]:
if s[5] == 3:
continue # A4, D4
if s[5] in [4, 5]:
u0 = u[0] + u[1] + u[2]
u1 = u[0] + u[3] + u[4]
u2 = u[1] + u[3] + u[5]
u3 = u[2] + u[4] + u[5]
ss = sorted([u0, u1, u2, u3])
if ss in [[7, 7, 9, 9], [7, 8, 8, 9],
[7, 8, 9, 10]]:
continue # F4, B4, H4
return False
else:
# The case of `l \geq 5` nodes. The finite
# groups to check for here are `A_l`, `B_l`, `D_l`,
# and `E_l` (for `l = 6, 7, 8`).
# Checking that the Coxeter matrix of the subgroup
# corresponding to the vertices ``comp`` has all its
# off-diagonal entries equal to 2, 3 or at most once 4
found_a_4 = False
for j in range(l):
for i in range(j):
coxeter_entry = coxeter_matrix[comp[i], comp[j]]
if coxeter_entry in [2, 3]:
continue
if coxeter_entry == 4 and not found_a_4:
found_a_4 = True
continue
return False
G0 = G.subgraph(comp)
if found_a_4:
# The case when a `4` has been found in the
# Coxeter matrix. This needs only to be checked
# against `B_l`. We use the observation that
# the group is `B_l` if and only if the Coxeter
# graph is an `l`-path (i.e., has diameter
# `l - 1`) and the `4` corresponds to one of
# its two outermost edges.
diameter = G0.diameter()
if diameter != l - 1:
return False
ecc = sorted(((u, v) for (v, u) in G0.eccentricity(with_labels=True).items()))
left_end = ecc[-1][1]
right_end = ecc[-2][1]
left_almost_end = G0.neigbors(left_end)[0]
right_almost_end = G0.neigbors(right_end)[0]
if (coxeter_matrix[left_end, left_almost_end] == 4
or coxeter_matrix[right_end, right_almost_end] == 4):
continue # Bl
return False
# Now, all off-diagonal entries of the Coxeter matrix
# are 2's and 3's. We need to check our group against
# `A_l`, `D_l` and `E_l`. Knowing that the Coxeter
# graph is a tree, we can use its vertex
# eccentricities to check this.
ecc = sorted(G0.eccentricity())
if ecc[-1] == l - 1:
continue # Al
if ecc[-3] == l - 2:
continue # Dl
if l <= 8 and ecc[-2] == l - 2 and ecc[-5] == l - 3:
continue # El
return False
return True
示例3: EuropeMap
# 需要导入模块: from sage.graphs.graph import Graph [as 别名]
# 或者: from sage.graphs.graph.Graph import subgraph [as 别名]
def EuropeMap(continental=False, year=2018):
"""
Return European states as a graph of common border.
"European state" here is defined as an independent
state having the capital city in Europe. The graph
has an edge between those countries that have common
*land* border.
INPUT:
- ``continental``, a Boolean -- if set, only return states in
the continental Europe
- ``year`` -- reserved for future use
EXAMPLES::
sage: Europe = graphs.EuropeMap(); Europe
Europe Map: Graph on 44 vertices
sage: Europe.neighbors('Ireland')
['United Kingdom']
sage: cont_Europe = graphs.EuropeMap(continental=True)
sage: cont_Europe.order()
40
sage: 'Iceland' in cont_Europe
False
"""
if year != 2018:
raise ValueError("currently only year 2018 is implemented")
common_border = {
'Poland': ['Slovakia', 'Czech Republic', 'Lithuania', 'Russia', 'Ukraine', 'Germany'],
'Germany': ['Czech Republic', 'Netherlands', 'Switzerland', 'Luxembourg', 'Denmark'],
'Croatia': ['Bosnia and Herzegovina', 'Serbia', 'Hungary', 'Montenegro', 'Slovenia'],
'Austria': ['Czech Republic', 'Germany', 'Switzerland', 'Slovenia', 'Liechtenstein'],
'France': ['Germany', 'Italy', 'Switzerland', 'Monaco', 'Luxembourg', 'Andorra'],
'Hungary': ['Slovakia', 'Serbia', 'Romania', 'Ukraine', 'Slovenia', 'Austria'],
'Italy': ['Switzerland', 'Vatican City', 'San Marino', 'Slovenia', 'Austria'],
'Belarus': ['Poland', 'Latvia', 'Lithuania', 'Russia', 'Ukraine'],
'Montenegro': ['Bosnia and Herzegovina', 'Serbia', 'Albania'],
'Belgium': ['Germany', 'Netherlands', 'Luxembourg', 'France'],
'Russia': ['Finland', 'Lithuania', 'Estonia', 'Ukraine'],
'Romania': ['Serbia', 'Moldova', 'Bulgaria', 'Ukraine'],
'Latvia': ['Lithuania', 'Russia', 'Estonia'],
'Slovakia': ['Czech Republic', 'Ukraine', 'Austria'], 'Switzerland': ['Liechtenstein'],
'Spain': ['Portugal', 'Andorra', 'France'], 'Norway': ['Finland', 'Sweden', 'Russia'],
'Ireland': ['United Kingdom'], 'Serbia': ['Bosnia and Herzegovina', 'Bulgaria'],
'Greece': ['Macedonia', 'Bulgaria', 'Albania'], 'Ukraine': ['Moldova'],
'Macedonia': ['Serbia', 'Bulgaria', 'Albania'], 'Sweden': ['Finland']
}
no_land_border = ['Iceland', 'Malta']
G = Graph(common_border, format='dict_of_lists')
if continental:
G = G.subgraph(G.connected_component_containing_vertex('Austria'))
G.name(new="Continental Europe Map")
else:
G.add_vertices(no_land_border)
G.name(new="Europe Map")
return G