Guide to AI and Data Courses · Lecture Notes

Chapter 30: Social Network Analysis

Imagine asking every student in a school "who are your closest friends?" and writing the answers on paper. We now hold not the students' traits but the relationships between them. Social network analysis is the course about the graph those relationships form: who sits at the center of the network, who links two groups, where the groups separate, and how a rumor moves through it. These notes introduce the course and work through its three core ideas on a six-node network, with examples you can compute by hand.

Learning outcomes

Building a network
Write relationship data as a graph of nodes and edges; compute degree and density.
Centrality
Compute degree, closeness and betweenness centrality by hand on a small network, and explain why they disagree when they do.
Clustering and paths
Compute the clustering coefficient and the average path length; show how a single shortcut turns a network into a "small world".
Community detection
Compute the modularity of a partition and compare two partitions by this measure.
Place of the course
State what separates social network analysis from data mining, information retrieval, linear algebra and multi-agent systems.

30.1What question does the course ask?

How are networks of people and their relationships analyzed?

Social network analysis studies the graph formed by treating people as nodes and relationships as edges. The question is not the traits of individuals but the structure of their ties: who is central, which groups exist, and how a rumor or a disease spreads through the network.

In most data analysis the rows are treated as independent: every customer and every patient is one record. In network data the real information lies between the records. Knowing the ages of two people does not tell us whether they are friends; friendship is a separate table and needs its own mathematics. The course therefore teaches two-part thinking about data: nodes (who) and edges (who is tied to whom).

The questions of the course fall into three groups. Importance: which node sits at a critical place in the network? Structure: is the network made of clusters or of one piece, and how many steps separate two random people? Process: how does news, a fashion or a disease spread through the network? The three core sections below take importance (centrality) and structure (clustering and small worlds, communities) in turn; the diffusion models (SIR) are built on top of these structures in the course.

30.1.1The example network used in these notes

We use the same small network in all three sections: six people and seven friendships. A, B and C know one another; D, E and F know one another; the two groups are joined only by the single tie between C and D. The edges are A–B, A–C, B–C, C–D, D–E, D–F, E–F. It is undirected and unweighted. Real networks have thousands of nodes, but the way of thinking is the same, and the computing is left to software (such as NetworkX).

NodeNeighborsDegree
AB, C2
BA, C2
CA, B, D3
DC, E, F3
ED, F2
FD, E2

The degrees add up to 2 + 2 + 3 + 3 + 2 + 2 = 14, twice the number of edges (m = 7), since every edge contributes to the degree of two nodes. There are 6·5/2 = 15 possible edges, so the density of the network is 7/15 ≈ 0.47.

30.1.2At a glance

CriterionSocial Network Analysis
Approachgraph theory and statistics
Inputnodes and links
Outputcentrality, communities, diffusion
Success measuremodularity, interpretability
Math loadmedium: graphs, matrices
Data needsnetwork data

30.2A short history

YearMilestone
1736Euler solves the Königsberg bridges problem; graph theory is born.
1934Moreno draws relationships in diagrams he calls sociograms.
1967Milgram's letter experiment popularizes the idea of "six degrees".
1973Granovetter shows "the strength of weak ties": job leads come from distant acquaintances.
1998Watts and Strogatz publish the small-world network model.
2008The Louvain method makes fast community detection possible on networks with millions of nodes.

30.3Core concepts

The rest of the course is built on the concepts below. The following sections work through the most important ones with examples.

ConceptMeaning
Nodes and edgesDirected or undirected, weighted or unweighted.
Degree distributionA few highly connected nodes, many with few links.
CentralityDegree, betweenness, closeness and eigenvector measures.
Clustering coefficientHow many of my friends know each other.
Small worldHigh clustering but short average paths.
CommunityNodes densely linked inside, sparsely to the outside.
ModularityHow good a partition into communities is.
Diffusion modelsSpread of information or disease on a network (SIR).

30.4Centrality: who is important?

"Important" has no single meaning. A node can matter because it has many neighbors, because it reaches everyone by short routes, or because it stands on the paths between other nodes. Each idea corresponds to a different centrality measure.

Definition 30.1 (Three centrality measures)

