# Calculating degree centrality using networkx in python3

When working with networks, it is often useful to calculate the degree centrality of each node. Degree centrality measures the number of connections a node has in a network, indicating its importance or influence within the network. In Python, the networkx library provides a convenient way to calculate degree centrality.

## Option 1: Using networkx’s degree_centrality() function

The simplest way to calculate degree centrality in networkx is by using the built-in `degree_centrality()` function. This function takes a graph as input and returns a dictionary where the keys are the nodes and the values are their corresponding degree centrality scores.

``````import networkx as nx

# Create a graph
G = nx.Graph()

# Add nodes and edges to the graph
G.add_edges_from([(1, 2), (2, 3), (3, 4), (4, 5)])

# Calculate degree centrality
centrality_scores = nx.degree_centrality(G)

# Print the degree centrality scores
for node, centrality in centrality_scores.items():
print(f"Node {node}: {centrality}")``````

This code snippet creates a simple graph with five nodes and four edges. It then calculates the degree centrality using the `degree_centrality()` function and prints the results. The output will be:

`Node 1: 0.25`

`Node 2: 0.5`

`Node 3: 0.5`

`Node 4: 0.5`

`Node 5: 0.25`

## Option 2: Manually calculating degree centrality

If you prefer a more hands-on approach, you can manually calculate the degree centrality of each node by dividing the number of connections a node has by the total number of nodes in the graph.

``````import networkx as nx

# Create a graph
G = nx.Graph()

# Add nodes and edges to the graph
G.add_edges_from([(1, 2), (2, 3), (3, 4), (4, 5)])

# Calculate degree centrality manually
centrality_scores = {}
total_nodes = len(G.nodes)

for node in G.nodes:
centrality_scores[node] = G.degree(node) / (total_nodes - 1)

# Print the degree centrality scores
for node, centrality in centrality_scores.items():
print(f"Node {node}: {centrality}")``````

This code snippet achieves the same result as the previous option but calculates the degree centrality manually. It iterates over each node in the graph, calculates its degree, and divides it by the total number of nodes minus one. The output will be the same as before.

## Option 3: Using networkx’s degree() function

Another way to calculate degree centrality is by using the `degree()` function from networkx. This function returns the degree of a node, which is simply the number of connections it has.

``````import networkx as nx

# Create a graph
G = nx.Graph()

# Add nodes and edges to the graph
G.add_edges_from([(1, 2), (2, 3), (3, 4), (4, 5)])

# Calculate degree centrality using degree() function
centrality_scores = {}

for node in G.nodes:
centrality_scores[node] = nx.degree(G, node)

# Print the degree centrality scores
for node, centrality in centrality_scores.items():
print(f"Node {node}: {centrality}")``````

This code snippet uses the `degree()` function to calculate the degree centrality. It iterates over each node in the graph and assigns its degree to the corresponding key in the centrality_scores dictionary. The output will be the same as before.

After analyzing the three options, it is clear that Option 1, using networkx’s `degree_centrality()` function, is the most concise and straightforward solution. It abstracts away the complexity of calculating degree centrality manually and provides a clean and efficient way to obtain the desired results. Therefore, Option 1 is the recommended approach for calculating degree centrality using networkx in Python.

Rate this post

### 3 Responses

1. Kohen Phelps says:

Option 2 seems like a waste of time when networkxs degree_centrality() function is available.

2. Bethany says:

Option 2 seems cool, but why not just use Option 1? Its simpler and more efficient! 🤔

3. Hamza Shaffer says:

Option 2 seems like a total waste of time when we have the easy peasy degree_centrality() function in Option 1.