Generating distinct groups of nodes in a network

后端 未结 1 522
悲哀的现实
悲哀的现实 2021-01-01 00:47

The Issue

Given the following network of nodes and edges, I would like to derive all possible groupings of nodes where all nodes within a group are

相关标签:
1条回答
  • 2021-01-01 01:27

    Convert your data frame representation of the network to an igraph object. Use max_cliques to find "all the maximal cliques in an undirected graph".

    library(igraph)
    g <- graph_from_data_frame(df, directed = FALSE)
    mc <- max_cliques(g, min = 1)
    mc
    # [[1]]
    # + 1/6 vertex, named, from eb2aa45:
    # [1] A
    # 
    # [[2]]
    # + 2/6 vertices, named, from eb2aa45:
    # [1] D E
    # 
    # [[3]]
    # + 2/6 vertices, named, from eb2aa45:
    # [1] D B
    # 
    # [[4]]
    # + 3/6 vertices, named, from eb2aa45:
    # [1] B F C
    

    Grab the names of the vertices of the maximal cliques. Create corresponding group numbers and convert to data frame:

    nm <- lapply(mc, attr, "names")
    d <- data.frame(g = rep(seq_len(length(nm)), lengths(nm)), vert = unlist(nm))
    d
    #   g vert
    # 1 1    A
    # 2 2    D
    # 3 2    E
    # 4 3    D
    # 5 3    B
    # 6 4    B
    # 7 4    F
    # 8 4    C
    

    simplify graph, plot it, highlight vertex groups using the list above in mark.groups. Prettify according to taste (see ?plot.igraph).

    plot(simplify(g), mark.groups = nm, mark.border = "red", mark.col = NA)
    

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