Degree: the number of neighbors of the node. Closeness: (n − 1) / Σ d(v, u), where d(v, u) is the length of the shortest path between v and u. A large value means the node is close to everyone. Betweenness: for the other pairs of nodes, how many of their shortest paths pass through this node; if a pair has several shortest paths, each gets an equal share.

Example 30.1: Centrality in the six-node network

Take the network above. First write down the distances from each node to the other five and add them up.

  1. Node C. A, B, D are one step away; E, F are two steps away. The total is 1 + 1 + 1 + 2 + 2 = 7. Closeness is 5/7 ≈ 0.71. D gives the same result.
  2. Node A. B, C are one step away; D is two (A–C–D); E, F are three (A–C–D–E). The total is 1 + 1 + 2 + 3 + 3 = 10. Closeness is 5/10 = 0.50. B, E and F give the same value.
  3. Betweenness of C. Every path between {A, B} and {D, E, F} goes through C: 2 × 3 = 6 pairs of nodes, each with a single shortest path. No other pair uses C as an intermediate node. Betweenness = 6. The same for D.
  4. Betweenness of A. A is in the middle of no pair's path: 0. The same for B, E, F.
  5. Normalizing. With n = 6 nodes the largest possible betweenness is (n − 1)(n − 2)/2 = 10, so C gets 6/10 = 0.6.

Result: C and D are the two most important nodes by all three measures, but betweenness shows the gap most clearly. By degree the contrast is 3 against 2; by betweenness it is 6 against 0. C and D alone join the two groups. If either leaves the network the two groups fall apart.

This small example shows why the course teaches more than one measure. Someone with many followers on social media (high degree) need not be the same person as someone who connects different communities (high betweenness). Granovetter's "strength of weak ties" finding can be read from here too: a single acquaintance who joins two groups may be a more valuable source of information than hundreds of close ties inside a group.

Common mistake

Treating one measure as "the" centrality, or comparing raw values across different networks. "Betweenness 6" says nothing by itself: it is large in a 6-node network and tiny in a 1000-node one. We compare the measure among the nodes of the same network, or normalize as above. The measure must also match the question: closeness suits how fast information spreads, betweenness suits finding the person who links groups.

30.5Clustering and the small world

In real social networks a friend of my friend is quite likely to be my friend too. This tendency is called triadic closure and it creates many triangles. The clustering coefficient turns the tendency into a number.

Definition 30.2 (Clustering coefficient)

The local clustering coefficient of a node is the share of its neighbors that are tied to one another: Cv = (number of edges among neighbors) / (k(k − 1)/2), where k is the degree of the node. By convention a node of degree 1 gets 0. For the network as a whole one uses either the average of the local values or the transitivity: 3 × (number of triangles) / (number of connected triples).

Example 30.2: Clustering in the same network

Take the six-node network again.

  1. Node A. Its neighbors are B and C, and these two are tied (B–C): 1 edge out of 2·1/2 = 1 possible. CA = 1. The same for B, and (by the same reasoning) for E and F.
  2. Node C. Its neighbors are A, B, D; of the 3·2/2 = 3 possible pairs only A–B is tied. CC = 1/3. The same for D.
  3. Average of the local values. (1 + 1 + 1/3 + 1/3 + 1 + 1)/6 = 4.67/6 ≈ 0.78.
  4. Transitivity. There are two triangles (ABC, DEF). The connected triples, i.e. a node with two of its neighbors, number 1 + 1 + 3 + 3 + 1 + 1 = 10. Transitivity is 3·2/10 = 0.6.

The two definitions give 0.78 and 0.6 on the same network: the bridge nodes (C and D) lie in few triangles but form many triples, and they weigh more in the transitivity.

30.5.1Short paths: the small world

Since Milgram's letter experiment it has been known that even in very large networks two people can reach each other in a few steps. Watts and Strogatz showed that a few "shortcut" edges are enough for this: most edges stay local (clustering is high) and a few jump far (the average path becomes short). Networks that show high clustering and a short average path together are called small-world networks.

Example 30.3: The effect of a single shortcut

Eight people sit in a ring and each knows only the two neighbors beside them. Let us compute the average path length.

  1. The ring. From one person the distances to the other seven are 1, 1, 2, 2, 3, 3, 4, a total of 16. Since all nodes are equivalent the average path is 16/7 ≈ 2.29.
  2. The shortcut. Add one edge between two people sitting opposite each other (nodes 0 and 4). The average distance over all pairs becomes 55/28 ≈ 1.96.
  3. Comparison. One edge shortened the average path by about 14%. Even in a ring of 8 the difference shows; as the ring grows, the effect of shortcuts becomes more pronounced.
