MansaSeyi
MansaSeyi

Reputation: 11

How can I convert an undirected graph generated by the Barabasi-Albert model to a directed one?

I have used the Barabasi-Albert model in networkx to generate a graph with n=200 and m=2. This gives me an undirected graph but I want a directed graph so that I can plot the in and out degree distributions. Would plotting the in and out degrees be possible?

This is my code:

N=200
m=2
G_barabasi=nx.barabasi_albert_graph(n=N,m=m)

Upvotes: 1

Views: 429

Answers (1)

SultanOrazbayev
SultanOrazbayev

Reputation: 16561

An easy way is to use the .to_directed method on the existing graph:

from networkx import barabasi_albert_graph
G = barabasi_albert_graph(200, 2)
print(G.is_directed())
# False
G_directed = G.to_directed()
print(G_directed.is_directed())
# True

Upvotes: 0

Related Questions