{"algorithm":"connected-components","about":["Two vertices are in the same connected component when some path joins them. The components are the separate pieces of a graph: no edge runs between two of them, so nothing in one piece can reach anything in another.","gograph searches breadth-first from the first vertex and gives every vertex it reaches the same component. When that search runs out, it starts the next one at the first vertex no search has reached, and so on, so the components come out in the order of their first vertex. Every vertex and edge is looked at once, so it costs O(V+E).","On a directed graph, gograph ignores the edge directions and returns the weakly connected components. It joins the two ends of each edge with union-find instead of searching, since a vertex doesn't keep a list of the edges that end at it. To find the groups that can reach each other along the edge directions, use Tarjan, Kosaraju or Gabow. Use connected components to find the islands of a network after an outage, or to handle each piece of a graph on its own."],"template":{"id":"home-network","name":"Home network","description":"A router and the devices connected to it.","family":"undirected","directed":false,"weighted":false,"acyclic":false,"vertices":[{"id":"Router","x":500,"y":320},{"id":"Laptop","x":260,"y":180},{"id":"Phone","x":740,"y":180},{"id":"TV","x":740,"y":480},{"id":"Printer","x":260,"y":480},{"id":"Speaker","x":900,"y":640}],"edges":[{"from":"Router","to":"Laptop"},{"from":"Router","to":"Phone"},{"from":"Router","to":"TV"},{"from":"Router","to":"Printer"},{"from":"Laptop","to":"Printer"},{"from":"TV","to":"Speaker"}],"details":{"vertices":6,"edges":6,"directed":false,"weighted":false,"acyclic":false,"negativeWeights":false,"density":0.4,"degree":{"min":1,"avg":2,"max":4},"selfLoops":0,"components":1}},"problem":"Is every device in the home connected to every other, or are some cut off?","params":{},"result":{"kind":"groups","groups":[["Laptop","Phone","Printer","Router","Speaker","TV"]]},"answer":"The graph is connected: all 6 vertices are in one component, so every vertex can reach every other.","legend":{"vertex":[{"id":"idle","label":"Not reached","color":"#64748B","shape":"hollow"},{"id":"waiting","label":"In queue","color":"#E69F00","shape":"dashed-square"},{"id":"current","label":"Being checked","color":"#56B4E9","shape":"ring"},{"id":"done","label":"In a component","color":"#009E73","shape":"filled"},{"id":"component-1","label":"Component 1, 6 vertices","color":"#56B4E9","shape":"filled"}],"edge":[{"id":"idle","label":"Edge","color":"#475569","shape":"line"},{"id":"tree","label":"Reached a new vertex","color":"#009E73","shape":"thick"}],"panels":[{"id":"queue","title":"Queue","kind":"list"},{"id":"components","title":"Components found","kind":"groups"}]},"steps":[{"say":"ConnectedComponents searches breadth-first from each vertex no earlier search has reached, in the graph's vertex order. Every vertex a search reaches is in the same component as the vertex it started from. When the queue runs out, the component is complete."},{"say":"Start component 1 at Router, the first vertex. Put it in the queue.","ops":[{"v":"Router","state":"waiting"},{"panel":"queue","list":[{"key":"Router"}]}]},{"say":"Take Router from the front of the queue. Its neighbors Laptop, Phone, TV and Printer are new, so they join the back of the queue.","ops":[{"v":"Router","state":"current"},{"v":"Laptop","state":"waiting"},{"e":["Router","Laptop"],"state":"tree"},{"v":"Phone","state":"waiting"},{"e":["Router","Phone"],"state":"tree"},{"v":"TV","state":"waiting"},{"e":["Router","TV"],"state":"tree"},{"v":"Printer","state":"waiting"},{"e":["Router","Printer"],"state":"tree"},{"panel":"queue","list":[{"key":"Laptop"},{"key":"Phone"},{"key":"TV"},{"key":"Printer"}]}]},{"say":"Take Laptop from the front of the queue. Every neighbor it has is already in the component.","ops":[{"v":"Laptop","state":"current"},{"v":"Router","state":"done"},{"panel":"queue","list":[{"key":"Phone"},{"key":"TV"},{"key":"Printer"}]}]},{"say":"Take Phone from the front of the queue. Every neighbor it has is already in the component.","ops":[{"v":"Phone","state":"current"},{"v":"Laptop","state":"done"},{"panel":"queue","list":[{"key":"TV"},{"key":"Printer"}]}]},{"say":"Take TV from the front of the queue. Its neighbor Speaker is new, so it joins the back of the queue.","ops":[{"v":"TV","state":"current"},{"v":"Phone","state":"done"},{"v":"Speaker","state":"waiting"},{"e":["TV","Speaker"],"state":"tree"},{"panel":"queue","list":[{"key":"Printer"},{"key":"Speaker"}]}]},{"say":"Take Printer from the front of the queue. Every neighbor it has is already in the component.","ops":[{"v":"Printer","state":"current"},{"v":"TV","state":"done"},{"panel":"queue","list":[{"key":"Speaker"}]}]},{"say":"Take Speaker from the front of the queue. Every neighbor it has is already in the component.","ops":[{"v":"Speaker","state":"current"},{"v":"Printer","state":"done"},{"panel":"queue","list":[]}]},{"say":"The queue is empty, so component 1 is complete with 6 vertices.","ops":[{"v":"Router","state":"done"},{"v":"Laptop","state":"done"},{"v":"Phone","state":"done"},{"v":"TV","state":"done"},{"v":"Printer","state":"done"},{"v":"Speaker","state":"done"},{"panel":"components","groups":[["Router","Laptop","Phone","TV","Printer","Speaker"]]}]},{"say":"Done. ConnectedComponents found 1 component. Every vertex now shows the component it belongs to.","ops":[{"v":"Router","state":"component-1"},{"v":"Laptop","state":"component-1"},{"v":"Phone","state":"component-1"},{"v":"TV","state":"component-1"},{"v":"Printer","state":"component-1"},{"v":"Speaker","state":"component-1"}]}],"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":"for _, component := range gograph.ConnectedComponents(g) {\n\tvar names []string\n\tfor _, v := range component {\n\t\tnames = append(names, v.Label())\n\t}\n\tfmt.Println(strings.Join(names, \", \"))\n}"}
