10. Graph generators and graph operations
In addition to constructing graphs node-by-node or edge-by-edge, they can also be generated by
- Applying classic graph operations, such as:
subgraph(G, nbunch) - induced subgraph view of G on nodes in nbunch
union(G1,G2) - graph union
disjoint_union(G1,G2) - graph union assuming all nodes are different
cartesian_product(G1,G2) - return Cartesian product graph
compose(G1,G2) - combine graphs identifying nodes common to both
complement(G) - graph complement
create_empty_copy(G) - return an empty copy of the same graph class
to_undirected(G) - return an undirected representation of G
to_directed(G) - return a directed representation of G
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
- Using a call to one of the classic small graphs, e.g.,
petersen = nx.petersen_graph()
tutte = nx.tutte_graph()
maze = nx.sedgewick_maze_graph()
tet = nx.tetrahedral_graph()
1
2
3
4
2
3
4
- Using a (constructive) generator for a classic graph, e.g.,
K_5 = nx.complete_graph(5)
K_3_5 = nx.complete_bipartite_graph(3, 5)
barbell = nx.barbell_graph(10, 10)
lollipop = nx.lollipop_graph(10, 20)
1
2
3
4
2
3
4
- Using a stochastic graph generator, e.g.,
er = nx.erdos_renyi_graph(100, 0.15)
ws = nx.watts_strogatz_graph(30, 3, 0.1)
ba = nx.barabasi_albert_graph(100, 5)
red = nx.random_lobster(100, 0.9, 0.9)
1
2
3
4
2
3
4
- Reading a graph stored in a file using common graph formats, such as edge lists, adjacency lists, GML, GraphML, pickle, LEDA and others.
nx.write_gml(red, "path.to.file")
mygraph = nx.read_gml("path.to.file")
1
2
2
For details on graph formats see Reading and writing graphs (opens new window) and for graph generator functions see Graph generators (opens new window)
编辑 (opens new window)
上次更新: 2021/02/16, 02:45:37