Common mistake

Reading "small world" as "the network is small". The network can have millions of nodes; what is small is the average path. Nor are high clustering and short paths mutually exclusive: in a random graph paths are short but clustering is low; in a regular ring clustering is high but paths are long. The small-world model captures both together.

30.6Community detection and modularity

In social networks nodes often gather in groups: classes, neighborhoods, shared interests. A community is a group of nodes densely linked inside and sparsely to the outside. To split a network into communities we need a criterion for how good a partition is. The most common is the modularity of Newman and Girvan.

Definition 30.3 (Modularity)

If the network has m edges, the modularity of a partition is Q = Σc [ Lc/m − (dc / 2m)2 ]. Here Lc is the number of edges inside community c and dc is the sum of the degrees of its nodes. The first term is "what share of the edges lies inside communities"; the second is "how much of that would be expected if the degrees stayed the same but the edges were placed at random". The larger Q is, the sharper the partition.

Example 30.4: Comparing partitions

In the six-node network m = 7 and the total degree is 2m = 14. Let us test three partitions.

  1. {A, B, C} and {D, E, F}. Each community has 3 internal edges and a degree sum of 2 + 2 + 3 = 7. Each contributes 3/7 − (7/14)2 = 0.4286 − 0.25 = 0.1786. Q = 2 × 0.1786 ≈ 0.357.
  2. {A, B, C, D} and {E, F}. The first community has 4 internal edges (A–B, A–C, B–C, C–D) and degree sum 10; the second has 1 internal edge (E–F) and degree sum 4. Q = (4/7 − (10/14)2) + (1/7 − (4/14)2) ≈ 0.061 + 0.061 ≈ 0.122.
  3. Everyone in one community. Lc = 7, dc = 14: Q = 7/7 − (14/14)2 = 0.

The best partition is the triangle–triangle split we would also pick by eye (0.357). Putting C and D in different groups takes the bridge edge inside a community and lowers the score. One big community gives 0: it explains no structure.

Community detection algorithms try to increase this measure. The Louvain method checks whether moving each node into a neighbor's community raises Q; when no more good moves remain, it collapses the communities into single nodes and repeats the process one level up. This makes it fast on networks with millions of nodes. Testing the three partitions above by hand is a small imitation of what the algorithm does.

Common mistake

Taking the partition an algorithm finds as "the real groups". Modularity is a score; the highest-scoring partition can conflict with domain knowledge, and different methods give different partitions. Even a randomly built network can have partitions with positive Q. The result should be compared with outside information (schools, neighborhoods, labels) and tested.

30.7How is the course taught?

30.7.1A typical workflow

Buildnetworkwho with whom,what tieDescribedegree,path lengthCentralityimportantnodesFindcommunitiesLouvain,modularityInterpretvisualization,testing
Figure 30.1: A typical workflow in Social Network Analysis

A researcher builds a network from a school's friendship survey, looks at the degree distribution, finds "bridge" students with high betweenness, extracts friend groups that cross classes with Louvain, and discusses the drawn result with the teachers.

30.7.2Topics over the semester

  1. Graph basics: nodes, edges, adjacency matrix
  2. Collecting network data: surveys, traces, ego nets
  3. Degree distribution, path length and diameter
  4. Centrality: degree, betweenness, closeness
  5. Eigenvector centrality and PageRank
  6. Clustering, triadic closure and weak ties
  7. Homophily and assortativity
  8. Random graphs and the small-world model
  9. Scale-free networks and preferential attachment
  10. Community detection: modularity and Louvain
  11. Spreading: the SIR model and information cascades
  12. Link prediction and network visualization

This is a typical semester; the order and coverage vary between universities.

30.7.3Skills you will gain

  • network analysis with NetworkX
  • network visualization with Gephi
  • interpreting centrality measures
  • finding and testing communities
  • writing a spreading simulation

30.7.4Try it yourself

