Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add edge attribute based on another network

I have two networks of same vertex based on different criteria. I want to add edge attributes of one of the network based on the connection of the other network. That is, if node A and B are connected in network 2, I want to note down "1" as the attribute in network 1, if not connected, note down "0". I am wondering how can I achieve my goal with R package or other software? Any suggestion is welcomed. Thanks a lot for suggestion!

like image 337
Jingy Niu Avatar asked Jul 14 '26 17:07

Jingy Niu


1 Answers

You can do this in R using the igraph package. Since you do not provide any data, I will make up an example.

Example Data

library(igraph)

set.seed(1234)
g1=erdos.renyi.game(10, 0.35)
g2=erdos.renyi.game(10, 0.35)
par(mfrow=c(1,2))
plot(g1)
plot(g2)

Two example graphs

Now we can create the edge attribute that you want. We initialize all values to zero, then loop through each edge in g2. If the same edge occurs in g1, we change the attribute to be 1.

E(g2)$net1 = 0
for(e in E(g2)) {
    if(are_adjacent(g1, ends(g2,e)[1], ends(g2,e)[2])) {
        E(g2)$net1[e] = 1 }
}

E(g2)$net1
[1] 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0
E(g2)[which(E(g2)$net1 > 0)]
+ 4/19 edges from 3bdc176:
[1] 3--4 4--5 4--6 5--7

You can see that the attribute net1 says that the shared links are:
3--4 4--5 4--6 5--7
which agrees with the plot.

like image 111
G5W Avatar answered Jul 16 '26 10:07

G5W



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!