Get startedGet started for free

The bipartite keyword

In the video, Eric introduced you to the 'bipartite' keyword. This keyword is part of a node's metadata dictionary, and can be assigned both when you add a node and after the node is added. Remember, though, that by definition, in a bipartite graph, a node cannot be connected to another node in the same partition.

Here, you're going to write a function that returns the nodes from a given partition in a bipartite graph. In this case, the relevant partitions of the Github bipartite graph you'll be working with are 'projects' and 'users'.

This exercise is part of the course

Intermediate Network Analysis in Python

View Course

Exercise instructions

  • Write a function called get_nodes_from_partition() which accepts two arguments - a bipartite graph G and a partition of G - and returns just the nodes from that partition.
    • Iterate over all the nodes of G (not including the metadata) using a for loop.
    • Access the 'bipartite' keyword of the current node's metadata dictionary. If it equals partition, append the current node to the list nodes.
  • Use your get_nodes_from_partition() function together with the len() function to:
    • Print the number of nodes in the 'projects' partition of G.
    • Print the number of nodes in the 'users' partition of G.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Define get_nodes_from_partition()
def ____:
    # Initialize an empty list for nodes to be returned
    nodes = []
    # Iterate over each node in the graph G
    for n in ____:
        # Check that the node belongs to the particular partition
        if G.nodes[n]['____'] == ____:
            # If so, append it to the list of nodes
            ____
    return nodes

# Print the number of nodes in the 'projects' partition
print(____(get_nodes_from_partition(____, '____')))

# Print the number of nodes in the 'users' partition
print(____(____))
Edit and Run Code