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
Exercise instructions
- Write a function called
get_nodes_from_partition()
which accepts two arguments - a bipartite graphG
and apartition
ofG
- and returns just the nodes from thatpartition
.- Iterate over all the nodes of
G
(not including the metadata) using afor
loop. - Access the
'bipartite'
keyword of the current node's metadata dictionary. If it equalspartition
, append the current node to the listnodes
.
- Iterate over all the nodes of
- Use your
get_nodes_from_partition()
function together with thelen()
function to:- Print the number of nodes in the
'projects'
partition ofG
. - Print the number of nodes in the
'users'
partition ofG
.
- Print the number of nodes in the
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(____(____))