[docs]@nodes_or_number(0)defempty_graph(n=0,create_using=None,default=Graph):ifcreate_usingisNone:G=default()elifhasattr(create_using,"_adj"):# create_using is a EasyGraph style GraphG=create_usingelse:# try create_using as constructorG=create_using()n_name,nodes=nG.add_nodes_from(nodes)returnG
[docs]@nodes_or_number(0)defcomplete_graph(n,create_using=None):"""Return the complete graph `K_n` with n nodes. A complete graph on `n` nodes means that all pairs of distinct nodes have an edge connecting them. Parameters ---------- n : int or iterable container of nodes If n is an integer, nodes are from range(n). If n is a container of nodes, those nodes appear in the graph. create_using : EasyGraph graph constructor, optional (default=eg.Graph) Graph type to create. If graph instance, then cleared before populated. Examples -------- >>> G = eg.complete_graph(9) >>> len(G) 9 >>> G.size() 36 >>> G = eg.complete_graph(range(11, 14)) >>> list(G.nodes()) [11, 12, 13] >>> G = eg.complete_graph(4, eg.DiGraph()) >>> G.is_directed() True """n_name,nodes=nG=empty_graph(n_name,create_using)iflen(nodes)>1:ifG.is_directed():edges=itertools.permutations(nodes,2)else:edges=itertools.combinations(nodes,2)G.add_edges_from(edges)returnG