Opened 3 months ago

Closed 2 months ago

#30214 closed enhancement (fixed)

Add to_digraph method to Permutation

Reported by: slelievre Owned by:
Priority: major Milestone: sage-9.2
Component: combinatorics Keywords: permutation, graph
Cc: slelievre Merged in:
Authors: David Coudert Reviewers: Sébastien Labbé
Report Upstream: N/A Work issues:
Branch: 12cadec (Commits) Commit: 12cadec9a8d01e5b7b804ee90e5a2df16dfef7a5
Dependencies: Stopgaps:

Description (last modified by slelievre)

Before this ticket, the show method for a permutation

  • computes a graph corresponding to that permutation
  • displays it
  • but does not return it

This ticket adds a to_digraph method to return the graph and changes the show method to use it.

This allows the following:

sage: p = Permutation([3, 1, 2])
sage: g = p.to_digraph()
sage: g.edges(labels=False)
[(1, 3), (2, 1), (3, 2)]

Inspired by a question by Ask Sage user magviana:

Change History (11)

comment:1 Changed 3 months ago by dcoudert

  • Authors set to David Coudert
  • Branch set to public/combinat/30214_to_digraph
  • Commit set to 498f093b7c0edfd61c16b1a9ec563a8563f8ec5b
  • Status changed from new to needs_review

This should do what you want.


New commits:

498f093trac #30214: add method to_digraph to Permutation

comment:2 Changed 3 months ago by dcoudert

  • Summary changed from Add graph method to permutations to Add to_digraph method to Permutation

comment:3 follow-up: Changed 2 months ago by slabbe

Personally, I would do

-E = [(i + 1, self[i]) for i in range(len(self))]
+E = list(enumerate(self, start=1)))

Do you agree?

Last edited 2 months ago by slelievre (previous) (diff)

comment:4 Changed 2 months ago by slabbe

  • Reviewers set to Sébastien Labbé

comment:5 in reply to: ↑ 3 Changed 2 months ago by slelievre

Replying to slabbe:

Personally, I would do

-E = [(i + 1, self[i]) for i in range(len(self))]
+E = list(enumerate(self, start=1)))

Do you agree?

+1

comment:6 Changed 2 months ago by git

  • Commit changed from 498f093b7c0edfd61c16b1a9ec563a8563f8ec5b to 12cadec9a8d01e5b7b804ee90e5a2df16dfef7a5

Branch pushed to git repo; I updated commit sha1. New commits:

08c3e05trac #30214: merged with 9.2.beta8
12cadectrac #30214: use enumerate and avoid creation of list

comment:7 Changed 2 months ago by dcoudert

Indeed a better solution. I'm also avoiding the creation of the list.

comment:8 Changed 2 months ago by slabbe

  • Status changed from needs_review to positive_review

good, I was not sure if it takes an iterable as well. even better.

comment:9 Changed 2 months ago by dcoudert

When the input format is specified, you can give an iterator. Otherwise...

comment:10 Changed 2 months ago by slelievre

  • Description modified (diff)

comment:11 Changed 2 months ago by vbraun

  • Branch changed from public/combinat/30214_to_digraph to 12cadec9a8d01e5b7b804ee90e5a2df16dfef7a5
  • Resolution set to fixed
  • Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.