Social network analysis
Social network analysis (SNA) studies graphs of people or entities to measure structure, influence, communities and diffusion — used by engineers for recommendation, security, routing and analytics.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Social network analysis (SNA) helps engineers model relationships between users, devices or components as a graph and measure influence, connectivity and communities. Engineers use SNA in recommendation systems, intrusion and fraud detection, network routing, and understanding how information or failures spread.
Key ideas
Start from first principles in simple terms.
- Graph: A mathematical structure G = (V, E) where V (vertices or nodes) is a finite set of items and E (edges) is a set of pairs (undirected) or ordered pairs (directed) of nodes. Use this definition before any others.
- Undirected graph: edges are unordered pairs {u, v}. Directed graph: edges are ordered pairs (u, v). Define direction before using terms like in-degree.
- Weighted graph: each edge e has a weight w(e) (a real number) representing strength or cost.
- Adjacency matrix A: For a graph with n nodes indexed 1..n, A is an n×n matrix with A[i,j] = 1 if edge (i,j) exists (or weight if weighted), else 0. For undirected graphs A is symmetric.
- Degree (undirected): For node v, degree k(v) is the number of incident edges. For directed graphs define in-degree k_in(v) and out-degree k_out(v) separately.
- Path: A sequence of nodes v0, v1, ..., vl where each consecutive pair is connected by an edge. The length is the number of edges l.
- Shortest path (geodesic): path of minimum length between two nodes. Many centralities use counts of shortest paths.
- Connected component: a maximal set of nodes where each pair has a path between them (for undirected graphs). For directed graphs distinguish strongly connected components (each node reachable from each other) and weakly connected components (connected when directions are ignored).
- Neighborhood N(v): set of nodes adjacent to v. For directed graphs, distinguish in-neighbors N_in(v) and out-neighbors N_out(v).
Local clustering and triangles
- Triangle: three nodes with edges between each pair. The local clustering coefficient C(v) measures how close neighbors of v are to forming a clique (fully connected). For node v with degree k(v) >= 2, C(v) = (number of triangles touching v) * 2 / (k(v)·(k(v)-1)). This is the fraction of possible neighbor pairs that are connected. The global clustering coefficient (transitivity) is 3 × (number of triangles) divided by the number of connected triples of nodes; equivalently, the fraction of connected triples that are closed.
Centrality measures (precise definitions universities expect)
Degree centrality: For node v in an undirected graph of n nodes, degree centrality is k(v) or normalized k(v)/(n-1).
Closeness centrality: For a node v in a connected graph with distances d(v, u) (length of shortest path), closeness(v) = 1 / (sum_{u != v} d(v,u)). Many courses use the normalized form (n-1)/sum d(v,u). If the graph is disconnected, use harmonic closeness: C_H(v) = sum_{u != v} 1/d(v,u), treating 1/∞ = 0; many libraries adopt this.
Betweenness centrality: Let σ_st be number of shortest paths between s and t, and σ_st(v) number of those that pass through v (s != t != v). Then betweenness(v) = sum_{s!=v!=t} σ_st(v) / σ_st. Normalization often divides by (n-1)(n-2)/2 for undirected graphs so values lie in [0,1]. For directed graphs, divide by (n-1)(n-2). Derivation used in exams: count all unordered pairs (s,t), then for each pair add fraction of their shortest paths that pass through v.
Eigenvector centrality: A vector x (size n) satisfying x = λ^{-1} A x. In practice find the dominant eigenvector of A (largest eigenvalue) and x[i] is the centrality. This measures influence where connections to high-scoring nodes contribute more.
PageRank (outline): A random-walk score with damping d (typical d = 0.85). Iterative formula used in exams: PR(v) = (1-d)/n + d * sum_{u in in-neighbors(v)} PR(u)/outdeg(u). To handle dangling nodes (outdeg(u)=0), redistribute their mass uniformly each iteration by adding d·S/n to every node, where S = sum_{u: outdeg(u)=0} PR(u). Converges with power iteration under mild conditions.
Community detection and global measures
- Modularity Q: Given a partition of nodes into communities, modularity measures density of edges inside communities versus random expectation. If A is adjacency and k_i degrees and m is number of edges, then Q = (1/(2m)) sum_{ij} (A_{ij} - k_i k_j/(2m)) δ(c_i, c_j) where δ = 1 if i,j in same community. Exams often ask to compute Q for a small partition. For weighted graphs, replace A_{ij} by weights w_{ij}, k_i by node strengths s_i = sum_j w_{ij}, and m by the total weight W/2 (for undirected). Common community detection algorithms: Girvan–Newman (removes edges with high edge-betweenness) and Louvain/Leiden (greedy modularity optimization).
Network models and properties
- Small-world property: real networks often have short average path length and high clustering compared to random graphs.
- Scale-free networks: degree distribution follows a power law P(k) ∝ k^{-γ}, often γ in (2,3). One generative model is Barabási–Albert (BA) preferential attachment: start with m0 nodes, add nodes one by one; each new node connects to m existing nodes with probability proportional to their degree. Derivation of P(k) using continuous approximation is a common exam question (see Worked examples).
Diagrams to draw for exams (describe exactly):
- To explain adjacency matrix and degrees: draw 5 nodes, label 1..5, draw edges: 1-2,1-3,2-3,3-4,4-5. Next to it draw 5×5 adjacency matrix with rows/cols 1..5 and fill 0/1 values. Underline degree of each node.
- To show betweenness: same graph, show all shortest paths between pairs and mark routes passing through node 3.
Where assumptions matter: many centrality formulas assume connectedness (closeness undefined if node disconnected). Betweenness normalization assumes undirected graph unless stated.
Formulas
A[i,j] = 1 if edge (i,j) exists, else 0 — A is adjacency matrix (unitless), applies to simple graphs without weights.
k(v) = sum_j A[v,j] — degree of node v (unitless), applies to unweighted graphs. For weighted graphs, degree is still the number of incident edges; the weighted analog is node strength s(v) = sum_j w(v,j).
C(v) = 2·T(v) / (k(v)·(k(v)-1)) — T(v) is number of triangles through v; dimensionless; applies when k(v) >= 2.
closeness(v) = 1 / sum_{u != v} d(v,u) — d is shortest-path length (unit: number of edges); applies when graph is connected. For disconnected graphs, use harmonic closeness C_H(v) = sum_{u != v} 1/d(v,u) with 1/∞ = 0.
betweenness(v) = sum_{s!=v!=t} σ_st(v) / σ_st — σ_st is count of shortest paths between s and t (unitless); normalized by dividing by ((n-1)(n-2)/2) for undirected graphs.
x = λ^{-1} A x — eigenvector centrality: x is eigenvector, λ is eigenvalue (unitless); take dominant eigenvector (largest λ) with nonnegative entries.
PR(v) = (1-d)/n + d · [sum_{u in N_in(v)} PR(u)/outdeg(u) + S/n] — PageRank formula with dangling-node correction; S is the sum of PR(u) over nodes with outdeg(u)=0. d is damping factor (dimensionless), typical d = 0.85.
Q = (1/(2m)) sum_{ij} (A_{ij} - k_i k_j/(2m)) δ(c_i,c_j) — modularity Q is dimensionless; m is number of edges.
Worked examples
Example 1 — manual centralities and clustering (graph used in diagrams). Graph edges: (1,2),(1,3),(2,3),(3,4),(4,5). Give steps and final answers.
- Degrees: k(1)=2, k(2)=2, k(3)=3, k(4)=2, k(5)=1. (Sum check 2+2+3+2+1 = 10 = 2m, so m=5 edges.)
- Adjacency matrix: (write a 5×5 with 1s at symmetric positions (1,2),(1,3),(2,3),(3,4),(4,5), zeros on diagonal.)
- Local clustering coefficients:
- Node 1: neighbors {2,3}, they are connected → T(1)=1 → C(1)=2·1/(2·1)=1. C(1)=1.0
- Node 2: same as node 1 → C(2)=1.0
- Node 3: neighbors {1,2,4}, only (1,2) pair connected → T(3)=1 → C(3)=2·1/(3·2)=2/6=0.3333333333333333. C(3)=0.3333333333333333
- Node 4: neighbors {3,5}, not connected → C(4)=0.0
- Node 5: degree 1 → C(5)=0.0
- Betweenness centrality: list all unordered node pairs (10 pairs), determine shortest paths and count how many pass through each node (explain as in Key ideas). Counts: node 3: 4, node 4: 3, others: 0. Normalized by ((n-1)(n-2)/2)=6 so: betweenness(3)=4/6=0.6666666666666666, betweenness(4)=3/6=0.5.
Example 2 — derive BA model degree distribution (standard continuous approximation, step-by-step). Assume BA model: start with m0 >= m nodes. Each new node connects to m existing nodes with probability proportional to node degree. Let k_i(t) be degree of node i at time t (t counts nodes added). Degree growth approximated by differential equation: dk_i/dt = m · (k_i / (2m t)) = k_i / (2t). (Because total degree ≈ 2m t.) Solve with initial condition k_i(t_i) = m when node i arrived at time t_i: separation of variables gives k_i(t) = m (t / t_i)^{1/2}. Probability that k_i(t) < k is probability t_i > t (m/k)^2. Since nodes arrive uniformly in time, density of t_i is 1/t. So degree distribution P(k) = d/dk [1 - (m/k)^2] ∝ k^{-3}. More precisely P(k) ≈ 2 m^2 k^{-3} for k >> m. (See verify list to check prefactor derivation details.)
Example 3 — Python example (NetworkX) computing degree, clustering and betweenness. Code (complete):
import networkx as nx G = nx.Graph() G.add_edges_from([(1,2),(1,3),(2,3),(3,4),(4,5)]) print('degrees:', dict(G.degree())) print('degree_centrality:', nx.degree_centrality(G)) print('clustering:', nx.clustering(G)) print('betweenness:', nx.betweenness_centrality(G))
Sample output (exact): degrees: {1: 2, 2: 2, 3: 3, 4: 2, 5: 1} degree_centrality: {1: 0.5, 2: 0.5, 3: 0.75, 4: 0.5, 5: 0.25} clustering: {1: 1.0, 2: 1.0, 3: 0.3333333333333333, 4: 0.0, 5: 0.0} betweenness: {1: 0.0, 2: 0.0, 3: 0.6666666666666666, 4: 0.5, 5: 0.0}
(These values match the manual calculations above.)
Common mistakes
- Using closeness without checking the graph is connected — division by zero or infinite distance.
- Mixing raw and normalized centrality values; not stating normalization used (n-1 or (n-1)(n-2)/2).
- For BA derivation: losing the factor 2 in total degree ≈ 2 m t or miscomputing the initial condition k(t_i)=m.
- Computing clustering coefficient formula with k<2 (must note it is undefined or set to 0 by convention in many libraries).
- In modularity, forgetting to divide by 2m or to use expected edge term k_i k_j/(2m).
For your exam
Types of questions:
- Definitions and short notes: Define graph terms, clustering coefficient, centralities. Full marks require clear definitions, units (usually unitless) and short examples.
- Derivations: BA preferential attachment degree distribution, or deriving betweenness formula normalization. Full answer must state assumptions (continuous approximation or discrete), show differential equation and solve, and explain normalization.
- Numericals: Given a small graph compute adjacency matrix, degrees, clustering coefficients, centrality values. Full marks show the adjacency matrix, list degrees, show triangle counting for each node, compute sums and normalizations step-by-step.
- Programs: Small Python/NetworkX code to compute centralities. Full marks include code, sample run and exact output.
- Diagrams: Draw graph and mark shortest paths; show how many pass through a node when computing betweenness. Full marks label nodes, list pairs, show routes and counting.
Exam technique: Always state whether graph is directed/undirected and whether values are normalized. When asked to compute modularity, compute m, degrees k_i, expected term and sum only over pairs in same community.
Quick check
- What is the adjacency matrix entry A[2,3] for an undirected simple graph if nodes 2 and 3 are connected? (one-word or number)
- For a node with degree 4 and 2 triangles around it, what is its local clustering coefficient? (give decimal)
- In the BA model with m edges added per new node, what is the approximate exponent γ of the power-law tail of P(k) ∝ k^{-γ}? Answers: 1) 1. 2) C = 2·2 / (4·3) = 4/12 = 0.3333333333. 3) γ = 3.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?