networkx.complete_multipartite_graph

networkx.complete_multipartite_graph(*block_sizes)[source]

Returns the complete multipartite graph with the specified block sizes.

Parameters:

block_sizes : tuple of integers or tuple of node iterables

The arguments can either all be integer number of nodes or they can all be iterables of nodes. If integers, they represent the number of vertices in each block of the multipartite graph. If iterables, each is used to create the nodes for that block. The length of block_sizes is the number of blocks.

Returns:

G : NetworkX Graph

Returns the complete multipartite graph with the specified blocks.

For each node, the node attribute ‘block’ is an integer indicating which block contains the node.

Notes

This function generalizes several other graph generator functions.

  • If no block sizes are given, this returns the null graph.
  • If a single block size n is given, this returns the empty graph on n nodes.
  • If two block sizes m and n are given, this returns the complete bipartite graph on m + n nodes.
  • If block sizes 1 and n are given, this returns the star graph on n + 1 nodes.

Examples

Creating a complete tripartite graph, with blocks of one, two, and three vertices, respectively.

>>> import networkx as nx
>>> G = nx.complete_multipartite_graph(1, 2, 3)
>>> [G.node[u]['block'] for u in G]
[0, 1, 1, 2, 2, 2]
>>> list(G.edges(0))
[(0, 1), (0, 2), (0, 3), (0, 4), (0, 5)]
>>> list(G.edges(2))
[(2, 0), (2, 3), (2, 4), (2, 5)]
>>> list(G.edges(4))
[(4, 0), (4, 1), (4, 2)]
>>> G = nx.complete_multipartite_graph('a', 'bc', 'def')
>>> [G.node[u]['block'] for u in sorted(G)]
[0, 1, 1, 2, 2, 2]