Get startedGet started for free

Deep dive - Twitter network

You're going to now take a deep dive into a Twitter network, which will help reinforce what you've learned earlier. First, you're going to find the nodes that can broadcast messages very efficiently to lots of people one degree of separation away.

NetworkX has been pre-imported for you as nx.

This exercise is part of the course

Introduction to Network Analysis in Python

View Course

Exercise instructions

  • Write a function find_nodes_with_highest_deg_cent(G) that returns the node(s) with the highest degree centrality using the following steps:
    • Compute the degree centrality of G.
    • Compute the maximum degree centrality using the max() function on list(deg_cent.values()).
    • Iterate over the degree centrality dictionary, deg_cent.items().
    • If the degree centrality value v of the current node k is equal to max_dc, add it to the set of nodes.
  • Use your function to find the node(s) that has the highest degree centrality in T.
  • Write an assertion statement that checks that the node(s) is/are correctly identified. This has been done for you, so hit 'Submit Answer' to see the result!

Hands-on interactive exercise

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

# Define find_nodes_with_highest_deg_cent()
def find_nodes_with_highest_deg_cent(G):

    # Compute the degree centrality of G: deg_cent
    deg_cent = ____

    # Compute the maximum degree centrality: max_dc
    max_dc = ____

    nodes = set()

    # Iterate over the degree centrality dictionary
    for k, v in ____:

        # Check if the current value has the maximum degree centrality
        if ____ == ____:

            # Add the current node to the set of nodes
            ____

    return nodes

# Find the node(s) that has the highest degree centrality in T: top_dc
top_dc = ____
print(top_dc)

# Write the assertion statement
for node in top_dc:
    assert nx.degree_centrality(T)[node] == max(nx.degree_centrality(T).values())
Edit and Run Code