def _digraph_mutate( dg, k, n, m ): """ Returns a digraph obtained from dg with n+m vertices by mutating at vertex k. INPUT: - ``dg`` -- a digraph with integral edge labels with ``n+m`` vertices - ``k`` -- the vertex at which ``dg`` is mutated EXAMPLES:: sage: from sage.combinat.cluster_algebra_quiver.mutation_class import _digraph_mutate sage: dg = ClusterQuiver(['A',4]).digraph() sage: dg.edges() [(0, 1, (1, -1)), (2, 1, (1, -1)), (2, 3, (1, -1))] sage: _digraph_mutate(dg,2,4,0).edges() [(0, 1, (1, -1)), (1, 2, (1, -1)), (3, 2, (1, -1))] """ edges = dict( ((v1,v2),label) for v1,v2,label in dg._backend.iterator_in_edges(dg,True) ) in_edges = [ (v1,v2,edges[(v1,v2)]) for (v1,v2) in edges if v2 == k ] out_edges = [ (v1,v2,edges[(v1,v2)]) for (v1,v2) in edges if v1 == k ] in_edges_new = [ (v2,v1,(-label[1],-label[0])) for (v1,v2,label) in in_edges ] out_edges_new = [ (v2,v1,(-label[1],-label[0])) for (v1,v2,label) in out_edges ] diag_edges_new = [] diag_edges_del = [] for (v1,v2,label1) in in_edges: for (w1,w2,label2) in out_edges: l11,l12 = label1 l21,l22 = label2 if (v1,w2) in edges: diag_edges_del.append( (v1,w2,edges[(v1,w2)]) ) a,b = edges[(v1,w2)] a,b = a+l11*l21, b-l12*l22 diag_edges_new.append( (v1,w2,(a,b)) ) elif (w2,v1) in edges: diag_edges_del.append( (w2,v1,edges[(w2,v1)]) ) a,b = edges[(w2,v1)] a,b = b+l11*l21, a-l12*l22 if a<0: diag_edges_new.append( (w2,v1,(b,a)) ) elif a>0: diag_edges_new.append( (v1,w2,(a,b)) ) else: a,b = l11*l21,-l12*l22 diag_edges_new.append( (v1,w2,(a,b)) ) del_edges = in_edges + out_edges + diag_edges_del new_edges = in_edges_new + out_edges_new + diag_edges_new new_edges += [ (v1,v2,edges[(v1,v2)]) for (v1,v2) in edges if not (v1,v2,edges[(v1,v2)]) in del_edges ] dg_new = DiGraph() for v1,v2,label in new_edges: dg_new._backend.add_edge(v1,v2,label,True) if dg_new.order() < n+m: dg_new_vertices = [ v for v in dg_new ] for i in [ v for v in dg if v not in dg_new_vertices ]: dg_new.add_vertex(i) return dg_new
def _matrix_to_digraph( M ): """ Returns the digraph obtained from the matrix ``M``. In order to generate a quiver, we assume that ``M`` is skew-symmetrizable. EXAMPLES:: sage: from sage.combinat.cluster_algebra_quiver.mutation_class import _matrix_to_digraph sage: _matrix_to_digraph(matrix(3,[0,1,0,-1,0,-1,0,1,0])) Digraph on 3 vertices """ n = M.ncols() dg = DiGraph(sparse=True) for i,j in M.nonzero_positions(): if i >= n: a,b = M[i,j],-M[i,j] else: a,b = M[i,j],M[j,i] if a > 0: dg._backend.add_edge(i,j,(a,b),True) elif i >= n: dg._backend.add_edge(j,i,(-a,-b),True) if dg.order() < M.nrows(): for i in [ index for index in xrange(M.nrows()) if index not in dg ]: dg.add_vertex(i) return dg
def to_dag(self): """ Returns a directed acyclic graph corresponding to the skew partition. EXAMPLES:: sage: dag = SkewPartition([[3, 2, 1], [1, 1]]).to_dag() sage: dag.edges() [('0,1', '0,2', None), ('0,1', '1,1', None)] sage: dag.vertices() ['0,1', '0,2', '1,1', '2,0'] """ i = 0 #Make the skew tableau from the shape skew = [[1]*row_length for row_length in self.outer()] inner = self.inner() for i in range(len(inner)): for j in range(inner[i]): skew[i][j] = None G = DiGraph() for row in range(len(skew)): for column in range(len(skew[row])): if skew[row][column] is not None: string = "%d,%d" % (row, column) G.add_vertex(string) #Check to see if there is a node to the right if column != len(skew[row]) - 1: newstring = "%d,%d" % (row, column+1) G.add_edge(string, newstring) #Check to see if there is anything below if row != len(skew) - 1: if len(skew[row+1]) > column: if skew[row+1][column] is not None: newstring = "%d,%d" % (row+1, column) G.add_edge(string, newstring) return G
def to_dag(self): """ Returns a directed acyclic graph corresponding to the skew partition. EXAMPLES:: sage: dag = SkewPartition([[3, 2, 1], [1, 1]]).to_dag() sage: dag.edges() [('0,1', '0,2', None), ('0,1', '1,1', None)] sage: dag.vertices() ['0,1', '0,2', '1,1', '2,0'] """ i = 0 #Make the skew tableau from the shape skew = [[1] * row_length for row_length in self.outer()] inner = self.inner() for i in range(len(inner)): for j in range(inner[i]): skew[i][j] = None G = DiGraph() for row in range(len(skew)): for column in range(len(skew[row])): if skew[row][column] is not None: string = "%d,%d" % (row, column) G.add_vertex(string) #Check to see if there is a node to the right if column != len(skew[row]) - 1: newstring = "%d,%d" % (row, column + 1) G.add_edge(string, newstring) #Check to see if there is anything below if row != len(skew) - 1: if len(skew[row + 1]) > column: if skew[row + 1][column] is not None: newstring = "%d,%d" % (row + 1, column) G.add_edge(string, newstring) return G
def _digraph(self): r""" Constructs the underlying digraph and stores the result as an attribute. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(2)] sage: Y = YangBaxterGraph(root=(1,2,3), operators=ops) sage: Y._digraph Digraph on 6 vertices """ digraph = DiGraph() digraph.add_vertex(self._root) queue = [self._root] while queue: u = queue.pop() for (v, l) in self._succesors(u): if v not in digraph: queue.append(v) digraph.add_edge(u, v, l) return digraph