如何绘制所选顶点和边总数

How to plot chosen vertices and total number of edges

我想绘制两个选定的节点及其所有边,而不仅仅是直接连接这两个节点的边。例如:

library(igraph)
o <- read.table(text="
    A   B   C   D   E   F   G
A   0   1   0   1   0   1   1
B   1   0   1   1   0   1   0
C   0   0   0   0   0   0   1
D   1   1   0   0   1   0   0
E   0   0   0   1   0   1   1
F   0   1   0   0   1   0   1
G   1   0   1   0   1   1   0", header=TRUE)

mat <- as.matrix(o)
g <- graph.adjacency(mat, mode="undirected", weighted = T, add.rownames = T)

我可以使用下面的代码选择 g 的两个节点,但绘图仅包含直接连接它们的边。我都要。

g2 <- induced_subgraph(g, c("A","E"))
plot(g2)

如何绘制两个顶点以及它们的 所有 边?另外,如何为每个顶点选择路径距离?

谢谢!

library(igraph)

o <- read.table(text="
                A   B   C   D   E   F   G
                A   0   1   0   1   0   1   1
                B   1   0   1   1   0   1   0
                C   0   0   0   0   0   0   1
                D   1   1   0   0   1   0   0
                E   0   0   0   1   0   1   1
                F   0   1   0   0   1   0   1
                G   1   0   1   0   1   1   0", header=TRUE)

mat <- as.matrix(o)
g <- graph.adjacency(mat, mode="undirected", weighted = T, add.rownames = T)
plot(g)

# get 1st connections of A and E (their friends)
vertices_input = c("A","E")                    # specify vertices of interest
ids = as.numeric(E(g)[from(vertices_input)])   # get the ids of the edges from g that correspond to those vertices
g2 = subgraph.edges(g, ids)                    # keep only those edges from g as a sub-graph g2
plot(g2)

# get up to 2nd connections of A and E (friends of friends)
nms = V(g2)$name                      # get names of vertices of your sub-graph g2 (main vertices and 1st connections)
ids = as.numeric(E(g)[from(nms)])     # get the ids of the edges from g that correspond to main vertices and 1st connections
g3 = subgraph.edges(g, ids)           # keep only those edges from g as a sub-graph g3
plot(g3)