{"algorithm":"randomized-k-cut","about":["The randomized k-cut starts with each vertex in its own group. It picks an edge at random, merges the groups at its two ends, and drops the edges that now run inside one group. It repeats until k groups are left. The edges still running between groups are the cut.","This is Karger's contraction method for the minimum cut, applied to k groups. A dense cluster has many edges, so a random edge is more likely to fall inside one, and clusters tend to merge before the few edges between them are picked. One run can still miss the smallest cut, so repeat it and keep the cut with the fewest edges.","One run costs O(V·E). k must be at least 2 and no more than the number of vertices. If the graph has more than k connected components, the edges run out first and each component is its own group. Use it to split a network into parts with few links between them, such as friend groups or shards of a service map."],"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":"Split the twelve friends into 3 groups and try to cut as few friendships as possible.","params":{"k":3},"result":{"kind":"cut","groups":[["Ana","Ben","Dev","Eli","Fay","Gus","Hana"],["Cara"],["Ivo","Jun","Kai","Lea"]],"edges":[["Ana","Cara"],["Ben","Cara"],["Cara","Dev"],["Cara","Eli"],["Hana","Ivo"],["Hana","Jun"]]},"answer":"On this run, the cut splits the vertices into 3 groups, of 7, 1 and 4 vertices, and cuts 6 edges: Ana and Cara; Ben and Cara; Cara and Dev; Cara and Eli; Hana and Ivo; Hana and Jun.","legend":{"vertex":[{"id":"idle","label":"A supernode of its own","color":"#64748B","shape":"hollow"},{"id":"current","label":"End of the contracted edge","color":"#56B4E9","shape":"ring"},{"id":"done","label":"In a merged supernode","color":"#009E73","shape":"filled"},{"id":"component-1","label":"Supernode 1, 7 vertices","color":"#56B4E9","shape":"filled"},{"id":"component-3","label":"Supernode 3, 4 vertices","color":"#CC79A7","shape":"dashed-square"},{"id":"single","label":"A supernode of one vertex","color":"#CBD5E1","shape":"hollow"}],"edge":[{"id":"idle","label":"Edge","color":"#475569","shape":"line"},{"id":"tree","label":"Contracted","color":"#009E73","shape":"thick"},{"id":"cut","label":"In the cut, between two supernodes","color":"#D55E00","shape":"dashed"}],"panels":[{"id":"supernodes","title":"Supernodes","kind":"groups"}]},"steps":[{"say":"Randomized k-cut starts with each of the 12 vertices as a supernode of its own. gograph shuffles the edges and takes them in that order. An edge between two supernodes is contracted: the two merge into one, and the edges between them now lie inside it and are dropped. It stops when k = 3 supernodes are left, and the edges between them form the cut. These steps contract only edges inside the supernodes gograph ended with, in the graph's edge order, so they end in the same cut.","ops":[{"panel":"supernodes","groups":[["Ana"],["Ben"],["Cara"],["Dev"],["Eli"],["Fay"],["Gus"],["Hana"],["Ivo"],["Jun"],["Kai"],["Lea"]]}]},{"say":"Contract the edge between Ana and Ben. Ana and Ben form a supernode of 2 vertices. 11 supernodes are left.","ops":[{"v":"Ana","state":"current"},{"v":"Ben","state":"current"},{"e":["Ana","Ben"],"state":"tree"},{"panel":"supernodes","groups":[["Ana","Ben"],["Cara"],["Dev"],["Eli"],["Fay"],["Gus"],["Hana"],["Ivo"],["Jun"],["Kai"],["Lea"]]}]},{"say":"Contract the edge between Ana and Dev. Dev joins the supernode of Ana and Ben, which now has 3 vertices. The edge between Ben and Dev joined the same two supernodes, so it now lies inside the new one and is dropped. 10 supernodes are left.","ops":[{"v":"Ana","state":"done"},{"v":"Ben","state":"done"},{"v":"Ana","state":"current"},{"v":"Dev","state":"current"},{"e":["Ana","Dev"],"state":"tree"},{"panel":"supernodes","groups":[["Ana","Ben","Dev"],["Cara"],["Eli"],["Fay"],["Gus"],["Hana"],["Ivo"],["Jun"],["Kai"],["Lea"]]}]},{"say":"Contract the edge between Dev and Eli. Eli joins the supernode of Ana, Ben and Dev, which now has 4 vertices. 9 supernodes are left.","ops":[{"v":"Ana","state":"done"},{"v":"Dev","state":"done"},{"v":"Dev","state":"current"},{"v":"Eli","state":"current"},{"e":["Dev","Eli"],"state":"tree"},{"panel":"supernodes","groups":[["Ana","Ben","Dev","Eli"],["Cara"],["Fay"],["Gus"],["Hana"],["Ivo"],["Jun"],["Kai"],["Lea"]]}]},{"say":"Contract the edge between Eli and Fay. Fay joins the supernode that holds Eli, which now has 5 vertices. 8 supernodes are left.","ops":[{"v":"Dev","state":"done"},{"v":"Eli","state":"done"},{"v":"Eli","state":"current"},{"v":"Fay","state":"current"},{"e":["Eli","Fay"],"state":"tree"},{"panel":"supernodes","groups":[["Ana","Ben","Dev","Eli","Fay"],["Cara"],["Gus"],["Hana"],["Ivo"],["Jun"],["Kai"],["Lea"]]}]},{"say":"Contract the edge between Eli and Gus. Gus joins the supernode that holds Eli, which now has 6 vertices. The edge between Fay and Gus joined the same two supernodes, so it now lies inside the new one and is dropped. 7 supernodes are left.","ops":[{"v":"Eli","state":"done"},{"v":"Fay","state":"done"},{"v":"Eli","state":"current"},{"v":"Gus","state":"current"},{"e":["Eli","Gus"],"state":"tree"},{"panel":"supernodes","groups":[["Ana","Ben","Dev","Eli","Fay","Gus"],["Cara"],["Hana"],["Ivo"],["Jun"],["Kai"],["Lea"]]}]},{"say":"Contract the edge between Fay and Hana. Hana joins the supernode that holds Fay, which now has 7 vertices. The edge between Gus and Hana joined the same two supernodes, so it now lies inside the new one and is dropped. 6 supernodes are left.","ops":[{"v":"Eli","state":"done"},{"v":"Gus","state":"done"},{"v":"Fay","state":"current"},{"v":"Hana","state":"current"},{"e":["Fay","Hana"],"state":"tree"},{"panel":"supernodes","groups":[["Ana","Ben","Dev","Eli","Fay","Gus","Hana"],["Cara"],["Ivo"],["Jun"],["Kai"],["Lea"]]}]},{"say":"Contract the edge between Ivo and Jun. Ivo and Jun form a supernode of 2 vertices. 5 supernodes are left.","ops":[{"v":"Fay","state":"done"},{"v":"Hana","state":"done"},{"v":"Ivo","state":"current"},{"v":"Jun","state":"current"},{"e":["Ivo","Jun"],"state":"tree"},{"panel":"supernodes","groups":[["Ana","Ben","Dev","Eli","Fay","Gus","Hana"],["Cara"],["Ivo","Jun"],["Kai"],["Lea"]]}]},{"say":"Contract the edge between Ivo and Kai. Kai joins the supernode of Ivo and Jun, which now has 3 vertices. The edge between Jun and Kai joined the same two supernodes, so it now lies inside the new one and is dropped. 4 supernodes are left.","ops":[{"v":"Ivo","state":"done"},{"v":"Jun","state":"done"},{"v":"Ivo","state":"current"},{"v":"Kai","state":"current"},{"e":["Ivo","Kai"],"state":"tree"},{"panel":"supernodes","groups":[["Ana","Ben","Dev","Eli","Fay","Gus","Hana"],["Cara"],["Ivo","Jun","Kai"],["Lea"]]}]},{"say":"Contract the edge between Ivo and Lea. Lea joins the supernode of Ivo, Jun and Kai, which now has 4 vertices. The 2 other edges between the two supernodes now lie inside the new one and are dropped. That leaves k = 3 supernodes.","ops":[{"v":"Ivo","state":"done"},{"v":"Kai","state":"done"},{"v":"Ivo","state":"current"},{"v":"Lea","state":"current"},{"e":["Ivo","Lea"],"state":"tree"},{"panel":"supernodes","groups":[["Ana","Ben","Dev","Eli","Fay","Gus","Hana"],["Cara"],["Ivo","Jun","Kai","Lea"]]}]},{"say":"Done. After 9 contractions, k = 3 supernodes are left, so the contraction stops. The 6 edges between different supernodes form the cut. Every vertex now shows its supernode.","ops":[{"v":"Ana","state":"component-1"},{"v":"Ben","state":"component-1"},{"v":"Dev","state":"component-1"},{"v":"Eli","state":"component-1"},{"v":"Fay","state":"component-1"},{"v":"Gus","state":"component-1"},{"v":"Hana","state":"component-1"},{"v":"Cara","state":"single"},{"v":"Ivo","state":"component-3"},{"v":"Jun","state":"component-3"},{"v":"Kai","state":"component-3"},{"v":"Lea","state":"component-3"},{"e":["Ana","Cara"],"state":"cut"},{"e":["Ben","Cara"],"state":"cut"},{"e":["Cara","Dev"],"state":"cut"},{"e":["Cara","Eli"],"state":"cut"},{"e":["Hana","Ivo"],"state":"cut"},{"e":["Hana","Jun"],"state":"cut"}]}],"traceNote":"","verification":"The steps replay one contraction order that leads to the cut gograph returned when the server started, not gograph's own random order.","snippet":"cut, err := partition.RandomizedKCut(g, 3)\nif err != nil {\n\treturn err\n}\nfor _, group := range cut.Supernodes {\n\tvar members []string\n\tfor _, v := range group {\n\t\tmembers = append(members, v.Label())\n\t}\n\tfmt.Println(members)\n}\nfor _, e := range cut.CutEdges {\n\tfmt.Println(e.Source().Label(), e.Destination().Label())\n}"}
