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'.
Diese Übung ist Teil des Kurses
Intermediate Network Analysis in Python
Anleitung zur Übung
- Write a function called
get_nodes_from_partition()which accepts two arguments - a bipartite graphGand apartitionofG- and returns just the nodes from thatpartition.- Iterate over all the nodes of
G(not including the metadata) using aforloop. - 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
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
# 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(____(____))