To get a directional width search tree with node 4:
tree = nx.bfs_tree(G, 4)

To get a directed tree of the first search with node 4:
tree = nx.dfs_tree(G, 4)

Graphs were generated as follows:
import matplotlib.pyplot as plt
import networkx as nx
G = nx.Graph()
G.add_edge(1,2)
G.add_edge(2,3)
G.add_edge(3,5)
G.add_edge(4,6)
G.add_edge(1,6)
G.add_edge(2,6)
G.add_edge(7,8)
G.add_edge(9,8)
tree = nx.bfs_tree(G, 4)
nx.draw(tree)
plt.savefig('/tmp/bfs_image.png')
source
share