Affiliation networks are represented in NetworkX using the same classes as other networks: Graph and DiGraph. The only difference is that you need to keep track of which nodes are of which type. In NetworkX, this is done by using a container (list, set, and so on) to store all node IDs for one node type. For convenience, the node type can also be stored as a node attribute. A network can be tested for an affiliation network structure using the sets() function. Usually, this function can also find the nodes of each type (the exception being when the network has groups of nodes that are not connected to the rest of the network). The following example loads our dear old friend from Chapter 2, Working with Networks in NetworkX, the Zachary karate club network, and checks it for affiliation network structure:
# Import bipartite module
from networkx.algorithms...