3
votes

I am learning python-igraph, and having difficulty in handling a graph which is divided to components (which are unconnected between them). When I apply one of the clustering algorithms on this graph it doesn't seem to work properly, and so I need to apply the algorithms to each subgraph (component) separately. So in order to maintain the identification of the vertices, I would like to add a vertex attribute that give me the id number in the original graph. My graph is constructed from a weighted adjacency matrix:

import numpy as np
import igraph

def symmetrize(a):
    return a + a.T - 2*np.diag(a.diagonal())

A = symmetrize(np.random.random((100,100)))

G = igraph.Graph.Adjacency(A.tolist(),attr="weight",mode="UPPER")

I see that there should be a way to add vertex attributes, but I don't understand how to use it..

1

1 Answers

5
votes

Adding a vertex attribute to all of the vertices works like this:

G.vs["attr"] = ["id1", "id2", "id3", ...]

You can also attach a vertex attribute to a single vertex:

G.vs[2]["attr"] = "id3"

For instance, if you simply need a unique identifier to all your vertices, you can do this:

G.vs["original_id"] = list(range(G.vcount()))

(You don't need the list() part if you are on Python 2.x as range() already produces a list).