{"algorithm":"girvan-newman","about":["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."],"template":{"id":"friend-groups","name":"Friend groups","description":"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.","family":"undirected","directed":false,"weighted":false,"acyclic":false,"vertices":[{"id":"Ana","x":120,"y":160},{"id":"Ben","x":380,"y":120},{"id":"Cara","x":400,"y":380},{"id":"Dev","x":140,"y":420},{"id":"Eli","x":320,"y":620},{"id":"Fay","x":520,"y":560},{"id":"Gus","x":500,"y":840},{"id":"Hana","x":700,"y":700},{"id":"Ivo","x":640,"y":440},{"id":"Jun","x":860,"y":520},{"id":"Kai","x":640,"y":200},{"id":"Lea","x":880,"y":260}],"edges":[{"from":"Ana","to":"Ben"},{"from":"Ana","to":"Cara"},{"from":"Ana","to":"Dev"},{"from":"Ben","to":"Cara"},{"from":"Ben","to":"Dev"},{"from":"Cara","to":"Dev"},{"from":"Cara","to":"Eli"},{"from":"Dev","to":"Eli"},{"from":"Eli","to":"Fay"},{"from":"Eli","to":"Gus"},{"from":"Fay","to":"Gus"},{"from":"Fay","to":"Hana"},{"from":"Gus","to":"Hana"},{"from":"Hana","to":"Ivo"},{"from":"Hana","to":"Jun"},{"from":"Ivo","to":"Jun"},{"from":"Ivo","to":"Kai"},{"from":"Ivo","to":"Lea"},{"from":"Jun","to":"Kai"},{"from":"Jun","to":"Lea"},{"from":"Kai","to":"Lea"}],"details":{"vertices":12,"edges":21,"directed":false,"weighted":false,"acyclic":false,"negativeWeights":false,"density":0.3181818181818182,"degree":{"min":3,"avg":3.5,"max":4},"selfLoops":0,"components":1}},"problem":"Can the three circles be found from who knows whom alone? Split the twelve friends into 3 communities.","params":{"k":3},"result":{"kind":"sets","sets":[["Ana","Ben","Cara","Dev","Eli"],["Fay","Gus","Hana"],["Ivo","Jun","Kai","Lea"]]},"answer":"The 3 communities have 5, 3 and 4 vertices: Ana, Ben, Cara, Dev and Eli; Fay, Gus and Hana; Ivo, Jun, Kai and Lea.","legend":{"vertex":[{"id":"idle","label":"Vertex","color":"#64748B","shape":"hollow"},{"id":"current","label":"End of the removed edge","color":"#56B4E9","shape":"ring"}],"edge":[{"id":"idle","label":"Edge","color":"#475569","shape":"line"},{"id":"removed","label":"Removed","color":"#CC79A7","shape":"dashed"}],"panels":[{"id":"betweenness","title":"Highest betweenness","kind":"table"},{"id":"communities","title":"Communities","kind":"groups"}]},"steps":[{"say":"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.","ops":[{"panel":"communities","groups":[["Ana","Ben","Cara","Dev","Eli","Fay","Gus","Hana","Ivo","Jun","Kai","Lea"]]}]},{"say":"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.","ops":[{"v":"Eli","state":"current"},{"v":"Fay","state":"current"},{"e":["Eli","Fay"],"state":"removed"},{"panel":"betweenness","set":{"Eli and Fay":"17.5"}},{"panel":"betweenness","set":{"Eli and Gus":"17.5"}},{"panel":"betweenness","set":{"Fay and Hana":"17.5"}},{"panel":"betweenness","set":{"Gus and Hana":"17.5"}},{"panel":"betweenness","set":{"Cara and Eli":"16"}}]},{"say":"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.","ops":[{"v":"Fay","state":"idle"},{"v":"Eli","state":"current"},{"v":"Gus","state":"current"},{"e":["Eli","Gus"],"state":"removed"},{"panel":"betweenness","unset":["Eli and Fay","Eli and Gus","Fay and Hana","Gus and Hana","Cara and Eli"]},{"panel":"betweenness","set":{"Eli and Gus":"35"}},{"panel":"betweenness","set":{"Gus and Hana":"30"}},{"panel":"betweenness","set":{"Cara and Eli":"16"}},{"panel":"betweenness","set":{"Dev and Eli":"16"}},{"panel":"betweenness","set":{"Hana and Ivo":"16"}},{"panel":"communities","groups":[["Ana","Ben","Cara","Dev","Eli"],["Fay","Gus","Hana","Ivo","Jun","Kai","Lea"]]}]},{"say":"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.","ops":[{"v":"Eli","state":"idle"},{"v":"Gus","state":"idle"},{"v":"Hana","state":"current"},{"v":"Ivo","state":"current"},{"e":["Hana","Ivo"],"state":"removed"},{"panel":"betweenness","unset":["Eli and Gus","Gus and Hana","Cara and Eli","Dev and Eli","Hana and Ivo"]},{"panel":"betweenness","set":{"Hana and Ivo":"6"}},{"panel":"betweenness","set":{"Hana and Jun":"6"}},{"panel":"betweenness","set":{"Fay and Hana":"5"}},{"panel":"betweenness","set":{"Gus and Hana":"5"}},{"panel":"betweenness","set":{"Ivo and Kai":"2.5"}}]},{"say":"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.","ops":[{"v":"Ivo","state":"idle"},{"v":"Hana","state":"current"},{"v":"Jun","state":"current"},{"e":["Hana","Jun"],"state":"removed"},{"panel":"betweenness","unset":["Hana and Ivo","Hana and Jun","Fay and Hana","Gus and Hana","Ivo and Kai"]},{"panel":"betweenness","set":{"Hana and Jun":"12"}},{"panel":"betweenness","set":{"Fay and Hana":"5"}},{"panel":"betweenness","set":{"Gus and Hana":"5"}},{"panel":"betweenness","set":{"Ivo and Jun":"4"}},{"panel":"betweenness","set":{"Jun and Kai":"4"}},{"panel":"communities","groups":[["Ana","Ben","Cara","Dev","Eli"],["Fay","Gus","Hana"],["Ivo","Jun","Kai","Lea"]]}]},{"say":"Done. Removing 4 edges left 3 communities.","ops":[{"v":"Hana","state":"idle"},{"v":"Jun","state":"idle"}]}],"traceNote":"","verification":"The steps come from a step-by-step copy of the algorithm. Its result is checked against gograph's on every start.","snippet":"communities, err := partition.GirvanNewman(g, 3)\nif err != nil {\n\treturn err\n}\nfor _, c := range communities {\n\tvar members []string\n\tfor _, v := range c.GetAllVertices() {\n\t\tmembers = append(members, v.Label())\n\t}\n\tfmt.Println(members)\n}"}
