# code for bipartite graphs

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: , 4: [0,5], 5:  }) 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 close merge delete

Sort by » oldest newest most voted 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: , 4: [0,5], 5:  })
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 more

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)

more

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?

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

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