The proposed problem can be solved with an algorithm called Flood Fill.
Important points to consider:
1) Implement a search algorithm (BFS or DFS) to traverse the graph nodes;
2) For each unidentified node that is found, you should assign a group identifier to it;
3) You should assign the same group identifier to all adjacent nodes, i.e., to all nodes that are accessible from that node;
4) When there are no more adjacent nodes that can be identified, you will start a new cycle by choosing another unnamed node;
5) Note that the fact that the new node is not identified implies that it was not accessible from our previous node and therefore is part of a different group of nodes;
6) When there are no more nodes to identify with, the number of identifiers is the same number of groups contained in the graph.
In this example graph, the identification of groups is done through distinct colors:
Pairs:
1) Grupo Azul:
a) 1-2
b) 2-3
c) 3-4
d) 3-5
e) 4-5
f) 4-7
g) 5-6
h) 6-7
2) Grupo Verde
i) 16-15
j) 15-17
k) 17-18
l) 15-18
m) 19-18
n) 19-17
o) 16-17
3) Grupo Vermelho:
p) 10-11
q) 14-11
r) 10-13
s) 12-13
t) 10-12
u) 12-14
v) 12-11
4) Grupo Amarelo
w) 8-9
This link also seemed interesting: Connected-Component Labeling
I hope I’ve helped!
In this example you passed, who without would be the A, the B and the C?
– Reginaldo Rigo
What is this connection? I don’t understand the criterion. honestly it’s still quite confused.
– user28595
@Diegof changed the explanation, did you understand? anything I put a graph explaining the links. Thank you
– RafaelPinheiro
@Reginaldorigo In example A, B and C could be any numbers, it’s just an example I meant, if the number A has connection to the number B and the number B has connection to C they will be grouped in the same group.
– RafaelPinheiro
really messed up your question.. I wish I could help you but it’s hard to interpret your doubt... so far I didn’t mean because "A" got the numbers 0,1,2,4,5 and B got the numbers 3,6,7
– Tiago
@James follows the modifications with a graph!
– RafaelPinheiro
@Diegof Following modifications with a graph
– RafaelPinheiro
In the graph I can see the link. But how can the program 'see' it? What in the matrix tells the program that there is this connection? Which rule could be applied?
– Reginaldo Rigo
@Reginaldorigo the program input is the matrix I put in the first line it knows that the '0' is on the '1' in the second line it knows that the '1' is on the '2' and so on. I didn’t want to use an adjacency matrix to show the links because I already have this matrix with the pairs at hand.
– RafaelPinheiro
This could be so. The routine starts and creates group 1 and adds to groups. Starts the loop from line 2. Reads the line and asks if the first or second number is contained in any existing group if yes adds that line to that group. Otherwise create a new group and add that line to the new group created. And return to the beginning of the loop.
– Reginaldo Rigo
I just didn’t understand this: where the number came from
8
of the last group (group C), if it does not exist in the input matrix? : ) Besides, have you tried anything? Your specific difficulty is in what? The algorithm to process this matrix? If so, it seems to be a very similar problem to this: http://answall.com/questions/127070/colocando-e-comparando-domin%C3%B3s-in-order-in-c-using-list-or-n%C3%A3o-case-succeed– Luiz Vieira
The knot
8
present alone in the graph is the representation of a pair that was omitted from the list. It would be the pair:8 - 8
.– Lacobus