Girvan-Newman
Splits a graph into k communities by removing, one at a time, the edge that the most shortest paths cross.
Time complexity O(E·V·(V+E)), gograph function partition.GirvanNewman.
How it works
Girvan-Newman finds communities by taking a graph apart. The few edges between two tight groups carry many shortest paths, because every route from one group to the other has to cross them. The algorithm counts how many shortest paths between pairs of vertices use each edge, the edge's betweenness. It removes the edge with the highest count, then counts again.
It stops once the graph has fallen into k connected components, and those are the communities. gograph counts with Brandes' method, a breadth-first search from every vertex, so the length of a path is its number of edges and weights aren't used. That costs O(V·(V+E)) for each edge it removes, and O(E·V·(V+E)) in the worst case. When edges tie, it removes the first one in the graph's edge order, so every run gives the same result. Scores within one part in a billion of the highest count as tied, since the same paths added up in another order can round differently.
It is slow on large graphs, but each split has a clear reason: it removes the link that the most routes cross. A directed graph splits as if it were undirected, and each community keeps the direction of its edges. It works on a copy, so the input stays as it was. Each community it returns keeps all of its edges, including the ones removed along the way.
Karate club
The 34 members of a university karate club, numbered 1 to 34 as in the study. Each edge joins two members who also spent time together outside the club's classes and meetings. A dispute between the instructor (1) and an officer (34) split the club in two, and most members sit near the side they joined.
An undirected graph with 34 vertices and 78 edges.
The club split in two after the dispute. Using only who spent time with whom, predict which side each member joined.
The 2 communities have 15 and 19 vertices: 1, 2, 4, 5, 6, 7, 8, 11, 12, 13 and 5 more; 3, 9, 10, 15, 16, 19, 21, 23, 24, 25 and 9 more.
Step by step
- Girvan-Newman removes edges one at a time until the graph falls apart into k = 2 communities. Before each removal it scores every edge by its betweenness: the number of shortest paths between pairs of vertices that cross it, with tied shortest paths sharing the credit. Then it removes the edge with the highest score. The graph starts as one community.
- Remove the edge between 1 and 32. Its betweenness, 71.39, is the highest. The graph is still in one piece.
- Remove the edge between 1 and 3. Its betweenness, 66.9, is the highest. The graph is still in one piece.
- Remove the edge between 1 and 9. Its betweenness, 77.32, is the highest. The graph is still in one piece.
- Remove the edge between 14 and 34. Its betweenness, 82, is the highest. The graph is still in one piece.
- Remove the edge between 20 and 34. Its betweenness, 123.23, is the highest. The graph is still in one piece.
6 more steps lead to the last one:
- Done. Removing 11 edges left 2 communities. gograph returns each community with every edge between its members, so the removed edge between 3 and 33 comes back.
Use it in Go
communities, err := partition.GirvanNewman(g, 2)
if err != nil {
return err
}
for _, c := range communities {
var members []string
for _, v := range c.GetAllVertices() {
members = append(members, v.Label())
}
fmt.Println(members)
}
Example graphs
- Friend groups, 12 vertices and 21 edges
- Office network, 13 vertices and 16 edges
- Karate club, 34 vertices and 78 edges
- Campus network, 100 vertices and 114 edges
More in Partitioning
- Maximal cliques: Finds every group of vertices that are all adjacent to each other and can't take one more vertex.
- Randomized k-cut: Splits an undirected graph into k groups by merging the ends of random edges until k groups are left, and cuts the edges between them.