简体   繁体   中英

How to split the graph comprised of several components and calculate its closeness with R?

I want to get the closeness of a graph, but sometimes the graph is comprised by several components which disconnected with each other. So the more appropriate method is to calculate the closeness by each component. components() can return the maximal connected components of a graph and also the numeric vector giving the cluster id to which each vertex belongs. But how to use those information to calculate the closeness by components?

For example,there are a graph of 8 nodes belong to two separate components, with 6 nodes(1-6) having at least one connection to the larger component and 2 nodes(7 and 8) belong to the smaller one and having no connection to the larger component

图1

and the paired data(JFE2015.txt) is as flow

1   2
1   6
2   3
2   4
4   5
4   6
5   6
7   8

when i use the components(), it return the following value

图2

My confusion is how to use the $membership to group the graph and calculate the closeness separately, and then combined the closeness from different components.

In real world, it is common to see a graph comprised of many components, so a loop is necessary when grouping to calculate the closeness.

Many Thanks!

Instead of components , here I think decompose would be a better option for your objective, since it directly splits your graph into disconnected subgraphs, and then you can apply closeness over each component, ie,

Map(
  closeness,
  decompose(
    graph_from_data_frame(
      df,
      directed = FALSE
    )
  )
)

which gives

[[1]]
         1          2          4          5          6          3
0.12500000 0.14285714 0.14285714 0.11111111 0.12500000 0.09090909

[[2]]
7 8
1 1

Data

> dput(df)
structure(list(from = c(1L, 1L, 2L, 2L, 4L, 4L, 5L, 7L), to = c(2L, 
6L, 3L, 4L, 5L, 6L, 6L, 8L)), class = "data.frame", row.names = c(NA,
-8L))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM