Find nodes with top degree centralities
In this exercise, you'll take a deeper dive to see whether there's anything interesting about the most connected students in the network. First off, you'll find the cluster of students that have the highest degree centralities. This result will be saved for the next plotting exercise.
This exercise is part of the course
Intermediate Network Analysis in Python
Exercise instructions
- Get the top 5 unique degree centrality scores. To do this, use the
sorted()
function, in which the first argument is the set of degree centrality values of G (because you want unique degree centralities), and the second argument isreverse=True
, to ensure that it is sorted in descending order. To limit the results to the top 5 scores, add in appropriate slicing to the end of the statement. Also, remember to use.values()
on the returned degree centrality results! - Create list of nodes that have the top 5 highest overall degree centralities. To do this:
- Iterate over the dictionary of degree centrality scores using the
.items()
method onnx.degree_centrality(G)
. - If
dc
is intop_dcs
, then append the noden
to thetop_connected
list.
- Iterate over the dictionary of degree centrality scores using the
- Print the number of nodes that share the top 5 degree centrality scores (
top_connected
) usinglen()
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Get the top 5 unique degree centrality scores: top_dcs
top_dcs = ____(set(____), reverse=True)[____:____]
# Create list of nodes that have the top 5 highest overall degree centralities
top_connected = []
for n, dc in ____:
if ____ in ____:
____
# Print the number of nodes that share the top 5 degree centrality scores
print(____)