如何使用 igraph 创建循环图?

How do I create a cycle graph with igraph?

如何在 R 或 Python 中使用 igraphn 节点上创建循环图?

它应该看起来像这样,但是对于任意数量的节点:


编辑:由于要求我添加代码尝试,因此我发现 R 中的一种可能性太复杂了:

n <- 5 
make_graph( c(tail(rep(1:n, each=2), -1), 1) )

在 R 中有 n 个顶点:

n <- 8

seq_len(n) %>%
  {c(head(., -1), tail(., -1))} %>%
  matrix(ncol = 2) %>%
  rbind(c(n, 1)) %>%
  graph_from_edgelist(directed = FALSE)

对于python,您需要安装:

pip install python-igraph
pip install pycairo

然后您可以使用 igraph 中的 Graph.Ring,如下所示:

import igraph as ig
nodes = 5
g = ig.Graph.Ring(n=nodes, circular=True)
ig.plot(g,vertex_label = range(1,nodes+1))

在R中,相关的igraph函数是make_ring()。您可以在文档中阅读:

Create a ring graph Description A ring is a one-dimensional lattice and this function is a special case of make_lattice.

Usage make_ring(n, directed = FALSE, mutual = FALSE, circular = TRUE)


输出:

如果您将 igraphR

一起使用,请尝试 make_ring
plot(make_ring(5))