The Try It: Who Should Start the Campaign? slide in the presentation lets you solve a typical problem of this course yourself: A club will hand its announcement to one person in the friendship network of its 18 members. Click a person to see their centrality, find the communities, then watch the news spread from that person step by step.

30.8Are you ready for this course?

30.8.1Prerequisites

CourseWhy it is needed
Linear Algebraadjacency matrix, spectral methods
Data Structuresadjacency lists and matrices: storing a graph in memory
AlgorithmsBFS, DFS and shortest paths: the basis of centrality computations
Discrete Mathematicsgraph theory, sets and counting

30.8.2Ask yourself

For every "no", the right-hand column shows where to start. If you can say "yes" to most of them, you are ready.

QuestionIf not, start with
Can you find the shortest path between two nodes of a graph with BFS?Graph search from an algorithms course
Can you explain what an eigenvector of a matrix means?Linear Algebra
Can you compute the mean and variance of a distribution?Probability and Statistics
Can you read an edge-list file into a Python dictionary?An intro programming course, then a short NetworkX tutorial

30.9Neighboring courses

Data MiningIR & RecommendersLinear AlgebraSocial Network Analysis
subfieldbuilds onshares methodsapplication
Figure 30.2: Neighbors of Social Network Analysis and the kind of relation

30.9.1Where it overlaps

RelationCourseShared ground
builds onThis course builds on Linear Algebraadjacency matrix, spectral methods
shares methodsShares methods with Data Miningcommunity detection
shares methodsShares methods with Information Retrieval and Recommender SystemsPageRank, link analysis

30.9.2Where it differs

  • Data Mining: Data mining looks for patterns in tabular records; this course studies the structure of the links between records.
  • Information Retrieval and Recommender Systems: Information retrieval uses PageRank as a ranking signal; this course asks what centrality says about the network.
  • Linear Algebra: Linear algebra computes the eigenvalues of the adjacency matrix; this course reads them as communities and importance.
  • Multi-Agent Systems: Multi-agent systems model how agents make decisions; this course analyzes the observed network of ties.

30.10In practice

30.10.1Applications

AreaWhat is done
Social mediaInfluencers and echo chambers.
EpidemiologyDisease spread on contact networks.
FraudRings of linked fake accounts.
Science networksCitation and co-authorship analysis.
Workplace networksHow knowledge flows in a company.
Friend suggestionsLink prediction from mutual friends.

30.10.2Risks and ethics

RiskExplanation
Inferring what was never sharedA friendship network can reveal political views or sexual orientation that a person never disclosed.
Removing names is not enoughEven with names removed, the pattern of links can identify a person, as structural re-identification attacks showed.
Consent and scrapingIn the Cambridge Analytica case, a personality quiz app also collected data on its users' friends without their consent.
Missing ties, wrong answersTies forgotten in a survey or invisible on a platform can badly distort centrality rankings and communities.

These issues are discussed in depth in Explainable AI and AI Ethics.

30.10.3Tools and datasets

Tools: NetworkX, igraph, Gephi, graph-tool, Neo4j, PyTorch Geometric.

Well-known datasets: Zachary's karate club, Les Misérables character network, Enron e-mails, SNAP network collection, DBLP co-authorship network, Facebook ego networks (SNAP).

30.11Common misconceptions

Common mistake

The person with the most friends is the most influential.

Degree is one measure; a bridge joining two groups (betweenness) can matter more.

Common mistake

Looking at a network drawing is enough to understand it.

Large networks draw as a "hairball"; numerical measures and tests are needed.

Common mistake

Community detection has one right answer.

Different methods give different partitions; results are read with domain knowledge.

Common mistake

Connected people have influenced each other.

Similar people choose each other anyway (homophily); influence is hard to separate from selection.

30.12Summary

  • Social network analysis treats data as nodes and edges; the real information lies not in the records themselves but in the relationships between them.
  • Centrality measures different meanings of "importance": degree counts neighbors, closeness the distance to everyone, betweenness being a bridge. Bridge nodes stand out by betweenness, not by degree.
  • The clustering coefficient is the share of neighbors who know each other; small-world networks combine high clustering with short average paths, and a few shortcuts shorten the paths markedly.
  • Modularity measures the community structure of a partition as observed minus expected internal edges; methods such as Louvain increase it, and the result is tested against domain knowledge.
  • This course focuses on the structure of the graph; tabular records (data mining), ranking (information retrieval), eigenvalues (linear algebra) and agent decisions (multi-agent systems) belong to the neighboring courses.

