# set_edge_label is setting labels on intended and unintended graph

38 views

### m_p_v_13

Mar 6, 2010, 2:03:17 AM3/6/10
to sage-support
set_edge_label is setting labels on intended and unintended graph

I generate some graphs and set one's edge labels. When I look at the
next genetated graph, it's edge labels have been changed.
I'm new to Sage/python so perhaps I goofed, but I doubt it. Thanks
for an explanation or workaround.

I am trying to help my daughter investigate an edge coloring graph
theory problem. Not the normal edge coloring constraint, though.

See the <<<<<<<<<<<<<<<< I added to the output section.

for G in graphs(3):
if G.is_connected():
print A
print "Edges:"
for e in G.edge_iterator( labels=True):
print e
#G.plot( edge_labels=True )
print "Setting edge labels..."
for i in G.edge_iterator( labels=True):
#print i
#print type( i )
G.set_edge_label( i[0], i[1], 'Red' )
print A
print "Edges:"
for j in G.edge_iterator( labels=True):
print j
#G.plot( edge_labels=True )
print "----------------------------"

[0 1 1]
[1 0 0]
[1 0 0]
Edges:
(0, 1, None)
(0, 2, None)
Setting edge labels...
[0 1 1]
[1 0 0]
[1 0 0]
Edges:
(0, 1, 'Red')
(0, 2, 'Red')
----------------------------
[0 1 1]
[1 0 1]
[1 1 0]
Edges:
(0, 1,
'Red')
<<<<<<<<<<<<<<<<
(0, 2,
'Red')
<<<<<<<<<<<<<<<<
(1, 2, None)
Setting edge labels...
[0 1 1]
[1 0 1]
[1 1 0]
Edges:
(0, 1, 'Red')
(0, 2, 'Red')
(1, 2, 'Red')
----------------------------

Message has been deleted

### Pat LeSmithe

Mar 6, 2010, 3:38:33 AM3/6/10
On 03/05/2010 11:03 PM, m_p_v_13 wrote:
> set_edge_label is setting labels on intended and unintended graph
>
> I generate some graphs and set one's edge labels. When I look at the
> next genetated graph, it's edge labels have been changed.
> I'm new to Sage/python so perhaps I goofed, but I doubt it. Thanks
> for an explanation or workaround.
>
> I am trying to help my daughter investigate an edge coloring graph
> theory problem. Not the normal edge coloring constraint, though.
>

> for G in graphs(3):

Replacing this with

for G in list(graphs(3)):

seems to work. But I think this generates all of the graphs in advance.

### m_p_v_13

Mar 6, 2010, 2:03:06 PM3/6/10
to sage-support
Minh,

Thanks for the quick look at this.

So, what you are saying is that the iterator for graphs() does not
return independent graphs which can be changed without affecting the
others.
That does explain what I am seeing and is consistent with Pat
LeSmith's suggested workaround.

Should this property of the iterators to the generated graphs be
documented?

So, I think I will try making a copy of just the graphs I want to
change or use the list() trick.

Thanks again,
Michael Vogt

On Mar 6, 3:30 am, Minh Nguyen <nguyenmi...@gmail.com> wrote:
> Hi,

>
> On Sat, Mar 6, 2010 at 6:03 PM, m_p_v_13 <michael_p_v...@yahoo.com> wrote:
> > set_edge_label is setting labels on intended and unintended graph
>
> > I generate some graphs and set one's edge labels. When I look at the
> > next genetated graph, it's edge labels have been changed.
>

> Unless I'm mistaken, the graph generator function graphs(3) returns
> three graphs: G_1 with 1 edge, G_2 with 2 edges, and G_3 with three
> edges. Each graph builds upon another one, so G_2 is built using G_1,
> and G_3 is built using G_2. Whatever properties that are in G_1 would
> be preserved in G_2, and similarly for G_3. From your report, I gather
> that you don't want graph properties of G_1 to be carried over to G_2,
> and neither do you want properties of G_2 to be carried over to G_3.
> For completeness, I have copied a Sage session with essentially the
> same code as you posted, but used within Sage 4.3.3:
>
> [mvngu@sage mvngu]\$ sage
> ----------------------------------------------------------------------
> | Sage Version 4.3.3, Release Date: 2010-02-21                       |
> | Type notebook() for the GUI, and license() for information.        |
> ----------------------------------------------------------------------
> sage: !more graph.py
> for G in graphs(3, implementation="networkx"):

>     if G.is_connected():
>         print A
>         print "Edges:"
>         for e in G.edge_iterator(labels=True):
>             print e

