Reputation: 2915
import networkx as nw
g=nw.random_regular_graph(3,10)
f=nw.dfs_tree(g,0)
print f
I want to generate a random graph and use BFS to obtain the diameter of the graph. I use above code but nothing displayed. What's the problem here? New to Python, hope someone could help me, thanks.
Upvotes: 0
Views: 915
Reputation: 1017
If you want to display the graph, you should use the Networkx function draw(). This function should be used with matplotlib.
So, here is your code with added function to display the graph:
import networkx as nw
import matplotlib.pyplot as plt
g=nw.random_regular_graph(3,10)
f=nw.dfs_tree(g,0)
nw.draw(g)
plt.show()
This code gives this following graph:
Read the links I provided you in order to personalize what you want to display in the graph.
Upvotes: 1
Reputation: 19328
You won't get anything displayed by print
a Graph.
Suppose you intend to display every edge of f
:
>>> g = nw.random_regular_graph(3, 10)
>>> f = nw.dfs_tree(g, 0)
>>> f
<networkx.classes.digraph.DiGraph object at 0x00000000030AA128>
>>> f.edges()
[(0, 9), (1, 3), (1, 7), (2, 1), (4, 6), (5, 4), (6, 2), (8, 5), (9, 8)]
Read the NetworkX Tutorial and you'll gain basic knowledge of how to use networkx
Upvotes: 2