{"algorithm":"condensation","about":["A strongly connected component is a group of vertices that can all reach each other. The condensation replaces each one with a single vertex, and all the edges from one component to another with a single edge. A cycle never leaves its component, so the condensed graph has none.","connectivity.Condense runs Tarjan once and numbers the components so that every edge between two of them goes from a lower number to a higher one, which is a topological order. The members of each component keep the graph's vertex order, and Members and ComponentOf map between the two graphs. It costs O(V+E).","WithComponentWeight gives each component a weight from its members, such as the sum of their weights, and WithCrossingWeight gives each condensed edge a weight from the edges it stands for. A build tool does this with packages that import each other: each loop is built as one unit, and the units are sorted like any other dependency graph."],"template":{"id":"service-calls","name":"Microservice calls","description":"Nine services of an online shop. Each edge is a call from one service to another, and some services call each other back.","family":"cyclic","directed":true,"weighted":false,"acyclic":false,"vertices":[{"id":"Gateway","x":96,"y":384},{"id":"Auth","x":320,"y":128},{"id":"Users","x":608,"y":128},{"id":"Orders","x":384,"y":384},{"id":"Payments","x":640,"y":320},{"id":"Shipping","x":640,"y":544},{"id":"Inventory","x":448,"y":640},{"id":"Search","x":192,"y":640},{"id":"Notify","x":896,"y":320}],"edges":[{"from":"Gateway","to":"Auth"},{"from":"Gateway","to":"Orders"},{"from":"Gateway","to":"Search"},{"from":"Auth","to":"Users"},{"from":"Users","to":"Auth"},{"from":"Users","to":"Notify"},{"from":"Orders","to":"Users"},{"from":"Orders","to":"Inventory"},{"from":"Orders","to":"Payments"},{"from":"Payments","to":"Shipping"},{"from":"Payments","to":"Notify"},{"from":"Shipping","to":"Orders"},{"from":"Shipping","to":"Notify"},{"from":"Inventory","to":"Search"},{"from":"Search","to":"Inventory"}],"details":{"vertices":9,"edges":15,"directed":true,"weighted":false,"acyclic":false,"negativeWeights":false,"density":0.20833333333333334,"inDegree":{"min":0,"avg":1.6666666666666667,"max":3},"outDegree":{"min":0,"avg":1.6666666666666667,"max":3},"selfLoops":0,"components":1}},"problem":"Which services call each other in a loop and have to be released together, and which way do the calls between those groups go?","params":{},"result":{"kind":"groups","groups":[["Gateway"],["Orders","Payments","Shipping"],["Inventory","Search"],["Auth","Users"],["Notify"]]},"answer":"The 9 vertices condense into 5 components joined by 7 edges, with no cycle left. The largest component has 3 vertices: Orders, Payments and Shipping. The components are listed in topological order: every edge between two of them goes from an earlier one to a later one, so in reverse, each component comes after every component it has an edge into.","legend":{"vertex":[{"id":"idle","label":"Not in a component yet","color":"#64748B","shape":"hollow"},{"id":"component-2","label":"Component 2, 3 vertices","color":"#56B4E9","shape":"filled"},{"id":"component-3","label":"Component 3, 2 vertices","color":"#CC79A7","shape":"dashed-square"},{"id":"component-4","label":"Component 4, 2 vertices","color":"#E69F00","shape":"filled"},{"id":"single","label":"A component of one vertex","color":"#CBD5E1","shape":"hollow"}],"edge":[{"id":"idle","label":"Edge","color":"#475569","shape":"line"},{"id":"inside","label":"Inside a component, dropped","color":"#475569","shape":"dashed"},{"id":"between","label":"Stands for the edges between two components","color":"#009E73","shape":"thick"},{"id":"merged","label":"Merged into the edge between the same components","color":"#CC79A7","shape":"dashed"}],"panels":[{"id":"components","title":"Components, in order","kind":"groups"},{"id":"edges","title":"Edges between components","kind":"list"}]},"steps":[{"say":"Condense runs Tarjan once to find the strongly connected components, the groups of vertices that can all reach each other. It numbers them so that every edge between two components goes from a lower number to a higher one. Then each component becomes one vertex, and the edges from one component to another become one edge."},{"say":"Component 1: Gateway reaches no vertex that reaches it back, so it is a component on its own.","ops":[{"v":"Gateway","state":"single"},{"panel":"components","groups":[["Gateway"]]}]},{"say":"Component 2: Orders, Payments and Shipping can all reach each other, so they become one vertex. The 3 edges among them drop out.","ops":[{"e":["Orders","Payments"],"state":"inside"},{"e":["Payments","Shipping"],"state":"inside"},{"e":["Shipping","Orders"],"state":"inside"},{"v":"Orders","state":"component-2"},{"v":"Payments","state":"component-2"},{"v":"Shipping","state":"component-2"},{"panel":"components","groups":[["Gateway"],["Orders","Payments","Shipping"]]}]},{"say":"Component 3: Inventory and Search reach each other, so they become one vertex. The 2 edges among them drop out.","ops":[{"e":["Inventory","Search"],"state":"inside"},{"e":["Search","Inventory"],"state":"inside"},{"v":"Inventory","state":"component-3"},{"v":"Search","state":"component-3"},{"panel":"components","groups":[["Gateway"],["Orders","Payments","Shipping"],["Inventory","Search"]]}]},{"say":"Component 4: Auth and Users reach each other, so they become one vertex. The 2 edges among them drop out.","ops":[{"e":["Auth","Users"],"state":"inside"},{"e":["Users","Auth"],"state":"inside"},{"v":"Auth","state":"component-4"},{"v":"Users","state":"component-4"},{"panel":"components","groups":[["Gateway"],["Orders","Payments","Shipping"],["Inventory","Search"],["Auth","Users"]]}]},{"say":"Component 5: Notify reaches no vertex that reaches it back, so it is a component on its own.","ops":[{"v":"Notify","state":"single"},{"panel":"components","groups":[["Gateway"],["Orders","Payments","Shipping"],["Inventory","Search"],["Auth","Users"],["Notify"]]}]},{"say":"Out of component 1: Gateway to Orders becomes the edge 1 to 2. Gateway to Search becomes the edge 1 to 3. Gateway to Auth becomes the edge 1 to 4.","ops":[{"e":["Gateway","Orders"],"state":"between"},{"e":["Gateway","Search"],"state":"between"},{"e":["Gateway","Auth"],"state":"between"},{"panel":"edges","list":[{"key":"1 to 2","note":"1 edge"},{"key":"1 to 3","note":"1 edge"},{"key":"1 to 4","note":"1 edge"}]}]},{"say":"Out of component 2: Orders to Inventory becomes the edge 2 to 3. Orders to Users becomes the edge 2 to 4. The 2 edges Payments to Notify and Shipping to Notify become one edge, 2 to 5.","ops":[{"e":["Orders","Inventory"],"state":"between"},{"e":["Orders","Users"],"state":"between"},{"e":["Payments","Notify"],"state":"between"},{"e":["Shipping","Notify"],"state":"merged"},{"panel":"edges","list":[{"key":"1 to 2","note":"1 edge"},{"key":"1 to 3","note":"1 edge"},{"key":"1 to 4","note":"1 edge"},{"key":"2 to 3","note":"1 edge"},{"key":"2 to 4","note":"1 edge"},{"key":"2 to 5","note":"2 edges"}]}]},{"say":"Out of component 4: The only edge, Users to Notify, becomes the edge 4 to 5.","ops":[{"e":["Users","Notify"],"state":"between"},{"panel":"edges","list":[{"key":"1 to 2","note":"1 edge"},{"key":"1 to 3","note":"1 edge"},{"key":"1 to 4","note":"1 edge"},{"key":"2 to 3","note":"1 edge"},{"key":"2 to 4","note":"1 edge"},{"key":"2 to 5","note":"2 edges"},{"key":"4 to 5","note":"1 edge"}]}]},{"say":"Done. The 9 vertices condense into 5 components and 7 edges between them. The condensed graph has no cycle, and its vertices are listed in topological order."}],"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":"sum := func(members []*gograph.Vertex[string]) float64 {\n\tvar total float64\n\tfor _, v := range members {\n\t\ttotal += v.Weight()\n\t}\n\treturn total\n}\nc, err := connectivity.Condense(g, connectivity.WithComponentWeight(sum))\nif err != nil {\n\treturn err\n}\nfor i, members := range c.Members {\n\tvar names []string\n\tfor _, v := range members {\n\t\tnames = append(names, v.Label())\n\t}\n\tfmt.Println(i, strings.Join(names, \", \"), c.Graph.GetVertexByID(i).Weight())\n}\nfor _, e := range c.Graph.AllEdges() {\n\tfmt.Println(e.Source().Label(), \"->\", e.Destination().Label())\n}"}
