Search code examples
pythongraphgraph-tool

efficient way of finding all unfiltered vertices of a filtered graph, in graph-tool library?


I'm using graph-tool for analyzing disease spread models on a graph. I want to have a filtered graph which only consists of the infected vertices, the vertices which are going to infect their neighbors.

the problem is that when I filter the infected vertices, the rest of the data is missing, I am aware of GraphView subclass, but the problem is that having the original graph stored somewhere doesn't help me when I'm iterating on the edges of the filtered graph, since I don't have access the the equivalent of the vertex, and can't infect it's neighbors. (change their properties.)

is there an efficient way to help my task?

thanks.


Solution

  • Yes, you do have access to the vertices of the original graph, by using the Graph.vertex() method.

    Say if g is your original graph and u is the filtered one, you can do:

    for e in u.edges():
        v = e.source()
        v_orig = g.vertex(v)             # corresponding vertex in unfiltered graph
        for w in v_orig.out_neighbors():
            print(w)                     # neighbors in the unfiltered graph