>         print "Setting edge labels..."
>         for e in G.edge_iterator(labels=True):
>             G.set_edge_label(e[0], e[1], "Red")
>         for e in G.edge_iterator(labels=True):
>             print e
>     else:
>         for e in G.edge_iterator(labels=True):
>             print e
>     print "-" * 20
> --------------------
> (0, 1, None)
> --------------------

> [0 1 1]
> [1 0 0]
> [1 0 0]
> Edges:
> (0, 1, None)
> (0, 2, None)
> Setting edge labels...

> (0, 1, 'Red')
> (0, 2, 'Red')
> --------------------

> [0 1 1]
> [1 0 1]
> [1 1 0]
> Edges:
> (0, 1, 'Red')
> (0, 2, 'Red')
> (1, 2, None)
> Setting edge labels...

> (0, 1, 'Red')
> (0, 2, 'Red')
> (1, 2, 'Red')
> --------------------
>
> --

> Regards
> Minh Van Nguyen

### m_p_v_13

Mar 6, 2010, 2:08:46 PM3/6/10
to sage-support
Pat,

Thanks so much for pointing out this workaround.

I think I can specify that I only want connected graphs as parameter
to graphs() to reduce the overhead of list().
Or, I'll just make copies of the ones I need to change and add them to
a collection.

Message has been deleted

### Michael Vogt

Mar 6, 2010, 5:27:38 PM3/6/10
 Hello Minh,Thanks again for the fast response. I fixed my code to have the graphs I need make a copy and add to a seperate list              ...              for C in graphs(3):                if C.is_connected():                    G = copy(C)                    L.append( G )                    A = G.adjacency_matrix()                    ...This seems to work.I'm just starting out with Sage and python.I see you are the author of   sage.graphs.graph_coloring.edge_coloring()I am about to study that to see how easily it could be modified to do a different edge coloring, detectable. The detectable coloring is such that no vertices have the same number of colors. That is, for three colors, that each vertex has a uniqque vector ( num red, num blue, num yellow ).I hope you might be able to answer some questions when I run into them.Thanks,Michael Vogt561 338-7151--- On Sat, 3/6/10, Minh Nguyen wrote:From: Minh Nguyen Subject: Re: [sage-support] Re: set_edge_label is setting labels on intended and unintended graphTo: sage-s...@googlegroups.comDate: Saturday, March 6, 2010, 2:19 PMHi Michael,On Sun, Mar 7, 2010 at 6:03 AM, m_p_v_13 wrote:
 > Should this property of the iterators to the generated graphs be> documented?
 This is now ticket #8458:http://trac.sagemath.org/sage_trac/ticket/8458
 -- RegardsMinh Van Nguyen
 -- To post to this group, send email to sage-s...@googlegroups.comTo unsubscribe from this group, send email to sage-support+unsub...@googlegroups.comFor more options, visit this group at http://groups.google.com/group/sage-supportURL: http://www.sagemath.org

### Jason Grout

Mar 6, 2010, 4:59:56 PM3/6/10
On 03/06/2010 02:30 AM, Minh Nguyen wrote:
> Hi,
>
> On Sat, Mar 6, 2010 at 6:03 PM, m_p_v_13<michael...@yahoo.com> wrote:
>> set_edge_label is setting labels on intended and unintended graph
>>
>> I generate some graphs and set one's edge labels. When I look at the
>> next genetated graph, it's edge labels have been changed.
>
> Unless I'm mistaken, the graph generator function graphs(3) returns
> three graphs: G_1 with 1 edge, G_2 with 2 edges, and G_3 with three
> edges. Each graph builds upon another one, so G_2 is built using G_1,
> and G_3 is built using G_2. Whatever properties that are in G_1 would
> be preserved in G_2, and similarly for G_3.

Wow, that seems really, really bad. Shouldn't a generator like graphs()
return *copies* of the graphs it is using internally? The extremely
common use-case:

for g in graphs(3):
do something to g, add it to a list, etc.

could potentially really mess up things, then! For example, what if I
delete a vertex? I get a big list index error for the following code:

for g in graphs(3):
g.delete_vertex(0)
print g.am()

Robert, do you agree that this should be changed to return a copy of the
graph, instead of the actual graph used to build other graphs?

Thanks,

Jason

### Robert Miller

Mar 7, 2010, 1:12:58 PM3/7/10
to sage-support
> Robert, do you agree that this should be changed to return a copy of the
> graph, instead of the actual graph used to build other graphs?

See my comments on the ticket.