ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 22 Apr 2013 09:33:13 +0200code for tail and head of an edge in a bipartite graphhttps://ask.sagemath.org/question/10050/code-for-tail-and-head-of-an-edge-in-a-bipartite-graph/how to write a code for finding tail and head of an edge in a bipartite graph?Mon, 22 Apr 2013 08:05:23 +0200https://ask.sagemath.org/question/10050/code-for-tail-and-head-of-an-edge-in-a-bipartite-graph/Answer by Jesustc for <p>how to write a code for finding tail and head of an edge in a bipartite graph?</p>
https://ask.sagemath.org/question/10050/code-for-tail-and-head-of-an-edge-in-a-bipartite-graph/?answer=14818#post-id-14818You may want to take a look to [this](http://www.sagemath.org/doc/reference/graphs/index.html), and in particular [this](http://www.sagemath.org/doc/reference/graphs/sage/graphs/bipartite_graph.html). Good luck!
**UPDATE**
Let $G$ be a bipartite graph that you have generated using one of the ways commented in the 2nd link above. Now you want to take an edge and get its head an tail. In order to do that, you can just identify the sets of tails and heads with the "bipartition" method of the graph, and check which of the vertices of an edge is in which set. It could be something like
tail_set, head_set = G.bipartition()
print "Set of tails: ",tail_set
print "Set of heads: ",head_set
for edge in G.edges():
v1, v2 = edge[:2]
tail = (v1 if v1 in tail_set else v2)
head = (v1 if tail == v2 else v2)
print "Edge: ",edge
print " Tail: ",tail,", Head: ",head
Mon, 22 Apr 2013 09:00:48 +0200https://ask.sagemath.org/question/10050/code-for-tail-and-head-of-an-edge-in-a-bipartite-graph/?answer=14818#post-id-14818Comment by REKHA BISWAL for <p>You may want to take a look to <a href="http://www.sagemath.org/doc/reference/graphs/index.html">this</a>, and in particular <a href="http://www.sagemath.org/doc/reference/graphs/sage/graphs/bipartite_graph.html">this</a>. Good luck!</p>
<p><strong>UPDATE</strong></p>
<p>Let $G$ be a bipartite graph that you have generated using one of the ways commented in the 2nd link above. Now you want to take an edge and get its head an tail. In order to do that, you can just identify the sets of tails and heads with the "bipartition" method of the graph, and check which of the vertices of an edge is in which set. It could be something like</p>
<pre><code>tail_set, head_set = G.bipartition()
print "Set of tails: ",tail_set
print "Set of heads: ",head_set
for edge in G.edges():
v1, v2 = edge[:2]
tail = (v1 if v1 in tail_set else v2)
head = (v1 if tail == v2 else v2)
print "Edge: ",edge
print " Tail: ",tail,", Head: ",head
</code></pre>
https://ask.sagemath.org/question/10050/code-for-tail-and-head-of-an-edge-in-a-bipartite-graph/?comment=17831#post-id-17831i looked to that but could not find proper code which i wanted..Mon, 22 Apr 2013 09:33:13 +0200https://ask.sagemath.org/question/10050/code-for-tail-and-head-of-an-edge-in-a-bipartite-graph/?comment=17831#post-id-17831