Visualizing Distance Between Nodes According to Weights - with R

Visualizing larger weights with thinner lines

Why not to define the width as inversely related to the weight in some way? For instance, using

E(g)$width <- 3 - E(g)$weight * 10

gives

Sample Image

You may experiment further and replace 3 and/or 10 with some other coefficients to achieve the desired result.

How to get the longest flow matrix of a graph with r-igraph package?

If the maximum distance is computed first and as weights the distances are subtracted from the maximum, then the algorithm will compute the longest paths.

The two methods below are equivalent.

library(igraph)

max_dist <- max(E(G)$distance)
all_shortest_paths(G, from = 1, to = V(G), weights = max_dist - E(G)$distance)
get.all.shortest.paths(G, 1, to = V(G), weights = max_dist - E(G)$distance)

Notes:

  1. I have changed the name of the graph object to G, since F is also a symbol for FALSE.
  2. The graph is recreated, this time with the RNG set, in order to make the results reproducible.

Graph creation code.

set.seed(1234)

n <- 6 # number of vertices
G <- erdos.renyi.game(n, p.or.m = 0.7, directed = FALSE)
m <- ecount(G)
min <- 1 # 1 km
max <- 50 # 50 km
G <- set.edge.attribute(G, name = "distance", value = runif(m , min , max))

How Igraph handle weights?

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.

Sample Image

Mean distance vs. mean of disances in igraph

I think you are getting a different answer because you are averaging the entire distance matrix instead of the lower|upper triangle of distance(toy.graph) this includes a 0's on the diagonal which lower the distance

library(igraph)
toy.graph <- graph.formula(1-2,1-3,1-5,2-5,3-5,3-6,4-6)
plot(toy.graph)
mean_distance(toy.graph)
#[1] 1.866667

average.path.length(toy.graph)
#[1] 1.866667

mean(distances(toy.graph))
#[1] 1.555556

mean(distances(toy.graph)[lower.tri(distances(toy.graph))])
#[1] 1.866667


Related Topics



Leave a reply



Submit