本文整理汇总了Python中sage.graphs.graph.Graph.set_vertices方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.set_vertices方法的具体用法?Python Graph.set_vertices怎么用?Python Graph.set_vertices使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sage.graphs.graph.Graph
的用法示例。
在下文中一共展示了Graph.set_vertices方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: IntervalGraph
# 需要导入模块: from sage.graphs.graph import Graph [as 别名]
# 或者: from sage.graphs.graph.Graph import set_vertices [as 别名]
def IntervalGraph(intervals, points_ordered = False):
r"""
Returns the graph corresponding to the given intervals.
An interval graph is built from a list `(a_i,b_i)_{1\leq i \leq n}` of
intervals : to each interval of the list is associated one vertex, two
vertices being adjacent if the two corresponding (closed) intervals
intersect.
INPUT:
- ``intervals`` -- the list of pairs `(a_i,b_i)` defining the graph.
- ``points_ordered`` -- states whether every interval `(a_i,b_i)` of
`intervals` satisfies `a_i<b_i`. If satisfied then setting
``points_ordered`` to ``True`` will speed up the creation of the graph.
.. NOTE::
* The vertices are named 0, 1, 2, and so on. The intervals used
to create the graph are saved with the graph and can be recovered
using ``get_vertex()`` or ``get_vertices()``.
EXAMPLE:
The following line creates the sequence of intervals
`(i, i+2)` for i in `[0, ..., 8]`::
sage: intervals = [(i,i+2) for i in range(9)]
In the corresponding graph ::
sage: g = graphs.IntervalGraph(intervals)
sage: g.get_vertex(3)
(3, 5)
sage: neigh = g.neighbors(3)
sage: for v in neigh: print g.get_vertex(v)
(1, 3)
(2, 4)
(4, 6)
(5, 7)
The is_interval() method verifies that this graph is an interval graph. ::
sage: g.is_interval()
True
The intervals in the list need not be distinct. ::
sage: intervals = [ (1,2), (1,2), (1,2), (2,3), (3,4) ]
sage: g = graphs.IntervalGraph(intervals,True)
sage: g.clique_maximum()
[0, 1, 2, 3]
sage: g.get_vertices()
{0: (1, 2), 1: (1, 2), 2: (1, 2), 3: (2, 3), 4: (3, 4)}
The endpoints of the intervals are not ordered we get the same graph
(except for the vertex labels). ::
sage: rev_intervals = [ (2,1), (2,1), (2,1), (3,2), (4,3) ]
sage: h = graphs.IntervalGraph(rev_intervals,False)
sage: h.get_vertices()
{0: (2, 1), 1: (2, 1), 2: (2, 1), 3: (3, 2), 4: (4, 3)}
sage: g.edges() == h.edges()
True
"""
n = len(intervals)
g = Graph(n)
if points_ordered:
for i in xrange(n-1):
li,ri = intervals[i]
for j in xrange(i+1,n):
lj,rj = intervals[j]
if ri < lj or rj < li: continue
g.add_edge(i,j)
else:
for i in xrange(n-1):
I = intervals[i]
for j in xrange(i+1,n):
J = intervals[j]
if max(I) < min(J) or max(J) < min(I): continue
g.add_edge(i,j)
rep = dict( zip(range(n),intervals) )
g.set_vertices(rep)
return g
示例2: ToleranceGraph
# 需要导入模块: from sage.graphs.graph import Graph [as 别名]
# 或者: from sage.graphs.graph.Graph import set_vertices [as 别名]
def ToleranceGraph(tolrep):
r"""
Returns the graph generated by the tolerance representation ``tolrep``.
The tolerance representation ``tolrep`` is described by the list
`((l_0,r_0,t_0), (l_1,r_1,t_1), ..., (l_k,r_k,t_k))` where `I_i = (l_i,r_i)`
denotes a closed interval on the real line with `l_i < r_i` and `t_i` a
positive value, called tolerance. This representation generates the
tolerance graph with the vertex set {0,1, ..., k} and the edge set `{(i,j):
|I_i \cap I_j| \ge \min{t_i, t_j}}` where `|I_i \cap I_j|` denotes the
length of the intersection of `I_i` and `I_j`.
INPUT:
- ``tolrep`` -- list of triples `(l_i,r_i,t_i)` where `(l_i,r_i)` denotes a
closed interval on the real line and `t_i` a positive value.
.. NOTE::
The vertices are named 0, 1, ..., k. The tolerance representation used
to create the graph is saved with the graph and can be recovered using
``get_vertex()`` or ``get_vertices()``.
EXAMPLE:
The following code creates a tolerance representation ``tolrep``, generates
its tolerance graph ``g``, and applies some checks::
sage: tolrep = [(1,4,3),(1,2,1),(2,3,1),(0,3,3)]
sage: g = graphs.ToleranceGraph(tolrep)
sage: g.get_vertex(3)
(0, 3, 3)
sage: neigh = g.neighbors(3)
sage: for v in neigh: print g.get_vertex(v)
(1, 2, 1)
(2, 3, 1)
sage: g.is_interval()
False
sage: g.is_weakly_chordal()
True
The intervals in the list need not be distinct ::
sage: tolrep2 = [(0,4,5),(1,2,1),(2,3,1),(0,4,5)]
sage: g2 = graphs.ToleranceGraph(tolrep2)
sage: g2.get_vertices()
{0: (0, 4, 5), 1: (1, 2, 1), 2: (2, 3, 1), 3: (0, 4, 5)}
sage: g2.is_isomorphic(g)
True
Real values are also allowed ::
sage: tolrep = [(0.1,3.3,4.4),(1.1,2.5,1.1),(1.4,4.4,3.3)]
sage: g = graphs.ToleranceGraph(tolrep)
sage: g.is_isomorphic(graphs.PathGraph(3))
True
TEST:
Giving negative third value::
sage: tolrep = [(0.1,3.3,-4.4),(1.1,2.5,1.1),(1.4,4.4,3.3)]
sage: g = graphs.ToleranceGraph(tolrep)
Traceback (most recent call last):
...
ValueError: Invalid tolerance representation at position 0; third value must be positive!
"""
n = len(tolrep)
for i in xrange(n):
if tolrep[i][2] <= 0:
raise ValueError("Invalid tolerance representation at position "+str(i)+"; third value must be positive!")
g = Graph(n)
for i in xrange(n-1):
li,ri,ti = tolrep[i]
for j in xrange(i+1,n):
lj,rj,tj = tolrep[j]
if min(ri,rj) - max(li,lj) >= min(ti,tj):
g.add_edge(i,j)
rep = dict( zip(range(n),tolrep) )
g.set_vertices(rep)
return g
示例3: graph
# 需要导入模块: from sage.graphs.graph import Graph [as 别名]
# 或者: from sage.graphs.graph.Graph import set_vertices [as 别名]
def graph(self):
"""
Returns a graph whose vertices correspond to curves in this class, and whose edges correspond to prime degree isogenies.
.. note:
There are only finitely many possible isogeny graphs for
curves over Q [M78]. This function tries to lay out the graph
nicely by special casing each isogeny graph.
.. note:
The vertices are labeled 1 to n rather than 0 to n-1 to
correspond to LMFDB and Cremona labels.
EXAMPLES::
sage: isocls = EllipticCurve('15a3').isogeny_class(use_tuple=False)
sage: G = isocls.graph()
sage: sorted(G._pos.items())
[(1, [-0.8660254, 0.5]), (2, [-0.8660254, 1.5]), (3, [-1.7320508, 0]), (4, [0, 0]), (5, [0, -1]), (6, [0.8660254, 0.5]), (7, [0.8660254, 1.5]), (8, [1.7320508, 0])]
REFERENCES:
.. [M78] B. Mazur. Rational Isogenies of Prime Degree.
*Inventiones mathematicae* 44,129-162 (1978).
"""
from sage.graphs.graph import Graph
M = self.matrix(fill = False)
n = M.nrows() # = M.ncols()
G = Graph(M, format='weighted_adjacency_matrix')
N = self.matrix(fill = True)
D = dict([(v,self.curves[v]) for v in G.vertices()])
# The maximum degree classifies the shape of the isogeny
# graph, though the number of vertices is often enough.
# This only holds over Q, so this code will need to change
# once other isogeny classes are implemented.
if n == 1:
# one vertex
pass
elif n == 2:
# one edge, two vertices. We align horizontally and put
# the lower number on the left vertex.
G.set_pos(pos={0:[-0.5,0],1:[0.5,0]})
else:
maxdegree = max(max(N))
if n == 3:
# o--o--o
centervert = [i for i in range(3) if max(N.row(i)) < maxdegree][0]
other = [i for i in range(3) if i != centervert]
G.set_pos(pos={centervert:[0,0],other[0]:[-1,0],other[1]:[1,0]})
elif maxdegree == 4:
# o--o<8
centervert = [i for i in range(4) if max(N.row(i)) < maxdegree][0]
other = [i for i in range(4) if i != centervert]
G.set_pos(pos={centervert:[0,0],other[0]:[0,1],other[1]:[-0.8660254,-0.5],other[2]:[0.8660254,-0.5]})
elif maxdegree == 27:
# o--o--o--o
centers = [i for i in range(4) if list(N.row(i)).count(3) == 2]
left = [j for j in range(4) if N[centers[0],j] == 3 and j not in centers][0]
right = [j for j in range(4) if N[centers[1],j] == 3 and j not in centers][0]
G.set_pos(pos={left:[-1.5,0],centers[0]:[-0.5,0],centers[1]:[0.5,0],right:[1.5,0]})
elif n == 4:
# square
opp = [i for i in range(1,4) if not N[0,i].is_prime()][0]
other = [i for i in range(1,4) if i != opp]
G.set_pos(pos={0:[1,1],other[0]:[-1,1],opp:[-1,-1],other[1]:[1,-1]})
elif maxdegree == 8:
# 8>o--o<8
centers = [i for i in range(6) if list(N.row(i)).count(2) == 3]
left = [j for j in range(6) if N[centers[0],j] == 2 and j not in centers]
right = [j for j in range(6) if N[centers[1],j] == 2 and j not in centers]
G.set_pos(pos={centers[0]:[-0.5,0],left[0]:[-1,0.8660254],left[1]:[-1,-0.8660254],centers[1]:[0.5,0],right[0]:[1,0.8660254],right[1]:[1,-0.8660254]})
elif maxdegree == 18:
# two squares joined on an edge
centers = [i for i in range(6) if list(N.row(i)).count(3) == 2]
top = [j for j in range(6) if N[centers[0],j] == 3]
bl = [j for j in range(6) if N[top[0],j] == 2][0]
br = [j for j in range(6) if N[top[1],j] == 2][0]
G.set_pos(pos={centers[0]:[0,0.5],centers[1]:[0,-0.5],top[0]:[-1,0.5],top[1]:[1,0.5],bl:[-1,-0.5],br:[1,-0.5]})
elif maxdegree == 16:
# tree from bottom, 3 regular except for the leaves.
centers = [i for i in range(8) if list(N.row(i)).count(2) == 3]
center = [i for i in centers if len([j for j in centers if N[i,j] == 2]) == 2][0]
centers.remove(center)
bottom = [j for j in range(8) if N[center,j] == 2 and j not in centers][0]
left = [j for j in range(8) if N[centers[0],j] == 2 and j != center]
right = [j for j in range(8) if N[centers[1],j] == 2 and j != center]
G.set_pos(pos={center:[0,0],bottom:[0,-1],centers[0]:[-0.8660254,0.5],centers[1]:[0.8660254,0.5],left[0]:[-0.8660254,1.5],right[0]:[0.8660254,1.5],left[1]:[-1.7320508,0],right[1]:[1.7320508,0]})
elif maxdegree == 12:
# tent
centers = [i for i in range(8) if list(N.row(i)).count(2) == 3]
left = [j for j in range(8) if N[centers[0],j] == 2]
right = []
for i in range(3):
right.append([j for j in range(8) if N[centers[1],j] == 2 and N[left[i],j] == 3][0])
G.set_pos(pos={centers[0]:[-0.75,0],centers[1]:[0.75,0],left[0]:[-0.75,1],right[0]:[0.75,1],left[1]:[-1.25,-0.75],right[1]:[0.25,-0.75],left[2]:[-0.25,-0.25],right[2]:[1.25,-0.25]})
G.set_vertices(D)
G.relabel(range(1,n+1))
return G