adj_mat = rbind(c(0,2,1,0,0,0,0), # A
c(2,0,1,0,0,0,0), # B
c(1,0,0,1,0,0,0), # C
c(0,0,1,0,2,0,0), # D
c(0,0,0,2,0,2,1), # E
c(0,0,0,1,2,0,1), # F
c(0,0,0,0,1,1,0) # G
)
rownames(adj_mat) = c("A", "B", "C", "D", "E", "F", "G")
colnames(adj_mat) = c("A", "B", "C", "D", "E", "F", "G")
ig = graph.adjacency(as.matrix(adj_mat), mode="undirected",weighted=TRUE)
G=vector(mode="list", length=7)
G[seq_len(length(G))] = 0
names(G) = c("A", "B", "C", "D", "E", "F", "G")
startNode = "A"
visitedNodes = c("B", "C")
coords = layout.fruchterman.reingold(ig)
V(ig)$x = coords[,1]
V(ig)$y = coords[,2]
adj_matAfter = graph.connectToExt(adj_mat, startNode, visitedNodes)
Run the code above in your browser using DataLab