本文整理汇总了Python中sage.graphs.graph.Graph.add_edge方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.add_edge方法的具体用法?Python Graph.add_edge怎么用?Python Graph.add_edge使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sage.graphs.graph.Graph
的用法示例。
在下文中一共展示了Graph.add_edge方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: to_bipartite_graph
# 需要导入模块: from sage.graphs.graph import Graph [as 别名]
# 或者: from sage.graphs.graph.Graph import add_edge [as 别名]
def to_bipartite_graph(self, with_partition=False):
r"""
Returns the associated bipartite graph
INPUT:
- with_partition -- boolean (default: False)
OUTPUT:
- a graph or a pair (graph, partition)
EXAMPLES::
sage: H = designs.steiner_triple_system(7).blocks()
sage: H = Hypergraph(H)
sage: g = H.to_bipartite_graph(); g
Graph on 14 vertices
sage: g.is_regular()
True
"""
from sage.graphs.graph import Graph
G = Graph()
domain = list(self.domain())
G.add_vertices(domain)
for s in self._sets:
for i in s:
G.add_edge(s, i)
if with_partition:
return (G, [domain, list(self._sets)])
else:
return G
示例2: coxeter_diagram
# 需要导入模块: from sage.graphs.graph import Graph [as 别名]
# 或者: from sage.graphs.graph.Graph import add_edge [as 别名]
def coxeter_diagram(self):
"""
Return the Coxeter diagram for ``self``.
EXAMPLES::
sage: cd = CartanType("A2xB2xF4").coxeter_diagram()
sage: cd
Graph on 8 vertices
sage: cd.edges()
[(1, 2, 3), (3, 4, 4), (5, 6, 3), (6, 7, 4), (7, 8, 3)]
sage: CartanType("F4xA2").coxeter_diagram().edges()
[(1, 2, 3), (2, 3, 4), (3, 4, 3), (5, 6, 3)]
sage: cd = CartanType("A1xH3").coxeter_diagram(); cd
Graph on 4 vertices
sage: cd.edges()
[(2, 3, 3), (3, 4, 5)]
"""
from sage.graphs.graph import Graph
relabelling = self._index_relabelling
g = Graph(multiedges=False)
g.add_vertices(self.index_set())
for i,t in enumerate(self._types):
for [e1, e2, l] in t.coxeter_diagram().edges():
g.add_edge(relabelling[i,e1], relabelling[i,e2], label=l)
return g
示例3: coxeter_diagram
# 需要导入模块: from sage.graphs.graph import Graph [as 别名]
# 或者: from sage.graphs.graph.Graph import add_edge [as 别名]
def coxeter_diagram(self):
"""
Returns a Coxeter diagram for type H.
EXAMPLES::
sage: ct = CartanType(['H',3])
sage: ct.coxeter_diagram()
Graph on 3 vertices
sage: sorted(ct.coxeter_diagram().edges())
[(1, 2, 3), (2, 3, 5)]
sage: ct.coxeter_matrix()
[1 3 2]
[3 1 5]
[2 5 1]
sage: ct = CartanType(['H',4])
sage: ct.coxeter_diagram()
Graph on 4 vertices
sage: sorted(ct.coxeter_diagram().edges())
[(1, 2, 3), (2, 3, 3), (3, 4, 5)]
sage: ct.coxeter_matrix()
[1 3 2 2]
[3 1 3 2]
[2 3 1 5]
[2 2 5 1]
"""
from sage.graphs.graph import Graph
n = self.n
g = Graph(multiedges=False)
for i in range(1, n):
g.add_edge(i, i+1, 3)
g.set_edge_label(n-1, n, 5)
return g
示例4: to_undirected_graph
# 需要导入模块: from sage.graphs.graph import Graph [as 别名]
# 或者: from sage.graphs.graph.Graph import add_edge [as 别名]
def to_undirected_graph(self):
r"""
Return the undirected graph obtained from the tree nodes and edges.
The graph is endowed with an embedding, so that it will be displayed
correctly.
EXAMPLES::
sage: t = OrderedTree([])
sage: t.to_undirected_graph()
Graph on 1 vertex
sage: t = OrderedTree([[[]],[],[]])
sage: t.to_undirected_graph()
Graph on 5 vertices
If the tree is labelled, we use its labelling to label the graph. This
will fail if the labels are not all distinct.
Otherwise, we use the graph canonical labelling which means that
two different trees can have the same graph.
EXAMPLES::
sage: t = OrderedTree([[[]],[],[]])
sage: t.canonical_labelling().to_undirected_graph()
Graph on 5 vertices
TESTS::
sage: t.canonical_labelling().to_undirected_graph() == t.to_undirected_graph()
False
sage: OrderedTree([[],[]]).to_undirected_graph() == OrderedTree([[[]]]).to_undirected_graph()
True
sage: OrderedTree([[],[],[]]).to_undirected_graph() == OrderedTree([[[[]]]]).to_undirected_graph()
False
"""
from sage.graphs.graph import Graph
g = Graph()
if self in LabelledOrderedTrees():
relabel = False
else:
self = self.canonical_labelling()
relabel = True
roots = [self]
g.add_vertex(name=self.label())
emb = {self.label(): []}
while roots:
node = roots.pop()
children = reversed([child.label() for child in node])
emb[node.label()].extend(children)
for child in node:
g.add_vertex(name=child.label())
emb[child.label()] = [node.label()]
g.add_edge(child.label(), node.label())
roots.append(child)
g.set_embedding(emb)
if relabel:
g = g.canonical_label()
return g
示例5: is_connected
# 需要导入模块: from sage.graphs.graph import Graph [as 别名]
# 或者: from sage.graphs.graph.Graph import add_edge [as 别名]
def is_connected(self, force_computation=False):
if not force_computation and self._connected:
return True
from sage.graphs.graph import Graph
G = Graph(loops=True, multiedges=True)
for i in xrange(self._total_darts):
if self._active_darts[i]:
G.add_edge(i,self._vertices[i])
G.add_edge(i,self._edges[i])
G.add_edge(i,self._faces[i])
return G.is_connected()
示例6: CayleyGraph
# 需要导入模块: from sage.graphs.graph import Graph [as 别名]
# 或者: from sage.graphs.graph.Graph import add_edge [as 别名]
def CayleyGraph(vspace, gens, directed=False):
"""
Generate a Cayley Graph over given vector space with edges
generated by given generators. The graph is optionally directed.
Try e.g. CayleyGraph(VectorSpace(GF(2), 3), [(1,0,0), (0,1,0), (0,0,1)])
"""
G = Graph()
for v in vspace:
G.add_vertex(tuple(v))
for g in gens:
g2 = vspace(g)
G.add_edge(tuple(v), tuple(v + g2))
return G
示例7: to_undirected_graph
# 需要导入模块: from sage.graphs.graph import Graph [as 别名]
# 或者: from sage.graphs.graph.Graph import add_edge [as 别名]
def to_undirected_graph(self):
r"""
Return the undirected graph obtained from the tree nodes and edges.
EXAMPLES::
sage: t = OrderedTree([])
sage: t.to_undirected_graph()
Graph on 1 vertex
sage: t = OrderedTree([[[]],[],[]])
sage: t.to_undirected_graph()
Graph on 5 vertices
If the tree is labelled, we use its labelling to label the graph.
Otherwise, we use the graph canonical labelling which means that
two different trees can have the same graph.
EXAMPLES::
sage: t = OrderedTree([[[]],[],[]])
sage: t.canonical_labelling().to_undirected_graph()
Graph on 5 vertices
sage: t.canonical_labelling().to_undirected_graph() == t.to_undirected_graph()
False
sage: OrderedTree([[],[]]).to_undirected_graph() == OrderedTree([[[]]]).to_undirected_graph()
True
sage: OrderedTree([[],[],[]]).to_undirected_graph() == OrderedTree([[[[]]]]).to_undirected_graph()
False
"""
from sage.graphs.graph import Graph
g = Graph()
if self in LabelledOrderedTrees():
relabel = False
else:
self = self.canonical_labelling()
relabel = True
roots = [self]
g.add_vertex(name=self.label())
while len(roots) != 0:
node = roots.pop()
for child in node:
g.add_vertex(name=child.label())
g.add_edge(child.label(), node.label())
roots.append(child)
if relabel:
g = g.canonical_label()
return g
示例8: _spring_layout
# 需要导入模块: from sage.graphs.graph import Graph [as 别名]
# 或者: from sage.graphs.graph.Graph import add_edge [as 别名]
def _spring_layout(self):
r"""
Return a spring layout for the vertices.
The layout is computed by creating a graph `G` on the vertices *and*
sets of the hypergraph. Each set is then made adjacent in `G` with all
vertices it contains before a spring layout is computed for this
graph. The position of the vertices in the hypergraph is the position of
the same vertices in the graph's layout.
.. NOTE::
This method also returns the position of the "fake" vertices,
i.e. those representing the sets.
EXAMPLES::
sage: H = Hypergraph([{1,2,3},{2,3,4},{3,4,5},{4,5,6}]); H
Hypergraph on 6 vertices containing 4 sets
sage: L = H._spring_layout()
sage: L # random
{1: (0.238, -0.926),
2: (0.672, -0.518),
3: (0.449, -0.225),
4: (0.782, 0.225),
5: (0.558, 0.518),
6: (0.992, 0.926),
{3, 4, 5}: (0.504, 0.173),
{2, 3, 4}: (0.727, -0.173),
{4, 5, 6}: (0.838, 0.617),
{1, 2, 3}: (0.393, -0.617)}
sage: all(v in L for v in H.domain())
True
sage: all(v in L for v in H._sets)
True
"""
from sage.graphs.graph import Graph
g = Graph()
for s in self._sets:
for x in s:
g.add_edge(s,x)
_ = g.plot(iterations = 50000,save_pos=True)
# The values are rounded as TikZ does not like accuracy.
return {k:(round(x,3),round(y,3)) for k,(x,y) in g.get_pos().items()}
示例9: _check
# 需要导入模块: from sage.graphs.graph import Graph [as 别名]
# 或者: from sage.graphs.graph.Graph import add_edge [as 别名]
def _check(self):
r"""
Check that the data of the Ribbon graph is coherent
"""
from sage.graphs.graph import Graph
G = Graph()
if len(self._active_darts) != self._total_darts:
raise ValueError, "the length of active darts is not total_darts"
if self._active_darts.count(True) != self._num_darts:
raise ValueError, "the number of darts do not coincide with active darts"
for i in xrange(self._total_darts):
if self._active_darts[i]:
G.add_edge(i,self._vertices[i])
G.add_edge(i,self._edges[i])
G.add_edge(i,self._faces[i])
if self._vertices[i] is None or self._vertices_inv[i] is None:
raise ValueError, "dart %d is active but has no vertex" %i
if self._edges[i] is None or self._edges_inv[i] is None:
raise ValueError, "dart %d is active but has no edge" %i
if self._faces[i] is None or self._faces_inv[i] is None:
raise ValueError, "dart %d is active but has no face" %i
if self._vertices[self._vertices_inv[i]] != i:
raise ValueError, "vertices is not the inverse of vertices_inv"
if self._edges[self._edges_inv[i]] != i:
raise ValueError, "edges is not the inverse of edges_inv"
if self._faces[self._faces_inv[i]] != i:
raise ValueError, "faces is not the inverse of faces_inv"
if self._faces[self._edges[self._vertices[i]]] != i:
raise ValueError, "the Ribbon graph condition vef=() is not satisfied for %d" %i
if self._edges[i] == i or self._edges[self._edges[i]] != i:
raise ValueError, "edges is not an involution without fixed point for %d" %i
else:
if self._vertices[i] is not None or self._vertices_inv[i] is not None:
raise ValueError, "dart %d is not active but has a vertex" %i
if self._edges[i] is not None or self._edges_inv[i] is not None:
raise ValueError, "dart %d is not active but has an edge" %i
if self._faces[i] is not None or self._faces_inv[i] is not None:
raise ValueError, "dart %d is not active but has a face" %i
if not G.is_connected():
raise ValueError, "the graph is not connected"
示例10: underlying_graph
# 需要导入模块: from sage.graphs.graph import Graph [as 别名]
# 或者: from sage.graphs.graph.Graph import add_edge [as 别名]
def underlying_graph(G):
r"""
Given a graph `G` with multi-edges, returns a graph where all the
multi-edges are replaced with a single edge.
EXAMPLES::
sage: from sage.graphs.tutte_polynomial import underlying_graph
sage: G = Graph(multiedges=True)
sage: G.add_edges([(0,1,'a'),(0,1,'b')])
sage: G.edges()
[(0, 1, 'a'), (0, 1, 'b')]
sage: underlying_graph(G).edges()
[(0, 1, None)]
"""
g = Graph()
g.allow_loops(True)
for edge in set(G.edges(labels=False)):
g.add_edge(edge)
return g
示例11: coxeter_graph
# 需要导入模块: from sage.graphs.graph import Graph [as 别名]
# 或者: from sage.graphs.graph.Graph import add_edge [as 别名]
def coxeter_graph(self):
"""
Return the Coxeter graph of ``self``.
EXAMPLES::
sage: W = CoxeterGroup(['H',3], implementation="reflection")
sage: G = W.coxeter_graph(); G
Graph on 3 vertices
sage: G.edges()
[(1, 2, None), (2, 3, 5)]
sage: CoxeterGroup(G) is W
True
sage: G = Graph([(0, 1, 3), (1, 2, oo)])
sage: W = CoxeterGroup(G)
sage: W.coxeter_graph() == G
True
sage: CoxeterGroup(W.coxeter_graph()) is W
True
"""
G = Graph()
G.add_vertices(self.index_set())
for i, row in enumerate(self._matrix.rows()):
for j, val in enumerate(row[i + 1 :]):
if val == 3:
G.add_edge(self._index_set[i], self._index_set[i + 1 + j])
elif val > 3:
G.add_edge(self._index_set[i], self._index_set[i + 1 + j], val)
elif val == -1: # FIXME: Hack because there is no ZZ\cup\{\infty\}
G.add_edge(self._index_set[i], self._index_set[i + 1 + j], infinity)
return G
示例12: to_graph
# 需要导入模块: from sage.graphs.graph import Graph [as 别名]
# 或者: from sage.graphs.graph.Graph import add_edge [as 别名]
def to_graph(self):
r"""
Returns the graph corresponding to the perfect matching.
OUTPUT:
The realization of ``self`` as a graph.
EXAMPLES::
sage: PerfectMatching([[1,3], [4,2]]).to_graph().edges(labels=False)
[(1, 3), (2, 4)]
sage: PerfectMatching([[1,4], [3,2]]).to_graph().edges(labels=False)
[(1, 4), (2, 3)]
sage: PerfectMatching([]).to_graph().edges(labels=False)
[]
"""
from sage.graphs.graph import Graph
G = Graph()
for a, b in self.value:
G.add_edge((a, b))
return G
示例13: connection_graph
# 需要导入模块: from sage.graphs.graph import Graph [as 别名]
# 或者: from sage.graphs.graph.Graph import add_edge [as 别名]
def connection_graph(self):
"""
Return the graph which has the variables of this system as
vertices and edges between two variables if they appear in the
same polynomial.
EXAMPLE::
sage: B.<x,y,z> = BooleanPolynomialRing()
sage: F = Sequence([x*y + y + 1, z + 1])
sage: F.connection_graph()
Graph on 3 vertices
"""
V = sorted(self.variables())
from sage.graphs.graph import Graph
g = Graph()
g.add_vertices(sorted(V))
for f in self:
v = f.variables()
a,tail = v[0],v[1:]
for b in tail:
g.add_edge((a,b))
return g
示例14: RandomTree
# 需要导入模块: from sage.graphs.graph import Graph [as 别名]
# 或者: from sage.graphs.graph.Graph import add_edge [as 别名]
def RandomTree(n):
"""
Returns a random tree on `n` nodes numbered `0` through `n-1`.
By Cayley's theorem, there are `n^{n-2}` trees with vertex
set `\{0,1,...,n-1\}`. This constructor chooses one of these uniformly
at random.
ALGORITHM:
The algoritm works by generating an `(n-2)`-long
random sequence of numbers chosen independently and uniformly
from `\{0,1,\ldots,n-1\}` and then applies an inverse
Prufer transformation.
INPUT:
- ``n`` - number of vertices in the tree
EXAMPLE::
sage: G = graphs.RandomTree(10)
sage: G.is_tree()
True
sage: G.show() # long
TESTS:
Ensuring that we encounter no unexpected surprise ::
sage: all( graphs.RandomTree(10).is_tree()
....: for i in range(100) )
True
"""
from sage.misc.prandom import randint
g = Graph()
# create random Prufer code
code = [ randint(0,n-1) for i in xrange(n-2) ]
# We count the number of symbols of each type.
# count[k] is the no. of times k appears in code
#
# (count[k] is set to -1 when the corresponding vertex is not
# available anymore)
count = [ 0 for i in xrange(n) ]
for k in code:
count[k] += 1
g.add_vertices(range(n))
for s in code:
for x in range(n):
if count[x] == 0:
break
count[x] = -1
g.add_edge(x,s)
count[s] -= 1
# Adding as an edge the last two available vertices
last_edge = [ v for v in range(n) if count[v] != -1 ]
g.add_edge(last_edge)
return g
示例15: RandomBipartite
# 需要导入模块: from sage.graphs.graph import Graph [as 别名]
# 或者: from sage.graphs.graph.Graph import add_edge [as 别名]
def RandomBipartite(n1,n2, p):
r"""
Returns a bipartite graph with `n1+n2` vertices
such that any edge from `[n1]` to `[n2]` exists
with probability `p`.
INPUT:
- ``n1,n2`` : Cardinalities of the two sets
- ``p`` : Probability for an edge to exist
EXAMPLE::
sage: g=graphs.RandomBipartite(5,2,0.5)
sage: g.vertices()
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 1)]
TESTS::
sage: g=graphs.RandomBipartite(5,-3,0.5)
Traceback (most recent call last):
...
ValueError: n1 and n2 should be integers strictly greater than 0
sage: g=graphs.RandomBipartite(5,3,1.5)
Traceback (most recent call last):
...
ValueError: Parameter p is a probability, and so should be a real value between 0 and 1
Trac ticket #12155::
sage: graphs.RandomBipartite(5,6,.2).complement()
complement(Random bipartite graph of size 5+6 with edge probability 0.200000000000000): Graph on 11 vertices
"""
if not (p>=0 and p<=1):
raise ValueError, "Parameter p is a probability, and so should be a real value between 0 and 1"
if not (n1>0 and n2>0):
raise ValueError, "n1 and n2 should be integers strictly greater than 0"
from numpy.random import uniform
g=Graph(name="Random bipartite graph of size "+str(n1) +"+"+str(n2)+" with edge probability "+str(p))
S1=[(0,i) for i in range(n1)]
S2=[(1,i) for i in range(n2)]
g.add_vertices(S1)
g.add_vertices(S2)
for w in range(n2):
for v in range(n1):
if uniform()<=p :
g.add_edge((0,v),(1,w))
pos = {}
for i in range(n1):
pos[(0,i)] = (0, i/(n1-1.0))
for i in range(n2):
pos[(1,i)] = (1, i/(n2-1.0))
g.set_pos(pos)
return g