Social Network Analysis — Graph Metrics, Centrality, Community Detection
940 words
5 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# Social Network Analysis — Graph Metrics, Centrality, Community Detection ## 🎯 Learning Objectives - Calculate degree, betweenness, and closeness centrality - Compute clustering coefficient for nodes and networks - Explain PageRank algorithm and its application - Detect communities using modularity * * * ## 1. Gra...

Social Network Analysis — Graph Metrics, Centrality, Community Detection
🎯 Learning Objectives
- Calculate degree, betweenness, and closeness centrality
- Compute clustering coefficient for nodes and networks
- Explain PageRank algorithm and its application
- Detect communities using modularity
1. Graph Fundamentals
1.1 Intuition
Social networks are graphs: people are nodes (vertices) and relationships (friendship, following, communication) are edges. SNA measures help us find influential people, detect communities, and understand information flow.
1.2 Graph Types
| Type | Edges | Example |
|---|---|---|
| Undirected | No direction | Facebook friendship |
| Directed | Has direction | Twitter follows |
| Weighted | Has strength | Communication frequency |
| Unweighted | Binary | Friend/not friend |
1.3 Basic Metrics
(Diagram)
Graph parameters:
- Nodes (N) = 5
- Edges (E) = 6
- Density = 2E / (N(N-1)) = 12 / 20 = 0.6
2. Centrality Measures
2.1 Degree Centrality
The number of direct connections a node has.
From the graph above:
| Node | Degree | Normalized (÷4) |
|---|---|---|
| A | 3 | 0.75 |
| B | 2 | 0.50 |
| C | 4 | 1.00 |
| D | 2 | 0.50 |
| E | 1 | 0.25 |
C is the most central by degree (connected to everyone except E).
2.2 Betweenness Centrality
Measures how often a node lies on the shortest path between other nodes.
Where σst is the number of shortest paths from s to t, and σst(v) is those passing through v.
For node C in the graph:
- Shortest paths A↔E: A-C-E (passes C). A-B-E? A-C-E is shorter.
- Shortest paths B↔E: B-C-E (passes C)
- Shortest paths D↔E: D-C-E (passes C)
- C lies on every path between nodes on different sides High betweenness = gatekeeper / bridge.
2.3 Closeness Centrality
Measures how close a node is to all other nodes.
Where d(v,u) is the shortest path distance.
| Node | Sum of Distances | Closeness (C) |
|---|---|---|
| A | 1(A-B)+1(A-C)+1(A-D)+2(A-E)=5 | 4/5 = 0.80 |
| B | 1(B-A)+1(B-C)+2(B-D)+2(B-E)=6 | 4/6 = 0.67 |
| C | 1(C-A)+1(C-B)+1(C-D)+1(C-E)=4 | 4/4 = 1.00 |
| D | 1(D-A)+2(D-B)+1(D-C)+2(D-E)=6 | 4/6 = 0.67 |
| E | 2(E-A)+2(E-B)+1(E-C)+2(E-D)=7 | 4/7 = 0.57 |
C has the highest closeness — can reach everyone most quickly.
3. Clustering Coefficient
3.1 Local Clustering Coefficient
Measures how connected a node's neighbors are.
Where kv is the degree of v.
For node A (neighbors: B, C, D):
- Edges between B-C: yes, C-D: yes, B-D: no
- (2k)=(23)=3
- Edges among neighbors = 2
- C(A)=2/3=0.67 For node E (neighbor: C):
- C(E)=0 (only one neighbor, no pairs to evaluate)
3.2 Global Clustering Coefficient
The average of all local clustering coefficients, or the ratio of closed triads to all triads.
4. PageRank Algorithm
4.1 Intuition
PageRank measures importance: a node is important if it's linked to by other important nodes. Like academic citations — a paper is important if it's cited by important papers.
4.2 Algorithm
pythondef pagerank(graph, damping=0.85, iterations=100): N = len(graph) PR = {node: 1.0/N for node in graph} for _ in range(iterations): new_PR = {} for node in graph: # Sum of PR(incoming neighbors) / out-degree rank_sum = 0 for neighbor in graph: if node in graph[neighbor]: # neighbor links to node rank_sum += PR[neighbor] / len(graph[neighbor]) # Damping factor + teleportation new_PR[node] = (1 - damping) / N + damping * rank_sum PR = new_PR return PR
4.3 Worked Example
(Diagram)
Iteration 1 (N=3, d=0.85):
pseudoPR(A) = 0.15/3 + 0.85 × (PR(C)/1) = 0.05 + 0.85 × 0.33 = 0.331 PR(B) = 0.05 + 0.85 × (PR(A)/2) = 0.05 + 0.85 × 0.165 = 0.190 PR(C) = 0.05 + 0.85 × (PR(A)/2 + PR(B)/1) = 0.05 + 0.85 × (0.165 + 0.190) = 0.352
After convergence: C has highest PageRank (receives links from both A and B).
5. Community Detection
5.1 Modularity
Measures the quality of a community partition:
Where Aij is the adjacency matrix, ki is degree, m is edges, δ is 1 if same community.
- Q > 0.3 → significant community structure
- Q = 0 → random connections
5.2 Louvain Algorithm
Greedy optimization: iteratively move nodes between communities to maximize modularity gain.
6. 📝 Practice Questions
Q1: Calculate degree centrality for each node in: A-B-C-D (path graph of 4 nodes).Answer: A=1, B=2, C=2, D=1. Normalized (÷3): A=0.33, B=0.67, C=0.67, D=0.33. Q2: Why does betweenness centrality identify "bridges" in a network?Answer: Betweenness counts how many shortest paths pass through a node. Nodes that connect different communities lie on many shortest paths between members of different communities. Removing such a bridge would disconnect the graph. Q3: Compute PageRank for a 2-node graph where A→B and B→A (d=0.85).Answer: PR(A) = 0.05 + 0.85×PR(B)/1 PR(B) = 0.05 + 0.85×PR(A)/1Solving: PR(A) = PR(B). 2×PR = 0.05 + 0.85·PR → PR = 0.05/0.15 = 0.33 each. Q4: What does a clustering coefficient of 1 mean for a node?Answer: A clustering coefficient of 1 means all the node's neighbors are connected to each other — they form a complete graph (clique). Every pair of friends is also friends. Q5: How is the small-world phenomenon measured?Answer: Small-world networks have high clustering (like regular lattices) and short average path lengths (like random graphs). Measured by comparing clustering coefficient and path length to equivalent random graphs.
7. 🔗 Cross-References
- Week 7 - Information Diffusion: Central nodes are influential spreaders
- Week 8 - Privacy: Anonymization preserves network structure
- BSCS4021 (Advanced Algorithms): PageRank, graph algorithms Join Discord NextData Collection