2

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 considered weights? Are they considered a cost, therefore reducing the capacity of an edge or are they considered indeed as the capacity of the edge?

Thank you very much

S. Mrtz
  • 33
  • 1
  • 4

2 Answers2

6

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.

enter image description here

nJGL
  • 819
  • 5
  • 17
  • Thank you very much. So if I want to use the bandwidth in igraph, lets say a vector bw= [1 , 5, 7, 2], where of course the third edge is the one with the largest bandwidth, I just have to put it as a weight in this way bw= [-1, -5, -7, -2], am I correct? Thank you again. – S. Mrtz Jan 18 '19 at 09:34
  • Or I can simply swaping the value. From bw=[1, 5, 7, 2] to bw= [7, 2, 1, 5]. – S. Mrtz Jan 18 '19 at 09:48
  • I'd go with the inverted example. In a graph `g` you can turn the weights upside down, like you suggest above, using `E(g)$weight <- max(E(g)$weight) - E(g)$weight`. – nJGL Jan 18 '19 at 13:17
  • nJGL assuming by "inverted example" the second alternative i proposed, I thank you deeply. All the Best. S.Mrtz – S. Mrtz Jan 18 '19 at 15:41
  • Hello everyone, does anyone if the two nodes connected by the green line are naturally"bigger"? Or have they just been enlarged to show the path between them. Can someone please take a look at this question: https://stackoverflow.com/questions/64870651/r-language-understanding-what-is-a-weighted-graph – stats_noob Nov 17 '20 at 14:46
  • I made the two nodes bigger to illustrate that the green and red paths run *between* them. All nodes get the *node attribute* of `size` set to 4: `V(g)$size <- 4` and then the two nodes (`1` and `N`) are enlarged `V(g)$size[c(1,N)] <- 12` – nJGL Nov 18 '20 at 10:33
0

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

  • I am deeply sorry but I did not understand your answer. I looked the documentatio, more than once, but I still couldn understand if weights are costs to pass through an edge or if those are the capacity of an edge. – S. Mrtz Jan 17 '19 at 10:26
  • passed as an attribute of the edge! –  Jan 17 '19 at 10:41