learngraphtheory.org

Guest

Sign in to keep your graphs and progress.

Cover of the book Graph Theory Through Problems

Practice book

Graph Theory Through Problems

300 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.

Available inKindlePaperback

13 chapters

Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on Amazon
Algorithm Selection

Select an algorithm and generate steps to begin visualization

Clustering Coefficient Calculator and Visualizer

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

Time: O(Σ k²)
Space: O(n + m)
Use Case: Social network analysis, small-world networks, biology
Showing the example graph for this algorithm.
Algorithm Execution

Select an algorithm and generate steps to begin visualization

About Clustering Coefficient

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.

How it works

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.

Applications

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.

Pseudocode

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, transitivity

Summing 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.

Worked example, step by step

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.

  1. The clique. A, B and C each have three neighbours, all linked to each other: 3 of 3 pairs, C = 1.
  2. D. Four neighbours, so six pairs. The three among A, B, C are linked, the three with E are not: C(D) = 0.50.
  3. E. Four neighbours, D, F, G and H. Of their six pairs only F–G is linked: C(E) = 1/6 = 0.17.
  4. F, G and H. F and G have two neighbours each, which are linked: C = 1. H has one neighbour, so C(H) = 0.
  5. The network. The eight values average to 0.71. There are 5 triangles (four in the clique, plus E, F, G) and 23 connected triples, so the transitivity is 15 / 23 = 0.65.

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.

Complexity, and where it comes from

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).

When to use Clustering Coefficient, and when not to

Several numbers describe how clustered a network is. They answer slightly different questions.

AlternativePrefer it whenCost
Average local clusteringHow 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 edgeHow embedded a single relationship is, used for community detection and link prediction.O(m·√m)

Common pitfalls

  • Mixing up the two global numbers. The average of C(v) and the transitivity can differ a lot, as in the example (0.71 against 0.65). Say which one a figure means.
  • Low-degree vertices. Vertices with fewer than two neighbours have no pairs. Counting them as 0 lowers the average, leaving them out raises it. Software packages differ here.
  • Directed or weighted graphs. The formula assumes simple undirected edges. Directed and weighted versions exist, but they count differently and give other values.

Frequently asked questions

What does a clustering coefficient of 1 mean?
All neighbours of the vertex are linked to each other: the vertex and its neighbours form a clique.
What is a small-world network?
A network with high clustering, like a regular lattice, but short average paths, like a random graph. Watts and Strogatz showed that a few random shortcuts in a lattice create one.
Why is transitivity 3 × triangles / triples?
A connected triple is a path of two edges. Each triangle contains three of them, one centred at each corner, and closes all three.
Is a high clustering coefficient good?
It depends on the question. Clustering means local redundancy and trust in social networks, but also slower spreading of information between groups.

Related algorithms: Louvain Community Detection, Betweenness Centrality, Label Propagation, PageRank, Maximal Clique

An example graph: you can change it, or start blank.

Zoom Controls

30%
Nodes: 8
Edges: 11