ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 28 Mar 2013 08:27:52 -0500code for bipartite graphshttp://ask.sagemath.org/question/9951/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], 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?Thu, 28 Mar 2013 07:32:13 -0500http://ask.sagemath.org/question/9951/code-for-bipartite-graphs/Answer by DSM for <p>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?</p>
http://ask.sagemath.org/question/9951/code-for-bipartite-graphs/?answer=14693#post-id-14693It'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](/upfiles/13644930378526317.png)
Thu, 28 Mar 2013 07:50:44 -0500http://ask.sagemath.org/question/9951/code-for-bipartite-graphs/?answer=14693#post-id-14693Answer by tmonteil for <p>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?</p>
http://ask.sagemath.org/question/9951/code-for-bipartite-graphs/?answer=14678#post-id-14678Instead 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)Thu, 28 Mar 2013 07:51:54 -0500http://ask.sagemath.org/question/9951/code-for-bipartite-graphs/?answer=14678#post-id-14678Comment by REKHA BISWAL for <p>Instead of a dictionary, do a dictionary of dictionaries specifying the labels of the edges. In your example:</p>
<pre><code>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} })
</code></pre>
<p>Note that you can label edges by strings as well if you want.</p>
<p>When plotting, if you want to see the labels, you should do:</p>
<pre><code>sage: D.plot(edge_labels=True)
</code></pre>
http://ask.sagemath.org/question/9951/code-for-bipartite-graphs/?comment=18007#post-id-18007but 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?Thu, 28 Mar 2013 08:03:38 -0500http://ask.sagemath.org/question/9951/code-for-bipartite-graphs/?comment=18007#post-id-18007Comment by fidbc for <p>Instead of a dictionary, do a dictionary of dictionaries specifying the labels of the edges. In your example:</p>
<pre><code>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} })
</code></pre>
<p>Note that you can label edges by strings as well if you want.</p>
<p>When plotting, if you want to see the labels, you should do:</p>
<pre><code>sage: D.plot(edge_labels=True)
</code></pre>
http://ask.sagemath.org/question/9951/code-for-bipartite-graphs/?comment=18006#post-id-18006You mean the labels and edges in the plot? Or what do you mean by merge?Thu, 28 Mar 2013 08:24:06 -0500http://ask.sagemath.org/question/9951/code-for-bipartite-graphs/?comment=18006#post-id-18006Comment by REKHA BISWAL for <p>Instead of a dictionary, do a dictionary of dictionaries specifying the labels of the edges. In your example:</p>
<pre><code>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} })
</code></pre>
<p>Note that you can label edges by strings as well if you want.</p>
<p>When plotting, if you want to see the labels, you should do:</p>
<pre><code>sage: D.plot(edge_labels=True)
</code></pre>
http://ask.sagemath.org/question/9951/code-for-bipartite-graphs/?comment=18005#post-id-18005yeah because it is difficult to read the labels from the edges.so is there any code so that labels will be clear?Thu, 28 Mar 2013 08:27:52 -0500http://ask.sagemath.org/question/9951/code-for-bipartite-graphs/?comment=18005#post-id-18005