Reputation: 181
I have a Graph G1 with 50 nodes and 100 edges. All edges are weighted. I have created a list of edges (sorted by a pre-defined order, removing specific edges with large values), and they are indexed like:
Edgelist: [75, 35, 32, 1, ...]
I want to add the edges to a different graph G2 in batches of 10 (to save computation time), but add.edges seems to want a tuple list of vertex pairs. So,
How can I convert the Edge list above into a tuple list, e.g. [(40,2),(10,1),(10,11),(0,0),...]. I've tried a loop with G1.es[edge].tuple, but iGraph reads the [edge] variable as an attribute, whereas if you just write G1.es[75].tuple, it works fine.
How can I look up weights from G1 and add them to G2 in batches of 10?
Upvotes: 1
Views: 2821
Reputation: 48051
You have to be aware that indexing G1.es
with a single number will return an object of type Edge
, while indexing it with a list of numbers will return an object of type EdgeSeq
. Edge
objects have a property named tuple
, but EdgeSeq
objects don't, so that's why G1.es[edgelist].tuple
does not work However, you can do this:
sorted_tuples = [edge.tuple for edge in G1.es[edgelist]]
You can also extract the value of the weight
attribute directly from the EdgeSeq
object:
sorted_weights = G1.es[edgelist]["weight"]
Here you can make use of the fact that if G2
has M edges and you add m extra edges, then the IDs of these new edges will be in the range from M (inclusive) to M+m (exclusive):
M = G2.ecount()
m = len(sorted_tuples)
G2.add_edges(sorted_tuples)
G2.es[M:(M+m)] = sorted_weights
Upvotes: 1
Reputation: 181
1) Graph G1 has had unwanted edges deleted already. Edgelist is the edge order for G1.
tuple_list=[]
for e in G1.es:
tuple_list.append(e.tuple)
sorted_tuples=[tuple_list[i] for i in Edgelist]
sorted_weights = [G1.es['weight'][o] for o in Edgelist]
2) Add edges - this can simply be looped for all edges in G1. Example below for first 10.
edges_to_add=sorted_tuples[0:10]
weights_to_add=sorted_weights[0:10]
G2.add_edges(edges_to_add)
for edge in range(len(edges_to_add)):
G2.es[G2.get_eid(edges_to_add[edge][0],edges_to_add[edge][1],0)]['weight'] = weights_to_add[edge]
Edge weights are added individually, which is a little slow, but there doesn't seem to be a way of adding edge weights in a batch in iGraph
Upvotes: 0