Igraph Random Graph
Igraph Random Graph
I want to create a graph in python using Igraph. I did not create the edges. I want to know how to create the random edges between the nodes that have already been created. I tried to use Graph.GRG
but it did not work.
Graph.GRG
g.add_vertices(3)
g.add_vertices(3)
1 Answer
1
g.add_edges()
is what you need. This method takes a list of pairs of vertex numbers. Here is a simple example:
g.add_edges()
from igraph import *
import random
## Generate graph with 8 vertices and no edges
g = Graph()
g.add_vertices(8)
## Now generate random edges
random.seed(123)
RandEdges =
for x in range(1, 13):
RandEdges.append(random.sample(range(0,g.vcount()), 2))
RandEdges
[[0, 2],
[1, 6],
[6, 2],
[1, 6],
[0, 3],
[5, 2],
[0, 1],
[2, 7],
[5, 7],
[3, 1],
[0, 3],
[1, 4]]
With this format, you can add the edges.
## Add edges and plot
g.add_edges(RandEdges)
plot(g)
Some additional examples of adding edges are available in the igraph tutorial
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.