Ask Your Question
0

code for bipartite graphs

asked 2013-03-28 13:32:13 +0200

REKHA BISWAL gravatar image

How to write a code for obtaining a bipartite graph where the edges are indexed by some numbers?e.g if i write the code as D = DiGraph({ 0: [1,2,3], 1: [0,2], 2: [3], 3: [4], 4: [0,5], 5: [1] }) then only graph is coming but the edges are not indexed here.How should i modify this so that edges of the graph will be indexed?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2013-03-28 13:51:54 +0200

tmonteil gravatar image

updated 2013-03-28 13:53:52 +0200

Instead of a dictionary, do a dictionary of dictionaries specifying the labels of the edges. In your example:

sage: D = DiGraph({ 0: {1:123 , 2:234 , 3:12}, 1: {0:'a string', 2:42}, 2: {3:0}, 3: {4:34}, 4: {0:40, 5:11}, 5: {1:123} })

Note that you can label edges by strings as well if you want.

When plotting, if you want to see the labels, you should do:

sage: D.plot(edge_labels=True)
edit flag offensive delete link more

Comments

but in your code the labels are merged on the edges.can i do so that labels will show above the edges without merging on them?

REKHA BISWAL gravatar imageREKHA BISWAL ( 2013-03-28 14:03:38 +0200 )edit

You mean the labels and edges in the plot? Or what do you mean by merge?

fidbc gravatar imagefidbc ( 2013-03-28 14:24:06 +0200 )edit

yeah because it is difficult to read the labels from the edges.so is there any code so that labels will be clear?

REKHA BISWAL gravatar imageREKHA BISWAL ( 2013-03-28 14:27:52 +0200 )edit
1

answered 2013-03-28 13:50:44 +0200

DSM gravatar image

It's not completely clear to me what you mean by "indexing" a graph edge. If you want to give them labels, you can do that using set_edge_label:

sage: D = DiGraph({ 0: [1,2,3], 1: [0,2], 2: [3], 3: [4], 4: [0,5], 5: [1] })
sage: D.edges()
[(0, 1, None),
 (0, 2, None),
 (0, 3, None),
 (1, 0, None),
 (1, 2, None),
 (2, 3, None),
 (3, 4, None),
 (4, 0, None),
 (4, 5, None),
 (5, 1, None)]
sage: for i, (u, v, l) in enumerate(D.edges()):                                                            
....:     D.set_edge_label(u, v, "edge # {}".format(i))
....:     
sage: D.edges()
[(0, 1, 'edge # 0'),
 (0, 2, 'edge # 1'),
 (0, 3, 'edge # 2'),
 (1, 0, 'edge # 3'),
 (1, 2, 'edge # 4'),
 (2, 3, 'edge # 5'),
 (3, 4, 'edge # 6'),
 (4, 0, 'edge # 7'),
 (4, 5, 'edge # 8'),
 (5, 1, 'edge # 9')]
sage: D.show(edge_labels=True)

produces

image description

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2013-03-28 13:32:13 +0200

Seen: 432 times

Last updated: Mar 28 '13