user2535338
user2535338

Reputation: 385

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.

g.add_vertices(3)

Upvotes: 1

Views: 805

Answers (1)

G5W
G5W

Reputation: 37661

g.add_edges() is what you need. This method takes a list of pairs of vertex numbers. Here is a simple example:

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)

Graph with random edges

Some additional examples of adding edges are available in the igraph tutorial

Upvotes: 1

Related Questions