开始使用免费开始使用

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.

本练习是课程的一部分

Introduction to Network Analysis in Python

查看课程

练习说明

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

交互式实操练习

通过完成这段示例代码来试试这个练习。

# 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())
编辑并运行代码