Get Started

Compute number of neighbors for each node

How do you evaluate whether a node is an important one or not? There are a few ways to do so, and here, you're going to look at one metric: the number of neighbors that a node has.

Every NetworkX graph G exposes a .neighbors(n) method that returns an iterator of nodes that are the neighbors of the node n. To begin, use this method in the IPython Shell on the Twitter network T to get the neighbors of of node 1. This will get you familiar with how the function works. Then, your job in this exercise is to write a function that returns all nodes that have m neighbors.

This is a part of the course

“Introduction to Network Analysis in Python”

View Course

Exercise instructions

  • Write a function called nodes_with_m_nbrs() that has two parameters - G and m - and returns all nodes that have m neighbors. To do this:
    • Iterate over all nodes in G (not including the metadata).
    • Use the len() and list() functions together with the .neighbors() method to calculate the total number of neighbors that node n in graph G has.
      • If the number of neighbors of node n is equal to m, add n to the set nodes using the .add() method.
    • After iterating over all the nodes in G, return the set nodes.
  • Use your nodes_with_m_nbrs() function to retrieve all the nodes that have 6 neighbors in the graph T.

Hands-on interactive exercise

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

# Define nodes_with_m_nbrs()
def ____:
    """
    Returns all nodes in graph G that have m neighbors.
    """
    nodes = set()

    # Iterate over all nodes in G
    for n in ____:

        # Check if the number of neighbors of n matches m
        if ____ == ____:

            # Add the node n to the set
            ____

    # Return the nodes with m neighbors
    return nodes

# Compute and print all nodes in T that have 6 neighbors
six_nbrs = ____
print(____)
Edit and Run Code

This exercise is part of the course

Introduction to Network Analysis in Python

IntermediateSkill Level
4.6+
7 reviews

This course will equip you with the skills to analyze, visualize, and make sense of networks using the NetworkX library.

You'll learn about ways to identify nodes that are important in a network. In doing so, you'll be introduced to more advanced concepts in network analysis as well as the basics of path-finding algorithms. The chapter concludes with a deep dive into the Twitter network dataset which will reinforce the concepts you've learned, such as degree centrality and betweenness centrality.

Exercise 1: Degree centralityExercise 2: Compute number of neighbors for each node
Exercise 3: Compute degree distributionExercise 4: Degree centrality distributionExercise 5: Graph algorithmsExercise 6: Shortest Path IExercise 7: Shortest Path IIExercise 8: Shortest Path IIIExercise 9: Betweenness centralityExercise 10: NetworkX betweenness centrality on a social networkExercise 11: Deep dive - Twitter networkExercise 12: Deep dive - Twitter network part II

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free