How Igraph handle weights?

前端 未结 2 895
有刺的猬
有刺的猬 2021-01-25 02:44

Good day to everyone.

I have a really simple question that I wasn\'t able to find an answer, because of lack of terminology I am afraid. In r\'s package igraph how are c

相关标签:
2条回答
  • 2021-01-25 03:26

    In igraph, weights are edge-attributes that represent the friction or cost of traveling that edge in a parth, not the capacity or bandwidth of the edge. Low weight makes for a low weight-sum of a path and get.shortest.paths() returns the path with the lowest weight-sum when run without disabling weights on a weighted graph.

    This code example shows a graph with different shortest paths in weighted and unweighted mode, and explains why the path is differently calculated.

    library(igraph)
    
    # Colours for the weighted edges
    N <- 22
    set.seed(7890123)
    
    # Make a random graph with randomly weighted edges coloured in gray
    g <- erdos.renyi.game(N, .2, type="gnp", directed=F, loops=F, weighted=T)
    E(g)$weight <- sample(1:40, length(E(g)), replace=T)
    #E(g)$weight <- E(g)$weight/10
    E(g)$color <- "gray"
    V(g)$size <- 4
    V(g)$size[c(1,N)] <- 12
    
    # Look how the shortest path is calculated differently when taken the graph weihgt into acocunt
    (weighted.path <- unlist(get.shortest.paths(g, 1, N)$vpath) )
    (unweighted.path <- unlist(get.shortest.paths(g, 1, N, weights=NA)$vpath) )
    
    # Set weights and colours of shortest paths to visualise them
    E(g, path=weighted.path)$color <- "red"
    E(g, path=unweighted.path)$color <- "green"
    
    # plot the graph with red shortest weighted path, and green shortest path
    same.sahpe <- layout_with_fr(g)
    plot(g, vertex.color="white", vertex.label=NA, edge.weight=2, edge.width=(E(g)$weight/5), layout=same.sahpe)
    
    # The two paths look like this. Even though path-length might be longer, a weighted
    # shortest path is determined using the sum of path-weights. As with path-lengths, the
    # lowest value is the path most easily travelled by. In this case, the weighted alternative
    # has a longer path but with lower friction.
    data.frame(path.length=c(length(weighted.path),
                            length(unweighted.path)
                            ),
               weight.sum=c(sum(E(g, path=unlist(weighted.path))$weight),
                            sum(E(g, path=unlist(unweighted.path))$weight)
               )
    )
    

    See the shortest unweighted path between the two larger vertices has the length of 4, but travels over rather thickly weighted green edges. The shortest weighted path has the lowest weight-sum and travels more steps (5) but over wedges with lower weight resulting in a lower weight-sum, or lower cost of travelling the parth, if you like.

    0 讨论(0)
  • 2021-01-25 03:34

    If you have a look at the official documentation in the R igraph package - https://igraph.org/r/doc/strength.html

    you will find that weights are referred to as:

    "Weight vector. If the graph has a weight edge attribute, then this is used by default. If the graph does not have a weight edge attribute and this argument is NULL, then a warning is given and degree is called."

    But also:

    https://igraph.org/r/doc/edge_attr.html

    explains that the edges are given as a weight attribute

    0 讨论(0)
提交回复
热议问题