Squaring an adjacency matrix

29 views
Skip to first unread message

Ken Levasseur

unread,
Apr 18, 2012, 1:21:02 PM4/18/12
to sage...@googlegroups.com
I taught a class for colleague this morning and he asked me to cover some material on relations. So I took that opportunity to figure out how to implement relations in Sage. It went pretty well, but one exception is that in squaring an adjacency matrix of a relation, I'd like to get a simple adjacency matrix with just 0's and 1's. The way I did the calculations, I got the numbers of different paths of length 2 connecting the vertices. That's fine except that the graphs of the squared relations become messy. If anyone knows how to fix this, I'd appreciate the help.

I've published my notebook at 
http://www.sagenb.org/home/pub/4715

Jason Grout

unread,
Apr 18, 2012, 1:54:57 PM4/18/12
to sage...@googlegroups.com

Nice; you're using an open-source textbook?

Use multiedges=False when you create the DiGraph:

DiGraph(H,multiedges=False)

See http://aleph.sagemath.org/?q=a5b9d087-bbc0-4062-b8c4-2d73e72f231b

Thanks,

Jason

Jason Grout

unread,
Apr 18, 2012, 1:57:27 PM4/18/12
to sage...@googlegroups.com

Or you can fix H (this implements what you say in your yellow comment):

H=H.apply_map(lambda x: 1 if x!=0 else 0)

Thanks,

Jason

Ken Levasseur

unread,
Apr 18, 2012, 3:01:29 PM4/18/12
to sage...@googlegroups.com
Jason

Thanks. I think I prefer using apply_map.

Yes, the book is open content. We made the it public about a month ago.

Ken

Jason Grout

unread,
Apr 19, 2012, 3:10:26 AM4/19/12
to sage...@googlegroups.com
On 4/18/12 2:01 PM, Ken Levasseur wrote:
> Yes, the book is open content. We made the it public about a month ago.

Oh, right, we had this discussion a month ago. Thanks again! I just
sent the URL to one of our faculty to consider the book for our discrete
class.

Jason

Reply all
Reply to author
Forward
0 new messages