LoslegenKostenlos loslegen

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

Kurs anzeigen

Anleitung zur Übung

  • 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.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# 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(____(____))
Code bearbeiten und ausführen