30.13Test Yourself

  1. In a star network (one center node tied to four leaf nodes; 5 nodes, 4 edges), compute the degree, betweenness and closeness of the center and the closeness of a leaf.

    Answer

    The center has degree 4. Betweenness: there are 4·3/2 = 6 pairs among the four leaves, and the single shortest path of each passes through the center: 6. Closeness of the center is 4/(1+1+1+1) = 1. A leaf is 1 step from the center and 2 from each of the other three leaves: 4/(1 + 2 + 2 + 2) = 4/7 ≈ 0.57.

  2. In the network with edges A–B, B–C, A–C, C–D (a triangle with one pendant node), find the local clustering coefficient of each node and the transitivity.

    Answer

    A: neighbors B, C, and B–C is tied: 1. B: the same, 1. C: neighbors A, B, D; of 3 possible pairs only A–B is tied: 1/3. D: degree 1, so 0 by convention. Average (1 + 1 + 1/3 + 0)/4 ≈ 0.58. Transitivity: one triangle, connected triples 1 + 1 + 3 + 0 = 5: 3·1/5 = 0.6.

  3. In the network made of two separate edges, A–B and C–D, compute the modularity of the partition {A, B}, {C, D}.

    Answer

    m = 2, total degree 4. Each community has 1 internal edge and degree sum 2: 1/2 − (2/4)2 = 0.5 − 0.25 = 0.25. The two communities give Q = 0.5. The network consists of two completely separate pieces, so the partition is very sharp.

  4. In a ring of six people (each tied to the two beside them) the average path length is 1.8. If one edge is added between two people sitting opposite each other, what does the average path become?

    Answer

    In the ring the distances over the 15 pairs add up to 6·1 + 6·2 + 3·3 = 27, and 27/15 = 1.8. The new edge only reduces the distance of that one pair from 3 to 1 (no other pair gets a shorter path). The total is 25 and the average 25/15 ≈ 1.67.

  5. In a school network, is the student with the most friends the most influential student? Argue using the example network of these notes.

    Answer

    Not necessarily. In the example network C and D lead by degree by only 3 against 2, but by betweenness they lead 6 against 0: they are the only bridges. If the friends of a well-connected student are all in his or her own group, the student's place in carrying information across the network may be limited. Influence should be studied with the measure that fits the question (degree, closeness, betweenness), and one must remember that being connected does not mean influencing (homophily).

  6. A bank wants (a) to predict which customer will give up a credit card and (b) to find rings of fake accounts that share the same device and phone number. Which is more a social network analysis question and which more a data mining question?

    Answer

    (a) is prediction on a table built from each customer's own traits (age, spending, tenure), closer to data mining and machine learning. (b) studies the structure of the links between accounts through shared devices and numbers: accounts are nodes, sharing is an edge, and one looks for connected components and communities. This is a social network analysis question. In practice the two are combined: measures taken from the network enter the prediction model as features.

30.14After this course

After this course you can move on to learning on networks with graph neural networks, link-based ranking with information retrieval, and diffusion models with epidemiology. Computational social science is an interdisciplinary field that applies these methods to questions about society.

Related careers: Network scientist, Data scientist, Social media analyst, Fraud analyst, Computational social scientist.

30.15Further reading

  • M. Newman, Networks (2nd ed., 2018): gives degree, centrality, clustering, small-world and modularity in full mathematical detail; the reference book of the field.
  • D. Easley and J. Kleinberg, Networks, Crowds, and Markets (2010): explains weak ties, small worlds and diffusion with everyday examples and little mathematics.
  • S. Wasserman and K. Faust, Social Network Analysis: Methods and Applications (1994): the classic textbook of the field; collecting network data, measures and social science methods.

30.16References

  1. Wasserman and Faust, Social Network Analysis: Methods and Applications, Cambridge University Press, 1994.
  2. Easley and Kleinberg, Networks, Crowds, and Markets, Cambridge University Press, 2010.
  3. Newman, Networks, 2nd ed., Oxford University Press, 2018.
  4. Barabási, Network Science, Cambridge University Press, 2016.

Related pages: Slides · Slides (PDF) · Course page