Build a graph as a subset of another, larger, graph [iGraph, Python] - python

I need to compute the density of a subgraph made of vertices belonging to the same attribute "group".
ie., let g be an iGraph graph,
g.vs.select(group = 1)
gives me an object with all vertices belonging to group 1
Is there any way to compute density on the graph which is formed by these vertices and the connections between them?
In a fashion similar to this maybe?
g2.vs(g2.vs.select(group = i)).density()

Try this:
g.vs.select(group=1).subgraph().density()

Related

How to create a Spektral graph with edge weights and edge features

I want to create a Graph in Spektral which has both edged weights and edge labels. I have three matrices: a nxn adjacency matrix, an nx4 feature matrix for the nodes, and an nxnx2 matrix which represents the label for each edge. I do not know how to incorporate this third matrix into my Graph so that the edges will be labeled. I would also be happy to label the edges manually, but I don't know how to do that either. Thanks!
The spektral.data.Graph data structure has an attribute called e to store edge attributes.
So, if you have your data stored into numpy arrays you can do:
g = Graph(x=x, a=a, e=e)
where e is your edge attributes matrix.
They will be picked up automatically by the data loaders, if you use them.
Check out the documentation here.

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.
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

How to create a graph with vertices weight in python with igraph?

I searched but found there are many examples about how to create a graph with edges weight, but none of them shows how to create a graph with vertices weight. I start to wonder if it is possible.
If a vertices-weighted graph can be created with igraph, then is it possible to calculate the weighted independence or other weighted numbers with igraph?
As far as I know, there are no functions in igraph that accept arguments for weighted vertices. However, the SANTA package that is a part of the Bioconductor suite for R does have routines for weighted vertices, if you are willing to move to R for this. (Seems like maybe you can run bioconductor in python.)
Another hacky option is the use (when possible) unweighted routines from igraph and then back in the weights. E.g. something like this for weighted maximal independent sets:
def maxset(graph,weight):
ms = g.maximal_independent_vertex_sets()
w = []
t = []
for i in range(0, 150):
m = weights.loc[weights['ids'].isin(ms[i]),"weights"]
w.append(m)
s = sum(w[i])
t.append(s)
return(ms[t.index(max(t))])
maxset(g,weights)
(Where weights is a two column data frame with column 1 = vertex ids and column 2 = weights). This gets the maximal independent set taking vertex weights into consideration.
You want to use vs class to define vertices and their attributes in igraph.
As example for setting weight on vertices, taken from documentation:
http://igraph.org/python/doc/igraph.VertexSeq-class.html
g=Graph.Full(3) # generate a full graph as example
for idx, v in enumerate(g.vs):
v["weight"] = idx*(idx+1) # set the 'weight' of vertex to integer, in function of a progressive index
>>> g.vs["weight"]
[0, 2, 6]
Note that a sequence of vertices are called through g.vs, here g the instance of your Graph object.
I suggested you this page, I found it practical to look for iGraph methods here:
http://igraph.org/python/doc/identifier-index.html

Removing cycles from an undirected multi graph using Python networkx

So I have a undirected multi graph (derived from an ontology), I wish to remove the edges that create cycles (but not all edges, the constituents of the multi graph have to remain connected). Is there a good way of doing this with the networkx package?
There may not be a unique way to do that for your graph. But maybe finding a spanning tree will solve your problem?
https://networkx.github.io/documentation/latest/reference/generated/networkx.algorithms.mst.minimum_spanning_tree.html
So I ended up with
def as_spanning_trees(G):
"""
For a given graph with multiple sub graphs, find the components
and draw a spanning tree.
Returns a new Graph with components as spanning trees (i.e. without cycles).
Parameters
---------
G: - networkx.Graph
"""
G2 = nx.Graph()
# We find the connected constituents of the graph as subgraphs
graphs = nx.connected_component_subgraphs(G, copy=False)
# For each of these graphs we extract the spanning tree, removing the cycles
for g in graphs:
T = nx.minimum_spanning_tree(g)
G2.add_edges_from(T.edges())
G2.add_nodes_from(T.nodes())
return G2

Separating two graphs based on connectivity and coordinates

I would like to separate existing data of vertices and edges into two or more graphs that are not connected. I would like to give the following as example:
Imagine two hexagons on top of each other but are lying in different Z.
Hexagon 1 has the following vertices A(0,0,1), B(1,0,2), C(2,1,2), D(1,2,1), E(0,2,1), F(-1,2,1). The connectivity is as following: A-B, B-C, C-D, D-E, E-F, F-A. This part of Graph 1 as all the vertices are connected in this layer.
Hexagon2 has the following vertices A1(0,0,6), B1(1,0,7), C1(2,1,7), D1(1,2,8), E1(0,2,7), F1(-1,2,6). The connectivity is as following: A1-B1, B1-C1, C1-D1, D1-E1, E1-F1, F1-A1. This is part of Graph 2
My data is in the following form: list of Vertices and list of Edges that i can form graphs with. I would like to eliminate graph 2 and give only vertices and connectivity of graph 1 to polygon determination part of my algorithm. My real data contains around 1000 connected polygons as graph 1 and around 100 (much larger in area) polygons as graph 2. I would like to eliminate graph 2.
The problem you're describing relates to connected components.
The Python Networkx module has functions for dealing with this type of graph problems. You're looking for the connected_components function which returns all of the components, you can then pick the appropriate one (possible by number of vertices).

Categories

Resources