Hi Tamas,

Thank you very much for your reply and your suggestion. Beyond the vertex
label, the only other attribute we will use is the edge weight.

I will try to implement the solution you gave us for vertex label. If it
works fine for what we need, we can try to add the edge weight later.

A last question: I suppose the same limitation is observed for
igraph.Graph.difference, right?

Do you think I could open an issue in igraph github asking for this change
in the implementation of union (and difference) in Python?

Best,

Charles

On 22 September 2015 at 11:24, Tamas Nepusz <[email protected]> wrote:

> Hi Charles,
>
> Unfortunately, graph union works differently in the R interface and in
> the Python interface.
>
> The native C core of igraph supports graph union only by IDs and not
> by vertex names. The R interface contains an implementation of "graph
> union by vertex names", and it is selected automatically if your graph
> has vertex names. There is no equivalent function in the Python
> interface as I did not have time to implement something similar yet.
> If you have no other vertex or edge attributes to preserve, you could
> probably roll your own as follows:
>
> def vertex_name_index(graph):
>     return dict((v, k) for k, v in enumerate(graph.vs["name"]))
>
> def graph_union_by_name(g1, g2):
>     result_names = sorted(set(g1.vs["name"]) + set(g2.vs["name"]))
>     result = Graph(len(result_names))
>     result.vs["name"] = names
>     result_name_index = vertex_name_index(graph)
>     edges = []
>     for edge in g1:
>         src = result_name_index[g1.vs[edge.source]["name"]]
>         dest = result_name_index[g1.vs[edge.target]["name"]]
>         edges.append((src, dest))
>     for edge in g2:
>         src = result_name_index[g2.vs[edge.source]["name"]]
>         dest = result_name_index[g2.vs[edge.target]["name"]]
>         edges.append((src, dest))
>     result.add_edges(edges)
>     return result
>
> The code above is totally untested but I think that it should work
> with minor modifications at worst.
>
> All the best,
> T.
> T.
>
>
> On Mon, Sep 21, 2015 at 11:33 PM, Charles Novaes de Santana
> <[email protected]> wrote:
> > Just found that the function graph.union does the "sum" I want to do.
> But,
> > again, I only can run this function correctly in R, not in Python.
> >
> > Given the code in R:
> >
> > library(igraph)
> > nodes1 = c("a","b","c")
> > nodes2 = c("c","d","e")
> > edges1 = matrix( c("a", "b", "b", "c","c","a"), nc = 2, byrow = TRUE)
> > edges2 = matrix( c("c", "d", "d", "e","e","c"), nc = 2, byrow = TRUE)
> > g1 = graph(edges1,directed=FALSE)
> > g2 = graph(edges2,directed=FALSE)
> >
> > g3 = graph.union(g1,g2);
> > plot(g3);
> >
> > Now the equivalent code in Python:
> >
> > import igraph
> > nodes1 = ["a","b","c"]
> > nodes2 = ["c","d","e"]
> > edges1 = [["a","b"],["b","c"],["c","a"]]
> > edges2 = [["c","d"],["d","e"],["e","c"]]
> > g1 = igraph.Graph(directed=False)
> > g2 = igraph.Graph(directed=False)
> > g1.add_vertices(nodes1)
> > g2.add_vertices(nodes2)
> > g1.add_edges(edges1)
> > g2.add_edges(edges2)
> >
> > g3 = igraph.Graph.union(g1,g2)
> > plot(g3)
> >
> > As you can see, the behaviour of the plot in Python is still different
> from
> > the one in R. Actually, it is worst than the one I had before, because
> now I
> > only can see 3 vertices. What am I missing here?
> >
> > Thanks again for any tip,
> >
> > Best,
> >
> > Charles
> >
> > On 21 September 2015 at 23:16, Charles Novaes de Santana
> > <[email protected]> wrote:
> >>
> >> Dear all,
> >>
> >> I am trying to plot a "sum of graphs" in R and in Python, using Igraph.
> I
> >> can do it correctly in R, but I am facing some problems to reproduce it
> in
> >> Python. I was wondering if you could help me with this.
> >>
> >> Please consider the following toy code in R:
> >>
> >> library(igraph)
> >> nodes1 = c("a","b","c")
> >> nodes2 = c("c","d","e")
> >> edges1 = matrix( c("a", "b", "b", "c","c","a"), nc = 2, byrow = TRUE)
> >> edges2 = matrix( c("c", "d", "d", "e","e","c"), nc = 2, byrow = TRUE)
> >> g1 = graph(edges1,directed=FALSE)
> >> g2 = graph(edges2,directed=FALSE)
> >>
> >> plot(g1+g2);
> >>
> >> you can see that in the plot(g1+g2) the vertex "c" is shared by both
> >> graphs. So actually I can plot one connected graph with the vertices
> >> "a","b","c","d","e".
> >>
> >> I would like to reproduce it in Python. The best way I think now is the
> >> following code, but it is failing:
> >>
> >> import igraph
> >> nodes1 = ["a","b","c"]
> >> nodes2 = ["c","d","e"]
> >> edges1 = [["a","b"],["b","c"],["c","a"]]
> >> edges2 = [["c","d"],["d","e"],["e","c"]]
> >> g1 = igraph.Graph(directed=False)
> >> g2 = igraph.Graph(directed=False)
> >> g1.add_vertices(nodes1)
> >> g2.add_vertices(nodes2)
> >> g1.add_edges(edges1)
> >> g2.add_edges(edges2)
> >>
> >> igraph.plot(g1+g2);
> >>
> >> you can see that the igraph.plot(g1+g2) does not consider that vertex
> "c"
> >> is shared by g1 and g2. Do you have any idea why these codes have
> different
> >> behaviour? I know I am building the graphs in different ways, but I was
> not
> >> able to build the graph in Python in the same way I do it in R.
> >>
> >> Sorry if it is a silly question (I used to program in R during some
> years,
> >> but I am just a beginner in Python). Thanks for any help or advice!
> >>
> >> Best,
> >>
> >> Charles
> >>
> >> --
> >> Um axé! :)
> >>
> >> --
> >> Charles Novaes de Santana, PhD
> >> http://www.imedea.uib-csic.es/~charles
> >
> >
> >
> >
> > --
> > Um axé! :)
> >
> > --
> > Charles Novaes de Santana, PhD
> > http://www.imedea.uib-csic.es/~charles
> >
> > _______________________________________________
> > igraph-help mailing list
> > [email protected]
> > https://lists.nongnu.org/mailman/listinfo/igraph-help
> >
>
> _______________________________________________
> igraph-help mailing list
> [email protected]
> https://lists.nongnu.org/mailman/listinfo/igraph-help
>



-- 
Um axé! :)

--
Charles Novaes de Santana, PhD
http://www.imedea.uib-csic.es/~charles
_______________________________________________
igraph-help mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/igraph-help

Reply via email to