본문 바로가기
Data Analysis/Network Analysis

[Network] Degree Centrality (차수 중심성)

by lucky__lucy 2025. 7. 24.

Degree Centrality란?

  • 한 노드가 네트워크 내에서 직접 연결된 이웃 노드의 개수를 측정하는 중심성 지표
  • Degree Centrality 값이 클수록 더 많은 노드와 연결되어 있음 → 중심 허브 의미

 

그래프의 종류

  • 무방향 그래프 (전체 노드에 대해 정규화)
    $$
    C_D(i)=\frac{deg(i)}{N-1}
    $$
    • $C_D (i)$: 노드 𝑖의 degree centrality
    • $deg(i)$: 노드 𝑖에 연결된 엣지(이웃 노드)의 수
    • 𝑁: 전체 노드 수
    • 𝑁−1: 노드 𝑖를 제외한 나머지 노드의 수 → 최대 연결 가능한 노드 수

예시 1, 예시 2

import networkx as nx
import matplotlib.pyplot as plt

# ==================
# 예시 1
# ==================
G = nx.DiGraph()
G.add_edges_from([
    (1, 2), (1, 3), (2, 3), (3, 4),
    (4, 1), (4, 2)
])

deg_centrality = nx.degree_centrality(G)
print(deg_centrality)
# {1: 1.0, 2: 1.0, 3: 1.0, 4: 1.0}

# ==================
# 예시 2
# ==================
G = nx.DiGraph()
G.add_edges_from([
    (1, 2), (2, 3), (2, 4), (3, 4)
])

deg_centrality = nx.degree_centrality(G)
print(deg_centrality)
# {1: 0.3333333333333333, 2: 1.0, 3: 0.6666666666666666, 4: 0.6666666666666666}

 

 

  • 방향 그래프(예: 교통망, 소셜미디어)
    • In-degree: 들어오는 연결 수
      $$
      C_D^{in}(i) = deg^{in}(i)
      $$
    • Out-degree: 나가는 연결 수
      $$
      C_D^{out}(i) = deg^{out}(i)
      $$
    • 총 degree
      $$
      C_D(i) = \frac{ deg^{in}(i) + deg^{out}(i)}{N - 1}
      $$

import networkx as nx

G = nx.DiGraph()
G.add_edges_from([
    (1, 2), (2, 3), (3, 1), (3, 4),
    (4, 2), (4, 5)
])

in_deg = dict(G.in_degree())
out_deg = dict(G.out_degree())
centrality = nx.degree_centrality(G)

print("In-Degree:", in_deg)
print("Out-Degree:", out_deg)
print("Degree Centrality:", centrality)

#In-Degree: {1: 1, 2: 2, 3: 1, 4: 1, 5: 1}
#Out-Degree: {1: 1, 2: 1, 3: 2, 4: 2, 5: 0}
#Degree Centrality: {1: 0.5, 2: 0.75, 3: 0.75, 4: 0.75, 5: 0.25}

 

그래프 시각화

import matplotlib
matplotlib.use('TkAgg')

# 레이아웃 설정
pos = nx.spring_layout(G, seed=42)

# 그래프 시각화
plt.figure(figsize=(6, 6))
nx.draw(G, pos, with_labels=True, node_color='pink', edge_color='gray',
        node_size=1000, font_weight='bold', arrows=True) # 무방향은 arrows=False
plt.show()

 

728x90
반응형