Search code examples
rigraphgraph-theorysimilarityedge-list

Similarity as edge attribute in iGraph


Lets say we have some graph g and then we compute some similarity between each pair of nodes from g:

g <- graph.ring(10)
g_sim <- similarity.dice(g)

The question is: how to use data from g_sim to assign weights to the edges from g? In other words: if there is and edge A--B in g, I want to have an attribute for that edge that is equal to value that we have for the pair A,B in g_sim.


Solution

  • In theory you could do

    g <- graph.ring(10)
    g_sim <- similarity.dice(g)
    el <- get.edgelist(g)
    E(g)$weight <- g_sim[el]
    

    But in this case similarity.dice isn't a good measure. If you compare adjacency (where edges actually exist) to the non-zero similarity values

    get.adjacency(g)
    #  [1,] . 1 . . . . . . . 1
    #  [2,] 1 . 1 . . . . . . .
    #  [3,] . 1 . 1 . . . . . .
    #  [4,] . . 1 . 1 . . . . .
    #  [5,] . . . 1 . 1 . . . .
    #  [6,] . . . . 1 . 1 . . .
    #  [7,] . . . . . 1 . 1 . .
    #  [8,] . . . . . . 1 . 1 .
    #  [9,] . . . . . . . 1 . 1
    # [10,] 1 . . . . . . . 1 .
    
    g_sim
    #       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
    #  [1,]  1.0  0.0  0.5  0.0  0.0  0.0  0.0  0.0  0.5   0.0
    #  [2,]  0.0  1.0  0.0  0.5  0.0  0.0  0.0  0.0  0.0   0.5
    #  [3,]  0.5  0.0  1.0  0.0  0.5  0.0  0.0  0.0  0.0   0.0
    #  [4,]  0.0  0.5  0.0  1.0  0.0  0.5  0.0  0.0  0.0   0.0
    #  [5,]  0.0  0.0  0.5  0.0  1.0  0.0  0.5  0.0  0.0   0.0
    #  [6,]  0.0  0.0  0.0  0.5  0.0  1.0  0.0  0.5  0.0   0.0
    #  [7,]  0.0  0.0  0.0  0.0  0.5  0.0  1.0  0.0  0.5   0.0
    #  [8,]  0.0  0.0  0.0  0.0  0.0  0.5  0.0  1.0  0.0   0.5
    #  [9,]  0.5  0.0  0.0  0.0  0.0  0.0  0.5  0.0  1.0   0.0
    # [10,]  0.0  0.5  0.0  0.0  0.0  0.0  0.0  0.5  0.0   1.0
    

    You will see that all the edges will be given weight 0. The similarity.dice measure is twice the number of common neighbors divided by the sum of the degrees of the vertices. Apparently the vertices themselves are excluded from the calculation. So that means two connected vertices on a circle share no neighbors so they would have weights of 0. So everything is working properly, just similarity.dice isn't a good choice to weight the edges of this graph because all the weights would be 0.

    Thanks to @user368090, here's one that looks better

    g <- graph.famous("Krackhardt_Kite")
    g_sim <- similarity.dice(g)
    el <- get.edgelist(g)
    E(g)$weight <- g_sim[el]
    plot(g, edge.width=E(g)$weight*10)
    

    weighted Krackhardt_Kite plot