Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Graph-tool: subgraphs as new Graph objects

I find graph-tool documentation extremely obscure and much more cryptic than other analogous libraries.

I really can't figure out how to "extract" components (aka connected components) from a graph in graph-tools. I would like to save subgraphs in separate files as separate Graphs but I don't understand how to identify them starting from my Graph object.

like image 403
Marco Necci Avatar asked Jan 23 '17 16:01

Marco Necci


People also ask

What are Subgraphs in graph theory?

Definition: A graph whose vertices and edges are subsets of another graph.

What is a subgraph example?

A subgraph H = (V ,E ) of a graph G = (V,E) is a pair V ⊆ V and E ⊆ E. We say that H is an induced subgraph of G if all the edges between the vertices in V from E are in E . Example Figure 4 shows two subgraphs of G1. The first subgraph is an induced subgraph.

What is a subgraph and when do you use it?

A subgraph extracts data from a blockchain, processing it and storing it so that it can be easily queried via GraphQL. The subgraph definition consists of a few files: subgraph. yaml : a YAML file containing the subgraph manifest.


1 Answers

The simplest (and fastest) way to do this is to do use a GraphView object.

# label the components in a property map
c = label_components(g)[0]

# "extract" component number 3
u = GraphView(g, vfilt=c.a == 3)

The object u is now an induced subgraph of g that contains all the vertices of the component label 3. Note that a GraphView object does not copy the graph, it simply masks out the other vertices/edges.

If you wish a copy of the subgraph (e.g. if you want to modify it, or the original graph), you just instantiate a new Graph object from it:

u = Graph(u, prune=True)

More information on graph views is available in the documentation: https://graph-tool.skewed.de/static/doc/quickstart.html#graph-views

like image 104
Tiago Peixoto Avatar answered Oct 18 '22 01:10

Tiago Peixoto