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.
Friend groups
Twelve friends in three circles: a climbing group, a book club and a band. Each edge joins two friends. Eli climbs and reads, and Hana reads and plays in the band, so each of them links two circles.
An undirected graph with 12 vertices and 21 edges.
Can the three circles be found from who knows whom alone? Split the twelve friends into 3 communities.
The 3 communities have 5, 3 and 4 vertices: Ana, Ben, Cara, Dev and Eli; Fay, Gus and Hana; Ivo, Jun, Kai and Lea.
Step by step
- Girvan-Newman removes edges one at a time until the graph falls apart into k = 3 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 Eli and Fay. Its betweenness, 17.5, ties for the highest with 3 other edges, and it comes first in gograph's AllEdges order. The graph is still in one piece.
- Remove the edge between Eli and Gus. Its betweenness, 35, is the highest. Ana, Ben, Cara, Dev and Eli split off, so there are now 2 communities.
- Remove the edge between Hana and Ivo. Its betweenness, 6, ties for the highest with the edge between Hana and Jun, and it comes first in gograph's AllEdges order. There are still 2 communities.
- Remove the edge between Hana and Jun. Its betweenness, 12, is the highest. Fay, Gus and Hana split off, so there are now 3 communities.
- Done. Removing 4 edges left 3 communities.
Use it in Go
communities, err := partition.GirvanNewman(g, 3)
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.