Get startedGet started for free

Shared nodes in other partition

In order to build up your concept of recommendation systems, we are going to start with the fundamentals. The focus here is on computing user similarity in bipartite graphs.

Your job is to write a function that takes in two nodes, and returns the set of repository nodes that are shared between the two user nodes.

You'll find the following methods and functions helpful in this exercise - .neighbors(), set(), and .intersection() - besides, of course, the shared_partition_nodes function that you will define!

This exercise is part of the course

Intermediate Network Analysis in Python

View Course

Exercise instructions

  • Write a function called shared_partition_nodes() that takes in 3 arguments - a graph G, node1, and node2 - and returns the set of nodes that are shared between node1 and node2.
    • Check that node1 and node2 belong to the same partition using an assert statement and the 'bipartite' keyword.
    • Obtain the neighbors of node1 and store them as nbrs1.
    • Obtain the neighbors of node2 and store them as nbrs2.
  • Compute the overlap between nbrs1 and nbrs2 using the set .intersection() method.
  • Print the number of shared repositories between users 'u7909' and 'u2148' using your shared_partition_nodes() function together with the len() function.

Hands-on interactive exercise

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

def ____:
    # Check that the nodes belong to the same partition
    assert G.nodes[____]['bipartite'] == G.nodes[____]['bipartite']

    # Get neighbors of node 1: nbrs1
    nbrs1 = ____
    # Get neighbors of node 2: nbrs2
    nbrs2 = ____

    # Compute the overlap using set intersections
    overlap = ____(____).____(____)
    return overlap

# Print the number of shared repositories between users 'u7909' and 'u2148'
print(____(____))
Edit and Run Code