Joy
Joy

Reputation: 41

Assign the nodes attributes to edge weights using igraph

I have a directed graph that ends to point '12'. I have calculated all the nodes attribute values based on the out-degree distribution. Now I am not able to assign the node's attribute value to its edges as weights. The help in this regard would be highly appreciated..

Please see the graph attached here.

enter image description here

These are my sample codes.

nodes <- read.csv("test_nodes1.csv", header=T, as.is=T)
links <- read.csv("test_edge1.csv", header=T, as.is=T)
links <- links[order(links$from, links$to),]
G <- graph_from_data_frame(d=links, vertices=nodes$id, directed=T)


V(G)$dist <- 1 

for (i in V(G)$name) {

  out_deg_i <- degree(G,i, mode = "out")

  if (out_deg_i >1){

    V(G)[i]$dist = V(G)[i]$dist/out_deg_i
  }
  else{
    V(G)[i]$dist =1
  }
 }

 V(G)$name
 V(G)$dist
 
 [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12" "13"
 [1] 1.0 0.5 1.0 1.0 1.0 1.0 1.0 1.0 1.0 0.5 1.0 1.0 1.0
 

Upvotes: 2

Views: 442

Answers (1)

ThomasIsCoding
ThomasIsCoding

Reputation: 101343

I guess you need tail_of like below

G <- set_edge_attr(
    G,
    name = "wt",
    value = 1 / pmax(degree(G, mode = "out"), 1)[names(tail_of(G, E(G)))]
)

and you can check the result via

plot(G, edge.label = E(G)$wt)

enter image description here

Data

G <- graph_from_data_frame(
    data.frame(
        from = c(1, 2, 4, 4, 4),
        to = c(4, 4, 3, 5, 6)
    )
)

Upvotes: 2

Related Questions