Guest
Sign in to keep your graphs and progress.

Practice book
Graph Theory Through Problems300 Exercises with Hints, Worked Algorithms and Complete Solutions
By Mohammed Islam Hadjoudj
Learn graph theory by solving problems: 300 exercises, from routine checks to challenging proofs, each with a complete solution.
Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on AmazonSelect an algorithm and generate steps to begin visualization
Are your friends friends with each other?
Measures how tightly knit each vertex's neighbourhood is: the share of neighbour pairs that are linked, then the network's average and its transitivity
Select an algorithm and generate steps to begin visualization
The local clustering coefficient of a vertex measures how tightly knit its neighbourhood is: of all pairs of its neighbours, what share are linked to each other? Each such link closes a triangle through the vertex. A vertex whose friends all know each other scores 1, the centre of a star scores 0. Averaged over the network, or measured globally as the transitivity, it tells whether a network is cliquish, like most social networks, or tree-like.
For each vertex v with k neighbours there are k(k − 1)/2 pairs of neighbours. The algorithm counts the pairs that are joined by an edge, the links, and divides: C(v) = links / (k(k − 1)/2). Vertices with fewer than two neighbours get 0 by convention. The average clustering is the mean of C(v). The transitivity counts globally: 3 × triangles divided by connected triples (paths of two edges), since each triangle closes three triples. The two can differ a lot, because the average gives small-degree vertices as much weight as hubs. Counting neighbour pairs takes O(Σ k²) time. The visualizer shows each vertex's neighbours, the linked pairs in green and the missing ones dashed.
Watts and Strogatz used the clustering coefficient in 1998 to define small-world networks: high clustering like a regular lattice, short paths like a random graph. It is a standard statistic in social network analysis, in biology (protein interaction and neural networks) and for testing network models, and a vertex's clustering helps tell brokers between groups (low) from members of tight groups (high). It also feeds link prediction: two people with many common friends who are friends with each other are likely to meet.
One pass over the vertices: count the links among each vertex's neighbours, then two network-wide averages.
Clustering(G):
for each vertex v:
k = degree(v)
if k < 2:
C[v] = 0
continue
links = number of edges between two neighbours of v
C[v] = links / (k * (k - 1) / 2)
average = mean of C[v]
transitivity = 3 * triangles / connected triples
return C, average, transitivitySumming the links over all vertices counts each triangle three times, once at each corner. So the transitivity can be written as the sum of the links over the sum of the neighbour pairs, both already computed in the loop.
The clustering of the eight-vertex example loaded on this page.
Example graph: A, B, C and D form a clique (all six edges). D is joined to E, and E to F, G and H. F and G are also joined.
Average clustering 0.71, transitivity 0.65. The average is higher because the small corner F, G scores 1 twice, while the transitivity weighs each vertex by how many pairs of neighbours it has.
Time: O(Σ k(v)²) · Space: O(n + m)
A vertex of degree k has k(k − 1)/2 neighbour pairs, and checking a pair takes O(1) with a hash set of edges, so the total is O(Σ k²), at most O(n·d²) for the largest degree d. On graphs with heavy hubs this sum is dominated by the hubs. Triangle-listing methods that orient each edge towards the higher-degree end bring it down to O(m·√m).
Several numbers describe how clustered a network is. They answer slightly different questions.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Average local clustering | How cliquish a typical vertex's neighbourhood is (Watts-Strogatz). Every vertex counts the same. | O(Σ k²) |
| Transitivity (global clustering) | The chance that a random connected triple is closed. Hubs weigh more. | O(Σ k²) |
| Triangle count per edge | How embedded a single relationship is, used for community detection and link prediction. | O(m·√m) |
Read the full article: Graph Centrality Measures: Degree, Betweenness, PageRank
Read the full article: Community Detection: Modularity, Louvain, Girvan-Newman
Related algorithms: Louvain Community Detection, Betweenness Centrality, Label Propagation, PageRank, Maximal Clique